Prepare deterministic D&D normalization in core

This commit is contained in:
2026-11-25 15:49:34 +00:00
parent e3ddc177ab
commit 99f00c808c
16 changed files with 172 additions and 79 deletions

View File

@@ -23,7 +23,7 @@ func TestNormalizeCanonicalizesNamesByIDAndClonesInputs(t *testing.T) {
SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}},
}}}
before := cloneList(input)
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
if err != nil {
t.Fatal(err)
}
@@ -34,7 +34,7 @@ func TestNormalizeCanonicalizesNamesByIDAndClonesInputs(t *testing.T) {
if !hasDiagnostic(result.Diagnostics, ReasonCodeNameCanonicalized, contracts.DiagnosticDispositionObservation) || !hasDiagnostic(result.Diagnostics, ReasonCodeSourceRefsNormalized, contracts.DiagnosticDispositionObservation) || !reflect.DeepEqual(input, before) {
t.Fatalf("diagnostics/input = %#v/%#v", result.Diagnostics, input)
}
second, err := normalizer.Normalize(context.Background(), normalizeRequest(result.Value, doc, contracts.ReferenceSet{}))
second, err := runNormalize(normalizer, context.Background(), normalizeRequest(result.Value, doc, contracts.ReferenceSet{}))
if err != nil || !reflect.DeepEqual(second.Value, result.Value) || len(second.Diagnostics) != 0 {
t.Fatalf("second normalization = %#v, %v", second, err)
}
@@ -62,7 +62,7 @@ func TestNormalizeKeepsSameNamedIDsAndDistinctEvidence(t *testing.T) {
{LocationID: first.ID, Name: first.Name, Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(10)}},
{LocationID: first.ID, Name: first.Name, Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{ref(999)}},
}}
result, err := newNormalizer(t, registryReferences(t, locations)).Normalize(context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
result, err := runNormalize(newNormalizer(t, registryReferences(t, locations)), context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
if err != nil {
t.Fatal(err)
}
@@ -79,14 +79,14 @@ func TestNormalizePreservesUnknownIDsAndMalformedOperationRegistry(t *testing.T)
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}}}
locations := registryLocations("The Mill")
unknown := dnd.LocationOccurrence{LocationID: "location:sha256:unknown", Name: "The Mill", Kind: "unexpected", SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}}
result, err := newNormalizer(t, registryReferences(t, locations)).Normalize(context.Background(), normalizeRequest(dnd.LocationOccurrenceList{Occurrences: []dnd.LocationOccurrence{unknown}}, doc, contracts.ReferenceSet{}))
result, err := runNormalize(newNormalizer(t, registryReferences(t, locations)), context.Background(), normalizeRequest(dnd.LocationOccurrenceList{Occurrences: []dnd.LocationOccurrence{unknown}}, doc, contracts.ReferenceSet{}))
if err != nil || !reflect.DeepEqual(result.Value.Occurrences[0], unknown) || !hasDiagnostic(result.Diagnostics, ReasonCodeUnknownLocationID, contracts.DiagnosticDispositionAdvisory) {
t.Fatalf("unknown normalization = %#v, %v", result, err)
}
malformed := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{LocationRegistryReferenceSlot: {
Items: []contracts.ReferenceItem{{SlotName: LocationRegistryReferenceSlot, MediaType: "application/json", Content: []byte(`{"private":"registry evidence"}`)}},
}}}
if _, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(dnd.LocationOccurrenceList{}, doc, malformed)); err == nil || !strings.Contains(err.Error(), "resolve location registry") || strings.Contains(err.Error(), "registry evidence") {
if _, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(dnd.LocationOccurrenceList{}, doc, malformed)); err == nil || !strings.Contains(err.Error(), "resolve location registry") || strings.Contains(err.Error(), "registry evidence") {
t.Fatalf("operation registry error = %v", err)
}
}
@@ -114,7 +114,7 @@ func TestNormalizerContractsRequiredRegistryAndFindingBounds(t *testing.T) {
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
t.Fatal("DecodeOptions() accepted unknown options")
}
if _, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(dnd.LocationOccurrenceList{}, nil, contracts.ReferenceSet{})); err == nil || !strings.Contains(err.Error(), "required") {
if _, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(dnd.LocationOccurrenceList{}, nil, contracts.ReferenceSet{})); err == nil || !strings.Contains(err.Error(), "required") {
t.Fatalf("unbound registry error = %v", err)
}
@@ -126,7 +126,7 @@ func TestNormalizerContractsRequiredRegistryAndFindingBounds(t *testing.T) {
doc.Units[index].ID = index + 1
input.Occurrences[index] = dnd.LocationOccurrence{LocationID: location.ID, Name: "not canonical", Kind: dnd.LocationOccurrenceKindMentioned, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: count - index, EndUnitID: count - index}}}
}
bounded, err := newNormalizer(t, registryReferences(t, dnd.LocationRegistry{Locations: []dnd.Location{location}})).Normalize(context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
bounded, err := runNormalize(newNormalizer(t, registryReferences(t, dnd.LocationRegistry{Locations: []dnd.Location{location}})), context.Background(), normalizeRequest(input, doc, contracts.ReferenceSet{}))
if err != nil || len(bounded.Diagnostics) == 0 {
t.Fatalf("bounded diagnostics = %#v, %v", bounded.Diagnostics, err)
}
@@ -161,8 +161,8 @@ func newNormalizer(t *testing.T, references ...contracts.ReferenceSet) *Normaliz
return normalizer
}
func normalizeRequest(value dnd.LocationOccurrenceList, doc *source.SourceDocument, references contracts.ReferenceSet) contracts.TypedNormalizeRequest[dnd.LocationOccurrenceList] {
return contracts.TypedNormalizeRequest[dnd.LocationOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.LocationOccurrenceList]{Value: value}, References: references}
func normalizeRequest(value dnd.LocationOccurrenceList, doc *source.SourceDocument, references contracts.ReferenceSet) contracts.NormalizeInput[dnd.LocationOccurrenceList] {
return contracts.NormalizeInput[dnd.LocationOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.LocationOccurrenceList]{Value: value}, References: references}
}
func registryLocations(names ...string) dnd.LocationRegistry {