Migrate configuration tests to captured resources

This commit is contained in:
2026-11-21 14:29:06 +00:00
parent 74a77b3c32
commit 92e1e225b2
11 changed files with 164 additions and 165 deletions

View File

@@ -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)
}
}