import { describe, expect, test } from 'bun:test'; import { SeededRng, seedN } from '@bg/engine'; import { decideBotAction } from '../bots/index'; import { botPlayout } from '../bots/playout'; import { FULL_DECK, dalmuti, type DCard, type DalmutiOptions, type DalmutiState } from './index'; const BY_ID = new Map(FULL_DECK.map((c) => [c.id, c])); const C = (n: number, k = 1) => `c${n}-${k}`; const P = ['A', 'B', 'C', 'D', 'E']; function make(opts: Partial = {}): DalmutiState { return dalmuti.setup({ players: P, options: dalmuti.optionsSchema.parse(opts), rng: SeededRng.fromSeed(seedN(1)), now: 0 }); } /** Seats A..E in order, gives A `hand` (others a few cards) and starts a trick, optionally under B's set. */ function rig(hand: string[], top?: string[]): DalmutiState { const s = make(); s.order = [...P]; const cards = (ids: string[]) => ids.map((id) => BY_ID.get(id)! as DCard); s.hands = { A: cards(hand), B: cards([C(12, 1)]), C: cards([C(12, 2)]), D: cards([C(12, 3)]), E: cards([C(12, 4)]) }; s.phase = { kind: 'playing' }; s.current = 'A'; s.trick = { leader: top ? 'B' : 'A', top: null, passesSinceTop: [], lockedOut: [], pile: [] }; if (top) { const n = BY_ID.get(top[0]!)!.n; s.trick.top = { cardIds: top, count: top.length, rank: n, by: 'B' }; s.trick.pile = [s.trick.top]; } return s; } const decide = (s: DalmutiState) => decideBotAction(dalmuti, s, 'A', { now: 0, random: () => 0.5 }) as { type: string; cardIds?: string[]; declareRevolution?: boolean }; describe('dalmuti bot', () => { test('leads its weakest number as a whole set', () => { const s = rig([C(3), C(7, 1), C(7, 2), C(11, 1), C(11, 2), C(11, 3)]); expect(decide(s)).toEqual({ type: 'play', cardIds: [C(11, 1), C(11, 2), C(11, 3)] }); }); test('follows with the weakest pair that beats the table, without jesters', () => { const s = rig([C(4, 1), C(4, 2), C(6, 1), C(6, 2), C(9, 1), C(9, 2), 'j1'], [C(10, 1), C(10, 2)]); expect(decide(s)).toEqual({ type: 'play', cardIds: [C(9, 1), C(9, 2)] }); }); test('passes rather than spend a top card while the hand is big', () => { const s = rig([C(2), C(3), C(8), C(9), C(10), C(11)], [C(5)]); expect(decide(s)).toEqual({ type: 'pass' }); }); test('gives back its two weakest numbers in the tax, never a jester', () => { const s = rig([C(1), C(2), C(11), C(12), 'j1']); s.phase = { kind: 'taxReturn', owed: { A: { to: 'E', count: 2, done: false } } }; expect(decide(s).cardIds!.sort()).toEqual([C(11), C(12)].sort()); }); test('declares a revolution only from the lower half', () => { for (const [order, declare] of [[['B', 'C', 'D', 'E', 'A'], true], [['A', 'B', 'C', 'D', 'E'], false]] as const) { const s = rig([C(5), 'j1', 'j2']); s.order = [...order]; s.phase = { kind: 'revolutionCheck', confirmed: Object.fromEntries(P.map((p) => [p, false])) }; expect(!!decide(s).declareRevolution).toBe(declare); } }); const modes: [string, Partial][] = [ ['pass locks out, 38 cards', { passLocksOut: true, removeHighCards: true }], ['no revolution, taxable jesters, 5 rounds by last round', { revolution: false, jesterTaxable: true, rounds: 5, scoring: 'last-round' }], ]; for (const [name, opts] of modes) { test(`bots finish a game: ${name}`, () => { const options = dalmuti.optionsSchema.parse(opts); for (let seed = 1; seed <= 2; seed++) { const r = botPlayout(dalmuti as never, { players: ['h', 'bot:1', 'bot:2', 'bot:3', 'bot:4'], options, seed: seedN(seed), isBot: (p) => p.startsWith('bot:') }); expect(r.finished).toBe(true); expect(r.botTimeouts).toBe(0); } }); } });