import { describe, expect, test } from 'bun:test'; import { SEOTDA_DECK, SeededRng, seedN } from '@bg/engine'; import { decideBotAction } from '../bots/index'; import { botPlayout } from '../bots/playout'; import { seotda, type SeotdaAction, type SeotdaOptions, type SeotdaState } from './index'; /** Special card of a month (1·3·8 광, others 열끗) and the plain card. */ const S = (m: number) => (m - 1) * 4; const P = (m: number) => (m - 1) * 4 + 1; let clock = 1000; function make(opts: Partial = {}, players = ['A', 'B', 'C'], dealer = 'A'): SeotdaState { const options = seotda.optionsSchema.parse(opts); for (let seed = 1; seed < 500; seed++) { const s = seotda.setup({ players, options, rng: SeededRng.fromSeed(seedN(seed)), now: 0 }); if (s.hand!.dealer === dealer) return s; } throw new Error('no seed with that dealer'); } function rig(s: SeotdaState, cards: Record, next: number[] = []): SeotdaState { const h = s.hand!; for (const [id, cs] of Object.entries(cards)) h.cards[id] = [...cs]; const used = new Set([...Object.values(h.cards).flat(), ...next]); h.deck = [...next, ...SEOTDA_DECK.filter((c) => !used.has(c))]; return s; } const act = (s: SeotdaState, p: string, a: SeotdaAction) => seotda.apply(s, p, a, { rng: SeededRng.fromSeed([7, 7, 7, 7]), now: (clock += 1000) }).state; const decide = (s: SeotdaState, p: string, r = 0.1) => decideBotAction(seotda, s, p, { now: clock, random: () => r }) as SeotdaAction; describe('seotda bot', () => { test('raises a 38광땡', () => { const s = rig(make({ dealMode: 'oneRound' }), { A: [S(3), S(8)], B: [P(5), P(6)], C: [P(2), P(7)] }); expect(['half', 'quarter', 'full']).toContain(decide(s, 'A').type); }); test('dies with 망통 facing a half bet, calls it with 장땡', () => { let s = rig(make({ dealMode: 'oneRound' }), { A: [S(3), S(8)], B: [P(2), P(8)], C: [S(10), P(10)] }); s = act(s, 'A', { type: 'half' }); expect(decide(s, 'B', 0.9).type).toBe('die'); s = act(s, 'B', { type: 'die' }); expect(['call', 'ddadang', 'quarter', 'half']).toContain(decide(s, 'C', 0.9).type); }); test('checks a weak hand when betting is free (never dies for nothing)', () => { const s = rig(make({ dealMode: 'oneRound' }), { A: [P(2), P(3)], B: [P(5), P(6)], C: [P(2), P(7)] }); expect(decide(s, 'A', 0.9).type).toBe('check'); }); test('3장 섯다: keeps the best pair of its three cards', () => { let s = make({ cardMode: 'three' }); s = rig(s, { A: [P(4), P(9)], B: [P(5), P(7)], C: [P(2), P(3)] }, [P(6), S(2), S(5)]); for (const p of ['A', 'B', 'C']) s = act(s, p, { type: 'check' }); expect(s.hand!.phase).toBe('choose'); const a = decide(s, 'A') as Extract; expect([...a.cardIds].sort((x, y) => x - y)).toEqual([P(4), P(6)]); // 세륙 over 구사·5끗 }); test.each([ ['2장 섯다', {}], ['3장 섯다', { cardMode: 'three' }], ] as const)('%s: 1 human + 3 bots finish without bot timeouts', (_, options) => { const opts = seotda.optionsSchema.parse(options); for (let s = 1; s <= 2; s++) { const r = botPlayout(seotda, { players: ['human', 'bot:1', 'bot:2', 'bot:3'], options: opts, seed: seedN(s), isBot: (p) => p.startsWith('bot:') }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); expect(r.maxBotMs).toBeLessThan(100); } }); });