import { describe, expect, test } from 'bun:test'; import { SeededRng, seedN } from '@bg/engine'; import { decideBotAction } from '../bots/index'; import { botPlayout } from '../bots/playout'; import { oldMaid as game, type OldMaidState } from './index'; import { SHUFFLE_CHANCE } from './bot'; const setup = (players: string[], seed = 1): OldMaidState => game.setup({ players, options: game.defaultOptions, rng: SeededRng.fromSeed(seedN(seed)), now: 0 }); const decide = (s: OldMaidState, p: string, r: number) => decideBotAction(game, s, p, { now: 0, random: () => r }) as Record; describe('old-maid bot', () => { test('draws a card from the neighbour it must draw from, at any spot', () => { const s = setup(['bot:1:x', 'B', 'C']); s.turn = 'bot:1:x'; const from = 'B'; const n = s.hands.B!.length; const spots = new Set(); for (const r of [0.2, 0.4, 0.6, 0.8, 0.99]) { const a = decide(s, 'bot:1:x', r); expect(a).toMatchObject({ type: 'draw', from }); spots.add(a.index as number); } expect(spots.size).toBeGreaterThan(2); expect(Math.max(...spots)).toBeLessThan(n); }); test('now and then shuffles its own hand first, but only once a turn', () => { let s = setup(['bot:1:x', 'B', 'C']); s.turn = 'bot:1:x'; expect(decide(s, 'bot:1:x', SHUFFLE_CHANCE / 2)).toEqual({ type: 'shuffle' }); s = game.apply(s, 'bot:1:x', { type: 'shuffle' }, { rng: SeededRng.fromSeed([1, 2, 3, 4]), now: 1 }).state; expect(decide(s, 'bot:1:x', SHUFFLE_CHANCE / 2).type).toBe('draw'); }); for (const n of [2, 5, 8]) { test(`bots finish a game: ${n} players`, () => { const players = ['h', ...Array.from({ length: n - 1 }, (_, i) => `bot:${i + 1}`)]; for (let seed = 1; seed <= 3; seed++) { const r = botPlayout(game as never, { players, seed: seedN(seed), isBot: (p) => p.startsWith('bot:') }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); } }); } });