package envspec import ( "testing" "code.nochebuena.dev/einherjar/mcp/internal/index" ) func TestParseTag(t *testing.T) { cases := []struct { tag string wantOK bool wantName string wantReq bool wantDef string wantHas bool }{ {`env:"EINHERJAR_PG_HOST,required"`, true, "EINHERJAR_PG_HOST", true, "", false}, {`env:"EINHERJAR_PG_PORT" envDefault:"5432"`, true, "EINHERJAR_PG_PORT", false, "5432", true}, {`env:"EINHERJAR_LOG_JSON" envDefault:"false"`, true, "EINHERJAR_LOG_JSON", false, "false", true}, {`env:"-"`, false, "", false, "", false}, {`json:"x"`, false, "", false, "", false}, {``, false, "", false, "", false}, } for _, c := range cases { got, ok := ParseTag(c.tag) if ok != c.wantOK { t.Errorf("ParseTag(%q) ok=%v want %v", c.tag, ok, c.wantOK) continue } if !ok { continue } if got.Name != c.wantName || got.Required != c.wantReq || got.Default != c.wantDef || got.HasDefault != c.wantHas { t.Errorf("ParseTag(%q) = %+v, want name=%q req=%v def=%q has=%v", c.tag, got, c.wantName, c.wantReq, c.wantDef, c.wantHas) } } } // synthIndex mirrors the shape of the real index: a web module whose server // sub-package and root both declare env vars, plus a db-postgres module. func synthIndex() *index.Index { return &index.Index{ Modules: []index.Module{ { Name: "web", Symbols: []index.Symbol{ {Kind: "type", Name: "Config", SubPackage: "server", Fields: []index.Field{ {Name: "Host", Type: "string", Tag: `env:"EINHERJAR_SERVER_HOST" envDefault:"0.0.0.0"`}, {Name: "Port", Type: "int", Tag: `env:"EINHERJAR_SERVER_PORT" envDefault:"8080"`}, {Name: "CORSOrigins", Type: "[]string", Tag: `env:"EINHERJAR_SERVER_CORS_ORIGINS" envSeparator:","`}, }}, {Kind: "type", Name: "Config", SubPackage: "", Fields: []index.Field{ {Name: "Server", Type: "server.Config"}, // nested, no env tag; root has no own env vars }}, }, }, { Name: "db-postgres", Symbols: []index.Symbol{ {Kind: "type", Name: "Config", SubPackage: "", Fields: []index.Field{ {Name: "Host", Type: "string", Tag: `env:"EINHERJAR_PG_HOST,required"`}, {Name: "Name", Type: "string", Tag: `env:"EINHERJAR_PG_NAME,required"`}, {Name: "Port", Type: "int", Tag: `env:"EINHERJAR_PG_PORT" envDefault:"5432"`}, }}, }, }, }, } } func TestForModule(t *testing.T) { idx := synthIndex() got := ForModule(idx, "web") if len(got) != 3 { // Host + Port + CORSOrigins, all on server.Config; the nested Server field is not a leaf and the root has no own env vars t.Fatalf("ForModule(web) = %d vars, want 3: %+v", len(got), got) } names := map[string]bool{} for _, v := range got { names[v.Name] = true } for _, want := range []string{"EINHERJAR_SERVER_HOST", "EINHERJAR_SERVER_PORT", "EINHERJAR_SERVER_CORS_ORIGINS"} { if !names[want] { t.Errorf("ForModule(web) missing %s", want) } } if ForModule(idx, "nope") != nil { t.Error("ForModule(unknown) should be nil") } } func TestFindStruct(t *testing.T) { idx := synthIndex() server := FindStruct(idx, "web", "server", "Config") if len(server) != 3 { t.Fatalf("FindStruct(web/server/Config) = %d, want 3", len(server)) } // CORS now lives on server.Config, so the server selector MUST include it. corsOnServer := false for _, v := range server { if v.Name == "EINHERJAR_SERVER_CORS_ORIGINS" { corsOnServer = true } } if !corsOnServer { t.Error("server selector should include EINHERJAR_SERVER_CORS_ORIGINS (it lives on server.Config)") } pg := FindStruct(idx, "db-postgres", "", "Config") if len(pg) != 3 { t.Fatalf("FindStruct(db-postgres//Config) = %d, want 3", len(pg)) } var host envspecVarFound for _, v := range pg { if v.Name == "EINHERJAR_PG_HOST" { host = envspecVarFound{found: true, required: v.Required, hasDefault: v.HasDefault} } } if !host.found || !host.required || host.hasDefault { t.Errorf("EINHERJAR_PG_HOST: %+v, want required with no default", host) } } type envspecVarFound struct { found, required, hasDefault bool } func TestKnownNames(t *testing.T) { names := KnownNames(synthIndex()) if _, ok := names["EINHERJAR_PG_NAME"]; !ok { t.Error("KnownNames missing EINHERJAR_PG_NAME") } if _, ok := names["EINHERJAR_PG_DATABASE"]; ok { t.Error("KnownNames must not contain the bogus EINHERJAR_PG_DATABASE") } }