/** * 라이어 게임 through a real Room with computers: one person and computers play a whole game with the real bot driver * and the room clock. The person's connection never learns the word (when they are the liar) or who the liar is (when * they are a citizen) before the reveal, and the room chat stays closed while the game runs. */ import { afterEach, describe, expect, test } from 'bun:test'; import { GAMES } from '@bg/games'; import type { LiarState, LiarView } from '@bg/games/liar'; import { botProfile, isBotId, type ServerMessage } from '@bg/shared'; 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(person = 'A') { const db = openDb(':memory:'); const store = new RoomStore(db); let now = 1_000_000; const clock = { now: () => now, advance: (ms: number) => (now += ms) }; const logs: string[] = []; const users = { publicUser: (id: string) => botProfile(id) ?? { id, nickname: '철수', avatar: null, kind: 'guest' as const } }; const manager = new RoomManager({ store, games: GAMES, users, now: clock.now, botDelay: () => 0, log: (level, msg) => level !== 'info' && logs.push(msg) }); const room = manager.createPractice(person, 'liar') as Room; rooms.push(room); const msgs: ServerMessage[] = []; const conn: Conn & { msgs: ServerMessage[] } = { userId: person, msgs, send: (m) => msgs.push(m), close: () => {} }; room.join(conn, 'player'); const state = () => room.game!.state as LiarState; const lastView = () => (msgs.filter((m) => m.t === 'state').at(-1) as Extract).view as LiarView; /** Lets the computers act (their own timers) and the room clock run, until `stop` or the end. */ const run = (stop: () => boolean = () => false, maxSteps = 4000) => { for (let i = 0; i < maxSteps && !room.game!.finished && !stop(); i++) { const next = Math.min(room.bots.dueAt ?? Infinity, room.effectiveDeadline() ?? Infinity); expect(Number.isFinite(next)).toBe(true); clock.advance(Math.max(0, next - clock.now())); room.bots.fire(); room.onTimer(); } }; return { room, clock, msgs, conn, state, lastView, run, logs }; } describe('라이어 게임 room flow with computers', () => { test('연습 방은 사람 1 + 컴퓨터 4, 컴퓨터 채팅은 항상 켜짐', () => { const { room } = practice(); expect(room.seatedIds().filter(isBotId)).toHaveLength(4); expect(room.start('A')).toBeNull(); expect(room.game!.players).toHaveLength(5); }); test('사람이 차례에 설명하고 토론·투표하면 컴퓨터와 한 판이 끝까지 가요', () => { const { room, state, lastView, msgs, run, logs } = practice(); expect(room.start('A')).toBeNull(); expect(room.chat('A', '안녕')).toBe('게임 중에는 게임 화면의 채팅을 써 주세요.'); const s0 = state(); const iAmLiar = s0.liar === 'A'; // Nothing the person receives names the liar or (if they are the liar) the word, until the reveal. const leaks = () => { const all = JSON.stringify(msgs.filter((m) => m.t === 'state').map((m) => ({ ...(m as { view: LiarView }).view, final: null, events: [] }))); if (iAmLiar) expect(all).not.toContain(`"${s0.word}"`); expect(all).not.toContain('"liar":true'); }; let cs = 1; let described = 0; // Reveal: the person confirms; the computers confirm on their own. room.act('A', cs++, { type: 'ready' }); run(() => state().phase.kind !== 'reveal'); expect(state().phase.kind).toBe('describe'); const speaker = () => GAMES.liar!.view(state(), null).phase.speaker; while (state().phase.kind === 'describe') { if (speaker() === 'A') { room.act('A', cs++, { type: 'chat', text: iAmLiar ? '저는 좋아하는 편이에요' : '음 다들 아실 거예요' }); described++; } run(() => state().phase.kind !== 'describe' || speaker() === 'A'); } leaks(); expect(described).toBe(state().options.rounds); // Every computer described in its turn. for (const d of state().descriptions) if (d.from !== 'A') expect(d.text).not.toBeNull(); expect(state().phase.kind).toBe('discussion'); room.act('A', cs++, { type: 'chat', text: '누가 라이어일까요' }); room.act('A', cs++, { type: 'ready' }); run(() => state().phase.kind !== 'discussion'); // The computers talked in the discussion. expect(state().chat.some((m) => isBotId(m.from))).toBe(true); leaks(); while (state().phase.kind === 'vote') { const v = lastView(); if (v.my!.voteTargets.length && v.my!.vote === null) room.act('A', cs++, { type: 'vote', target: v.my!.voteTargets[0]! }); run(() => state().phase.kind !== 'vote' || (state().phase.kind === 'vote' && (state().phase as { round: number }).round !== v.phase.voteRound)); } if (state().phase.kind === 'guess' && state().liar === 'A') room.act('A', cs++, { type: 'guess', text: '모르겠어요' }); run(); expect(room.game!.finished).toBe(true); const f = state().final!; expect(f.summary).toMatch(/^(라이어|시민) 승리!/); const end = lastView(); expect(end.final!.liar).toBe(s0.liar); expect(end.players.find((p) => p.id === s0.liar)!.liar).toBe(true); expect(logs).toEqual([]); }); test('사람이 아무것도 안 해도(시간 초과만) 끝나요', () => { const { room, run, state, logs } = practice(); room.start('A'); run(); expect(room.game!.finished).toBe(true); expect(state().descriptions.filter((d) => d.from === 'A').every((d) => d.text === null)).toBe(true); expect(logs).toEqual([]); }); test('모든 시간 무제한: 컴퓨터는 알아서 하고, 연결이 끊긴 사람은 유예 시간 뒤 넘겨져 끝까지 가요', () => { const { room, conn, run, state, logs } = practice(); const none = { describeSeconds: null, discussionSeconds: null, voteSeconds: null, guessSeconds: null }; expect(room.configure('A', { options: { ...(room.config.options as object), ...none } })).toBeNull(); room.start('A'); room.disconnect(conn); run(); expect(room.game!.finished).toBe(true); expect(state().options.describeSeconds).toBeNull(); expect(state().descriptions.filter((d) => d.from === 'A').every((d) => d.text === null)).toBe(true); expect(logs).toEqual([]); }); });