aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/reconfig_test.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-15 15:35:52 -0400
committerhistoria <[not public]>2026-09-15 15:35:52 -0400
commit0b0b25222b3131ada79ff78c28eb8bf07d977deb (patch)
tree2ceaef5203595044f66a8f00064cd8e017f8f48d /internal/casino/reconfig_test.go
parent396c3facb8287b9a34076cae8f3d7b300d366d99 (diff)
downloadthehouseoficarus-0b0b25222b3131ada79ff78c28eb8bf07d977deb.tar.gz
feat: single-owner game state, transactional lifecycle, durable money on server stop
Diffstat (limited to 'internal/casino/reconfig_test.go')
-rw-r--r--internal/casino/reconfig_test.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/casino/reconfig_test.go b/internal/casino/reconfig_test.go
new file mode 100644
index 0000000..37b3a65
--- /dev/null
+++ b/internal/casino/reconfig_test.go
@@ -0,0 +1,84 @@
+package casino
+
+// Reconfiguration contract regressions: live rules replace only when idle;
+// occupied games keep their rules (and wagers); deleted rooms evict their
+// cached games with refunds.
+
+import "testing"
+
+func TestEnsureMachineReplacesIdleRules(t *testing.T) {
+ m := NewManager(1)
+ first := m.EnsureMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 2})
+ if first == nil {
+ t.Fatal("machine not created")
+ }
+ // Same config: same machine.
+ if again := m.EnsureMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 2}); again != first {
+ t.Fatal("same config returned a different machine")
+ }
+ // Changed config while idle: live rules replaced.
+ changed := m.EnsureMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 5})
+ if changed == first {
+ t.Fatal("idle machine did not adopt the new configuration")
+ }
+ if changed.Config.SpinTicks != 5 {
+ t.Fatalf("replacement config spin ticks = %d, want 5", changed.Config.SpinTicks)
+ }
+}
+
+func TestEnsureMachineKeepsOccupiedRules(t *testing.T) {
+ m := NewManager(1)
+ m.EnsureMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 2})
+ m.JoinMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 2}, "alice")
+
+ // Occupied machine: config changes do NOT replace live rules.
+ kept := m.EnsureMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 9})
+ if kept.Config.SpinTicks != 2 {
+ t.Fatalf("occupied machine replaced live rules: spin ticks %d", kept.Config.SpinTicks)
+ }
+}
+
+func TestEnsureTableKeepsOccupiedRules(t *testing.T) {
+ m := NewManager(1)
+ m.Join(1, TableConfig{ID: "bj", Game: GameBlackjack, MinBet: 1, BettingTicks: 50}, "alice")
+
+ // Occupied table: changed config does not replace live rules.
+ kept := m.EnsureTable(1, TableConfig{ID: "bj", Game: GameBlackjack, MinBet: 1, BettingTicks: 3})
+ if kept.Config.BettingTicks != 50 {
+ t.Fatalf("occupied table replaced live rules: betting ticks %d", kept.Config.BettingTicks)
+ }
+}
+
+func TestEnsureTableReplacesIdleRules(t *testing.T) {
+ m := NewManager(1)
+ m.Join(1, TableConfig{ID: "bj", Game: GameBlackjack, MinBet: 1, BettingTicks: 50}, "alice")
+ m.Leave(1, "bj", "alice")
+
+ changed := m.EnsureTable(1, TableConfig{ID: "bj", Game: GameBlackjack, MinBet: 1, BettingTicks: 3})
+ if changed.Config.BettingTicks != 3 {
+ t.Fatalf("idle table did not adopt new rules: betting ticks %d", changed.Config.BettingTicks)
+ }
+}
+
+func TestEvictRoomRefundsAndRemovesGames(t *testing.T) {
+ m := NewManager(1)
+ w := &testWallet{credits: 100}
+ m.JoinMachine(1, MachineConfig{ID: "slots", Game: GameSlots, MinBet: 1}, "alice")
+ m.SetMachineBet(1, "slots", "alice", 10, w)
+ if w.credits != 90 {
+ t.Fatalf("wallet before evict = %d, want 90", w.credits)
+ }
+
+ events := m.EvictRoom(1)
+ if w.paid != 10 {
+ t.Fatalf("outstanding wager not refunded on evict: paid %d", w.paid)
+ }
+ if len(events) == 0 {
+ t.Fatal("eviction produced no refund events")
+ }
+ if m.Table(1, "any") != nil {
+ t.Fatal("stale table survived eviction")
+ }
+ // Re-entering the room creates a fresh game.
+ m.JoinMachine(1, MachineConfig{ID: "slots", Game: GameSlots, MinBet: 1}, "alice")
+}