diff options
| author | historia <[not public]> | 2026-09-18 05:31:09 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-09-18 05:31:09 -0400 |
| commit | 153acb7f644d755c45533b7c5311e3ffa47e8973 (patch) | |
| tree | 7645e6f7b65bc3830b078e3b9336d2e6b199d584 /internal/game/hardening_pickup_quality_test.go | |
| parent | 87d08f0151e643951769e323995f1c5ed89ee200 (diff) | |
| download | thehouseoficarus-153acb7f644d755c45533b7c5311e3ffa47e8973.tar.gz | |
fix: harden engine across casino, economy, concurrency, security, and content
Diffstat (limited to 'internal/game/hardening_pickup_quality_test.go')
| -rw-r--r-- | internal/game/hardening_pickup_quality_test.go | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/internal/game/hardening_pickup_quality_test.go b/internal/game/hardening_pickup_quality_test.go new file mode 100644 index 0000000..135b5a6 --- /dev/null +++ b/internal/game/hardening_pickup_quality_test.go @@ -0,0 +1,123 @@ +package game + +// Regression: a picked-up stack of a quality-bearing stackable item may only +// merge into a slot with the same Quality/MaxQuality. Otherwise it takes a +// free slot, mirroring withdrawBankItem in cmd_bank.go, so a merge can never +// silently destroy the picked-up stack's quality. + +import ( + "path/filepath" + "testing" + + "thehouseoficarus/internal/net" + "thehouseoficarus/internal/player" +) + +const hardeningGemID = "test_gem" + +func seedQualityGem(t *testing.T, h *harness) { + t.Helper() + writeSeedFile(t, filepath.Join(h.dir, "items", hardeningGemID+".yaml"), + []byte("id: test_gem\nname: test gem\nstackable: true\nmax_quality: 5\n")) +} + +// gemSlots returns every inventory slot holding the test gem. +func gemSlots(p *player.Player) map[int]*player.InventorySlot { + found := make(map[int]*player.InventorySlot) + for i := 0; i < 28; i++ { + if slot := p.InvSlot(i); slot != nil && slot.ItemID == hardeningGemID { + found[i] = slot + } + } + return found +} + +func TestPickupMismatchedQualityUsesFreeSlot(t *testing.T) { + h := newHarness(t) + h.seedAccount("alice") + p := h.seedCharacter("alice", "alice") + seedQualityGem(t, h) + sess := &net.Session{Conn: newChanConn(), Player: p} + sess.SetState(net.StateGame) + h.g.RunOnOwner(func() { + h.g.ItemStore.Reload(h.dir) + p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 1, MaxQuality: 5}) + h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5) + + h.g.doGet(sess, "test gem") + + slots := gemSlots(p) + if len(slots) != 2 { + t.Fatalf("gem slots = %d, want the pickup in its own slot: %v", len(slots), slots) + } + if low := p.InvSlot(0); low.Quantity != 10 || low.Quality != 1 { + t.Errorf("existing low-quality stack changed: %+v", low) + } + found := false + for i, slot := range slots { + if i == 0 { + continue + } + found = true + if slot.Quantity != 5 || slot.Quality != 5 || slot.MaxQuality != 5 { + t.Errorf("picked-up stack lost its properties: %+v", slot) + } + } + if !found { + t.Error("pickup did not take a free slot") + } + if got := h.g.World.GroundItems(p.RoomID)[hardeningGemID]; got != 0 { + t.Errorf("ground gems = %d, want 0", got) + } + }) +} + +func TestPickupMatchingQualityMergesIntoStack(t *testing.T) { + h := newHarness(t) + h.seedAccount("alice") + p := h.seedCharacter("alice", "alice") + seedQualityGem(t, h) + sess := &net.Session{Conn: newChanConn(), Player: p} + sess.SetState(net.StateGame) + h.g.RunOnOwner(func() { + h.g.ItemStore.Reload(h.dir) + p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 5, MaxQuality: 5}) + h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5) + + h.g.doGet(sess, "test gem") + + slots := gemSlots(p) + if len(slots) != 1 { + t.Fatalf("matching-quality pickup must merge, slots = %v", slots) + } + if slot := p.InvSlot(0); slot.Quantity != 15 || slot.Quality != 5 || slot.MaxQuality != 5 { + t.Errorf("merged stack = %+v, want quantity 15 at quality 5", slot) + } + }) +} + +func TestPickupMismatchedQualityFullInventoryRefused(t *testing.T) { + h := newHarness(t) + h.seedAccount("alice") + p := h.seedCharacter("alice", "alice") + seedQualityGem(t, h) + sess := &net.Session{Conn: newChanConn(), Player: p} + sess.SetState(net.StateGame) + h.g.RunOnOwner(func() { + h.g.ItemStore.Reload(h.dir) + for i := 0; i < 28; i++ { + p.SetInvSlot(i, &player.InventorySlot{ItemID: "filler", Quantity: 1}) + } + p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 1, MaxQuality: 5}) + h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5) + + h.g.doGet(sess, "test gem") + + if slot := p.InvSlot(0); slot.Quantity != 10 || slot.Quality != 1 { + t.Errorf("a refused pickup must leave the mismatched stack alone: %+v", slot) + } + if got := h.g.World.GroundItems(p.RoomID)[hardeningGemID]; got != 5 { + t.Errorf("ground gems = %d, want the stack left in place", got) + } + }) +} |
