package config import ( "bytes" "errors" "reflect" "strings" "testing" "go.maximumdirect.net/configkit" ) func loadDocumentBytes(content string) (*configkit.Result[Document], error) { return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{ Definition: documentDefinition(), }) } func TestDocumentDefaultsAndExplicitValues(t *testing.T) { defaultsOnly, err := configkit.Load(configkit.NoSource(), configkit.Options[Document]{Definition: documentDefinition()}) if err != nil { t.Fatalf("defaults-only load: %v", err) } if !reflect.DeepEqual(defaultsOnly.Config, documentDefaults()) || len(defaultsOnly.Sources()) != 0 { t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources()) } result, err := loadDocumentBytes(`promptkit_sources: - file: " ./bundle/../framework.yml " - directory: "./profiles" schema_dir: "" transient_retries: 0 server: addr: "" artifact_root: "" max_request_bytes: 0 max_artifact_bytes: 0 max_response_bytes: 0 defaults: render_format: " JSON " `) if err != nil { t.Fatalf("explicit-value load: %v", err) } config := result.Config if config.SchemaDir != "" || config.Server.Addr != "" || config.Server.MaxRequestBytes != 0 || config.Server.MaxArtifactBytes != 0 || config.Server.MaxResponseBytes != 0 { t.Fatalf("explicit empty and zero values did not replace defaults: %+v", config) } if config.TransientRetries == nil || *config.TransientRetries != 0 { t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries) } if want := []SourceSelection{{File: "framework.yml"}, {Directory: "profiles"}}; !reflect.DeepEqual(config.PromptKitSources, want) { t.Fatalf("source selectors did not decode: %+v", config.PromptKitSources) } if config.Defaults.RenderFormat != "json" { t.Fatalf("render format normalization failed: %q", config.Defaults.RenderFormat) } nullable, err := loadDocumentBytes("transient_retries: null\npromptkit_sources: null\n") if err != nil { t.Fatalf("nullable-value load: %v", err) } if nullable.Config.TransientRetries != nil || nullable.Config.PromptKitSources != nil { t.Fatalf("nullable fields did not remain nil: %+v", nullable.Config) } } func TestDocumentSourceSelectionsDoNotAcquirePaths(t *testing.T) { for _, tc := range []struct { name string content string want []SourceSelection }{ {name: "omitted", content: "{}\n"}, {name: "null", content: "promptkit_sources: null\n"}, {name: "empty", content: "promptkit_sources: []\n"}, {name: "nonexistent file", content: "promptkit_sources:\n - file: ./missing/framework.yml\n", want: []SourceSelection{{File: "missing/framework.yml"}}}, {name: "nonexistent directory", content: "promptkit_sources:\n - directory: ./missing/framework\n", want: []SourceSelection{{Directory: "missing/framework"}}}, } { t.Run(tc.name, func(t *testing.T) { result, err := loadDocumentBytes(tc.content) if err != nil { t.Fatalf("load source selectors: %v", err) } if len(result.Config.PromptKitSources) != len(tc.want) || len(tc.want) > 0 && !reflect.DeepEqual(result.Config.PromptKitSources, tc.want) { t.Fatalf("source selectors changed: got=%+v want=%+v", result.Config.PromptKitSources, tc.want) } }) } } func TestSettingsCopiesPromptKitSourcesInOrder(t *testing.T) { document := Document{PromptKitSources: []SourceSelection{ {File: "./first.yml"}, {Directory: "./second"}, }} settings := Settings(document) if !reflect.DeepEqual(settings.PromptKitSources, document.PromptKitSources) { t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, document.PromptKitSources) } document.PromptKitSources[0].File = "changed.yml" if settings.PromptKitSources[0].File != "./first.yml" { t.Fatalf("settings retained the document's source slice: %+v", settings.PromptKitSources) } if empty := Settings(Document{PromptKitSources: []SourceSelection{}}).PromptKitSources; empty == nil { t.Fatal("explicit empty source list was not copied") } } func TestDocumentRecordsEffectiveNormalization(t *testing.T) { result, err := loadDocumentBytes(`promptkit_sources: - file: " ./one/../two " - file: "./blank.yml" directory: " " - directory: "./three/../three" server: artifact_root: " ./data/../artifacts " addr: " :9000 " defaults: render_format: " JSON " `) if err != nil { t.Fatalf("load normalized document: %v", err) } if want := []SourceSelection{{File: "two"}, {File: "blank.yml"}, {Directory: "three"}}; !reflect.DeepEqual(result.Config.PromptKitSources, want) { t.Fatalf("unexpected normalized source selections: %+v", result.Config.PromptKitSources) } if result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" { t.Fatalf("unexpected normalized values: %+v", result.Config) } var effective bytes.Buffer if err := result.WriteEffective(&effective); err != nil { t.Fatalf("write effective configuration: %v", err) } for _, fragment := range []string{"file: two", "directory: ''", "directory: three", "artifact_root: artifacts", "addr: :9000", "render_format: json"} { if !strings.Contains(effective.String(), fragment) { t.Fatalf("effective output omitted %q: %s", fragment, effective.String()) } } explanation, known := result.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file")) if !known || explanation.Effective.YAML != "two\n" { t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known) } found := false for _, change := range explanation.History { if change.Origin.Operation == configkit.ChangeNormalization { found = true } } if !found { t.Fatalf("normalization was not recorded: %+v", explanation.History) } unchanged, err := loadDocumentBytes("promptkit_sources:\n - file: framework.yml\n") if err != nil { t.Fatalf("load unchanged source path: %v", err) } unchangedExplanation, known := unchanged.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file")) if !known { t.Fatal("unchanged source path was not inspectable") } for _, change := range unchangedExplanation.History { if change.Origin.Operation == configkit.ChangeNormalization { t.Fatalf("unchanged source path recorded a normalization: %+v", unchangedExplanation.History) } } } func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) { for _, tc := range []struct{ name, content, path string }{ {"empty source selector", "promptkit_sources:\n - file: ''\n", "/promptkit_sources/0"}, {"multiple source selectors", "promptkit_sources:\n - file: secret-path\n directory: private-path\n", "/promptkit_sources/0"}, {"negative retry", "transient_retries: -9173\n", "/transient_retries"}, {"negative request limit", "server:\n max_request_bytes: -9173\n", "/server/max_request_bytes"}, {"negative artifact limit", "server:\n max_artifact_bytes: -9173\n", "/server/max_artifact_bytes"}, {"negative response limit", "server:\n max_response_bytes: -9173\n", "/server/max_response_bytes"}, {"empty format", "defaults:\n render_format: \"\"\n", "/defaults/render_format"}, {"unknown format", "defaults:\n render_format: private-invalid-value\n", "/defaults/render_format"}, } { t.Run(tc.name, func(t *testing.T) { _, err := loadDocumentBytes(tc.content) var loadError *configkit.LoadError if !errors.As(err, &loadError) { t.Fatalf("expected typed validation error, got %v", err) } found := false for _, diagnostic := range loadError.Diagnostics() { if diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == tc.path { found = true } if strings.Contains(diagnostic.Message, "9173") || strings.Contains(diagnostic.Message, "private-invalid-value") || strings.Contains(diagnostic.Message, "secret-path") || strings.Contains(diagnostic.Message, "private-path") { t.Fatalf("diagnostic disclosed rejected value: %+v", diagnostic) } } if !found { t.Fatalf("expected validation at %s, got %+v", tc.path, loadError.Diagnostics()) } }) } } func TestDocumentRejectsNativeShapeErrors(t *testing.T) { for _, content := range []string{ "server: null\n", "defaults: null\n", "server:\n addr: null\n", "server:\n max_request_bytes: null\n", "defaults:\n render_format: null\n", "server:\n unknown_field: 1\n", "promptkit_sources:\n - file: ./selected.yml\n unknown_field: 1\n", "promptkit_sources:\n - file: ./selected.yml\n directory: ./also-selected\n", "promptkit_sources:\n - file: null\n", "promptkit_sources: ./not-a-sequence.yml\n", "prompt_dir: ./old-prompts\n", "profile_dir: ./old-profiles\n", "backends:\n local:\n endpoint: http://example.invalid/v1\n", } { _, err := loadDocumentBytes(content) if err == nil { t.Fatalf("unexpected acceptance of %q", content) } } }