Add D&D refactoring plan and isolate merger results

This commit is contained in:
2026-07-25 12:51:36 +00:00
parent e4471fc300
commit 9d1356a20e
3 changed files with 397 additions and 2 deletions

View File

@@ -7,12 +7,21 @@ import (
func appendSpellLists(values []dnd.SpellList) (dnd.SpellList, error) {
count := 0
present := false
for _, value := range values {
if value.SpellCasts != nil {
present = true
}
count += len(value.SpellCasts)
}
if !present {
return dnd.SpellList{}, nil
}
combined := dnd.SpellList{SpellCasts: make([]dnd.SpellCast, 0, count)}
for _, value := range values {
combined.SpellCasts = append(combined.SpellCasts, value.SpellCasts...)
for _, spellCast := range value.SpellCasts {
combined.SpellCasts = append(combined.SpellCasts, cloneSpellCast(spellCast))
}
}
return combined, nil
}
@@ -31,7 +40,9 @@ func appendNPCLists(values []dnd.NPCList) (dnd.NPCList, error) {
}
combined := dnd.NPCList{NPCs: make([]dnd.NPC, 0, count)}
for _, value := range values {
combined.NPCs = append(combined.NPCs, value.NPCs...)
for _, npc := range value.NPCs {
combined.NPCs = append(combined.NPCs, cloneNPC(npc))
}
}
return combined, nil
}
@@ -105,6 +116,22 @@ func cloneCombatTurn(value dnd.CombatTurn) dnd.CombatTurn {
return clone
}
func cloneSpellCast(value dnd.SpellCast) dnd.SpellCast {
clone := value
if value.SourceRefs != nil {
clone.SourceRefs = append([]source.SourceRef(nil), value.SourceRefs...)
}
return clone
}
func cloneNPC(value dnd.NPC) dnd.NPC {
clone := value
if value.SourceRefs != nil {
clone.SourceRefs = append([]source.SourceRef(nil), value.SourceRefs...)
}
return clone
}
func cloneNPCInteraction(value dnd.NPCInteraction) dnd.NPCInteraction {
clone := value
if value.SourceRefs != nil {

View File

@@ -285,6 +285,47 @@ func TestAppendNPCListsPreservesOrderAndArrayPresence(t *testing.T) {
}
})
}
input := []dnd.NPCList{{NPCs: []dnd.NPC{{Name: "first", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}}}}
merged, err := appendNPCLists(input)
if err != nil {
t.Fatalf("appendNPCLists() error = %v", err)
}
merged.NPCs[0].SourceRefs[0].StartUnitID = 999
if input[0].NPCs[0].SourceRefs[0].StartUnitID == 999 {
t.Fatal("merged NPCs share source reference storage")
}
}
func TestAppendSpellListsPreservesOrderPresenceAndOwnership(t *testing.T) {
tests := []struct {
name string
in []dnd.SpellList
want dnd.SpellList
}{
{name: "no values", in: nil, want: dnd.SpellList{}},
{name: "nil values", in: []dnd.SpellList{{}, {}}, want: dnd.SpellList{}},
{name: "present empty", in: []dnd.SpellList{{SpellCasts: []dnd.SpellCast{}}}, want: dnd.SpellList{SpellCasts: []dnd.SpellCast{}}},
{name: "ordered values", in: []dnd.SpellList{{SpellCasts: []dnd.SpellCast{{Spell: "first"}}}, {SpellCasts: []dnd.SpellCast{{Spell: "second"}}}}, want: dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "first"}, {Spell: "second"}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := appendSpellLists(tt.in)
if err != nil || !reflect.DeepEqual(got, tt.want) {
t.Fatalf("appendSpellLists() = %#v, error = %v, want %#v", got, err, tt.want)
}
})
}
input := []dnd.SpellList{{SpellCasts: []dnd.SpellCast{{Spell: "Shield", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}}}}
merged, err := appendSpellLists(input)
if err != nil {
t.Fatalf("appendSpellLists() error = %v", err)
}
merged.SpellCasts[0].SourceRefs[0].StartUnitID = 999
if input[0].SpellCasts[0].SourceRefs[0].StartUnitID == 999 {
t.Fatal("merged spell casts share source reference storage")
}
}
func TestAppendNPCInteractionListsPreservesOrderPresenceAndOwnership(t *testing.T) {