Fix nested inheritance history and budget accounting

This commit is contained in:
2026-09-30 00:04:02 +00:00
parent b45f7a0410
commit 83c59ea7d3
5 changed files with 187 additions and 9 deletions

View File

@@ -8,6 +8,98 @@ import (
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
)
func TestNestedInheritanceHistoryRetainsUnresolvedTemplates(t *testing.T) {
input := `presets:
base:
settings:
primary: {width: 10}
secondary: {extends: primary}
custom:
extends: base
settings:
primary: {width: 20}
grandchild:
extends: custom
settings:
primary: {width: 30}
`
loaded, err := Load[map[string]any](Bytes("nested", []byte(input)), testSecretBinding())
if err != nil {
t.Fatal(err)
}
for name, width := range map[string]string{"custom": "20", "grandchild": "30"} {
t.Run(name, func(t *testing.T) {
root := Keys("presets", name)
value, _ := loaded.Inspection.Explain(root.Key("settings").Key("secondary").Key("width"))
introduced := false
for _, change := range value.History {
if change.Origin.Operation == ChangeInheritance && !change.Before.Present && change.After.YAML == width+"\n" {
introduced = true
}
}
if !introduced || value.Effective.YAML != width+"\n" {
t.Fatalf("missing nested introduction: %#v", value)
}
controlPath := root.Key("settings").Key("secondary").Key("extends")
control, _ := loaded.Inspection.Explain(controlPath)
arrived, consumed := false, false
for _, change := range control.History {
arrived = arrived || change.After.YAML == "primary\n"
consumed = consumed || (change.Before.YAML == "primary\n" && !change.After.Present)
}
if !arrived || !consumed || control.Effective.Present {
t.Fatalf("missing control history: %#v", control)
}
container, _ := loaded.Inspection.Explain(root)
unresolved := false
for _, change := range container.History {
if change.Origin.Operation == ChangeInheritance && strings.Contains(change.After.YAML, "extends: primary") {
unresolved = true
}
}
if !unresolved || strings.Contains(container.Effective.YAML, "extends:") {
t.Fatalf("invalid enclosing snapshot: %#v", container)
}
if _, present := loaded.Inspection.Metadata().Lookup(controlPath); present {
t.Fatal("control is effective")
}
if len(loaded.Inspection.Metadata().History(controlPath)) != len(control.History) {
t.Fatal("metadata and explanation disagree")
}
})
}
}
func TestNestedInheritanceHistoryInsideSequence(t *testing.T) {
input := `base:
items:
- first: {value: inherited}
second: {extends: first}
child: {extends: base}
`
loaded, err := Load[map[string]any](Bytes("sequence", []byte(input)), testSecretBinding())
if err != nil {
t.Fatal(err)
}
path := Keys("child", "items").Index(0).Key("second")
control, _ := loaded.Inspection.Explain(path.Key("extends"))
consumed := false
for _, change := range control.History {
consumed = consumed || (change.Before.YAML == "first\n" && !change.After.Present)
}
if !consumed || control.Effective.Present {
t.Fatalf("sequence control history = %#v", control)
}
value, _ := loaded.Inspection.Explain(path.Key("value"))
introduced := false
for _, change := range value.History {
introduced = introduced || (!change.Before.Present && change.After.YAML == "inherited\n")
}
if !introduced || value.Effective.YAML != "inherited\n" {
t.Fatalf("sequence value history = %#v", value)
}
}
func inheritanceInspectionForTest(t *testing.T, sources ...*document.Node) (*Inspection, *inheritanceScope) {
t.Helper()
composed, diagnostics := composeDocuments(sources...)