Files
wikideck/tests/ui-duel-bots.test.ts
claudeandClaude Opus 5.5 848cfbbaf1 ui-duel: view-only fallback duel bots (Easy, Normal) for the browser bot match (#58)
Checked locally against duel-core's engine-wip: 40 full games through the board
pipeline (adapter, declaration reducers, expand) with every sent action legal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:55:08 -05:00

54 lines
3.9 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { EASY_BOT, NORMAL_BOT } from '../src/game/duel/bots.ts';
import type { DuelAction, DuelCard, PermanentView, PlayerView } from '../src/game/duel/contract.ts';
const card = (id: string, extra: Partial<DuelCard> = {}): DuelCard => ({
cardId: id, pageId: 1, title: id, type: 'Figure', portal: 'Society', cost: 2, power: 2, toughness: 2, keywords: [], effects: [], rulesText: '', flavor: '', ...extra,
});
const perm = (uid: string, controller: 0 | 1, power: number, toughness: number, extra: Partial<PermanentView> = {}): PermanentView => ({
uid, owner: controller, controller, card: card(uid, { power, toughness }), tapped: false, sick: false, damage: 0, counters: 0, power, toughness, keywords: [], ...extra,
});
const side = (seat: 0 | 1, life = 20) => ({ seat, life, citations: 3, maxCitations: 3, handSize: 0, librarySize: 30, graveyard: [], bankSeconds: 300, ready: true, mulligansTaken: 0 });
const view = (p: Partial<PlayerView>): PlayerView => ({
engine: 'duel', seat: 1, turn: 4, active: 1, phase: 'main', toAct: 1, you: { ...side(1), hand: [] }, opponent: side(0),
battlefield: [], combat: { attackers: [], blocks: {} }, result: null, version: 1, rules: {}, ...p,
});
const rng = () => 0.5;
test('normal bot: plays the priciest card, harmful effects at the opponent', () => {
const hand = [{ uid: '1:1', owner: 1 as const, card: card('cheap', { cost: 1 }) },
{ uid: '1:2', owner: 1 as const, card: card('bolt', { type: 'Event', cost: 3, power: null, toughness: null, effects: [{ op: 'damage', amount: 3 }] }) }];
const v = view({ you: { ...side(1), hand }, battlefield: [perm('0:9', 0, 3, 3), perm('1:8', 1, 2, 2)] });
const legal: DuelAction[] = [{ type: 'play', uid: '1:1' },
{ type: 'play', uid: '1:2', targets: [{ kind: 'card', uid: '1:8' }] }, { type: 'play', uid: '1:2', targets: [{ kind: 'card', uid: '0:9' }] }, { type: 'endPhase' }];
assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[2]);
});
test('normal bot: attacks unless a blocker eats it, all-in on lethal, then confirms', () => {
const bf = [perm('1:1', 1, 3, 3), perm('1:2', 1, 1, 1), perm('0:1', 0, 2, 4)];
const legal: DuelAction[] = [{ type: 'toggleAttacker', uid: '1:1' }, { type: 'toggleAttacker', uid: '1:2' }, { type: 'confirmAttack' }];
// 1:1 (3/3) faces a 2/4 that survives but doesn't kill: attack. 1:2 (1/1) would die to it for nothing.
const v = view({ phase: 'attack', battlefield: bf });
assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[0]);
const after = view({ phase: 'attack', battlefield: bf, combat: { attackers: ['1:1'], blocks: {} } });
assert.deepEqual(NORMAL_BOT.choose(after, legal.slice(1), rng), { type: 'confirmAttack' });
const lethal = view({ phase: 'attack', battlefield: bf, opponent: side(0, 1), combat: { attackers: ['1:1'], blocks: {} } });
assert.deepEqual(NORMAL_BOT.choose(lethal, legal.slice(1), rng), legal[1]);
});
test('normal bot: takes the kill-and-survive block, otherwise no block', () => {
const bf = [perm('0:1', 0, 2, 2), perm('1:1', 1, 3, 3), perm('1:2', 1, 1, 1)];
const v = view({ phase: 'block', active: 0, battlefield: bf, combat: { attackers: ['0:1'], blocks: {} } });
const legal: DuelAction[] = [{ type: 'block', blocker: '1:1', attacker: '0:1' }, { type: 'block', blocker: '1:2', attacker: '0:1' }, { type: 'confirmBlocks' }];
assert.deepEqual(NORMAL_BOT.choose(v, legal, rng), legal[0]);
assert.deepEqual(NORMAL_BOT.choose(v, legal.slice(1), rng), { type: 'confirmBlocks' });
});
test('bots always return a listed action and keep their opening hand', () => {
const legal: DuelAction[] = [{ type: 'keep' }, { type: 'mulligan' }];
assert.deepEqual(NORMAL_BOT.choose(view({ phase: 'mulligan' }), legal, rng), { type: 'keep' });
let x = 1; const r = () => ((x = (x * 48271) % 2147483647) / 2147483647);
for (let i = 0; i < 50; i++) assert.ok(legal.includes(EASY_BOT.choose(view({ phase: 'mulligan' }), legal, r)));
});