Prepare item and enemy event sort keys once

This commit is contained in:
2026-11-26 03:00:08 +00:00
parent 7077cee35e
commit 97e9d51db0
10 changed files with 176 additions and 57 deletions

View File

@@ -92,6 +92,7 @@ type normalizedRecord struct {
occurrence dnd.ItemOccurrence
identity string
inputIndex int
orderKey itemoccurrencemodel.CanonicalOrderKey
}
func normalizeList(input dnd.ItemOccurrenceList, index source.DocumentIndex, order shared.SourceRefOrder, registry *itemregistry.Registry) (dnd.ItemOccurrenceList, []diagnostics.Finding) {
@@ -103,7 +104,7 @@ func normalizeList(input dnd.ItemOccurrenceList, index source.DocumentIndex, ord
findings := make([]diagnostics.Finding, 0)
for index, inputOccurrence := range input.Occurrences {
occurrence, changedFields, found, refsChanged := normalizeOccurrence(inputOccurrence, order, registry)
records[index] = normalizedRecord{occurrence: occurrence, identity: itemoccurrencemodel.CanonicalExactIdentity(occurrence), inputIndex: index}
records[index] = normalizedRecord{occurrence: occurrence, identity: itemoccurrencemodel.CanonicalExactIdentity(occurrence), inputIndex: index, orderKey: itemoccurrencemodel.PrepareCanonicalOrder(order, occurrence)}
if len(changedFields) != 0 {
findings = append(findings, diagnostics.Finding{
Scope: occurrenceScope(index),
@@ -131,7 +132,8 @@ func normalizeList(input dnd.ItemOccurrenceList, index source.DocumentIndex, ord
}
sort.SliceStable(records, func(left, right int) bool {
return itemoccurrencemodel.CanonicalLess(order, records[left].occurrence, records[right].occurrence)
l, r := records[left], records[right]
return itemoccurrencemodel.CanonicalLessPrepared(order, l.occurrence, r.occurrence, l.orderKey, r.orderKey)
})
for position, record := range records {
if position == record.inputIndex {

View File

@@ -2,6 +2,7 @@ package itemoccurrences
import (
"context"
"reflect"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
@@ -63,6 +64,33 @@ func TestNormalizeRequiresRegistryAndRegistersSlot(t *testing.T) {
}
}
func TestNormalizeOrdersTiedItemEvidenceWithoutMergingIt(t *testing.T) {
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 50}, {ID: 10}, {ID: 90}}}
ref := func(id int) source.SourceRef {
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
}
item := func(refs ...source.SourceRef) dnd.ItemOccurrence {
return dnd.ItemOccurrence{ItemID: itemidentity.DeriveID("Torch"), Name: "Torch", Kind: dnd.ItemOccurrenceKindDiscovered, SourceRefs: refs}
}
earlier, later, invalid := item(ref(50), ref(10)), item(ref(50), ref(90)), item(ref(999))
input := dnd.ItemOccurrenceList{Occurrences: []dnd.ItemOccurrence{later, invalid, earlier, later, invalid}}
references := registryReferences(t)
normalizer, err := New(Options{}, references)
if err != nil {
t.Fatal(err)
}
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.ItemOccurrenceList]{Source: doc, References: references, MergeOutput: contracts.MergeArtifact[dnd.ItemOccurrenceList]{Value: input}})
if err != nil {
t.Fatal(err)
}
if want := []dnd.ItemOccurrence{earlier, later, invalid, invalid}; !reflect.DeepEqual(result.Value.Occurrences, want) {
t.Fatalf("occurrences = %#v, want %#v", result.Value.Occurrences, want)
}
if !hasDiagnostic(result.Diagnostics, ReasonCodeOccurrencesReordered, contracts.DiagnosticDispositionObservation) || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateCollapsed, contracts.DiagnosticDispositionObservation) {
t.Fatalf("diagnostics = %#v", result.Diagnostics)
}
}
func hasDiagnostic(diagnostics []contracts.ProducerDiagnostic, reason string, disposition contracts.DiagnosticDisposition) bool {
for _, diagnostic := range diagnostics {
if diagnostic.ReasonCode == reason && diagnostic.Disposition == disposition {