Collapse duplicate D&D spell casts during normalization
This commit is contained in:
@@ -260,6 +260,176 @@ func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeCollapsesDuplicateGroupsAfterCanonicalization(t *testing.T) {
|
||||
normalizer := newNormalizer(t)
|
||||
doc := sourceDocument(6)
|
||||
firstEvidence := source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 2}
|
||||
secondEvidence := source.SourceRef{SourceID: "source", StartUnitID: 3, EndUnitID: 4}
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{
|
||||
{Caster: " Aria \t", Spell: " cure wounds ", Effect: "first effect", NarrativeDescription: "first narrative", SourceRefs: []source.SourceRef{secondEvidence, firstEvidence, firstEvidence}},
|
||||
{Caster: "Borin", Spell: "Healing Word", Effect: "distinct effect", NarrativeDescription: "distinct narrative", SourceRefs: []source.SourceRef{firstEvidence}},
|
||||
{Caster: " Kyle ", Spell: "Cure Wounds", Effect: "kept effect", NarrativeDescription: "kept narrative", SourceRefs: []source.SourceRef{firstEvidence}},
|
||||
{Caster: "aria", Spell: " cure wounds ", Effect: "removed effect", NarrativeDescription: "removed narrative", SourceRefs: []source.SourceRef{firstEvidence, secondEvidence}},
|
||||
{Caster: "KYLE", Spell: " cure wounds ", Effect: "removed effect two", NarrativeDescription: "removed narrative two", SourceRefs: []source.SourceRef{firstEvidence}},
|
||||
{Caster: " kyle ", Spell: "Cure Wounds", Effect: "removed effect three", NarrativeDescription: "removed narrative three", SourceRefs: []source.SourceRef{firstEvidence}},
|
||||
}}
|
||||
|
||||
result, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if len(result.Value.SpellCasts) != 3 {
|
||||
t.Fatalf("normalized casts = %#v, want first occurrences plus distinct cast", result.Value.SpellCasts)
|
||||
}
|
||||
if got := result.Value.SpellCasts[0]; got.Caster != " Aria \t" || got.Effect != "first effect" || got.NarrativeDescription != "first narrative" {
|
||||
t.Fatalf("retained first cast = %#v, want first occurrence fields unchanged", got)
|
||||
}
|
||||
if got := result.Value.SpellCasts[0].SourceRefs; !reflect.DeepEqual(got, []source.SourceRef{firstEvidence, secondEvidence}) {
|
||||
t.Fatalf("retained first evidence = %#v, want canonical first evidence only", got)
|
||||
}
|
||||
if got := result.Value.SpellCasts[2].Caster; got != " Kyle " {
|
||||
t.Fatalf("Unicode caster output = %q, want first occurrence text unchanged", got)
|
||||
}
|
||||
|
||||
if len(result.Warnings) != 6 {
|
||||
t.Fatalf("warnings = %#v, want per-cast warnings followed by two group warnings", result.Warnings)
|
||||
}
|
||||
if result.Warnings[0].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[0].Scope != "spell_casts[0]" {
|
||||
t.Fatalf("warning[0] = %#v, want input name warning", result.Warnings[0])
|
||||
}
|
||||
if result.Warnings[1].ReasonCode != ReasonCodeSourceReferencesNormalized || result.Warnings[1].Scope != "spell_casts[0]" {
|
||||
t.Fatalf("warning[1] = %#v, want input source warning", result.Warnings[1])
|
||||
}
|
||||
if result.Warnings[2].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[2].Scope != "spell_casts[3]" {
|
||||
t.Fatalf("warning[2] = %#v, want removed occurrence warning", result.Warnings[2])
|
||||
}
|
||||
if result.Warnings[3].ReasonCode != ReasonCodeSpellNameCanonicalized || result.Warnings[3].Scope != "spell_casts[4]" {
|
||||
t.Fatalf("warning[3] = %#v, want removed occurrence warning", result.Warnings[3])
|
||||
}
|
||||
if result.Warnings[4].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed || result.Warnings[4].Scope != "spell_casts[0]" || !strings.Contains(result.Warnings[4].Message, "retained input index 0") || !strings.Contains(result.Warnings[4].Message, "removed input indices [3]") {
|
||||
t.Fatalf("warning[4] = %#v, want first duplicate group warning", result.Warnings[4])
|
||||
}
|
||||
if result.Warnings[5].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed || result.Warnings[5].Scope != "spell_casts[2]" || !strings.Contains(result.Warnings[5].Message, "removed input indices [4, 5]") {
|
||||
t.Fatalf("warning[5] = %#v, want second duplicate group warning", result.Warnings[5])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeKeepsDistinctAndIneligibleCastsSeparate(t *testing.T) {
|
||||
doc := sourceDocument(6)
|
||||
ref := source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 2}
|
||||
otherRef := source.SourceRef{SourceID: "source", StartUnitID: 3, EndUnitID: 4}
|
||||
tests := []struct {
|
||||
name string
|
||||
variant dnd.SpellCast
|
||||
}{
|
||||
{name: "distinct canonical spell", variant: dnd.SpellCast{Spell: "Healing Word", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}},
|
||||
{name: "distinct caster", variant: dnd.SpellCast{Spell: "Cure Wounds", Caster: "Borin", SourceRefs: []source.SourceRef{ref}}},
|
||||
{name: "distinct evidence", variant: dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{otherRef}}},
|
||||
{name: "unknown spell", variant: dnd.SpellCast{Spell: "Unknown Spell", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}},
|
||||
{name: "empty evidence", variant: dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria"}},
|
||||
{name: "invalid evidence", variant: dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "other", StartUnitID: 1, EndUnitID: 2}}}},
|
||||
}
|
||||
base := dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: []dnd.SpellCast{base, test.variant}}, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if len(result.Value.SpellCasts) != 2 {
|
||||
t.Fatalf("normalized casts = %#v, want both casts retained", result.Value.SpellCasts)
|
||||
}
|
||||
for _, warning := range result.Warnings {
|
||||
if warning.ReasonCode == ReasonCodeDuplicateSpellCastCollapsed {
|
||||
t.Fatalf("warnings = %#v, want no duplicate collapse", result.Warnings)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDoesNotCollapseAdjacentOrOverlappingEvidence(t *testing.T) {
|
||||
doc := sourceDocument(8)
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{
|
||||
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}}},
|
||||
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 3, EndUnitID: 4}}},
|
||||
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 3}}},
|
||||
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 2, EndUnitID: 4}}},
|
||||
}}
|
||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if len(result.Value.SpellCasts) != len(input.SpellCasts) {
|
||||
t.Fatalf("normalized casts = %#v, want adjacent and overlapping evidence retained", result.Value.SpellCasts)
|
||||
}
|
||||
for _, warning := range result.Warnings {
|
||||
if warning.ReasonCode == ReasonCodeDuplicateSpellCastCollapsed {
|
||||
t.Fatalf("warnings = %#v, want no duplicate collapse", result.Warnings)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeBoundsDuplicateWarningIndices(t *testing.T) {
|
||||
doc := sourceDocument(2)
|
||||
ref := source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 1}
|
||||
casts := make([]dnd.SpellCast, 22)
|
||||
for index := range casts {
|
||||
casts[index] = dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
||||
}
|
||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: casts}, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if len(result.Value.SpellCasts) != 1 || len(result.Warnings) != 1 || result.Warnings[0].ReasonCode != ReasonCodeDuplicateSpellCastCollapsed {
|
||||
t.Fatalf("result = %#v, warnings = %#v, want one retained cast and one bounded warning", result.Value, result.Warnings)
|
||||
}
|
||||
message := result.Warnings[0].Message
|
||||
if !strings.Contains(message, "removed input indices [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]") || strings.Contains(message, ", 21]") || !strings.Contains(message, "1 additional removed input indices omitted") {
|
||||
t.Fatalf("warning message = %q, want 20 displayed indices and exact omitted count", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeIsIdempotentForAlreadyNormalizedInput(t *testing.T) {
|
||||
doc := sourceDocument(3)
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{{
|
||||
Spell: "Cure Wounds",
|
||||
Caster: "Aria",
|
||||
Effect: "effect",
|
||||
NarrativeDescription: "narrative",
|
||||
SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}},
|
||||
}}}
|
||||
normalizer := newNormalizer(t)
|
||||
first, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("first Normalize() error = %v, want nil", err)
|
||||
}
|
||||
second, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(first.Value, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("second Normalize() error = %v, want nil", err)
|
||||
}
|
||||
if !reflect.DeepEqual(second.Value, first.Value) || len(first.Warnings) != 0 || len(second.Warnings) != 0 {
|
||||
t.Fatalf("first = %#v/%#v, second = %#v/%#v, want identical artifacts without mutation warnings", first.Value, first.Warnings, second.Value, second.Warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDuplicateOutputDoesNotShareInputSlices(t *testing.T) {
|
||||
doc := sourceDocument(3)
|
||||
ref := source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 2}
|
||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{
|
||||
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}},
|
||||
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{ref}},
|
||||
}}
|
||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||
}
|
||||
result.Value.SpellCasts[0].SourceRefs[0].SourceID = "changed"
|
||||
if input.SpellCasts[0].SourceRefs[0].SourceID != "source" || input.SpellCasts[1].SourceRefs[0].SourceID != "source" {
|
||||
t.Fatalf("normalized output shares input references: input = %#v", input)
|
||||
}
|
||||
}
|
||||
|
||||
func newNormalizer(t *testing.T) *Normalizer {
|
||||
t.Helper()
|
||||
normalizer, err := New(Options{}, overlayReference())
|
||||
@@ -281,6 +451,20 @@ func normalizeRequest(value dnd.SpellList) contracts.TypedNormalizeRequest[dnd.S
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeRequestWithSource(value dnd.SpellList, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.SpellList] {
|
||||
request := normalizeRequest(value)
|
||||
request.Source = doc
|
||||
return request
|
||||
}
|
||||
|
||||
func sourceDocument(unitCount int) *source.SourceDocument {
|
||||
units := make([]source.SourceUnit, unitCount)
|
||||
for index := range units {
|
||||
units[index] = source.SourceUnit{ID: index + 1}
|
||||
}
|
||||
return &source.SourceDocument{ID: "source", Units: units}
|
||||
}
|
||||
|
||||
func overlayReference() contracts.ReferenceSet {
|
||||
return spellCatalogReference(`{"schema_version":"notarius.dnd.spell-catalog-overlay.v1","catalogs":[{"id":"campaign.example","ruleset":"dnd-5e-2014","source":{"title":"Private campaign source","version":"1","url":"file:///private-source.json","license":"private"},"spells":[{"name":"Aegis of Emberfall","aliases":["Emberfall Aegis"]}]}]}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user