diff --git a/configuration_sources_test.go b/configuration_sources_test.go index b629350..16015ea 100644 --- a/configuration_sources_test.go +++ b/configuration_sources_test.go @@ -23,8 +23,8 @@ func TestCollectionInputValidatesWithoutReadingSource(t *testing.T) { if err != nil { t.Fatal(err) } - if input.Destination.String() != "/collections/docs" || !input.Imports { - t.Fatalf("collection input = %+v, want destination /collections/docs with imports enabled", input) + if input.Destination.String() != "/promptkit/collections/docs" || !input.Imports { + t.Fatalf("collection input = %+v, want destination /promptkit/collections/docs with imports enabled", input) } for _, namespace := range []string{"", " ", "docs/api"} { if _, err := promptkit.CollectionInput(namespace, configkit.NoSource()); !errors.Is(err, promptkit.ErrInvalidConfig) { @@ -51,7 +51,7 @@ func TestCollectionInputLoadsNativeImportsAndLaterOverride(t *testing.T) { Underlays: []configkit.Underlay{{Inputs: []configkit.Input{input}}}, Main: &configkit.Input{ Source: configkit.Mapping("later collection override", map[string]any{"prompts": []any{map[string]any{"id": "greeting", "inputs": []any{map[string]any{"name": "overridden"}}, "messages": []any{map[string]any{"role": "user", "content": "Updated"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}}}), - Destination: configkit.Keys("collections", "docs"), + Destination: configkit.Keys("promptkit", "collections", "docs"), }, }}) if err != nil { @@ -119,7 +119,7 @@ func TestCollectionInputLoadsEmbeddedImportsAndLaterOverride(t *testing.T) { Underlays: []configkit.Underlay{{Inputs: []configkit.Input{input}}}, Main: &configkit.Input{ Source: configkit.Mapping("later collection override", map[string]any{"prompts": []any{map[string]any{"id": "greeting", "inputs": []any{map[string]any{"name": "overridden"}}, "messages": []any{map[string]any{"role": "user", "content": "Updated"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}}}), - Destination: configkit.Keys("collections", "docs"), + Destination: configkit.Keys("promptkit", "collections", "docs"), }, }}) if err != nil { diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 74968fa..6a4b777 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -477,6 +477,9 @@ rules, with a single load only on the source-loading path. ## Stage 10 — Migrate Internal Configuration And Source Tests +Status: complete. Configuration tests passed under the race detector, source +helper tests passed, and the updated benchmarks ran without thresholds. + Focus: `internal/configuration/*_test.go` and source-helper contract tests. 1. Migrate effective-tree paths and document wrappers; leave explicitly mounted diff --git a/internal/configuration/benchmark_test.go b/internal/configuration/benchmark_test.go index 9b02555..ca003fa 100644 --- a/internal/configuration/benchmark_test.go +++ b/internal/configuration/benchmark_test.go @@ -13,7 +13,7 @@ import ( const benchmarkDefinitionCount = 128 var ( - constructionResult *loaded + constructionResult *testLoaded selectedPrompt *domain.PromptDefinition selectedPromptHash string selectedPromptInspection *domain.PromptInspection diff --git a/internal/configuration/bundled_test.go b/internal/configuration/bundled_test.go index 9b002b4..55dce2b 100644 --- a/internal/configuration/bundled_test.go +++ b/internal/configuration/bundled_test.go @@ -12,7 +12,7 @@ import ( ) func TestBundledOpenRouterUnderlay(t *testing.T) { - state, err := Load(configkit.Input{Source: configkit.NoSource()}, []configkit.Underlay{BundledOpenRouterUnderlay()}) + state, err := load(configkit.Input{Source: configkit.NoSource()}, []configkit.Underlay{BundledOpenRouterUnderlay()}) if err != nil { t.Fatal(err) } @@ -48,7 +48,7 @@ func TestBundledOpenRouterUnderlay(t *testing.T) { t.Fatalf("profile %s target = %+v, want %+v", id, *profile.ResolvedTarget, want) } } - if !strings.Contains(fmt.Sprint(state.result.Sources()), "Promptkit bundled OpenRouter") { - t.Fatalf("source evidence = %+v", state.result.Sources()) + if !strings.Contains(fmt.Sprint(state.inspection.Sources()), "Promptkit bundled OpenRouter") { + t.Fatalf("source evidence = %+v", state.inspection.Sources()) } } diff --git a/internal/configuration/inspection_test.go b/internal/configuration/inspection_test.go index 3f40552..ba8f2b1 100644 --- a/internal/configuration/inspection_test.go +++ b/internal/configuration/inspection_test.go @@ -12,20 +12,20 @@ func TestSecretProviderPathRecognizesResourceExtraParams(t *testing.T) { path configkit.Path want bool }{ - {name: "backend nested secret", path: configkit.Keys("backends").Index(0).Key("extra_params").Key("nested").Key("Token"), want: true}, - {name: "profile nested secret", path: configkit.Keys("profiles").Index(1).Key("extra_params").Key("authorization"), want: true}, - {name: "collection backend nested secret", path: configkit.Keys("collections", "provider", "backends").Index(2).Key("extra_params").Key("nested").Key("Token"), want: true}, - {name: "collection profile secret", path: configkit.Keys("collections", "assistant", "profiles").Index(0).Key("extra_params").Key("authorization"), want: true}, + {name: "backend nested secret", path: configkit.Keys("promptkit", "backends").Index(0).Key("extra_params").Key("nested").Key("Token"), want: true}, + {name: "profile nested secret", path: configkit.Keys("promptkit", "profiles").Index(1).Key("extra_params").Key("authorization"), want: true}, + {name: "collection backend nested secret", path: configkit.Keys("promptkit", "collections", "provider", "backends").Index(2).Key("extra_params").Key("nested").Key("Token"), want: true}, + {name: "collection profile secret", path: configkit.Keys("promptkit", "collections", "assistant", "profiles").Index(0).Key("extra_params").Key("authorization"), want: true}, {name: "unrelated nested mapping", path: configkit.Keys("application", "backends", "provider", "extra_params", "token"), want: false}, - {name: "extra params outside resource map", path: configkit.Keys("backends", "provider", "options", "extra_params", "token"), want: false}, - {name: "collection extra params outside resource map", path: configkit.Keys("collections", "provider", "settings", "backends", "default", "extra_params", "token"), want: false}, - {name: "missing resource key", path: configkit.Keys("backends", "", "extra_params", "token"), want: false}, - {name: "ordinary provider value", path: configkit.Keys("profiles").Index(0).Key("extra_params").Key("provider").Key("mode"), want: false}, + {name: "extra params outside resource map", path: configkit.Keys("promptkit", "backends", "provider", "options", "extra_params", "token"), want: false}, + {name: "collection extra params outside resource map", path: configkit.Keys("promptkit", "collections", "provider", "settings", "backends", "default", "extra_params", "token"), want: false}, + {name: "missing resource key", path: configkit.Keys("promptkit", "backends", "", "extra_params", "token"), want: false}, + {name: "ordinary provider value", path: configkit.Keys("promptkit", "profiles").Index(0).Key("extra_params").Key("provider").Key("mode"), want: false}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - if got := secretProviderPath(test.path); got != test.want { - t.Fatalf("secretProviderPath(%q) = %t, want %t", test.path, got, test.want) + if got := Sensitive(test.path); got != test.want { + t.Fatalf("Sensitive(%q) = %t, want %t", test.path, got, test.want) } }) } diff --git a/internal/configuration/interpretation_test.go b/internal/configuration/interpretation_test.go index 6b0de6b..3d84a59 100644 --- a/internal/configuration/interpretation_test.go +++ b/internal/configuration/interpretation_test.go @@ -33,26 +33,26 @@ func TestTargetResolutionNormalizesNestedValues(t *testing.T) { if err != nil || profile.ResolvedTarget.ServiceTier != "standard" || profile.ResolvedTarget.APIKeyEnv != "TOKEN" { t.Fatalf("interpreted profile = %+v, %v", profile, err) } - composed := state.result.Config.Backends[0] + composed := state.backendRecords["local"] if *composed.Endpoint != "https://example.invalid/v1" || composed.QueueCapacity == nil || *composed.QueueCapacity != 1024 || *composed.SmokeTests.Reachability.Endpoint != "https://example.invalid/health" || *composed.SmokeTests.Authentication.Kind != "none" || *composed.SmokeTests.Authentication.Endpoint != "https://example.invalid/key" || - *state.result.Config.Profiles[0].ServiceTier != "standard" || *state.result.Config.Profiles[0].APIKeyEnv != "TOKEN" { - t.Fatalf("resolved values were not normalized: %+v", state.result.Config) + *state.profileRecords["partial"].ServiceTier != "standard" || *state.profileRecords["partial"].APIKeyEnv != "TOKEN" { + t.Fatalf("resolved values were not normalized: %+v", composed) } - path := configkit.Keys("backends").Index(0).Key("smoke_tests").Key("authentication").Key("endpoint") - field, found := state.result.Metadata().Lookup(path) + path := configkit.Keys("promptkit", "backends").Index(0).Key("smoke_tests").Key("authentication").Key("endpoint") + field, found := state.inspection.Metadata().Lookup(path) if !found || field.Origin.Operation != configkit.ChangeNormalization { t.Fatalf("nested field origin changed: %+v", field) } for _, path := range []configkit.Path{ - configkit.Keys("backends").Index(0).Key("endpoint"), - configkit.Keys("backends").Index(0).Key("smoke_tests").Key("reachability").Key("endpoint"), - configkit.Keys("backends").Index(0).Key("smoke_tests").Key("authentication").Key("kind"), - configkit.Keys("profiles").Index(0).Key("service_tier"), - configkit.Keys("profiles").Index(0).Key("api_key_env"), + configkit.Keys("promptkit", "backends").Index(0).Key("endpoint"), + configkit.Keys("promptkit", "backends").Index(0).Key("smoke_tests").Key("reachability").Key("endpoint"), + configkit.Keys("promptkit", "backends").Index(0).Key("smoke_tests").Key("authentication").Key("kind"), + configkit.Keys("promptkit", "profiles").Index(0).Key("service_tier"), + configkit.Keys("promptkit", "profiles").Index(0).Key("api_key_env"), } { - field, found := state.result.Metadata().Lookup(path) + field, found := state.inspection.Metadata().Lookup(path) if !found || field.Origin.Operation != configkit.ChangeNormalization { t.Errorf("normalization at %v: %+v", path, field) } @@ -76,19 +76,19 @@ func TestPromptResolutionNormalizesOutputAndRejectsDuplicateNames(t *testing.T) if err != nil || selected.Validation.SchemaPath != "schema.json" || selected.Inputs[0].Name != "document" { t.Fatalf("interpreted prompt = %+v, %+v, %v", selected, inspection, err) } - composed := state.result.Config.Prompts[0] + composed := state.prompts["example"].resource if *composed.Output.SchemaPath != "schema.json" || composed.Inputs[0].Name != "document" { t.Fatalf("prompt was not normalized: %+v", composed) } definition["inputs"] = []any{map[string]any{"name": " document "}, map[string]any{"name": "document"}} _, err = load(mapping("duplicate inputs", map[string]any{"prompts": []any{definition}}), nil) - if err == nil || !strings.Contains(err.Error(), "/prompts/0/inputs/1") || !strings.Contains(err.Error(), "duplicated") { + if err == nil || !strings.Contains(err.Error(), "/promptkit/prompts/0/inputs/1") || !strings.Contains(err.Error(), "duplicated") { t.Fatalf("duplicate normalized names were not rejected: %v", err) } } func TestPromptResolutionRecordsMessageFields(t *testing.T) { - loadPrompt := func(role, cacheType, ttl string) *loaded { + loadPrompt := func(role, cacheType, ttl string) *testLoaded { t.Helper() state, err := load(mapping("prompt", map[string]any{"prompts": []any{map[string]any{ "id": "example", "inputs": []any{map[string]any{"name": " input ", "content_type": nil, "description": " text "}}, @@ -110,14 +110,15 @@ func TestPromptResolutionRecordsMessageFields(t *testing.T) { if err != nil || inspection.PromptHash != otherInspection.PromptHash || definition.Templates[0].Role != other.Templates[0].Role || definition.Templates[0].Content != "" { t.Fatalf("equivalent prompt mismatch: %+v %+v %v", definition, other, err) } - base := configkit.Keys("prompts").Index(0) + base := configkit.Keys("promptkit", "prompts").Index(0) for _, path := range []configkit.Path{base.Key("inputs").Index(0).Key("name"), base.Key("inputs").Index(0).Key("description"), base.Key("messages").Index(0).Key("role"), base.Key("messages").Index(0).Key("cache_control").Key("type"), base.Key("messages").Index(0).Key("cache_control").Key("ttl")} { - field, found := padded.result.Metadata().Lookup(path) + field, found := padded.inspection.Metadata().Lookup(path) if !found || field.Origin.Operation != configkit.ChangeNormalization { t.Errorf("normalization at %v: %+v", path, field) } } - if padded.promptMetadata["example"][0].contentType != fieldNull { + field, ok := padded.inspection.Metadata().Lookup(base.Key("inputs").Index(0).Key("content_type")) + if !ok || !field.Null { t.Fatal("null input metadata was not retained") } } @@ -133,12 +134,12 @@ func TestResolutionRecordsTargetDependencies(t *testing.T) { if err != nil { t.Fatal(err) } - for _, base := range []configkit.Path{configkit.Keys("backends").Index(0), configkit.Keys("collections", "scoped", "backends").Index(0)} { - endpoint, ok := state.result.Explain(base.Key("endpoint")) + for _, base := range []configkit.Path{configkit.Keys("promptkit", "backends").Index(0), configkit.Keys("promptkit", "collections", "scoped", "backends").Index(0)} { + endpoint, ok := state.inspection.Explain(base.Key("endpoint")) if !ok || !strings.Contains(endpoint.Effective.YAML, "https://example.invalid/v1") { t.Fatalf("endpoint explanation: %+v", endpoint) } - history := state.result.Metadata().History(base.Key("queue_capacity")) + history := state.inspection.Metadata().History(base.Key("queue_capacity")) found := false for _, change := range history { if change.Origin.Operation == configkit.ChangeDerivedDefault { diff --git a/internal/configuration/listing_test.go b/internal/configuration/listing_test.go index 98b6346..d7104d2 100644 --- a/internal/configuration/listing_test.go +++ b/internal/configuration/listing_test.go @@ -11,7 +11,7 @@ import ( ) func TestLoadedListsSortedEffectiveIdentities(t *testing.T) { - state, err := Load(mapping("identities", map[string]any{ + state, err := load(mapping("identities", map[string]any{ "backends": []any{map[string]any{"id": "local", "endpoint": "http://localhost:8080/v1"}}, "collections": map[string]any{ "z": map[string]any{"profiles": []any{map[string]any{"id": "profile", "backend": "local", "model": "z-model"}}, "prompts": []any{listingPromptDefinition("prompt")}}, @@ -56,7 +56,7 @@ func TestLoadedListsSortedEffectiveIdentities(t *testing.T) { } func TestLoadedListsAreIndependent(t *testing.T) { - state, err := Load(mapping("identities", map[string]any{ + state, err := load(mapping("identities", map[string]any{ "backends": []any{map[string]any{"id": "backend", "endpoint": "https://example.test/v1"}}, "profiles": []any{map[string]any{"id": "profile"}}, "prompts": []any{listingPromptDefinition("example")}, @@ -151,7 +151,7 @@ func TestLoadedListsSelectNamespaces(t *testing.T) { } func TestLoadedListsEmptyInventory(t *testing.T) { - state, err := Load(configkit.Input{Source: configkit.NoSource()}, nil) + state, err := load(configkit.Input{Source: configkit.NoSource()}, nil) if err != nil { t.Fatal(err) } @@ -170,7 +170,7 @@ func TestLoadedListsEmptyInventory(t *testing.T) { } func TestLoadedListsHonorCanceledContext(t *testing.T) { - state, err := Load(mapping("identities", map[string]any{ + state, err := load(mapping("identities", map[string]any{ "profiles": []any{map[string]any{"id": "partial"}}, "prompts": []any{listingPromptDefinition("example")}, }), nil) diff --git a/internal/configuration/load_test.go b/internal/configuration/load_test.go index f6ba06d..76e1559 100644 --- a/internal/configuration/load_test.go +++ b/internal/configuration/load_test.go @@ -28,7 +28,10 @@ func TestLoadIdentifiedResourceLists(t *testing.T) { if err != nil { t.Fatalf("load identified resources with header %t: %v", header, err) } - if loaded.result.Config.Backends[0].ID != "local" || loaded.result.Config.Collections["team"].Backends[0].ID != "local" { + if _, rootErr := loaded.GetBackend("local"); rootErr != nil { + t.Fatal(rootErr) + } + if _, collectionErr := loaded.GetBackend("team/local"); collectionErr != nil { t.Fatal("identified root and collection resources were not retained") } } @@ -201,9 +204,10 @@ func TestFinalIdentitiesAreCheckedAfterApplicationResolution(t *testing.T) { for _, kind := range []string{"backends", "profiles", "prompts"} { for _, namespace := range []string{"", "*"} { t.Run(kind+"/"+namespace, func(t *testing.T) { - definition := configurationDefinition(&loaded{}) - definition.Resolve = func(config *document, resolution *configkit.Resolution) error { - path := configkit.Keys(kind) + definition := testDefinition() + definition.Resolve = func(root *testDocument, resolution *configkit.Resolution) error { + config := &root.PromptKit + path := configkit.Keys("promptkit", kind) if namespace == "" { switch kind { case "backends": @@ -224,7 +228,7 @@ func TestFinalIdentitiesAreCheckedAfterApplicationResolution(t *testing.T) { collection.Prompts = []promptConfig{{ID: "same"}, {ID: "same"}} } config.Collections = map[string]collectionConfig{namespace: collection} - path = configkit.Keys("collections", namespace, kind) + path = configkit.Keys("promptkit", "collections", namespace, kind) } resolution.RecordNormalization(path, "supply resolved records") return nil @@ -235,7 +239,8 @@ func TestFinalIdentitiesAreCheckedAfterApplicationResolution(t *testing.T) { namespace: map[string]any{kind: []any{}}, }}) } - _, err := configkit.LoadLayered(input, configkit.LayeredOptions[document]{Definition: definition}) + input.Destination = configkit.Keys("promptkit") + _, err := configkit.LoadLayered(input, configkit.LayeredOptions[testDocument]{Definition: definition}) var loadErr *configkit.LoadError if !errors.As(err, &loadErr) { t.Fatalf("load error = %v, want a final identity diagnostic", err) @@ -272,14 +277,14 @@ func TestIdentifiedResourceLayerOperations(t *testing.T) { values := map[string]any{"profiles": test.records} if test.mode != "" { values["config"] = map[string]any{"sequences": []any{map[string]any{ - "destination": []any{"profiles"}, "mode": test.mode, + "destination": []any{"promptkit", "profiles"}, "mode": test.mode, }}} } state, err := load(mapping("overlay profiles", values), []configkit.Underlay{{Inputs: []configkit.Input{base}}}) if err != nil { t.Fatal(err) } - profiles := indexedProfiles(&state.result.Config) + profiles := state.profileRecords if len(profiles) != len(test.models) { t.Fatalf("profiles = %+v, want IDs and models %+v", profiles, test.models) } @@ -315,7 +320,7 @@ func TestIdentifiedResourceLayersRejectDisplacedParentAndDuplicateAppend(t *test { name: "append cannot duplicate an identity", values: map[string]any{ - "config": map[string]any{"sequences": []any{map[string]any{"destination": []any{"profiles"}, "mode": "append"}}}, + "config": map[string]any{"sequences": []any{map[string]any{"destination": []any{"promptkit", "profiles"}, "mode": "append"}}}, "profiles": []any{map[string]any{"id": "shared", "model": "other"}}, }, code: configkit.CodeSequenceConflict, @@ -337,62 +342,11 @@ func TestIdentifiedResourceLayersRejectDisplacedParentAndDuplicateAppend(t *test } } -func TestLoadChecksOptionalConfigurationVersion(t *testing.T) { - for _, test := range []struct { - name string - version any - include bool - valid bool - }{ - {name: "string two", version: "2", include: true, valid: true}, - {name: "integer two", version: int64(2), include: true, valid: true}, - {name: "omitted", valid: true}, - {name: "explicit null", include: true}, - {name: "unsupported string", version: "1", include: true}, - } { - t.Run(test.name, func(t *testing.T) { - values := map[string]any{} - if test.include { - values["version"] = test.version - } - loaded, err := load(mapping("main", values), nil) - if test.valid { - if err != nil { - t.Fatalf("load accepted version: %v", err) - } - if test.include && (loaded.result.Config.Version == nil || *loaded.result.Config.Version != "2") { - t.Fatalf("typed version = %v, want 2", loaded.result.Config.Version) - } - if !test.include && loaded.result.Config.Version != nil { - t.Fatalf("omitted version = %q, want nil", *loaded.result.Config.Version) - } - return - } - var loadErr *configkit.LoadError - if err == nil || !errors.As(err, &loadErr) { - t.Fatalf("load error = %v, want ConfigKit load error", err) - } - found := false - for _, diagnostic := range loadErr.Diagnostics() { - if diagnostic.Path.String() == "/version" && diagnostic.Message == "unsupported configuration specification version" { - found = true - } - } - if !found { - t.Fatalf("load diagnostics = %+v, want safe version diagnostic", loadErr.Diagnostics()) - } - }) - } -} - -func TestLoadRejectsMalformedHeadersAndLegacyResourceMappings(t *testing.T) { +func TestLoadRejectsLegacyResourceMappings(t *testing.T) { for name, values := range map[string]map[string]any{ - "boolean header": {"version": true}, - "fractional header": {"version": 2.5}, - "sequence header": {"version": []any{2}}, - "backend mapping": {"backends": map[string]any{"local": map[string]any{"endpoint": "https://example.invalid/v1"}}}, - "profile mapping": {"profiles": map[string]any{"local": map[string]any{}}}, - "prompt mapping": {"prompts": map[string]any{"local": map[string]any{}}}, + "backend mapping": {"backends": map[string]any{"local": map[string]any{"endpoint": "https://example.invalid/v1"}}}, + "profile mapping": {"profiles": map[string]any{"local": map[string]any{}}}, + "prompt mapping": {"prompts": map[string]any{"local": map[string]any{}}}, } { t.Run(name, func(t *testing.T) { _, err := load(mapping(name, values), nil) @@ -420,23 +374,10 @@ func TestNestedConfigRemainsApplicationData(t *testing.T) { } } -func TestLoadConsumesAcceptedUnderlayVersionAndValidatesHeaderlessPayload(t *testing.T) { - underlay := mapping("declared underlay", map[string]any{ - "version": int64(2), - }) - loaded, err := load(mapping("headerless main", map[string]any{}), []configkit.Underlay{{Inputs: []configkit.Input{underlay}}}) - if err != nil { - t.Fatalf("load accepted non-main declaration: %v", err) - } - if loaded.result.Config.Version != nil { - t.Fatalf("consumed non-main declaration remained in effective payload: %q", *loaded.result.Config.Version) - } - - invalidFragment := mapping("headerless prompt fragment", map[string]any{ - "prompts": []any{map[string]any{"id": "incomplete", "description": "still validated"}}, - }) - if _, err := load(configkit.Input{Source: configkit.NoSource()}, []configkit.Underlay{{Inputs: []configkit.Input{invalidFragment}}}); err == nil || !strings.Contains(err.Error(), "at least one message is required") { - t.Fatalf("headerless fragment validation error = %v, want final prompt validation", err) +func TestLoadValidatesHeaderlessPromptFragment(t *testing.T) { + invalid := mapping("prompt fragment", map[string]any{"prompts": []any{map[string]any{"id": "incomplete", "description": "still validated"}}}) + if _, err := load(configkit.Input{Source: configkit.NoSource()}, []configkit.Underlay{{Inputs: []configkit.Input{invalid}}}); err == nil || !strings.Contains(err.Error(), "at least one message is required") { + t.Fatalf("invalid fragment error = %v", err) } } @@ -484,7 +425,7 @@ func TestLoadComposesLiteralKeysAndEffectivePresence(t *testing.T) { } t.Fatal(err) } - profile := loaded.result.Config.Collections["base"].Profiles[1] + profile := loaded.profileRecords["base/child.v1"] if profile.Temperature == nil || *profile.Temperature != 0 || profile.TopP != nil || profile.APIKeyRequired == nil || *profile.APIKeyRequired { t.Fatalf("effective values were not retained: %+v", profile) } @@ -492,7 +433,7 @@ func TestLoadComposesLiteralKeysAndEffectivePresence(t *testing.T) { if provider["first"] != int64(1) || provider["second"] != int64(3) { t.Fatalf("recursive overlay failed: %+v", provider) } - backend := loaded.result.Config.Backends[0] + backend := loaded.backendRecords["local.v1"] if backend.QueueCapacity == nil || *backend.QueueCapacity != 0 { t.Fatalf("explicit zero queue capacity was lost: %+v", backend) } @@ -503,13 +444,11 @@ func TestLoadComposesLiteralKeysAndEffectivePresence(t *testing.T) { if !exists || collectionProfile.ID != "base/namespaced" || collectionProfile.ResolvedTarget == nil || collectionProfile.ResolvedTarget.BackendID != "base/local.v1" { t.Fatalf("collection profile snapshot = %+v, exists %t", collectionProfile, exists) } - if loaded.result.Config.Collections["invoice"].Prompts[0].Messages[0].Content == nil { + if loaded.prompts["invoice/summary"].definition.Templates[0].Content != "Summarize." { t.Fatal("literal prompt content was not decoded") } - if loaded.result.Config.Version == nil || *loaded.result.Config.Version != "2" || - loaded.result.Config.Collections["base"].ReleaseVersion == nil || *loaded.result.Config.Collections["base"].ReleaseVersion != "" || - loaded.result.Config.Collections["invoice"].ReleaseVersion == nil || *loaded.result.Config.Collections["invoice"].ReleaseVersion != "r7" { - t.Fatalf("root or collection version metadata was not retained: %+v", loaded.result.Config) + if len(loaded.collections) != 2 || loaded.collections[0].ReleaseVersion == nil || *loaded.collections[0].ReleaseVersion != "" || loaded.collections[1].ReleaseVersion == nil || *loaded.collections[1].ReleaseVersion != "r7" { + t.Fatalf("collection version metadata was not retained: %+v", loaded.collections) } if _, _, err := loaded.prompt("invoice/version"); err != nil { t.Fatalf("resource named version was not indexed under its qualified identity: %v", err) @@ -574,20 +513,20 @@ func TestLoadInterpretsBackendNormalizationAndNullDefault(t *testing.T) { backend.APIKeyEnv != "TOKEN_NAME" || backend.QueueCapacity != 1024 { t.Fatalf("backend normalization/default was not applied: %+v", backend) } - composed := loaded.result.Config.Backends[0] + composed := loaded.backendRecords["local"] if composed.Endpoint == nil || *composed.Endpoint != "http://localhost:8080/v1" || composed.APIKeyEnv == nil || *composed.APIKeyEnv != "TOKEN_NAME" || composed.QueueCapacity == nil || *composed.QueueCapacity != 1024 { t.Fatalf("resolved backend was not normalized: %+v", composed) } - field, found := loaded.result.Metadata().Lookup(configkit.Keys("backends").Index(0).Key("endpoint")) + field, found := loaded.inspection.Metadata().Lookup(configkit.Keys("promptkit", "backends").Index(0).Key("endpoint")) if !found || field.Origin.Operation != configkit.ChangeNormalization { t.Fatalf("endpoint origin was not preserved: %+v", field) } - field, found = loaded.result.Metadata().Lookup(configkit.Keys("backends").Index(0).Key("queue_capacity")) + field, found = loaded.inspection.Metadata().Lookup(configkit.Keys("promptkit", "backends").Index(0).Key("queue_capacity")) if !found || field.Origin.Operation != configkit.ChangeDerivedDefault { t.Fatalf("queue capacity default was not recorded: %+v", field) } - field, found = loaded.result.Metadata().Lookup(configkit.Keys("backends").Index(0).Key("concurrency_limit")) + field, found = loaded.inspection.Metadata().Lookup(configkit.Keys("promptkit", "backends").Index(0).Key("concurrency_limit")) if !found || field.Origin.Operation == configkit.ChangeNormalization || field.Origin.Operation == configkit.ChangeDerivedDefault { t.Fatalf("unchanged concurrency acquired a resolution record: %+v", field) } @@ -624,7 +563,7 @@ func TestLoadBackendSmokeTestsOmissionAndNull(t *testing.T) { if err != nil { t.Fatal(err) } - if got := loaded.result.Config.Backends[0].SmokeTests; got != nil { + if got := loaded.backendRecords["local"].SmokeTests; got != nil { t.Fatalf("smoke_tests = %+v, want nil", got) } selected, err := loaded.registry.GetBackend("local") @@ -697,32 +636,32 @@ func TestLoadRejectsInvalidFinalBackendSmokeTests(t *testing.T) { { name: "blank default profile", data: map[string]any{"default_profile": " \t "}, - path: "/backends/0/smoke_tests/default_profile", + path: "/promptkit/backends/0/smoke_tests/default_profile", }, { name: "authentication kind required", data: map[string]any{"authentication": map[string]any{}}, - path: "/backends/0/smoke_tests/authentication/kind", + path: "/promptkit/backends/0/smoke_tests/authentication/kind", }, { name: "unknown authentication kind", data: map[string]any{"authentication": map[string]any{"kind": "bearer"}}, - path: "/backends/0/smoke_tests/authentication/kind", + path: "/promptkit/backends/0/smoke_tests/authentication/kind", }, { name: "non-http reachability URL", data: map[string]any{"reachability": map[string]any{"endpoint": "ftp://probe.example/ready"}}, - path: "/backends/0/smoke_tests/reachability/endpoint", + path: "/promptkit/backends/0/smoke_tests/reachability/endpoint", }, { name: "blank authentication URL", data: map[string]any{"authentication": map[string]any{"kind": "none", "endpoint": " "}}, - path: "/backends/0/smoke_tests/authentication/endpoint", + path: "/promptkit/backends/0/smoke_tests/authentication/endpoint", }, { name: "relative authentication URL", data: map[string]any{"authentication": map[string]any{"kind": "none", "endpoint": "/key"}}, - path: "/backends/0/smoke_tests/authentication/endpoint", + path: "/promptkit/backends/0/smoke_tests/authentication/endpoint", }, } { t.Run(test.name, func(t *testing.T) { @@ -757,7 +696,7 @@ func TestLoadDefaultsAbsentQueueCapacity(t *testing.T) { t.Fatal(err) } backend, err := loaded.GetBackend("local") - if err != nil || backend.APIKeyEnv != "" || backend.QueueCapacity != 1024 || loaded.result.Config.Backends[0].QueueCapacity == nil || *loaded.result.Config.Backends[0].QueueCapacity != 1024 { + if err != nil || backend.APIKeyEnv != "" || backend.QueueCapacity != 1024 || loaded.backendRecords["local"].QueueCapacity == nil || *loaded.backendRecords["local"].QueueCapacity != 1024 { t.Fatalf("absent queue capacity did not receive its default: %+v", backend) } } @@ -769,7 +708,7 @@ func TestLoadCapturesProfilePresence(t *testing.T) { if err != nil { t.Fatal(err) } - profile := loaded.result.Config.Profiles[0] + profile := loaded.profileRecords["local"] if profile.Temperature == nil || *profile.Temperature != 0 || profile.TopP != nil || profile.APIKeyRequired == nil || *profile.APIKeyRequired { t.Fatalf("profile values were not preserved: %+v", profile) } @@ -799,7 +738,7 @@ func TestLoadValidatesOnlyFinalDomainValues(t *testing.T) { if err != nil { t.Fatalf("replaced lower-layer domain value should not fail: %v", err) } - if loaded.result.Config.Profiles[0].Model != nil { + if loaded.profileRecords["partial"].Model != nil { t.Fatal("unused partial profile unexpectedly gained a model") } @@ -810,7 +749,7 @@ func TestLoadValidatesOnlyFinalDomainValues(t *testing.T) { if !errors.As(err, &loadError) { t.Fatalf("expected path-aware effective backend failure, got %v", err) } - if diagnostics := loadError.Diagnostics(); len(diagnostics) != 1 || diagnostics[0].Path.String() != "/backends/0/endpoint" { + if diagnostics := loadError.Diagnostics(); len(diagnostics) != 1 || diagnostics[0].Path.String() != "/promptkit/backends/0/endpoint" { t.Fatalf("expected endpoint diagnostic, got %+v", diagnostics) } } diff --git a/internal/configuration/prompts_test.go b/internal/configuration/prompts_test.go index 71eeb1b..1a6ebb1 100644 --- a/internal/configuration/prompts_test.go +++ b/internal/configuration/prompts_test.go @@ -51,26 +51,18 @@ func TestLoadCapturesInheritedImportedContent(t *testing.T) { if message.Role != "user" || message.CacheControl == nil || message.CacheControl.Type != "ephemeral" || message.CacheControl.TTL != "1h" { t.Fatalf("message metadata was not normalized: %+v", message) } - childIndex := -1 - for index, item := range state.result.Config.Collections["docs"].Prompts { - if item.ID == "child" { - childIndex = index - } - } - if childIndex < 0 { - t.Fatal("inherited prompt missing from effective list") - } - composed := state.result.Config.Collections["docs"].Prompts[childIndex] + childIndex := 1 + composed := state.prompts["docs/child"].resource if *composed.Description != "description" || *composed.SessionID != "session" || composed.Inputs[0].Name != "document" || *composed.Inputs[0].ContentType != "text/plain" || *composed.Inputs[0].Description != "document source" { t.Fatalf("resolved metadata was not normalized: %+v", composed) } - field, ok := state.result.Metadata().Lookup(configkit.Keys("collections", "docs", "prompts").Index(childIndex).Key("messages").Index(0).Key("content_file")) + field, ok := state.inspection.Metadata().Lookup(configkit.Keys("promptkit", "collections", "docs", "prompts").Index(childIndex).Key("messages").Index(0).Key("content_file")) if !ok || len(field.Origin.Locations) != 1 { t.Fatalf("content origin was lost during normalization: %+v", field) } var originPath string - for _, source := range state.result.Sources() { + for _, source := range state.inspection.Sources() { if source.ID == field.Origin.Locations[0].SourceID { originPath = source.Path } @@ -172,11 +164,8 @@ func TestConfiguredRepairBudgets(t *testing.T) { if err != nil || report.Output.RepairAttempts != tc.want { t.Fatalf("inspection: %+v, %v", report, err) } - if got := state.result.Config.Prompts[0].Output.RepairAttempts; got == nil || *got != tc.want { - t.Fatalf("effective budget = %v", got) - } var effective strings.Builder - if err := state.result.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), fmt.Sprintf("repair_attempts: %d", tc.want)) { + if err := state.inspection.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), fmt.Sprintf("repair_attempts: %d", tc.want)) { t.Fatalf("effective inspection = %q, %v", effective.String(), err) } }) @@ -243,7 +232,7 @@ func TestLoadUsesExplicitMappingContextAndClosesContent(t *testing.T) { t.Fatalf("mapping context or handle ownership failed: %+v, opens=%d closes=%d, %v", definition, filesystem.contentOpened, filesystem.contentClosed, err) } _, err = load(mapping("contextless", promptWithFile("body.md")), nil) - if err == nil || !strings.Contains(err.Error(), "/prompts/0/messages/0/content_file") { + if err == nil || !strings.Contains(err.Error(), "/promptkit/prompts/0/messages/0/content_file") { t.Fatalf("expected semantic path for contextless resource, got %v", err) } } @@ -378,8 +367,12 @@ func TestLoadAcceptsEmptyInlineContentAndRejectsInvalidPromptFields(t *testing.T "id": "example", "messages": []any{map[string]any{"role": "user", "content": "ok"}}, "output": map[string]any{"format": "text", "validation_mode": "basic"}, }}}), nil) - if err != nil || extended.result.Config.Prompts[0].Application["version"] != "v1" { - t.Fatalf("application field not retained: %+v, %v", extended, err) + if err != nil { + t.Fatal(err) + } + var effective strings.Builder + if err := extended.inspection.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "version: v1") { + t.Fatalf("application field not retained: %q, %v", effective.String(), err) } _, err = load(mapping("null-prompt", map[string]any{"prompts": []any{nil}}), nil) if err == nil { diff --git a/internal/configuration/targets_test.go b/internal/configuration/targets_test.go index 013811a..3945112 100644 --- a/internal/configuration/targets_test.go +++ b/internal/configuration/targets_test.go @@ -229,7 +229,7 @@ func TestProfileNullFallbackAndExplicitValues(t *testing.T) { t.Errorf("%s target = %+v", id, target) } } - child := state.result.Config.Profiles[1] + child := state.profileRecords["inherited-null"] if child.Endpoint != nil || child.ExtraParams != nil || child.APIKeyEnv != nil { t.Fatalf("inherited nulls were not retained in resolved values: %+v", child) } diff --git a/internal/configuration/test_helpers_test.go b/internal/configuration/test_helpers_test.go new file mode 100644 index 0000000..5e72f53 --- /dev/null +++ b/internal/configuration/test_helpers_test.go @@ -0,0 +1,63 @@ +package configuration + +import ( + "fmt" + + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit/internal/settings" +) + +type testDocument struct { + PromptKit settings.Settings `yaml:"promptkit"` + Application map[string]any `yaml:",inline"` +} + +type testLoaded struct { + *loaded + inspection configkit.Inspection +} + +func testDefinition() configkit.Definition[testDocument] { + return configkit.Definition[testDocument]{ + SequenceIdentities: SequenceIdentities(), + Sensitive: Sensitive, + Resolve: func(value *testDocument, resolution *configkit.Resolution) error { + Resolve(&value.PromptKit, resolution) + return nil + }, + Validate: func(value *testDocument, validation *configkit.Validation) { Validate(&value.PromptKit, validation) }, + } +} + +// The adapter fixtures are wrapperless settings fragments. Mount each with +// ConfigKit's native destination and run the same rules and capture as callers. +func load(main configkit.Input, underlays []configkit.Underlay) (*testLoaded, error) { + mount := func(input configkit.Input) configkit.Input { + if input.Destination.String() == "" { + input.Destination = configkit.Keys("promptkit") + } + return input + } + main = mount(main) + selected := make([]configkit.Underlay, len(underlays)) + for i, underlay := range underlays { + selected[i] = underlay + selected[i].Inputs = make([]configkit.Input, len(underlay.Inputs)) + for j, input := range underlay.Inputs { + selected[i].Inputs[j] = mount(input) + } + } + result, err := configkit.LoadLayered(main, configkit.LayeredOptions[testDocument]{Definition: testDefinition(), Underlays: selected}) + if err != nil { + return nil, fmt.Errorf("load configuration: %w", err) + } + view, ok := result.InspectSubtree(configkit.Keys("promptkit")) + if !ok { + return nil, fmt.Errorf("inspection unavailable") + } + state, err := Capture(&result.Config.PromptKit, view, result.Diagnostics()) + if err != nil { + return nil, fmt.Errorf("capture configuration: %w", err) + } + return &testLoaded{loaded: state, inspection: view}, nil +}