package player import "testing" // TestRemoveItemRejectsNonPositive: a non-positive quantity must never mutate // a stack; a negative value used to increase it. func TestRemoveItemRejectsNonPositive(t *testing.T) { p := New("alice") p.SetInvSlot(0, &InventorySlot{ItemID: "coin", Quantity: 5}) if p.RemoveItem("coin", -3) { t.Fatal("negative quantity accepted") } if slot := p.InvSlot(0); slot == nil || slot.Quantity != 5 { t.Fatalf("stack mutated by a negative removal: %+v", slot) } if p.RemoveItem("coin", 0) { t.Fatal("zero quantity accepted") } if slot := p.InvSlot(0); slot == nil || slot.Quantity != 5 { t.Fatalf("stack mutated by a zero removal: %+v", slot) } } // Regression: RemoveItem used to drain every stack and only then return false, // so a caller that rolled back on failure lost the items anyway. func TestRemoveItemIsAllOrNothing(t *testing.T) { p := New("alice") p.SetInvSlot(0, &InventorySlot{ItemID: "coin", Quantity: 2}) p.SetInvSlot(1, &InventorySlot{ItemID: "coin", Quantity: 3}) if p.RemoveItem("coin", 10) { t.Fatal("removal of more than held reported success") } if got := p.CountItem("coin"); got != 5 { t.Fatalf("failed removal drained stacks: count = %d, want 5", got) } }