package config import ( "errors" "os" "path/filepath" "reflect" "testing" "go.maximumdirect.net/configkit" ) func writeApplicationFile(t *testing.T, path, body string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(body), 0600); err != nil { t.Fatal(err) } } func isolatedDiscovery(t *testing.T) string { t.Helper() root := t.TempDir() t.Chdir(root) t.Setenv("HOME", filepath.Join(root, "home")) t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg")) return root } func useIsolatedSearch(t *testing.T) { t.Helper() previous := applicationSearch applicationSearch = configkit.WorkingDirectory() t.Cleanup(func() { applicationSearch = previous }) } func TestLoadNativeDiscoveryOrderAndFilenames(t *testing.T) { root := isolatedDiscovery(t) // A later candidate must never be loaded as a configuration layer. paths := []string{ filepath.Join(root, "scriptorium.yaml"), filepath.Join(root, "xdg", "scriptorium", "main.yaml"), filepath.Join(root, "home", ".scriptorium", "config.yaml"), } for index, path := range paths { writeApplicationFile(t, path, "server:\n addr: '"+string(rune('a'+index))+"'\n") } for index, path := range paths { result, err := Load(LoadOptions{Path: "ignored.yml"}) if err != nil { t.Fatal(err) } if result.Config.Server.Addr != string(rune('a'+index)) || len(result.Sources()) != 1 || result.Sources()[0].Path != path { t.Fatalf("selected wrong source: %+v %+v", result.Config, result.Sources()) } if err := os.Remove(path); err != nil { t.Fatal(err) } } // Do not let a host's system configuration influence exhaustion checks. // The system preset's filename expansion remains upstream-owned. useIsolatedSearch(t) result, err := Load(LoadOptions{}) if err != nil || len(result.Sources()) != 0 || !reflect.DeepEqual(result.Config, documentDefaults()) { t.Fatalf("exhaustion: %+v %v", result, err) } } func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) { root := isolatedDiscovery(t) writeApplicationFile(t, filepath.Join(root, "scriptorium.yml"), "server: [\n") writeApplicationFile(t, filepath.Join(root, "scriptorium.yaml"), "{}\n") if _, err := Load(LoadOptions{}); err == nil { t.Fatal("invalid first candidate fell through") } } func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) { root := isolatedDiscovery(t) useIsolatedSearch(t) for _, content := range []string{"", "server: [\n"} { path := filepath.Join(root, "selected.yml") writeApplicationFile(t, path, content) if _, err := Load(LoadOptions{Explicit: true, Path: path}); err == nil { t.Fatal("accepted invalid explicit file") } } _, err := Load(LoadOptions{Explicit: true}) var native *configkit.LoadError if !errors.As(err, &native) { t.Fatalf("empty path lost typed error: %v", err) } base := filepath.Join(root, "selected.yml") writeApplicationFile(t, base, "{}\n") for _, overlay := range []string{"", "default"} { result, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: overlay}) if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) { t.Fatalf("no overlay: %+v %v", result, err) } } if _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: "missing"}); err == nil { t.Fatal("missing overlay accepted") } if _, err := Load(LoadOptions{Overlay: "local"}); err == nil { t.Fatal("overlay without main context accepted") } } func TestLoadImportsOverlayAndNativeOverrides(t *testing.T) { root := t.TempDir() main := filepath.Join(root, "config.yml") writeApplicationFile(t, main, "config:\n imports:\n - glob: fragments/*.yml\n required: true\npromptkit:\n configuration: base.yml\n") writeApplicationFile(t, filepath.Join(root, "fragments", "a.yml"), "server:\n addr: :8081\n") writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "promptkit:\n schema_dir: ./schemas\n") writeApplicationFile(t, filepath.Join(root, "overlays", "local.yml"), "promptkit:\n configuration: overlay.yml\nserver:\n addr: :9090\n") result, err := Load(LoadOptions{Explicit: true, Path: main, Overlay: "local", Overrides: []configkit.Override{{Path: configkit.Keys("promptkit", "configuration"), Value: "native.yml", Flag: "--set"}}}) if err != nil { t.Fatal(err) } if result.Config.PromptKit.Configuration != "native.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9090" || len(result.Sources()) != 5 { t.Fatalf("composition: %+v %+v", result.Config, result.Sources()) } writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "server:\n addr: :8082\n") if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil { t.Fatal("peer conflict accepted") } } func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) { root := isolatedDiscovery(t) useIsolatedSearch(t) result, err := Load(LoadOptions{Overrides: []configkit.Override{ {Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}, {Path: configkit.Keys("promptkit", "configuration"), Value: " ./one/../framework.yml ", Flag: "--set"}, }}) if err != nil { t.Fatal(err) } if result.Config.Server.MaxRequestBytes != 0 || result.Config.PromptKit.Configuration != "framework.yml" { t.Fatal(result.Config) } explanation, ok := result.Explain(configkit.Keys("promptkit", "configuration")) if !ok || len(explanation.History) < 2 { t.Fatalf("native evidence missing: %+v", explanation) } for index, records := range [][]configkit.Override{ {{Path: configkit.Keys("server", "addr"), Value: "a", Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}}, {{Path: configkit.Keys("server"), Value: map[string]any{"addr": "a"}, Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}}, } { _, err := Load(LoadOptions{Overrides: records}) var native *configkit.LoadError if !errors.As(err, &native) { t.Fatalf("native conflict lost: %v", err) } found := false want := []configkit.Code{configkit.CodeOverrideDuplicate, configkit.CodeOverrideOverlap}[index] for _, diagnostic := range native.Diagnostics() { found = found || diagnostic.Code == want } if !found { t.Fatalf("missing conflict diagnostics: %+v", native.Diagnostics()) } } path := filepath.Join(root, "invalid.yml") writeApplicationFile(t, path, "server:\n max_request_bytes: -1\n") result, err = Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}) if err != nil || result.Config.Server.MaxRequestBytes != 0 { t.Fatalf("override did not precede validation: %+v %v", result, err) } writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n") if _, err := Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}); err == nil { t.Fatal("override repaired decode failure") } } func TestLoadImportRequirednessAndDestination(t *testing.T) { root := t.TempDir() main := filepath.Join(root, "config.yml") writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n") if _, err := Load(LoadOptions{Explicit: true, Path: main}); err != nil { t.Fatal(err) } writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n required: true\n") if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil { t.Fatal("required import accepted") } writeApplicationFile(t, filepath.Join(root, "fragments", "server.yml"), "addr: ':8181'\n") writeApplicationFile(t, main, "config:\n imports:\n - dir: fragments\n pattern: '*.yml'\n destination: [server]\n required: true\n") result, err := Load(LoadOptions{Explicit: true, Path: main}) if err != nil { t.Fatal(err) } info, ok := result.Metadata().Lookup(configkit.Keys("server", "addr")) if result.Config.Server.Addr != ":8181" || !ok || len(info.Origin.Locations) == 0 { t.Fatalf("value/origin: %+v %+v", result.Config, info) } }