Test native application configuration behavior

This commit is contained in:
2026-11-17 22:06:58 +00:00
parent 5c780a4cc0
commit 03b479b4d7
3 changed files with 111 additions and 13 deletions

View File

@@ -217,6 +217,10 @@ construct a PromptKit engine.
## Stage 4: Migrate And Extend Application Configuration Tests ## Stage 4: Migrate And Extend Application Configuration Tests
**Complete.** `go test ./internal/config` passed with Go 1.27.4. Native
diagnostics for the nonregular discovery case omit the filesystem path; the
test checks the typed diagnostic and that no raw control character leaks.
**Primary files:** `internal/config/document_test.go` and `load_test.go`. **Primary files:** `internal/config/document_test.go` and `load_test.go`.
**Work** **Work**

View File

@@ -7,7 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
configkit "gitea.maximumdirect.net/eric/ConfigKit" "go.maximumdirect.net/configkit"
) )
func loadDocumentBytes(content string) (*configkit.Result[Document], error) { func loadDocumentBytes(content string) (*configkit.Result[Document], error) {

View File

@@ -8,7 +8,7 @@ import (
"strings" "strings"
"testing" "testing"
configkit "gitea.maximumdirect.net/eric/ConfigKit" "go.maximumdirect.net/configkit"
) )
func useConfigSearchPaths(t *testing.T, paths ...string) { func useConfigSearchPaths(t *testing.T, paths ...string) {
@@ -113,7 +113,7 @@ func TestLoadDefaultsOnlyAndOverridesBeforeValidation(t *testing.T) {
if err != nil || result.Config.Defaults.RenderFormat != "json" { if err != nil || result.Config.Defaults.RenderFormat != "json" {
t.Fatalf("override did not precede validation: result=%+v err=%v", result, err) t.Fatalf("override did not precede validation: result=%+v err=%v", result, err)
} }
if len(result.Sources()) != 2 || result.Sources()[0].Layer != configkit.LayerBase || result.Sources()[1].Layer != configkit.LayerCLI { if len(result.Sources()) != 2 || result.Sources()[0].Layer != configkit.LayerMain || result.Sources()[1].Layer != configkit.LayerCLI {
t.Fatalf("source order changed: %+v", result.Sources()) t.Fatalf("source order changed: %+v", result.Sources())
} }
writeApplicationFile(t, path, "defaults: [\n") writeApplicationFile(t, path, "defaults: [\n")
@@ -158,14 +158,22 @@ defaults:
func TestLoadDiscoveryErrorQuotesPath(t *testing.T) { func TestLoadDiscoveryErrorQuotesPath(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
loop := filepath.Join(dir, "loop-\n.yml") path := filepath.Join(dir, "directory-\n.yml")
if err := os.Symlink(loop, loop); err != nil { if err := os.Mkdir(path, 0o700); err != nil {
t.Skipf("symlink unavailable: %v", err) t.Fatal(err)
} }
useConfigSearchPaths(t, loop) useConfigSearchPaths(t, path)
_, err := Load(LoadOptions{}) _, err := Load(LoadOptions{})
if err == nil || !strings.Contains(err.Error(), `\n`) || strings.Contains(err.Error(), "loop-\n.yml") { var loadError *configkit.LoadError
t.Fatalf("discovery error did not quote path safely: %v", err) if !errors.As(err, &loadError) {
t.Fatalf("expected structured error: %v", err)
}
var diagnostics strings.Builder
if err := configkit.WriteDiagnostics(&diagnostics, loadError.Diagnostics()); err != nil {
t.Fatal(err)
}
if !strings.Contains(diagnostics.String(), string(configkit.CodeSourceNotRegular)) || strings.Contains(diagnostics.String(), "directory-\n.yml") {
t.Fatalf("discovery diagnostics exposed an unescaped path: %s", diagnostics.String())
} }
} }
@@ -178,7 +186,7 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
base := filepath.Join(dir, "config.yml") base := filepath.Join(dir, "config.yml")
writeApplicationFile(t, base, "configkit:\n imports:\n - glob: fragments/*.yml\npromptkit_sources:\n - file: ./base-framework.yml\n") writeApplicationFile(t, base, "config:\n imports:\n - glob: fragments/*.yml\n required: true\npromptkit_sources:\n - file: ./base-framework.yml\n")
writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "server:\n addr: :8081\n") writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "server:\n addr: :8081\n")
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "defaults:\n render_format: json\n") writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "defaults:\n render_format: json\n")
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - directory: ./overlay-framework\nserver:\n addr: :9090\n") writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - directory: ./overlay-framework\nserver:\n addr: :9090\n")
@@ -211,17 +219,103 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) {
} }
} }
func TestLoadRejectsOverlayWithoutBase(t *testing.T) { func TestLoadOverlayWithoutMainContext(t *testing.T) {
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml")) useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil { if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
t.Fatal("overlay without base accepted") t.Fatal("overlay without a main context accepted")
} else { } else {
var loadError *configkit.LoadError var loadError *configkit.LoadError
if !errors.As(err, &loadError) { if !errors.As(err, &loadError) {
t.Fatalf("no-base overlay lost structured diagnostic: %v", err) t.Fatalf("no-context overlay lost structured diagnostic: %v", err)
} }
} }
if _, err := Load(LoadOptions{Overlay: "default"}); err != nil { if _, err := Load(LoadOptions{Overlay: "default"}); err != nil {
t.Fatalf("default must select no overlay: %v", err) t.Fatalf("default must select no overlay: %v", err)
} }
} }
func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) {
dir := t.TempDir()
first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml")
writeApplicationFile(t, first, "server: [\n")
writeApplicationFile(t, second, "server:\n addr: ':9090'\n")
useConfigSearchPaths(t, first, second)
if _, err := Load(LoadOptions{}); err == nil {
t.Fatal("malformed selected file fell through to the second candidate")
}
}
func TestLoadImportRequirednessAndDestination(t *testing.T) {
dir := t.TempDir()
main := filepath.Join(dir, "config.yml")
for _, tc := range []struct {
name, declaration string
wantError bool
}{
{"optional by default", "- path: absent.yml", false},
{"required", "- path: absent.yml\n required: true", true},
{"removed optional field", "- path: absent.yml\n optional: true", true},
} {
t.Run(tc.name, func(t *testing.T) {
writeApplicationFile(t, main, "config:\n imports:\n "+tc.declaration+"\n")
_, err := Load(LoadOptions{Path: main, Explicit: true})
if (err != nil) != tc.wantError {
t.Fatalf("load error = %v, want error %t", err, tc.wantError)
}
})
}
fragments := filepath.Join(dir, "fragments")
if err := os.Mkdir(fragments, 0o700); err != nil {
t.Fatal(err)
}
writeApplicationFile(t, filepath.Join(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{Path: main, Explicit: true})
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("mounted value or origin missing: config=%+v origin=%+v", result.Config.Server, info)
}
}
func TestLoadAuthoredSelectorCollection(t *testing.T) {
dir := t.TempDir()
for _, subdir := range []string{"fragments", "overlays"} {
if err := os.Mkdir(filepath.Join(dir, subdir), 0o700); err != nil {
t.Fatal(err)
}
}
main := filepath.Join(dir, "config.yml")
writeApplicationFile(t, main, "config:\n sequences:\n - destination: [promptkit_sources]\n key: file\n mode: upsert\n imports:\n - dir: fragments\n pattern: '*.yml'\n required: true\n")
writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "promptkit_sources:\n - file: a.yml\n")
peer := filepath.Join(dir, "fragments", "b.yml")
writeApplicationFile(t, peer, "promptkit_sources:\n - file: b.yml\n")
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - file: c.yml\n")
result, err := Load(LoadOptions{Path: main, Explicit: true, Overlay: "local"})
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "a.yml"}, {File: "b.yml"}, {File: "c.yml"}}) {
t.Fatalf("authored upsert did not retain selectors: result=%+v err=%v", result, err)
}
result, err = Load(LoadOptions{Path: main, Explicit: true, Overlay: "local", Overrides: []configkit.Override{{
Path: configkit.Keys("promptkit_sources"), Value: []any{map[string]any{"file": "cli.yml"}}, Flag: "--promptkit-source",
}}})
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "cli.yml"}}) {
t.Fatalf("CLI list did not replace selectors: result=%+v err=%v", result, err)
}
writeApplicationFile(t, peer, "promptkit_sources:\n - file: a.yml\n")
if _, err := Load(LoadOptions{Path: main, Explicit: true}); err == nil {
t.Fatal("duplicate authored peer identity accepted")
}
}
func TestLoadOverrideCannotRepairDecodeError(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.yml")
writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n")
_, err := Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{
Path: configkit.Keys("server", "max_request_bytes"), Value: int64(12), Flag: "--max-request-bytes",
}}})
if err == nil {
t.Fatal("override repaired an undecodable source value")
}
}