import { describe, expect, test } from 'bun:test'; import { SeededRng, seedN } from '@bg/engine'; import { decideBotAction } from '../bots'; import { botPlayout } from '../bots/playout'; import { hotStreakBot, runsFor, ticketEv } from './bot'; import { hotStreak as def, type HotStreakAction, type HotStreakOptions, type HotStreakState } from './index'; import { seededRng, simulate } from './sim'; 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`)); const opts = (o: Partial = {}) => def.optionsSchema.parse({ ...def.defaultOptions, ...o }); describe('핫스트릭 컴퓨터', () => { test('시뮬레이션 횟수: 레벨 1은 30번, 60은 400번', () => { expect(runsFor(1)).toBe(30); expect(runsFor(60)).toBe(400); expect(runsFor(30)).toBeGreaterThan(30); }); test('몬테카를로: 시작 카드만 많은 덱이면 마스코트 넷이 고르게, 등수 분포 합은 시행 수', () => { const st = simulate(() => ['G-S2', 'H-S2', 'D-S2', 'M-S2', 'A1a', 'A1b', 'A2a', 'A2b', 'A3a', 'A3b', 'G-F3', 'H-F3', 'D-F3', 'M-F3', 'G-F2', 'H-F2', 'D-F2', 'M-F2'], 200, { burn: 3, hurdles: false, side: 'any-dq' }, seededRng(5)); for (const m of ['G', 'H', 'D', 'M'] as const) expect(st.low[m].reduce((a, b) => a + b, 0)).toBe(200); // Lots of 3s for 고블러 only → 고블러 wins more often. const g = simulate(() => ['G-S2', 'H-S2', 'D-S2', 'M-S2', 'G-F3', 'G-F2', 'G-W1', 'G-ST', 'A1a', 'A1b', 'H-TA', 'D-TA', 'M-TA', 'H-B2', 'D-B2', 'M-B2', 'A2a', 'A2b'], 300, { burn: 3, hurdles: false, side: 'any-dq' }, seededRng(6)); expect(g.low.G[0]! / 300).toBeGreaterThan(0.5); expect(ticketEv({ stack: 'G', tier: 1, side: 'risky', reverse: false }, g)).toBeGreaterThan(ticketEv({ stack: 'M', tier: 1, side: 'risky', reverse: false }, g)); }); for (const level of [1, 60]) { for (const n of [2, 3, 6, 9, 10]) { test(`레벨 ${level}, ${n}명: 컴퓨터끼리 끝까지, 모든 수 합법, 한 수가 빠름`, () => { const r = botPlayout(def, { players: seats(n, 0), seed: seedN(n + level), isBot, level, maxSteps: 20_000 }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); expect(r.maxBotMs).toBeLessThan(level === 60 ? 120 : 60); }); } } test('하우스 룰 조합(거꾸로·2장·상금 나누기 두 배)에서도 합법한 수만', () => { for (const o of [{ reverseBets: true, cardsPerRace: 2 as const, races: 1 as const }, { bettingMode: 'pots' as const, reverseBets: true, races: 1 as const }, { finalBet: 'bothBonus' as const, hurdles: true }]) { const r = botPlayout(def, { players: seats(4, 1), options: opts(o), seed: seedN(77), isBot, level: 30, maxSteps: 20_000 }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); } }); test('레벨 60은 아무렇게나 고르는 사람보다 돈을 더 많이 벌어요', () => { let bot = 0; let rnd = 0; for (let g = 1; g <= 12; g++) { const players = ['bot:0:x', 'rand1', 'bot:2:x', 'rand3']; const rng = SeededRng.fromSeed(seedN(300 + g)); const pick = SeededRng.fromSeed(seedN(400 + g)); let s: HotStreakState = def.setup({ players, options: def.defaultOptions, rng, now: 0 }); let now = 0; for (let step = 0; step < 5000 && !def.result(s); step++) { now += 500; const p = def.activePlayers(s)[0] ?? def.timeoutPlayers!(s)[0]!; let a: HotStreakAction; if (s.phase === 'race' || s.phase === 'payout') a = { type: 'TIMEOUT' }; else if (isBot(p)) a = decideBotAction(def, s, p, { now, random: () => pick.next(), level: 60 }) as HotStreakAction; else { const legal = def.legalActions!(s, p); a = legal[pick.int(legal.length)]!; } now = Math.max(now, s.phase === 'race' ? s.race!.nextFlipAt : now); s = def.apply(s, p, a, { rng, now }).state; } expect(def.result(s)).not.toBeNull(); bot += s.money['bot:0:x']! + s.money['bot:2:x']!; rnd += s.money.rand1! + s.money.rand3!; } expect(bot).toBeGreaterThan(rnd); }, 60_000); test('카드 넣기: 내가 건 마스코트를 미는 카드를 골라요', () => { const s = def.setup({ players: ['bot:0:x', 'b'], options: opts({ races: 1, finalBet: 'none' }), rng: SeededRng.fromSeed(seedN(3)), now: 0 }); s.phase = 'submit'; s.tickets['bot:0:x'] = [ { stack: 'D', tier: 1, side: 'risky', reverse: false, doubled: false }, { stack: 'D', tier: 2, side: 'risky', reverse: false, doubled: false }, ]; s.hands['bot:0:x'] = ['D-F3#1', 'D-FD#1', 'D-B2#1', 'D-TA#1']; s.hands.b = s.hands.b!.filter((c) => !c.startsWith('D-')); s.pool = s.pool.filter((c) => !c.startsWith('D-') || c.startsWith('D-S2')); while (s.pool.length < 14) s.pool.push(`A1a#9${s.pool.length}`); const legal = def.legalActions!(s, 'bot:0:x'); let fd = 0; for (let i = 0; i < 6; i++) { const r = SeededRng.fromSeed(seedN(i)); const a = hotStreakBot({ me: 'bot:0:x', view: def.view(s, 'bot:0:x'), legal, random: () => r.next(), level: 60 }) as Extract; expect(a.type).toBe('SUBMIT'); expect(a.cards).toHaveLength(2); if (a.cards.includes('D-FD#1') && a.cards.includes('D-TA#1')) fd++; } expect(fd).toBe(0); }); });