Select history using matched entry positions

This commit is contained in:
2026-11-02 14:49:32 +00:00
parent 4549fbaeef
commit 151b56e7ec
4 changed files with 98 additions and 18 deletions

View File

@@ -265,3 +265,48 @@ func TestRegisteredResolutionValidationLocationFollowsMovedRecord(t *testing.T)
}
t.Fatalf("diagnostics = %#v", failure.Diagnostics())
}
func TestRegisteredResolutionHistoryFollowsMatchedSource(t *testing.T) {
type item struct {
ID string `yaml:"id"`
Value string `yaml:"value"`
}
type config struct {
Items []item `yaml:"items"`
}
definition := DefinitionWithSequenceIdentityRegistrations(Definition[config]{Resolve: func(value *config, r *Resolution) error {
value.Items[0], value.Items[1] = value.Items[1], value.Items[0]
value.Items = append(value.Items, item{ID: "new", Value: "added"})
r.RecordNormalization(Keys("items"), "reorder and add")
return nil
}}, []SequenceIdentityRegistrationInput{{ExactPath: pathPointer(Keys("items")), IdentityFields: []string{"id"}}})
result, err := testLoad(Bytes("records", []byte("items:\n - id: a\n value: first\n - id: b\n value: second\n")), Options[config]{Definition: definition})
if err != nil {
t.Fatal(err)
}
for _, check := range []struct{ index, line int }{{0, 5}, {1, 3}} {
path := Keys("items").Index(check.index).Key("value")
if location, ok := result.history.latestHistoricalLocation(path); !ok || location.Line != check.line {
t.Errorf("historical location for %s = %#v, %t", path.String(), location, ok)
}
history := result.Metadata().History(path)
found := false
for _, change := range history {
for _, location := range change.Origin.Locations {
if location.Line == check.line {
found = true
} else if location.Line == 3 || location.Line == 5 {
t.Errorf("wrong record in %s: %#v", path.String(), history)
}
}
}
if !found {
t.Errorf("missing source line %d in %#v", check.line, history)
}
}
for _, change := range result.Metadata().History(Keys("items").Index(2).Key("value")) {
if change.Origin.Layer == LayerMain {
t.Fatalf("added record inherited input: %#v", change)
}
}
}