/** 우노 with computers in a real room (docs/games/uno.md §9.1): a person and three computers play to the end. */ import { afterEach, expect, test } from 'bun:test'; import { GAMES } from '@bg/games'; import { botProfile, isBotId, type ServerMessage } from '@bg/shared'; import type { UnoState } from '@bg/games/uno'; import { openDb } from '../db'; import { RoomManager } from './manager'; import type { Conn, Room } from './room'; import { RoomStore } from './store'; const rooms: Room[] = []; afterEach(() => { for (const r of rooms.splice(0)) r.clearTimer(); }); function practice(options: Record = {}) { const store = new RoomStore(openDb(':memory:')); let now = 1_000_000; const clock = { now: () => now, advance: (ms: number) => (now += ms) }; const users = { publicUser: (id: string) => botProfile(id) ?? { id, nickname: id, avatar: null, kind: 'guest' as const } }; const manager = new RoomManager({ store, games: GAMES, users, now: clock.now, botDelay: () => 0 }); const room = manager.createPractice('A', 'uno') as Room; rooms.push(room); const msgs: ServerMessage[] = []; const a: Conn = { userId: 'A', send: (m) => msgs.push(m), close: () => {} }; room.join(a, 'player'); room.addBot('A'); room.addBot('A'); room.configure('A', { options: { ...(room.config.options as object), ...options } }); expect(room.start('A')).toBeNull(); return { room, clock, msgs, a }; } /** Bots move at once; when the game waits on the person, they play their first legal action (or the clock runs out). */ function playToEnd(room: Room, clock: { advance: (ms: number) => void }, humanActs: boolean): number { const def = GAMES.uno!; let cs = 0; let steps = 0; for (; steps < 20_000 && !room.game!.finished; steps++) { if (room.bots.dueAt !== null) { room.bots.fire(); continue; } const s = room.game!.state as UnoState; const legal = def.legalActions!(s, 'A'); if (humanActs && legal.length && def.activePlayers(s).includes('A')) { room.act('A', ++cs, legal.find((x) => (x as { type: string }).type !== 'catchUno') ?? legal[0]); continue; } clock.advance(61_000); room.onTimer(); } return steps; } test('a person and three computers finish a game of 우노 (person plays)', () => { const { room, clock } = practice(); expect(room.game!.players.filter(isBotId)).toHaveLength(3); playToEnd(room, clock, true); expect(room.game!.finished).toBe(true); expect(room.status).toBe('finished'); }); test('500 points with house rules: the person only times out, the computers still finish', () => { const { room, clock, msgs } = practice({ scoring: 'to500', stacking: true, drawUntilPlayable: true }); playToEnd(room, clock, false); expect(room.game!.finished).toBe(true); const s = room.game!.state as UnoState; expect(s.rounds.length).toBeGreaterThanOrEqual(1); expect(msgs.some((m) => m.t === 'state')).toBe(true); // The person's screen shows card counts only for the computers. const view = JSON.stringify(GAMES.uno!.view(s, 'A')); for (const bot of s.seats.filter(isBotId)) for (const c of s.hands[bot]!) expect(view).not.toContain(`"${c.id}"`); }); test('무제한 time: no clock while the person is here; a dropped connection is still auto-played after the grace time', () => { const { room, clock, a } = practice({ turnSeconds: null }); // Let the computers move until the game waits on the person. for (let i = 0; i < 200 && room.bots.dueAt !== null; i++) room.bots.fire(); const s = () => room.game!.state as UnoState; expect(GAMES.uno!.activePlayers(s())).toContain('A'); expect(room.effectiveDeadline()).toBeNull(); const turn = s().turnNo; room.disconnect(a); const d = room.effectiveDeadline(); expect(d).not.toBeNull(); clock.advance(d! - clock.advance(0) + 1); room.onTimer(); expect(s().turnNo !== turn || s().log.some((e) => e.t === 'timeout' && e.p === 'A')).toBe(true); });