aboutsummaryrefslogtreecommitdiff
path: root/internal/game/polish_shop_merge_test.go
blob: 88514647810ddbffbfc3501931fdcb024c94303f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package game

// Regression: giveShopItems used to merge a purchase into any inventory slot
// holding the same item id, ignoring Quality/MaxQuality, while every other
// merge site (craft, harvest, ammo return, pickup) merges only into a stack
// with the properties a fresh stack gets (stackProps). A purchase could
// therefore merge into a differently qualified stack. shopInvCapacity had the
// same blind spot, which mattered because the buy path charges for the
// quantity capacity reports as placeable.

import (
	"path/filepath"
	"testing"

	"thehouseoficarus/internal/player"
)

func TestGiveShopItemsDoesNotMergeIntoDifferentQualityStack(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	p := h.seedCharacter("alice", "alice")
	writeSeedFile(t, filepath.Join(h.dir, "items", "polish_shop_gem.yaml"),
		[]byte("id: polish_shop_gem\nname: polish shop gem\nstackable: true\nmax_quality: 5\nvalue: 1\n"))

	h.g.RunOnOwner(func() {
		h.g.ItemStore.Reload(h.dir)
		def := mustLoadItem(t, h.g, "polish_shop_gem")

		// A differently qualified stack is not a merge target: with no free
		// slot, capacity must report 0 and the purchase must not be placed.
		p.SetInvSlot(0, &player.InventorySlot{ItemID: def.ID, Quantity: 10, Quality: 1, MaxQuality: 5})
		fillInventoryExcept(p)
		if got := h.g.shopInvCapacity(p, def.ID, def); got != 0 {
			t.Fatalf("shopInvCapacity = %d, want 0 (the only stack has a different quality)", got)
		}
		h.g.giveShopItems(p, def.ID, 3, def)
		if slot := p.InvSlot(0); slot.Quantity != 10 || slot.Quality != 1 || slot.MaxQuality != 5 {
			t.Errorf("purchase merged into a differently qualified stack: %+v", slot)
		}
		if got := p.CountItem(def.ID); got != 10 {
			t.Errorf("gems carried = %d, want 10 (nothing should have been delivered)", got)
		}

		// The same full inventory accepts the purchase once the stack carries
		// the properties a fresh stack gets.
		p.SetInvSlot(0, &player.InventorySlot{ItemID: def.ID, Quantity: 10, Quality: 5, MaxQuality: 5})
		if got := h.g.shopInvCapacity(p, def.ID, def); got == 0 {
			t.Fatalf("shopInvCapacity = %d, want room for a matching stack", got)
		}
		h.g.giveShopItems(p, def.ID, 3, def)
		if slot := p.InvSlot(0); slot.Quantity != 13 || slot.Quality != 5 || slot.MaxQuality != 5 {
			t.Errorf("matched stack = %+v, want quantity 13 at quality 5", slot)
		}
	})
}