/** Every bot-enabled game: bots play only accepted actions, react in every window and never stall a game. */ import { describe, expect, test } from 'bun:test'; import { seedN, type AnyGameDefinition } from '@bg/engine'; import { GAMES } from '../index'; import { BOT_STRATEGIES, decideBotAction } from './index'; import { botPlayout } from './playout'; const isBot = (p: string) => p.startsWith('bot:'); const seats = (n: number, humans: number) => Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i}:x`)); function counts(def: AnyGameDefinition): number[] { const range = def.playersFor?.(def.defaultOptions) ?? { min: def.minPlayers, max: def.maxPlayers }; const out = new Set([Math.max(2, range.min), Math.min(range.max, 4)]); return [...out].filter((n) => !def.playerCountError?.(def.defaultOptions, n)); } describe.each(Object.keys(BOT_STRATEGIES))('%s', (id) => { const def = GAMES[id]!; test('has a game definition with legalActions', () => { expect(def).toBeDefined(); expect(typeof def.legalActions).toBe('function'); }); for (const n of counts(def)) { test(`${n} players: one human who only times out, the rest bots — the game finishes`, () => { for (let s = 1; s <= 3; s++) { const r = botPlayout(def, { players: seats(n, 1), seed: seedN(s), isBot, maxSteps: 20_000 }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); expect(r.maxBotMs).toBeLessThan(250); } }); } test('bots only: a whole game finishes without timeouts', () => { const n = counts(def).at(-1)!; for (let s = 1; s <= 2; s++) { const r = botPlayout(def, { players: seats(n, 0), seed: seedN(100 + s), isBot, maxSteps: 30_000 }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); } }); }); test('decideBotAction returns null when the bot has nothing to do', () => { const def = GAMES.omok!; const s = def.setup({ players: ['a', 'bot:1:x'], options: def.defaultOptions, rng: { next: () => 0.5, int: () => 0 } as never, now: 0 }); const waiting = def.activePlayers(s)[0]!; const other = waiting === 'a' ? 'bot:1:x' : 'a'; expect(decideBotAction(def, s, other, { now: 0 })).toBeNull(); expect(decideBotAction(def, s, waiting, { now: 0 })).not.toBeNull(); }); describe('levels (docs/09 §9.1)', () => { const BOARD = ['omok', 'chess', 'janggi', 'othello', 'baduk', 'connect-four']; test.each(BOARD)('%s: the weakest and strongest computers still finish a game against each other', (id) => { const def = GAMES[id]!; for (const level of [1, 60]) { const r = botPlayout(def, { players: seats(2, 0), seed: seedN(7), isBot, maxSteps: 30_000, level }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); } }); });