Resolve inheritance between identified records

This commit is contained in:
2026-10-23 20:48:46 +00:00
parent 268fd03579
commit 33a51606da
10 changed files with 447 additions and 31 deletions

View File

@@ -147,6 +147,93 @@ func TestIdentifiedInheritanceScopeAdapterReusesTemplateExecution(t *testing.T)
}
}
func TestIdentifiedInheritanceResolvesNestedCollectionsAtEachDestination(t *testing.T) {
root := parseItemPolicyDocument(t, "identified-nested", `jobs:
- id: child
extends: {id: parent}
- id: parent
extends: {id: base}
steps:
- id: child-step
extends: {id: parent-step}
value: child-value
- id: parent-step
value: parent-value
- id: base
steps:
- id: base-step
value: base-value
`)
identity := collectionPolicy{identityFields: []string{"id"}, identityPresent: true}
policies := collectionPolicyScope{}
policies.set(Keys("jobs"), identity)
for index := 0; index < 3; index++ {
policies.set(Keys("jobs").Index(index).Key("steps"), identity)
}
expanded, diagnostics := expandInheritanceWithCollectionRules(root, nil, policies, sequencePolicy{}, defaultProjectionLimits())
if len(diagnostics) != 0 {
t.Fatalf("expand nested identified inheritance: %#v", diagnostics)
}
steps, found := documentAtPath(expanded.root, Keys("jobs").Index(0).Key("steps"))
if !found || steps.Kind != document.Sequence || len(steps.Sequence) != 3 {
t.Fatalf("child nested steps = %#v, found %t", steps, found)
}
for index, want := range []string{"base-step", "child-step", "parent-step"} {
id, exists := documentAtPath(steps.Sequence[index], Keys("id"))
if !exists || id.Text != want {
t.Fatalf("child step %d identity = %#v, found %t; want %q", index, id, exists, want)
}
}
value, found := documentAtPath(steps.Sequence[1], Keys("value"))
if !found || value.Text != "child-value" {
t.Fatalf("nested record inheritance = %#v, found %t", value, found)
}
if containsInheritanceDirective(expanded.root) {
t.Fatal("a nested record relationship remained after destination-local expansion")
}
childIdentity, found := expanded.origins["/jobs/0/id"]
if !found || len(childIdentity.Locations) == 0 || childIdentity.Locations[0].SourceID != "identified-nested" {
t.Fatalf("child identity origin = %#v, found %t", childIdentity, found)
}
if _, found := expanded.origins["/jobs/0/steps/1/value"]; !found {
t.Fatal("nested inherited value did not retain its destination origin")
}
for _, replacement := range expanded.replacements {
if replacement.path.String() == "/jobs/0/id" {
t.Fatal("parent identity was treated as payload replaced by the child's identity")
}
}
}
func TestIdentifiedInheritanceRespectsOutputNodeBudget(t *testing.T) {
root := parseItemPolicyDocument(t, "identified-budget", `jobs:
- id: child
extends: {id: parent}
- id: parent
value: inherited
`)
policies := collectionPolicyScope{}
policies.set(Keys("jobs"), collectionPolicy{identityFields: []string{"id"}, identityPresent: true})
baseline, diagnostics := expandInheritanceWithCollectionRules(root, nil, policies, sequencePolicy{}, defaultProjectionLimits())
if len(diagnostics) != 0 {
t.Fatalf("unbounded identified inheritance: %#v", diagnostics)
}
outputNodes, within := inheritanceLowerBoundCount(baseline.root, -1)
if !within || outputNodes < 2 {
t.Fatalf("identified output node count = %d, within %t", outputNodes, within)
}
limits := defaultProjectionLimits()
limits.nodes = outputNodes
if result, diagnostics := expandInheritanceWithCollectionRules(root, nil, policies, sequencePolicy{}, limits); result == nil || len(diagnostics) != 0 {
t.Fatalf("at output-node boundary: result = %#v, diagnostics = %#v", result, diagnostics)
}
limits.nodes = outputNodes - 1
if result, diagnostics := expandInheritanceWithCollectionRules(root, nil, policies, sequencePolicy{}, limits); result != nil ||
len(diagnostics) != 1 || diagnostics[0].Code != CodeSchemaProjectionLimit {
t.Fatalf("below output-node boundary: result = %#v, diagnostics = %#v", result, diagnostics)
}
}
func TestIdentifiedInheritanceRequiresChildIdentityBeforeParentLookup(t *testing.T) {
sequence := parseIdentifiedInheritanceSequence(t, "missing-child-field", `- id: child
extends: {id: parent, tenant: acme}