aboutsummaryrefslogtreecommitdiff
path: root/internal/game/economy_recovery_regression_test.go
blob: 738dece6709f1c4ca51f3ab4164735eac417cc59 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package game

// Economy recovery regressions for the outstanding-stake ledger: a crash
// with a round in flight refunds the stake, chip stakes recover in chips,
// a failed payout is re-applied or preserved for operator inspection, and
// a resolved round (payout evidence) is never refunded twice.

import (
	"testing"

	"thehouseoficarus/internal/casino"
	"thehouseoficarus/internal/player"
)

// TestRecoveryRefundsInFlightMachineWager: a wager debited durably with the
// round still in flight (no graceful shutdown, no resolution) is refunded by
// startup recovery — the documented refund-on-crash policy.
func TestRecoveryRefundsInFlightMachineWager(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.Credits = 100
		h.g.Casino.JoinMachine(1, casino.MachineConfig{ID: "slots", Game: casino.GameSlots}, "alice")
		h.g.Casino.SetMachineBet(1, "slots", "alice", 10, casinoWallet{g: h.g, p: p})
	})
	h.barrier()

	// A fresh Game over the saved files simulates losing all live round
	// state in a crash. The stake record is open, so recovery refunds it.
	restarted := harnessFromDir(t, h.dir)
	loaded, err := restarted.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Credits != 100 {
		t.Fatalf("crash with an active 10-credit spin: recovered %d credits, want 100", loaded.Credits)
	}
	if open := restarted.g.Economy.openStakes("alice"); len(open) != 0 {
		t.Fatalf("refunded stake left open: %+v", open)
	}
}

