2026-09-26 18:00:50 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-11-17 22:06:58 +00:00
|
|
|
"go.maximumdirect.net/configkit"
|
2026-09-26 18:00:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func writeApplicationFile(t *testing.T, path, body string) {
|
|
|
|
|
t.Helper()
|
2026-11-20 04:26:17 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
2026-09-26 18:00:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
2026-09-26 18:00:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
func useIsolatedSearch(t *testing.T) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
previous := applicationSearch
|
|
|
|
|
applicationSearch = configkit.WorkingDirectory()
|
|
|
|
|
t.Cleanup(func() { applicationSearch = previous })
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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"),
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
for index, path := range paths {
|
|
|
|
|
writeApplicationFile(t, path, "server:\n addr: '"+string(rune('a'+index))+"'\n")
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
// 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)
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
}
|
2026-09-26 18:00:50 +00:00
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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")
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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")
|
|
|
|
|
}
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
_, 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)
|
|
|
|
|
}
|
2026-11-17 22:06:58 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
if _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: "missing"}); err == nil {
|
|
|
|
|
t.Fatal("missing overlay accepted")
|
2026-11-17 22:06:58 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
|
|
|
|
|
t.Fatal("overlay without main context accepted")
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
}
|
2026-11-17 22:18:13 +00:00
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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 {
|
2026-11-17 22:18:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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")
|
2026-11-17 22:18:13 +00:00
|
|
|
}
|
2026-09-26 18:00:50 +00:00
|
|
|
}
|
2026-09-26 18:12:27 +00:00
|
|
|
|
2026-11-20 04:26:17 +00:00
|
|
|
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 {
|
2026-09-26 18:12:27 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
if result.Config.Server.MaxRequestBytes != 0 || result.Config.PromptKit.Configuration != "framework.yml" {
|
|
|
|
|
t.Fatal(result.Config)
|
2026-09-26 18:12:27 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
explanation, ok := result.Explain(configkit.Keys("promptkit", "configuration"))
|
|
|
|
|
if !ok || len(explanation.History) < 2 {
|
|
|
|
|
t.Fatalf("native evidence missing: %+v", explanation)
|
2026-10-04 11:26:40 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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)
|
2026-09-26 18:12:27 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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())
|
2026-09-26 18:12:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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)
|
2026-09-26 18:12:27 +00:00
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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")
|
2026-11-17 22:06:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLoadImportRequirednessAndDestination(t *testing.T) {
|
2026-11-20 04:26:17 +00:00
|
|
|
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 {
|
2026-11-17 22:06:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-20 04:26:17 +00:00
|
|
|
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")
|
2026-11-17 22:06:58 +00:00
|
|
|
writeApplicationFile(t, main, "config:\n imports:\n - dir: fragments\n pattern: '*.yml'\n destination: [server]\n required: true\n")
|
2026-11-20 04:26:17 +00:00
|
|
|
result, err := Load(LoadOptions{Explicit: true, Path: main})
|
2026-11-17 22:06:58 +00:00
|
|
|
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 {
|
2026-11-20 04:26:17 +00:00
|
|
|
t.Fatalf("value/origin: %+v %+v", result.Config, info)
|
2026-11-17 22:06:58 +00:00
|
|
|
}
|
|
|
|
|
}
|