package casino import ( "math/rand" "testing" ) func TestMachineFreeSpinsUseProfileCount(t *testing.T) { m := newMachine(1, MachineConfig{ID: "slots", Game: GameSlots}, rand.New(rand.NewSource(7))) m.profile.FreeSpinCount = 5 m.player = &Participant{Name: "alice", Connected: true, Wallet: &testWallet{credits: 100}} m.spinBet = 10 m.spin = &SlotSpin{ScatterCount: 3} events := m.finishSpin() if m.freeSpins != 4 { t.Fatalf("freeSpins=%d, want 4 remaining after the first of 5 chained spins", m.freeSpins) } if m.phase != machineSpinning { t.Fatalf("phase=%s, want spinning", m.phase) } if !hasMessage(events, "trigger 5 free spins") { t.Fatalf("missing profile-count free spin message: %+v", events) } } func TestMachineMenuEventCarriesStructuredMenu(t *testing.T) { m := newMachine(1, MachineConfig{ID: "slots", Game: GameSlots}, rand.New(rand.NewSource(7))) m.player = &Participant{Name: "alice", Connected: true} events := m.privateMenu("You sit down at the slot machine.") if len(events) != 1 || events[0].Menu == nil || events[0].Menu.Kind != MenuSlotCommands || len(events[0].Menu.Commands) == 0 { t.Fatalf("slot menu event=%+v", events) } } func TestMachineLeaveResetsPayoutRemainder(t *testing.T) { m := newMachine(1, MachineConfig{ID: "slots", Game: GameSlots}, rand.New(rand.NewSource(7))) m.player = &Participant{Name: "alice", Connected: true} m.bet = 10 m.payoutRemainder = 0.5 m.leave("alice") if m.payoutRemainder != 0 { t.Fatalf("payoutRemainder=%v leaked to the next player", m.payoutRemainder) } } func TestMachineLeaveStopsAutoBet(t *testing.T) { m := newMachine(1, MachineConfig{ID: "slots", Game: GameSlots}, rand.New(rand.NewSource(7))) m.player = &Participant{Name: "alice", Connected: true} m.autoBet, m.autoCountdown = true, 2 m.leave("alice") if m.autoBet || m.autoCountdown != 0 { t.Fatalf("leave did not stop autobet: autoBet=%v countdown=%d", m.autoBet, m.autoCountdown) } if events := m.tick(); len(events) != 0 { t.Fatalf("empty machine with stale autobet emitted events: %+v", events) } } func TestMachineDisconnectFreesSeatAndForfeitsSpin(t *testing.T) { m := newMachine(1, MachineConfig{ID: "slots", Game: GameSlots, SpinTicks: 1}, rand.New(rand.NewSource(7))) w := &testWallet{credits: 100} m.player = &Participant{Name: "alice", Connected: true, Wallet: w} m.autoBet, m.autoCountdown = true, 2 m.spin = &SlotSpin{} m.phase = machineSpinning m.bet = 20 // Disconnect removes the player and discards the in-flight spin. events := m.markDisconnected("alice") if m.player != nil { t.Fatal("disconnect did not free the seat") } if m.spin != nil || m.phase != machineReady { t.Fatalf("in-flight spin was not discarded: spin=%v phase=%s", m.spin, m.phase) } if m.autoBet || m.autoCountdown != 0 { t.Fatalf("autobet not cleared: autoBet=%v countdown=%d", m.autoBet, m.autoCountdown) } if m.bet != 0 || m.payoutRemainder != 0 { t.Fatalf("wager state leaked: bet=%d remainder=%.2f", m.bet, m.payoutRemainder) } if len(events) == 0 || events[0].Type != EventLeft { t.Fatalf("no public stand-up event: %+v", events) } // The seatel is immediately available. m.join("bob") if m.player == nil || m.player.Name != "bob" { t.Fatalf("seat not available for the next player: player=%+v", m.player) } // The forfeited spin never pays out. if w.paid != 0 { t.Fatalf("forfeited spin paid credits: paid=%d", w.paid) } // The machine is idle after a disconnect. if events := m.tick(); len(events) != 0 { t.Fatalf("empty machine emitted events after disconnect: %+v", events) } }