Prepare collection policy transport through inheritance

This commit is contained in:
2026-10-23 20:04:06 +00:00
parent 80d8c1dc82
commit 9e75496664
8 changed files with 282 additions and 46 deletions

View File

@@ -110,6 +110,91 @@ beta:
}
}
func TestInheritanceTransportsReusableCollectionPolicyTemplates(t *testing.T) {
root := parseItemPolicyDocument(t, "templates", `base:
prompts:
- id: shared
children:
derived:
extends: nested-base
own: destination-local
nested-base:
inherited: source-value
first:
extends: base
second:
extends: base
`)
policyLocation := Location{SourceID: "base-resource", Line: 2, Column: 3}
nestedLocation := Location{SourceID: "nested-resource", Line: 5, Column: 11}
nested := collectionPolicy{identityFields: []string{"key"}, identityPresent: true, identityLocation: &nestedLocation}
recordScope := collectionPolicyScope{}
recordScope.set(Keys("children", "prompts"), nested)
basePolicy := collectionPolicy{identityFields: []string{"id"}, identityPresent: true, identityLocation: &policyLocation}
basePolicy.setRecordPolicyAt(0, collectionRecordPolicy{owner: newCollectionRecordPolicy().owner, scope: recordScope})
policies := collectionPolicyScope{}
policies.set(Keys("base", "prompts"), basePolicy)
got, diagnostics := expandSiblingDefinitionsWithPolicies(root, Path{}, defaultProjectionLimits(), nil, policies)
if len(diagnostics) != 0 {
t.Fatalf("expand sibling templates: %#v", diagnostics)
}
withoutPolicies, diagnostics := expandSiblingDefinitionsWithOrigins(root, Path{}, defaultProjectionLimits(), nil)
if len(diagnostics) != 0 {
t.Fatalf("expand sibling templates without policy: %#v", diagnostics)
}
if !reflect.DeepEqual(got.root, withoutPolicies.root) {
t.Fatalf("policy transport changed inherited values:\n got %#v\nwant %#v", got.root, withoutPolicies.root)
}
var transfers []inheritanceOperation
for _, operation := range got.operations {
if operation.transfer != nil {
transfers = append(transfers, operation)
}
}
if len(transfers) != 2 {
t.Fatalf("transfer operations = %d, want two destination transfers", len(transfers))
}
for index, destination := range []string{"/first", "/second"} {
operation := transfers[index]
if operation.transfer == nil || len(operation.transfer.projectedPolicies) != 2 {
t.Fatalf("transfer %d projected policies = %#v, want collection and record-local policy", index, operation.transfer)
}
projected := operation.transfer.projectedPolicies
if projected[0].path.String() != destination+"/prompts" || projected[1].path.String() != destination+"/prompts/0/children/prompts" {
t.Fatalf("transfer %d policy paths = %q, %q", index, projected[0].path, projected[1].path)
}
if got := projected[0].policy.identityLocation; got == nil || *got != policyLocation {
t.Fatalf("transfer %d collection declaration location = %#v", index, got)
}
if got := projected[1].policy.identityLocation; got == nil || *got != nestedLocation {
t.Fatalf("transfer %d record-local declaration location = %#v", index, got)
}
if len(operation.templatePolicies.collections) != 0 {
t.Fatalf("transfer %d unexpectedly copied parent YAML policy onto child template", index)
}
}
first, second := transfers[0].transfer.projectedPolicies[0].policy, transfers[1].transfer.projectedPolicies[0].policy
if first.recordOrder[0] == second.recordOrder[0] {
t.Fatal("destination policy templates share a mutable record owner")
}
first.identityFields[0] = "changed"
if second.identityFields[0] != "id" {
t.Fatalf("mutating one destination changed another: %v", second.identityFields)
}
if basePolicy.identityFields[0] != "id" {
t.Fatalf("mutating a destination changed the source template: %v", basePolicy.identityFields)
}
firstValue, found := documentAtPath(got.root.Mapping[1].Value, Keys("prompts").Index(0).Key("children").Key("derived").Key("extends"))
if !found || firstValue.Text != "nested-base" {
t.Fatalf("nested template instruction = %#v, found %t; want original instruction retained", firstValue, found)
}
}
func TestInheritanceExpansionInterleavesUnequalBranches(t *testing.T) {
root := parseCompositionDocument(t, "branches", `a-grandchild:
extends: z-child