forked from WikiDeck/wikideck
51 lines
2.4 KiB
TypeScript
51 lines
2.4 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(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`);
|
|
}
|
|
});
|