// TestRecoveryRefundsChipOnlyWager: a chip-only debit must be recognized by
// comparing chip balances, not credits alone (the credit checkpoints are
// identical before and after a chip-only wager).
func TestRecoveryRefundsChipOnlyWager(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.Credits = 100
		p.SetInvSlot(0, &player.InventorySlot{ItemID: "chips", Quantity: 20})
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		// Simulate the crash window: the chip debit is durable in the
		// character file but the stake record never resolved.
		p.RemoveItem("chips", 10)
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		if _, err := h.g.Economy.start("wager", p, 10, 0, 100, 100, 20, 10); err != nil {
			t.Error(err)
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if got := countChips(loaded); got != 20 {
		t.Fatalf("chip stake after recovery = %d, want 20 (refunded in chips)", got)
	}
}

// TestRecoveryReappliesLostPayoutWithEvidence: a won hand whose payout
// failed to save leaves an open payout record AND an open stake record.
// Recovery re-applies the payout (which includes the stake return) and
// settles the stake from that evidence — exactly once, no double refund.
func TestRecoveryReappliesLostPayoutWithEvidence(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.Credits = 90 // post-debit balance: the 10-credit stake was charged
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		// Open stake for the lost round (before=100, after=90).
		if _, err := h.g.Economy.start("wager", p, 0, 10, 100, 90, 0, 0); err != nil {
			t.Error(err)
		}
		// Open payout for the won hand (2x bet = 20, before=90, after=110).
		if _, err := h.g.Economy.start("payout", p, 0, 20, 90, 110, 0, 0); err != nil {
			t.Error(err)
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Credits != 110 {
		t.Fatalf("balance after recovery = %d, want 110 (payout re-applied once, no double stake refund)", loaded.Credits)
	}
	if open := h.g.Economy.loadOpen(); len(open) != 0 {
		t.Fatalf("records left open after recovery: %+v", open)
	}
}

// TestRecoveryRefundsSequentialStakes: a bet plus a double (two open stake
// records, newest processed first) both refund after a crash.
func TestRecoveryRefundsSequentialStakes(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.Credits = 80 // bet 10 then doubled: 100 -> 90 -> 80
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		if _, err := h.g.Economy.start("wager", p, 0, 10, 100, 90, 0, 0); err != nil {
			t.Error(err)
		}
		if _, err := h.g.Economy.start("wager", p, 0, 10, 90, 80, 0, 0); err != nil {
			t.Error(err)
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Credits != 100 {
		t.Fatalf("balance after recovery = %d, want 100 (both stakes refunded)", loaded.Credits)
	}
}

// TestRecoveryReappliesIdenticalFailedPayouts: two won hands whose payout
// saves both failed leave two OPEN payout records with identical
// checkpoints (the in-memory balance reverts on save failure, so both were
// journaled against balance 90). Recovery must re-apply BOTH — settling
// the second as "already durable" because the first re-application moved
// the balance to `after` would silently drop 20 owed credits.
func TestRecoveryReappliesIdenticalFailedPayouts(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.Credits = 90 // post-debit balance: both payouts failed to save
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		for i := 0; i < 2; i++ {
			if _, err := h.g.Economy.start("payout", p, 0, 20, 90, 110, 0, 0); err != nil {
				t.Error(err)
			}
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Credits != 130 {
		t.Fatalf("balance after recovery = %d, want 130 (both identical failed payouts re-applied)", loaded.Credits)
	}
	if open := h.g.Economy.loadOpen(); len(open) != 0 {
		t.Fatalf("records left open after recovery: %+v", open)
	}
}

// TestRefundOutstandingKeepsStakesOpenWithoutInventoryRoom: a chip refund
// into a FULL inventory must be refused (stake records stay open) instead
// of silently dropping the chips and marking the refund complete. Once the
// player frees a slot, the same outstanding stake refunds successfully.
func TestRefundOutstandingKeepsStakesOpenWithoutInventoryRoom(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		p.SetInvSlot(0, &player.InventorySlot{ItemID: "chips", Quantity: 10})
		w := casinoWallet{g: h.g, p: p}
		if _, err := w.Wager(10); err != nil {
			t.Error(err)
			return
		}
		// The debit emptied the chips slot; fill EVERY slot (including the
		// one the wager just freed) so the chip return has nowhere to go.
		for p.FirstFreeSlot() >= 0 {
			p.SetInvSlot(p.FirstFreeSlot(), &player.InventorySlot{ItemID: "filler", Quantity: 1})
		}
		chips, credits, err := w.RefundOutstanding()
		if err != casino.ErrWalletPersistence {
			t.Errorf("refund error = %v, want ErrWalletPersistence (chips=%d credits=%d)", err, chips, credits)
		}
		if got := countChips(p); got != 0 {
			t.Errorf("chips after refused refund = %d, want 0 (nothing delivered; tracked in the open stake)", got)
		}
		if open := h.g.Economy.openStakes(p.Name); len(open) != 1 {
			t.Errorf("stake records after refused refund = %d, want 1 (kept open for a later attempt)", len(open))
		}

		// Free a slot: the SAME outstanding stake now refunds in full.
		p.RemoveItem("filler", 1)
		chips, credits, err = w.RefundOutstanding()
		if err != nil || chips != 10 || credits != 0 {
			t.Errorf("retry refund = (%d chips, %d credits, %v), want (10, 0, nil)", chips, credits, err)
		}
		if got := countChips(p); got != 10 {
			t.Errorf("chips after retried refund = %d, want 10", got)
		}
		if open := h.g.Economy.openStakes(p.Name); len(open) != 0 {
			t.Errorf("stake records after successful refund = %d, want 0", len(open))
		}
	})
}

// TestRecoveryKeepsWagerOpenWhenChipsDoNotFit: recovery must not settle a
// chip stake as refunded when the character's inventory cannot hold the
// chips — the record stays open for operator inspection.
func TestRecoveryKeepsWagerOpenWhenChipsDoNotFit(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		// Durable state at crash time: the 10-chip debit landed (10 -> 0
		// chips on disk), credits untouched, and the inventory is FULL of
		// non-chip filler, so recovery has nowhere to put the refund.
		p.Credits = 100
		for i := 0; i < 28; i++ {
			p.SetInvSlot(i, &player.InventorySlot{ItemID: "filler", Quantity: 1})
		}
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		if _, err := h.g.Economy.start("wager", p, 10, 0, 100, 100, 10, 0); err != nil {
			t.Error(err)
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if got := countChips(loaded); got != 0 {
		t.Fatalf("chips after recovery = %d, want 0 (no inventory room; nothing delivered)", got)
	}
	if open := h.g.Economy.loadOpen(); len(open) != 1 {
		t.Fatalf("chip wager must stay open for operator inspection, open records: %+v", open)
	}
}

// TestRecoveryPreservesAmbiguousPayout: a payout whose checkpoints match
// neither balance state stays open and is surfaced for operator inspection —
// a liability is never silently dropped.
func TestRecoveryPreservesAmbiguousPayout(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice", "pw", false)
	p := h.seedCharacter("alice", "alice")
	h.g.RunOnOwner(func() {
		// A payout owed at balance 100 failed to save; the player then
		// spent 5 credits elsewhere before the crash.
		p.Credits = 95
		if err := h.g.AccountStore.SaveCharacter(p); err != nil {
			t.Error(err)
		}
		if _, err := h.g.Economy.start("payout", p, 0, 20, 100, 120, 0, 0); err != nil {
			t.Error(err)
		}
		h.g.recoverEconomy()
	})
	loaded, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatal(err)
	}
	if loaded.Credits != 95 {
		t.Fatalf("ambiguous recovery mutated the balance: %d, want 95", loaded.Credits)
	}
	if open := h.g.Economy.loadOpen(); len(open) != 1 {
		t.Fatalf("ambiguous payout must stay open for operator inspection: %+v", open)
	}
}