Integrate history prefixes into load handoffs

This commit is contained in:
2026-10-26 17:15:30 +00:00
parent 738c8a9cc7
commit 408dd13c24
7 changed files with 200 additions and 20 deletions

View File

@@ -71,9 +71,68 @@ func TestIndexedNearestLocationsPreserveMissingAndSyntheticCoordinates(t *testin
}
final := parseDocumentForDecode(t, "value: cli\n")
synthetic := Location{SourceID: "cli-1"}
metadata := Metadata{fields: map[string]FieldInfo{"/value": {Origin: Origin{Layer: LayerCLI, Locations: []Location{synthetic}}}}}
metadata := Metadata{
effectivePaths: effectivePathIndex(final),
fields: map[string]FieldInfo{"/value": {Origin: Origin{Layer: LayerCLI, Locations: []Location{synthetic}}}},
}
located := documentWithMetadataLocations(final, metadata, root)
if location := located.Mapping[0].Value.Location; location.SourceID != "cli-1" || location.Line != 0 || location.Column != 0 {
t.Fatalf("synthetic CLI location was replaced by fallback: %#v", location)
}
}
func TestMetadataLocationFallbackDoesNotCrossKeyIndexShapes(t *testing.T) {
fallback := parseDocumentForDecode(t, "settings:\n - value:\n child: input\n")
final := parseDocumentForDecode(t, "settings:\n '0':\n value: final\n")
metadata := Metadata{
effectivePaths: effectivePathIndex(final),
fields: map[string]FieldInfo{"/settings/0/value": {Origin: Origin{Layer: LayerDefault}}},
}
located := documentWithMetadataLocations(final, metadata, fallback)
settings, _ := documentAtPath(located, Keys("settings"))
value, _ := documentAtPath(located, Keys("settings", "0", "value"))
if settings == nil || value == nil || value.Location != settings.Location {
t.Fatalf("fallback crossed from a mapping key to an input sequence item: settings %#v, value %#v", settings, value)
}
inputChild, _ := documentAtPath(fallback, Keys("settings").Index(0).Key("value").Key("child"))
if inputChild == nil || value.Location == inputChild.Location {
t.Fatalf("fallback fabricated a key location from an index: input child %#v, value %#v", inputChild, value)
}
}
func TestHistoricalLocationSelectionSeparatesNumericKeyAndIndex(t *testing.T) {
mappingRoot := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
Key: "shape", Value: &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
Key: "0", Value: &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
Key: "value", Value: &document.Node{Kind: document.String, Text: "mapping", Location: document.Location{SourceID: "mapping", Line: 3, Column: 5}},
}}},
}}},
}}}
sequenceRoot := &document.Node{Kind: document.Mapping, Mapping: []document.Entry{{
Key: "shape", Value: &document.Node{Kind: document.Sequence, Sequence: []*document.Node{{
Kind: document.Mapping, Mapping: []document.Entry{{
Key: "value", Value: &document.Node{Kind: document.String, Text: "sequence", Location: document.Location{SourceID: "sequence", Line: 3, Column: 5}},
}},
}}},
}}}
builder := newHistoryBuilder()
builder.observeSourceEvidence(sequenceRoot, nil, []historyApplication{
{layer: LayerBase, value: mappingRoot},
{layer: LayerOverlay, value: sequenceRoot},
}, nil)
model := builder.freeze()
metadata := Metadata{historyModel: model}
resolutionOrigin := Origin{Layer: LayerResolution, Operation: ChangeNormalization}
for _, test := range []struct {
path Path
want string
}{
{Keys("shape", "0", "value"), "mapping"},
{Keys("shape").Index(0).Key("value"), "sequence"},
} {
location, ok := metadataFieldLocation(metadata, test.path, resolutionOrigin)
if !ok || location.SourceID != test.want {
t.Fatalf("historical location for %s = %#v, %v; want %q", test.path, location, ok, test.want)
}
}
}