Prepare item and enemy event sort keys once
This commit is contained in:
@@ -93,6 +93,7 @@ type normalizedRecord struct {
|
||||
event dnd.EnemyEvent
|
||||
identity string
|
||||
inputIndex int
|
||||
orderKey enemyeventmodel.CanonicalOrderKey
|
||||
}
|
||||
|
||||
type nameCanonicalization struct {
|
||||
@@ -108,7 +109,7 @@ func normalizeList(input dnd.EnemyEventList, order shared.SourceRefOrder, regist
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
for index, inputEvent := range input.Events {
|
||||
event, nameChange, refsChanged := normalizeEvent(inputEvent, order, registry)
|
||||
records[index] = normalizedRecord{event: event, identity: enemyeventmodel.CanonicalIdentity(event), inputIndex: index}
|
||||
records[index] = normalizedRecord{event: event, identity: enemyeventmodel.CanonicalIdentity(event), inputIndex: index, orderKey: enemyeventmodel.PrepareCanonicalOrder(order, event)}
|
||||
if nameChange != nil {
|
||||
findings = append(findings, diagnostics.Finding{
|
||||
Scope: eventScope(index),
|
||||
@@ -128,7 +129,8 @@ func normalizeList(input dnd.EnemyEventList, order shared.SourceRefOrder, regist
|
||||
}
|
||||
|
||||
sort.SliceStable(records, func(left, right int) bool {
|
||||
return enemyeventmodel.Less(order, records[left].event, records[right].event)
|
||||
l, r := records[left], records[right]
|
||||
return enemyeventmodel.CanonicalLessPrepared(order, l.event, r.event, l.orderKey, r.orderKey)
|
||||
})
|
||||
for position, record := range records {
|
||||
if position == record.inputIndex {
|
||||
|
||||
@@ -81,6 +81,28 @@ func TestNormalizeUsesExplicitKindOrderAndPreservesDistinctObservations(t *testi
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeOrdersTiedEnemyEvidenceWithoutMergingIt(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}
|
||||
}
|
||||
event := func(refs ...source.SourceRef) dnd.EnemyEvent {
|
||||
return dnd.EnemyEvent{Name: "Ashfang", Kind: dnd.EnemyEventKindEngaged, SourceRefs: refs}
|
||||
}
|
||||
earlier, later, invalid := event(ref(50), ref(10)), event(ref(50), ref(90)), event(ref(999))
|
||||
input := dnd.EnemyEventList{Events: []dnd.EnemyEvent{later, invalid, earlier, later}}
|
||||
result, err := runNormalize(newNormalizer(t, npcReferences(t)), context.Background(), normalizeRequest(doc, input, contracts.ReferenceSet{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []dnd.EnemyEvent{earlier, later, invalid}; !reflect.DeepEqual(result.Value.Events, want) {
|
||||
t.Fatalf("events = %#v, want %#v", result.Value.Events, want)
|
||||
}
|
||||
if !hasDiagnostic(result.Diagnostics, ReasonCodeEventsReordered) || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateCollapsed) {
|
||||
t.Fatalf("diagnostics = %#v", result.Diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePreservesDistinctEvidenceWhenEncodedKeysCoincide(t *testing.T) {
|
||||
invalidSourceID := string([]byte{'s', 0xff})
|
||||
replacementSourceID := "s\uFFFD"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user