/** 라이어 게임 computers (docs/games/liar.md §13): legal, never stuck, never leak, and higher levels play better. */ import { describe, expect, test } from 'bun:test'; import { SeededRng, seedN } from '@bg/engine'; import { botPace, botPersona, type BotPersona } from '../bots/persona'; import { liveBotPlayout } from '../bots/live-playout'; import { chatProblem } from '../mafia'; import { _internal, liarBot } from './bot'; import { liar, liarOptionsSchema, saysWord, topicById, type LiarOptions, type LiarState } from './index'; const { LiarBot, fill } = _internal; const isBot = (p: string) => p.startsWith('bot:'); const seats = (n: number, humans: number) => Array.from({ length: n }, (_, i) => (i < humans ? `human${i}` : `bot:${i + 1}:t${i}`)); const opts = (o: Partial = {}) => liarOptionsSchema.parse(o); const persona = (smart: boolean, p: string): BotPersona => ({ smart, pace: botPace(p), chatty: 0.8 }); describe('lines', () => { test('josa follows the name', () => { expect(fill('{X:이} 라이어 같아요', { X: '컴퓨터 3님' })).toBe('컴퓨터 3님이 라이어 같아요'); expect(fill('{X:이랑} 같은 생각', { X: '철수' })).toBe('철수랑 같은 생각'); expect(fill('{X:은} 아닌듯?', { X: '3번' })).toBe('3번은 아닌듯?'); }); }); describe('whole games', () => { const variants: [string, Partial][] = [ ['default', {}], ['바보 모드, 한 바퀴, 고르기', { foolMode: true, rounds: 1, guessMode: 'choice' }], ['짧은 시간', { describeSeconds: 15, discussionSeconds: 30, voteSeconds: 15, guessSeconds: 20, topic: 'fairy' }], ]; for (const [label, o] of variants) { test(`${label}: 3~10명, 사람 1명(가만히) + 컴퓨터, 컴퓨터만 — 모두 끝나고, 거절된 행동 없음, 제시어를 말하지 않음`, () => { for (const n of [3, 5, 8, 10]) { for (const humans of [1, 0]) { const r = liveBotPlayout(liar, liarBot, { players: seats(n, humans), options: opts(o), seed: seedN(n * 10 + humans), isBot }); expect(r.finished).toBe(true); expect(r.rejected).toEqual([]); expect(r.maxBotMs).toBeLessThan(50); const st = r.state as LiarState; for (const line of r.chat) { expect(chatProblem(line.text, true)).toBeNull(); expect(line.text).not.toMatch(/undefined|null|NaN|\{|bot:/); // Nobody says their own word (the game refuses it; bots never even try). const own = line.from === st.liar ? st.liarWord : st.word; if (own) expect(saysWord(line.text, st.topic, own)).toBe(false); } // Every computer described on its turn (none ran out of time). const bots = st.players.filter((p) => isBot(p.id)).map((p) => p.id); for (const d of st.descriptions) if (bots.includes(d.from)) expect(d.text).not.toBeNull(); expect(st.descriptions.length).toBe(n * st.options.rounds); } } }, 60_000); } test('모든 시간 무제한, 컴퓨터만: 설명·토론 끝내기·투표·맞히기를 해서 모두 끝나요', () => { const options = opts({ describeSeconds: null, discussionSeconds: null, voteSeconds: null, guessSeconds: null }); for (const n of [3, 5, 8, 10]) { const r = liveBotPlayout(liar, liarBot, { players: seats(n, 0), options, seed: seedN(900 + n), isBot }); expect(r.finished).toBe(true); expect(r.rejected).toEqual([]); const st = r.state as LiarState; expect(st.descriptions.every((d) => d.text !== null)).toBe(true); expect(r.elapsed).toBeGreaterThan(30_000); } }, 60_000); test('컴퓨터는 토론 때 말하고, 라이어 컴퓨터는 자기를 지목하지 않아요', () => { for (let s = 1; s <= 10; s++) { const r = liveBotPlayout(liar, liarBot, { players: seats(6, 0), seed: seedN(300 + s), isBot }); const st = r.state as LiarState; expect(st.chat.length).toBeGreaterThan(0); const lastVote = st.events.filter((e) => e.t === 'vote').at(-1); if (lastVote && lastVote.t === 'vote') expect(lastVote.votes[st.liar]).not.toBe(st.liar); } }, 60_000); test('레벨이 높은 컴퓨터가 더 잘해요: 똑똑한 시민이 라이어를 더 자주 잡고, 똑똑한 라이어가 더 자주 맞혀요', () => { const run = (citizenLevel: number, liarLevel: number) => { let citizens = 0; let guessed = 0; let caught = 0; const n = 40; for (let s = 1; s <= n; s++) { const r = liveBotPlayout(liar, liarBot, { players: seats(5, 0), seed: seedN(1000 + s), isBot, personaOf: (p, st) => persona(((st as LiarState).liar === p ? liarLevel : citizenLevel) > 30, p), levelOf: (p, st) => ((st as LiarState).liar === p ? liarLevel : citizenLevel), }); const f = (r.state as LiarState).final!; if (f.winner === 'citizens') citizens++; if (f.accused === f.liar) caught++; if (f.reason === 'guessed') guessed++; } return { citizens: citizens / n, caught: caught / n, guessed }; }; const strongTown = run(60, 1); const weakTown = run(1, 60); expect(strongTown.caught).toBeGreaterThan(weakTown.caught); expect(strongTown.citizens - weakTown.citizens).toBeGreaterThan(0.2); // A strong liar who is caught names the word more often than a weak one. const strongLiar = run(30, 60); const weakLiar = run(30, 1); expect(strongLiar.guessed).toBeGreaterThan(weakLiar.guessed); }, 60_000); }); describe('reading', () => { function atDiscussion(o: Partial = {}) { const players = seats(5, 0); let st = liar.setup({ players, options: opts({ topic: 'fruit', rounds: 1, ...o }), rng: SeededRng.fromSeed(seedN(3)), now: 0 }); st.word = '수박'; st.liar = players[4]!; st.order = [...players]; const hints = topicById('fruit')!.words.find((w) => w.word === '수박')!.hints; const rng = SeededRng.fromSeed(seedN(4)); let now = 0; st = liar.apply(st, players[0]!, { type: 'phaseTimeout' }, { rng, now: (now = st.phase.kind === 'reveal' ? st.phase.endsAt : 0) }).state; for (let i = 0; i < 4; i++) st = liar.apply(st, players[i]!, { type: 'describe', text: hints[i]! }, { rng, now: (now += 2000) }).state; st = liar.apply(st, players[4]!, { type: 'describe', text: '저는 좋아하는 편이에요' }, { rng, now: (now += 2000) }).state; const names = Object.fromEntries(players.map((p, i) => [p, `컴퓨터 ${i + 1}`])); return { st, players, names, now }; } test('라이어 컴퓨터(높은 레벨)는 설명을 듣고 제시어를 맞혀요', () => { const { st, players, names, now } = atDiscussion(); const me = players[4]!; const bot = new LiarBot({ me, view: liar.view(st, me), now, random: Math.random, persona: botPersona(me, 50), level: 60, names, memory: {} }); expect(bot.guessWord()).toBe('수박'); }); test('시민 컴퓨터(높은 레벨)는 제시어와 안 맞는 설명을 한 사람을 가장 의심해요', () => { const { st, players, names, now } = atDiscussion(); const me = players[0]!; const bot = new LiarBot({ me, view: liar.view(st, me), now, random: Math.random, persona: botPersona(me, 50), level: 60, names, memory: {} }); const others = players.slice(1); const top = others.sort((a, b) => bot.sus(b) - bot.sus(a))[0]; expect(top).toBe(players[4]); expect(bot.stance('컴퓨터 5 라이어 아니야?').accused).toEqual([players[4]!]); expect(bot.stance('컴퓨터 2님은 맞게 설명한 것 같아요').cleared).toEqual([players[1]!]); }); test('라이어 컴퓨터가 먼저 말하면 주제에 다 맞는 무난한 말을 해요', () => { const players = seats(4, 0); let st = liar.setup({ players, options: opts({ topic: 'animal' }), rng: SeededRng.fromSeed(seedN(5)), now: 0 }); st.order = [...players]; st.liar = players[0]!; st = liar.apply(st, players[0]!, { type: 'phaseTimeout' }, { rng: SeededRng.fromSeed(seedN(6)), now: 10_000 }).state; const names = Object.fromEntries(players.map((p, i) => [p, `컴퓨터 ${i + 1}`])); const bot = new LiarBot({ me: players[0]!, view: liar.view(st, players[0]!), now: 10_000, random: Math.random, persona: botPersona(players[0]!, 50), level: 50, names, memory: {} }); expect(topicById('animal')!.generic).toContain(bot.description()); }); });