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) } }) }