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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
package game
// Readiness regressions for the account-rename, disconnect, and durable-money
// paths reviewed before world/casino expansion: a transport disconnect must
// be race-free, renames must not break ownership or strand owed payouts,
// reserved-loot pickups must never destroy items, and recovery failure paths
// must retry or surface instead of silently dropping money.
import (
"os"
"path/filepath"
"testing"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
)
// TestDisconnectRaceIsFreeAndNeverLost drives DisconnectTick (owner) while a
// transport goroutine arms the disconnect (exactly net.Server.handleSession's
// deferred Hub.Remove path). Guards the atomic disconnect flags: a plain
// bool/int pair is a data race and can lose the disconnect entirely.
func TestDisconnectRaceIsFreeAndNeverLost(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
h.seedCharacter("alice", "alice")
sess, _ := h.connect("alice")
started, finished := make(chan struct{}), make(chan struct{})
go func() {
h.g.RunOnOwner(func() {
close(started)
for i := 0; i < 10000; i++ {
h.g.DisconnectTick()
}
})
close(finished)
}()
<-started
h.hub.Remove(sess) // transport goroutine; never on the owner
<-finished
if sessions := h.g.Hub.AllSessions(); len(sessions) != 0 {
t.Fatalf("disconnect lost under the race: %d sessions remain", len(sessions))
}
}
// TestReservedPickupDoesNotDestroyItems: picking up a ground stack that
// shares a name with another player's reserved stack must take only the
// takeable stock — never half-remove the ground with nothing delivered.
func TestReservedPickupDoesNotDestroyItems(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
p := h.seedCharacter("alice", "alice")
writeSeedFile(t, filepath.Join(h.dir, "items", "chips.yaml"), []byte("id: chips\nname: chips\nstackable: true\n"))
sess := &net.Session{Conn: newChanConn(), Player: p, State: net.StateGame}
h.g.RunOnOwner(func() {
h.g.ItemStore.Reload(h.dir)
if _, err := h.g.ItemStore.Load("chips"); err != nil {
t.Error(err)
return
}
h.g.World.AddGroundItem(1, "chips", 10)
// A reserved stack of the same item sits after the takeable one:
// the old mid-loop abort destroyed the already-removed half.
h.g.World.AddReservedItem(1, "chips", 10, "bob")
h.g.doGet(sess, "chips")
total := countChips(p) + h.g.World.GroundItems(1)["chips"]
if total != 20 {
t.Errorf("pickup destroyed items: inventory+ground=%d, want 20", total)
}
})
}
// TestOwedPayoutSurvivesCharacterRename: economy records resolve their
// subject by the character's STABLE ID, so renaming the character between
// the crash and recovery still delivers the owed payout.
func TestOwedPayoutSurvivesCharacterRename(t *testing.T) {
h := newHarness(t)
h.seedAccount("acct")
p := h.seedCharacter("alice", "acct")
h.g.RunOnOwner(func() {
p.ID = "stable-char-id" // production characters carry a stable ID
p.Credits = 90
if err := h.g.AccountStore.SaveCharacter(p); err != nil {
t.Error(err)
return
}
if _, err := h.g.Economy.start("payout", p, 0, 20, 90, 110, 0, 0); err != nil {
t.Error(err)
return
}
if err := h.g.renameCharacterTxn("acct", "alice", "alicia"); err != nil {
t.Error(err)
return
}
h.g.recoverEconomy()
loaded, err := h.g.AccountStore.LoadCharacter("alicia")
if err != nil {
t.Error(err)
return
}
if loaded.Credits != 110 {
t.Errorf("renamed character never receives owed payout: credits=%d, want 110", loaded.Credits)
}
})
}
// TestAccountRenamePreservesAccessAndOwnership: renaming an account
// rewrites the owned character files' owner field in the same transaction
// and republishes stale sessions (still carrying the old cached name) so
// both sessions stay authenticated and keep character access.
func TestAccountRenamePreservesAccessAndOwnership(t *testing.T) {
h := newHarness(t)
s1, s2 := setupAcct(t, h)
h.g.RunOnOwner(func() {
h.g.handleRenameAccount(s1, "renamed")
if !h.g.AccountStore.AccountExists("renamed") {
t.Error("rename did not succeed")
return
}
if s1.Account.Name != "renamed" || s2.Account.Name != "renamed" {
t.Errorf("sessions remain bound to %q/%q, want renamed/renamed", s1.Account.Name, s2.Account.Name)
}
if !h.g.isMemberOf("renamed", "alice") {
t.Error("renamed account cannot access its own character")
}
})
}
// TestSettledSurrenderDoesNotRefundChipStake: a settlement payout — even a
// partial one like a surrendered hand paying half a chip stake — proves the
// round resolved, so recovery must not ALSO return the chip stake in kind.
func TestSettledSurrenderDoesNotRefundChipStake(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
p := h.seedCharacter("alice", "alice")
h.g.RunOnOwner(func() {
p.Credits = 100
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
}
// Surrender settles for half the stake; the crash lands before the
// resolution marker.
if err := w.PayCredits(5); err != nil {
t.Error(err)
return
}
h.g.recoverEconomy()
loaded, err := h.g.AccountStore.LoadCharacter("alice")
if err != nil {
t.Error(err)
return
}
if loaded.Credits != 105 || countChips(loaded) != 0 {
t.Errorf("settled surrender recovered %d credits / %d chips; want 105 / 0", loaded.Credits, countChips(loaded))
}
})
}
// TestRecoverySaveFailureRetriesOnNextStartup: a TRANSIENT save failure
// during recovery must not re-anchor the owed record to unreadable
// checkpoints — the record keeps its original checkpoints and a later
// recovery (after the disk is fixed) re-applies the owed payout.
func TestRecoverySaveFailureRetriesOnNextStartup(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
p := h.seedCharacter("alice", "alice")
charDir := filepath.Join(h.dir, "players", "characters")
t.Cleanup(func() { os.Chmod(charDir, 0755) })
h.g.RunOnOwner(func() {
p.Credits = 90
if err := h.g.AccountStore.SaveCharacter(p); err != nil {
t.Error(err)
return
}
if _, err := h.g.Economy.start("payout", p, 0, 20, 90, 110, 0, 0); err != nil {
t.Error(err)
return
}
if err := os.Chmod(charDir, 0500); err != nil {
t.Error(err)
return
}
h.g.recoverEconomy() // every save fails
if err := os.Chmod(charDir, 0755); err != nil {
t.Error(err)
return
}
h.g.recoverEconomy() // retry with a healthy disk
loaded, err := h.g.AccountStore.LoadCharacter("alice")
if err != nil {
t.Error(err)
return
}
if loaded.Credits != 110 {
t.Errorf("after fixing the disk and retrying: %d credits, want 110; open=%+v", loaded.Credits, h.g.Economy.mustLoadOpen(t))
}
})
}
// TestResolvedStakeEvidenceSurvivesJournalFailure: a resolution marker
// that cannot be journaled (persistent append failure) leaves the stake
// OPEN — never silently forgotten — and records the resolution in the
// character file as durable evidence. Recovery then settles the stake
// WITHOUT refunding it: the round genuinely resolved, and the file is the
// proof. When the file save also fails, no evidence exists and recovery
// applies the documented refund-on-lost-state policy instead.
func TestResolvedStakeEvidenceSurvivesJournalFailure(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
p := h.seedCharacter("alice", "alice")
h.g.RunOnOwner(func() {
p.Credits = 100
w := casinoWallet{g: h.g, p: p}
if _, err := w.Wager(10); err != nil {
t.Error(err)
return
}
path := h.g.Economy.path
h.g.Economy.path = h.dir // persistent append failure
w.ResolveStakes()
h.g.Economy.path = path
if open := h.g.Economy.openStakes(economyKey(p)); len(open) != 1 {
t.Errorf("unresolvable stake was silently dropped: open=%d", len(open))
}
// The resolution is recorded durably in the character file.
loaded, err := h.g.AccountStore.LoadCharacter("alice")
if err != nil {
t.Error(err)
return
}
if len(loaded.ResolvedStakes) != 1 {
t.Errorf("resolved-stake evidence not recorded: %+v", loaded.ResolvedStakes)
return
}
h.g.recoverEconomy()
loaded, err = h.g.AccountStore.LoadCharacter("alice")
if err != nil {
t.Error(err)
return
}
if loaded.Credits != 90 {
t.Errorf("after recovery: credits=%d, want 90 (round resolved; evidence prevents a refund)", loaded.Credits)
}
if open := h.g.Economy.mustLoadOpen(t); len(open) != 0 {
t.Errorf("records left open after recovery: %+v", open)
}
if len(loaded.ResolvedStakes) != 0 {
t.Errorf("stale resolved-stake evidence not pruned: %+v", loaded.ResolvedStakes)
}
})
}
// TestUnjournalablePayoutPaidThroughCharacterFile: a payout that cannot be
// journaled (persistent append failure) is still made durable through the
// character file — the save is the commit point, so the win is real and
// announced. The resolved stake is recorded as file evidence so recovery
// neither refunds the stake (double pay) nor drops the round.
func TestUnjournalablePayoutPaidThroughCharacterFile(t *testing.T) {
h := newHarness(t)
h.seedAccount("alice")
p := h.seedCharacter("alice", "alice")
h.g.RunOnOwner(func() {
p.Credits = 100
w := casinoWallet{g: h.g, p: p}
if _, err := w.Wager(10); err != nil {
t.Error(err)
return
}
path := h.g.Economy.path
h.g.Economy.path = h.dir
err := w.PayCredits(20)
h.g.Economy.path = path
if err != nil {
t.Errorf("character-file fallback failed: %v", err)
return
}
if p.Credits != 110 {
t.Errorf("fallback payout did not land: credits=%d, want 110", p.Credits)
}
// As the games do after a successful settlement: resolve the stake.
w.ResolveStakes()
h.g.recoverEconomy()
loaded, err := h.g.AccountStore.LoadCharacter("alice")
if err != nil {
t.Error(err)
return
}
if loaded.Credits != 110 {
t.Errorf("after recovery: credits=%d, want 110 (win durable once; no stake refund on top)", loaded.Credits)
}
if open := h.g.Economy.mustLoadOpen(t); len(open) != 0 {
t.Errorf("open records: %+v", open)
}
})
}
|