Files
wikideck/tests/ui-bots.test.ts
T
claudeandClaude Opus 5.5 7f3f033e00 Normal bot plays sim-balance's lookahead on the engine view
Issue #5 follow-up. When ui-a's adapter attaches MatchView.engine (viewFor
output only), the normal bot uses shared/bots makeViewBot('lookahead-lite').
Easy stays a view-model bot. Real engine via ui-a's adapter: normal beats
random 93% (100 matches) and beats easy 77%; the default test run plays 16
matches per pairing (~50 s); UI_BOTS_FULL=1 plays 100.

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

82 lines
3.9 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_RULESET } from '../shared/game/index.ts';
import { engine } from '../src/game/adapter.ts';
import { BOTS as LOBBY_BOTS, randomBot } from '../src/game/bots.ts';
import { opponentDeck, starterDeck } from '../src/game/cards.ts';
import { BOTS, makeUiBot } from '../src/game/ai/index.ts';
import type { MatchView, PlayerIndex, UIAction } from '../src/game/types.ts';
type B = { choose(view: MatchView, legal: UIAction[], rand: () => number): UIAction };
function rand(seed: number) { return () => ((seed = (seed * 1103515245 + 12345) >>> 0) / 4294967296); }
/** Plays one real-engine match through ui-a's adapter, the way the match screen drives a bot seat. */
function play(bots: [B, B], seed: string) {
let s = engine.createMatch(DEFAULT_RULESET, [starterDeck(), opponentDeck(seed)], seed);
const r = rand(seed.length * 7919 + seed.charCodeAt(seed.length - 1));
for (let steps = 0; steps < 2000 && !engine.isOver(s); steps++) {
for (const p of [0, 1] as PlayerIndex[]) {
const legal = engine.legal(s, p);
if (!legal.length) continue;
const pick = bots[p].choose(engine.view(s, p), legal.filter(a => a.type !== 'concede'), r);
assert.notEqual(pick.type, 'concede');
s = engine.apply(s, p, pick);
}
}
assert.ok(engine.isOver(s), 'match finishes');
return engine.result(s, 0).winner;
}
function winRate(a: B, b: B, n: number) {
let score = 0;
for (let i = 0; i < n; i++) {
const flip = i % 2 === 1;
const w = play(flip ? [b, a] : [a, b], `bots-${i}`);
score += w === null ? 0.5 : w === (flip ? 1 : 0) ? 1 : 0;
}
return score / n;
}
const [easy, normal] = BOTS;
// Normal runs sim-balance's lookahead (~1.5 s of CPU per match), so the default run is small;
// UI_BOTS_FULL=1 plays 100 matches per pairing.
const N = process.env.UI_BOTS_FULL ? 100 : 16;
test('BOTS exports easy and normal in the Bot shape', () => {
assert.deepEqual(BOTS.map(b => b.id), ['easy', 'normal']);
assert.deepEqual(LOBBY_BOTS.map(b => b.id), ['easy', 'normal'], 'the lobby offers the easy and normal bots');
for (const b of BOTS) assert.ok(b.name && b.description);
});
test('normal bot beats random play more than 80% of the time', () => {
const r = winRate(normal, randomBot, N);
console.log(`normal vs random ${r}`); assert.ok(r > 0.8, `normal vs random ${r}`);
});
test('easy bot beats random, and normal beats easy', () => {
const e = winRate(easy, randomBot, 100), g = winRate(normal, easy, N);
console.log(`easy vs random ${e}, normal vs easy ${g}`); assert.ok(e > 0.5, `easy vs random ${e}`);
assert.ok(g > 0.55, `normal vs easy ${g}`);
});
test('bots never concede or un-queue, and end the turn when nothing fits', () => {
const view = { hand: [], lanes: [] } as unknown as MatchView;
for (const b of BOTS) {
const only: UIAction[] = [{ type: 'unstage', cardId: 'x' }, { type: 'concede' }, { type: 'endTurn' }];
for (let i = 0; i < 20; i++) assert.equal(b.choose(view, only, rand(i)).type, 'endTurn');
}
});
test('an engine-level chooser is used when the adapter attaches engine actions', () => {
const seen: unknown[] = [];
const bot = makeUiBot('normal', (ev, _r) => { seen.push(ev); return { type: 'play', player: 1, cardId: 'b', lane: 2 }; });
const legal = [
{ type: 'play', cardId: 'a', lane: 0, engine: { type: 'play', player: 1, cardId: 'a', lane: 0 } },
{ type: 'play', cardId: 'b', lane: 2, engine: { lane: 2, cardId: 'b', player: 1, type: 'play' } },
{ type: 'endTurn', engine: { type: 'commit', player: 1 } },
] as unknown as UIAction[];
const view = { hand: [], lanes: [], engine: { view: { you: 1 } } } as unknown as MatchView;
assert.equal(bot.choose(view, legal, rand(1)), legal[1]);
assert.deepEqual(seen, [{ view: { you: 1 } }], 'the engine chooser gets only the viewFor output');
});