package appconfig import ( "context" "os" "path/filepath" "slices" "testing" ck "go.maximumdirect.net/configkit" ) func TestGlossaryFilesRequireExplicitSelection(t *testing.T) { dir := t.TempDir() write := func(name, data string) { t.Helper() file := filepath.Join(dir, name) if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(file, []byte(data), 0644); err != nil { t.Fatal(err) } } write("config.yml", "version: 3\nglossary_collections:\n party:\n entries: [{id: configured, name: Configured, category: person}]\n") write("glossary.yml", "invalid: [\n") write("glossary/nested/spells.yaml", "invalid: [\n") main := ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))} result, _, err := Load(context.Background(), main, Options{}) if err != nil { t.Fatal(err) } if len(result.Config.Application.GlossaryCollections) != 1 || result.Config.Application.GlossaryCollections["party"].Entries[0].ID != "configured" { t.Fatalf("unexpected vocabulary: %+v", result.Config.Application.GlossaryCollections) } write("glossary.yml", `config: imports: [{path: shared/extra.yml, required: true}] schema_version: maximumdirect.glossary.v1 collections: party: entries: [{id: selected, name: Selected, category: person}] `) write("shared/extra.yml", "collections:\n lore:\n entries: [{id: lore, name: Lore, category: topic}]\n") write("glossary/nested/spells.yaml", "schema_version: maximumdirect.glossary.v1\ncollections:\n spells:\n entries: [{id: bolt, name: Bolt, category: spell}]\n") paths := []string{filepath.Join(dir, "glossary.yml"), filepath.Join(dir, "glossary/nested/spells.yaml")} result, _, err = Load(context.Background(), main, Options{GlossaryFiles: paths}) if err != nil { t.Fatal(err) } collections := result.Config.Application.GlossaryCollections if collections["party"].Entries[0].ID != "selected" || len(collections["lore"].Entries) != 1 || len(collections["spells"].Entries) != 1 { t.Fatalf("explicit vocabulary = %+v", collections) } for _, name := range []string{"glossary.yml", "shared/extra.yml", "glossary/nested/spells.yaml"} { if !slices.Contains(result.GlossarySources, filepath.Join(dir, name)) { t.Errorf("missing source %s: %v", name, result.GlossarySources) } } } func TestInlineGlossaryValidation(t *testing.T) { dir := t.TempDir() main := filepath.Join(dir, "config.yml") write := func(path, data string) { t.Helper() if err := os.WriteFile(path, []byte(data), 0644); err != nil { t.Fatal(err) } } for _, data := range []string{ "glossary_collections:\n empty: {entries: []}\n", "glossary_collections:\n x:\n entries:\n - {id: 'id with spaces', name: N, category: C}\n", } { write(main, data) if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{}); err != nil { t.Fatalf("valid %q: %v", data, err) } } for _, data := range []string{ "glossary_collections:\n x:\n entries:\n - {id: '', name: N, category: C}\n", "glossary_collections:\n x:\n entries:\n - {id: x, name: '', category: C}\n", "glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, aliases: ['']}\n", "glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, plural: ''}\n", } { write(main, data) if _, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{}); err == nil { t.Fatalf("accepted %q", data) } } } func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) { dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "glossary.yml"), []byte("leak:\n entries:\n - {id: a, name: A, category: C}\n"), 0644); err != nil { t.Fatal(err) } t.Chdir(dir) result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, Options{}) if err != nil { t.Fatal(err) } if len(result.Config.Application.GlossaryCollections) != 0 { t.Fatalf("unanchored glossary loaded: %+v", result.Config.Application.GlossaryCollections) } }