package engine import ( "fmt" "strings" "testing" ) func TestRecordInheritanceHistoryPreservesChildIdentity(t *testing.T) { t.Parallel() for _, compound := range []bool{false, true} { t.Run(fmt.Sprintf("compound=%t", compound), func(t *testing.T) { key, reference := "id", "{id: base}" identities := map[string]string{"id": "child"} if compound { key, reference = "[id, tenant]", "{id: base, tenant: parent-team}" identities["tenant"] = "child-team" } body := "configkit:\n sequences: [{destination: [items], key: " + key + "}]\nitems:\n - id: base\n tenant: parent-team\n value: inherited\n - id: child\n tenant: child-team\n extends: " + reference + "\n" loaded, err := testLoad(Bytes("records", []byte(body)), Options[map[string]any]{}) if err != nil { t.Fatal(err) } child := Keys("items").Index(1) for field, want := range identities { path := child.Key(field) info, found := loaded.Metadata().Lookup(path) if !found || !info.Explicit || len(info.Origin.Inheritance) != 0 { t.Fatalf("child identity provenance = %#v, found %t", info, found) } for _, change := range loaded.Metadata().History(path) { if change.Origin.Operation == ChangeInheritance { t.Fatalf("child identity received inheritance history: %#v", change) } } explanation, found := loaded.Explain(path) if !found { t.Fatalf("missing identity explanation at %s", path) } for _, change := range explanation.History { for _, snapshot := range []ValueSnapshot{change.Before, change.After} { if snapshot.Present && snapshot.YAML != want+"\n" { t.Fatalf("child identity snapshot = %q, want %q", snapshot.YAML, want) } } } } for _, query := range []Path{child, Keys("items"), {}} { explanation, found := loaded.Explain(query) if !found { t.Fatalf("missing ancestor explanation at %s", query) } relative, _ := relativeHistoryPath(query, child) for _, change := range explanation.History { for _, snapshot := range []ValueSnapshot{change.Before, change.After} { if !snapshot.Present { continue } wrapped := "snapshot:\n " + strings.ReplaceAll(snapshot.YAML, "\n", "\n ") + "\n" root := parseItemPolicyDocument(t, "history", wrapped) for field, want := range identities { value, present := documentAtPath(root, appendDestination(Keys("snapshot"), relative.Key(field))) if present && value.Text != want { t.Fatalf("ancestor %s snapshot has child %s = %q", query, field, value.Text) } } } } } payload, found := loaded.Explain(child.Key("value")) if !found || payload.Effective.YAML != "inherited\n" { t.Fatalf("inherited payload = %#v, found %t", payload, found) } inherited := false for _, change := range payload.History { inherited = inherited || change.Origin.Operation == ChangeInheritance } if !inherited { t.Fatal("ordinary inherited payload lost its history") } parent, found := loaded.Explain(Keys("items").Index(0).Key("id")) if !found || parent.Effective.YAML != "base\n" || len(parent.History) == 0 { t.Fatalf("parent identity history = %#v, found %t", parent, found) } }) } }