/** Computer players over the real HTTP/WS stack (docs/06-lobby-rooms.md §12). */ import { afterEach, describe, expect, test } from 'bun:test'; import { Client, api, boot, guest, sleep } from './test-utils'; type Booted = ReturnType; const running: Booted[] = []; const clients: Client[] = []; afterEach(async () => { for (const c of clients.splice(0)) { try { c.close(); } catch {} } for (const s of running.splice(0)) { try { await s.stop(); } catch {} } }); const fastBots = { botDelay: () => 30 }; function start(dbPath?: string): Booted { const s = boot(dbPath, fastBots); running.push(s); return s; } async function client(s: Booted, nick: string, cookie?: { cookie: string; id: string }): Promise { const g = cookie ?? (await guest(s.base, nick)); const c = await new Client(s.base, g.cookie, g.id).connect(); clients.push(c); return c; } async function practiceOmok(s: Booted, a: Client): Promise { const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: JSON.stringify({ gameId: 'omok', practice: true }) }); expect(r.res.status).toBe(200); const code = r.body.code as string; a.send({ t: 'join', code, as: 'player' }); await a.next('room'); a.send({ t: 'config', options: { colorAssignment: 'hostBlack', ruleSet: 'free' } }); await a.next('room', (m) => (m.room.options as any).colorAssignment === 'hostBlack'); const st = a.next('state'); a.send({ t: 'start' }); await st; return code; } describe('computer players', () => { test('practice room: host + computer seated, private, not listed; the computer answers moves', async () => { const s = start(); const a = await client(s, '호랑이'); const code = await practiceOmok(s, a); const room = a.lastOf('room')!.room; expect(room.visibility).toBe('private'); expect(room.seats.map((x) => x?.user.kind)).toEqual(['guest', 'bot']); expect(room.seats[1]!.user.nickname).toBe('컴퓨터 1'); const pub = await api(s.base, '/api/games/omok/rooms'); expect(pub.body.rooms.find((r: { code: string }) => r.code === code)).toBeUndefined(); for (let i = 0; i < 3; i++) { const answered = a.next('state', (m) => (m.view as any).moveCount === 2 * i + 2); const board = (a.lastOf('state')!.view as any).board as number[]; const free = board.indexOf(0); a.act({ type: 'place', x: free % 15, y: Math.floor(free / 15) }); await answered; } }); test('addBot / removeBot over the socket: host only, errors for others', async () => { const s = start(); const a = await client(s, '호랑이'); const b = await client(s, '고양이'); const r = await api(s.base, '/api/rooms', { method: 'POST', cookie: a.cookie, body: JSON.stringify({ gameId: 'one-card', visibility: 'public' }) }); const code = r.body.code as string; a.send({ t: 'join', code, as: 'player' }); await a.next('room'); b.send({ t: 'join', code, as: 'player' }); await b.next('room'); b.send({ t: 'addBot' }); expect((await b.next('error')).message).toBe('방장만 컴퓨터를 넣을 수 있어요.'); a.send({ t: 'addBot', seat: 3 }); const withBot = await a.next('room', (m) => m.room.seats.some((x) => x?.user.kind === 'bot')); expect(withBot.room.visibility).toBe('private'); const botId = withBot.room.seats[3]!.user.id; a.send({ t: 'removeBot', userId: botId }); await a.next('room', (m) => !m.room.seats.some((x) => x?.user.kind === 'bot')); }); test('restart with the computer to move: after restore it plays on its own', async () => { const s1 = boot(undefined, fastBots); const a = await client(s1, '호랑이'); const code = await practiceOmok(s1, a); // Stop the bot before it answers, then crash. for (const r of s1.rooms.all()) r.clearTimer(); a.act({ type: 'place', x: 7, y: 7 }); await a.next('state', (m) => (m.view as any).moveCount === 1); for (const r of s1.rooms.all()) r.clearTimer(); await Promise.race([s1.server.stop(true), sleep(300)]); for (const r of s1.rooms.all()) r.clearTimer(); s1.db.close(); const s2 = start(s1.dbPath); await sleep(200); // the restored driver plays without anyone connected const room = s2.rooms.get(code)!; expect((room.game!.state as any).moves.length).toBe(2); const a2 = await client(s2, '', { cookie: a.cookie, id: a.id }); a2.send({ t: 'join', code, as: 'player' }); const st = await a2.next('state'); expect((st.view as any).moveCount).toBe(2); }); });