Files
wikideck/tests/bots-sim.test.ts
T

68 lines
3.5 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { existsSync } from 'node:fs';
import { makeBot, mulberry32 } from '../shared/bots/index.ts';
import type { EngineAdapter } from '../shared/bots/types.ts';
import { playMatch, decidedTurn } from '../scripts/sim/match.ts';
import { aggregate, rate, toMarkdown } from '../scripts/sim/stats.ts';
import { toyEngine } from '../scripts/sim/toy-engine.ts';
test('toy matches are deterministic and terminate', () => {
const decks = toyEngine.decks('toy');
const a = playMatch(toyEngine, 'toy', [decks[0], decks[1]], ['greedy', 'random'], 42);
const b = playMatch(toyEngine, 'toy', [decks[0], decks[1]], ['greedy', 'random'], 42);
assert.equal(a.truncated, false);
assert.deepEqual({ ...a, ms: 0 }, { ...b, ms: 0 });
});
test('greedy beats random on the toy engine', () => {
const decks = toyEngine.decks('toy');
const recs = Array.from({ length: 60 }, (_, i) => playMatch(toyEngine, 'toy', [decks[i % 3], decks[(i + 1) % 3]], i % 2 ? ['greedy', 'random'] : ['random', 'greedy'], i));
const stats = aggregate(recs);
assert.ok(stats.skill['greedy>random'].rate > 0.7, JSON.stringify(stats.skill));
assert.match(toMarkdown(stats), /First-player win rate/);
});
test('stats helpers', () => {
const r = rate(50, 100);
assert.ok(r.lo < 0.5 && r.hi > 0.5);
assert.equal(decidedTurn([0.2, -0.3, 0.4, 0.6], 0), 3);
assert.equal(decidedTurn([0.2, 0.3], null), null);
});
test('bots only return legal actions on shared/game', { skip: !existsSync(new URL('../shared/game/index.ts', import.meta.url)) }, async () => {
const { gameEngine } = await import('../shared/bots/game-adapter.ts');
const engine = gameEngine as EngineAdapter<any, any>;
const decks = engine.decks(engine.rulesets()[0]);
for (const name of ['random', 'greedy', 'lookahead-lite']) {
const bot = makeBot(name, engine);
let s = engine.createMatch(engine.rulesets()[0], [decks[0], decks[decks.length - 1]], 7);
const rng = mulberry32(1);
let steps = 0;
while (!engine.isOver(s) && steps++ < 500) {
const seat = engine.toAct(s);
const a = bot(engine.observe ? engine.observe(s, seat) : s, seat, rng);
assert.ok(engine.legalActions(s, seat).some(x => JSON.stringify(x) === JSON.stringify(a)), `${name} chose illegal ${JSON.stringify(a)}`);
s = engine.applyAction(s, a);
}
assert.ok(engine.isOver(s), `${name} match did not finish`);
}
});
test('seat 1 bot input does not depend on what seat 0 has queued', { skip: !existsSync(new URL('../shared/game/index.ts', import.meta.url)) }, async () => {
const { gameEngine, stateFromView, evaluateFor, starterPoolCards } = await import('../shared/bots/game-adapter.ts');
const engine = gameEngine as EngineAdapter<any, any>;
const decks = engine.decks(engine.rulesets()[0]);
for (let seed = 1; seed <= 20; seed++) {
const s0 = engine.createMatch(engine.rulesets()[0], [decks[0], decks[1]], seed);
const plays = engine.legalActions(s0, 0).filter((a: any) => a.type === 'play');
if (!plays.length) continue;
const s1 = engine.applyAction(s0, plays[0]);
const s2 = engine.applyAction(s1, { type: 'commit', player: 0 });
const pool = starterPoolCards();
const ev = (s: any) => evaluateFor(stateFromView(engine.observe!(s, 1) as any, mulberry32(9), pool), 1);
assert.equal(ev(s1), ev(s0), `seed ${seed}: queued play changed seat 1's evaluation`);
assert.equal(ev(s2), ev(s0), `seed ${seed}: commit changed seat 1's evaluation`);
}
});