Files
audita/internal/core/config/config_test.go

95 lines
2.9 KiB
Go
Raw Normal View History

2026-05-10 23:19:59 +00:00
package config
import (
"os"
"path/filepath"
2026-05-10 23:19:59 +00:00
"testing"
ck "go.maximumdirect.net/configkit"
2026-05-10 23:19:59 +00:00
)
func TestDefaultsAndVersion(t *testing.T) {
result, err := Load(ck.Input{Source: ck.NoSource()}, "")
2026-05-12 21:17:35 +00:00
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")
}
2026-05-10 23:19:59 +00:00
}
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 retries", "llm:\n transient_retries: 0\n", true},
{"retired repair budget", "llm:\n repair_attempts: 0\n", false},
} {
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)
}
}