package config import ( "os" "path/filepath" "testing" ck "go.maximumdirect.net/configkit" ) func TestDefaultsAndVersion(t *testing.T) { result, err := Load(ck.Input{Source: ck.NoSource()}, "") if err != nil { t.Fatal(err) } if result.Config.Version != 3 || len(result.Config.Pipeline.Stages) != 5 { t.Fatalf("defaults: %+v", result.Config) } path := filepath.Join(t.TempDir(), "config.yml") for _, text := range []string{"version: 2\n", "version: null\n", "version: invalid\n"} { if err := os.WriteFile(path, []byte(text), 0644); err != nil { t.Fatal(err) } if _, err := Load(ck.Input{Source: ck.File(path)}, ""); err == nil { t.Fatalf("accepted %q", text) } } if err := os.WriteFile(path, []byte("pipeline:\n stages: []\n"), 0644); err != nil { t.Fatal(err) } empty, err := Load(ck.Input{Source: ck.File(path)}, "") if err != nil { t.Fatal(err) } if len(empty.Config.Pipeline.Stages) != 0 { t.Fatal("empty pipeline was not preserved") } } func TestSchemaConstraintsAndExplicitValues(t *testing.T) { path := filepath.Join(t.TempDir(), "config.yml") for _, tc := range []struct { name, text string valid bool }{ {"nullable selections", "glossaries: null\nprotected_terms: []\nllm:\n validation_profile: null\n", true}, {"repeated modules", "pipeline:\n stages:\n - module: grammar\n - module: grammar\n", true}, {"removed backends", "backends: {}\n", false}, {"removed debug", "diagnostics:\n debug: true\n", false}, {"unknown module", "pipeline:\n stages:\n - module: unknown\n", false}, {"invalid chunk limit", "chunking:\n target_sections: 0\n", false}, {"null group", "llm: null\n", false}, {"zero budgets", "llm:\n transient_retries: 0\n repair_attempts: 0\n", true}, } { t.Run(tc.name, func(t *testing.T) { if err := os.WriteFile(path, []byte(tc.text), 0644); err != nil { t.Fatal(err) } _, err := Load(ck.Input{Source: ck.File(path)}, "") if (err == nil) != tc.valid { t.Fatalf("load error: %v", err) } }) } } func TestOverridesFollowInheritance(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "config.yml") data := `glossary_collections: common: entries: - id: a name: First category: character derived: extends: common ` if err := os.WriteFile(path, []byte(data), 0644); err != nil { t.Fatal(err) } result, err := Load(ck.Input{Source: ck.File(path)}, "", ck.Override{Path: ck.Keys("glossary_collections", "common", "entries"), Value: []any{map[string]any{"id": "a", "name": "Changed", "category": "character"}}, Flag: "--set"}) if err != nil { t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics()) } if got := result.Config.GlossaryCollections["derived"].Entries[0].Name; got != "First" { t.Fatalf("inherited name = %q", got) } if got := result.Config.GlossaryCollections["common"].Entries[0].Name; got != "Changed" { t.Fatalf("overridden name = %q", got) } }