files-tcg/app/test/match.test.ts
2026-10-01 17:20:33 -04:00

148 lines
6.1 KiB
TypeScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { greedyBot } from '../../sim/src/bots/greedy.ts';
import { openWindow } from '../../sim/src/engine.ts';
import { loadCards } from '../../sim/src/load.ts';
import { makeRng, nextFloat, pick } from '../../sim/src/rng.ts';
import type { Action, PlayerId } from '../../sim/src/types.ts';
import { Match, randomDecks, rulesFrom, type MatchOptions, type Stops } from '../src/match.ts';
const pool = loadCards();
function newMatch(seed: number, options: Partial<MatchOptions> = {}, rules: string[] = []): Match {
const human = (seed % 2) as PlayerId;
return new Match(pool, { seed, human, decks: randomDecks(pool, seed, rulesFrom(rules)), rules }, options);
}
/** A "human" that clicks at random: ends or passes a third of the time, otherwise any move. */
function clicker(seed: number) {
const rng = makeRng(seed);
return (legal: Action[]): Action => {
const stop = legal.find((a) => a.type === 'end' || a.type === 'pass');
const moves = legal.filter((a) => a.type !== 'end' && a.type !== 'pass');
if (moves.length === 0 || (stop && nextFloat(rng) < 0.35)) return stop ?? legal[0];
return pick(rng, moves);
};
}
/**
* Plays a game to the end. After every call into the controller, control must come back: the game
* is over, or it's the bot's move, or you have at least one legal move.
*/
function playOut(m: Match, seed: number, onHuman?: (m: Match) => boolean | void, choose: (legal: Action[]) => Action = clicker(seed)): number {
let decisions = 0;
for (let n = 0; !m.over; n++) {
assert.ok(n < 20_000, `seed ${seed}: the game didn't finish`);
if (m.botToAct()) {
m.runBot();
continue;
}
assert.ok(m.humanToAct(), `seed ${seed}: nobody is deciding at turn ${m.s.turn}`);
const legal = m.legal();
assert.ok(legal.length > 0, `seed ${seed}: no legal moves at turn ${m.s.turn}`);
if (onHuman?.(m)) continue;
decisions++;
m.act(choose(legal));
}
return decisions;
}
test('a random human plays 200 games against the bot to the end, and control always comes back', () => {
const stops: Stops[] = ['combat', 'all', 'never'];
for (let seed = 1; seed <= 200; seed++) {
const m = newMatch(seed, { checkInvariants: true, stops: stops[seed % 3], autoSkip: seed % 4 !== 0 });
playOut(m, seed);
assert.deepEqual(m.errors, [], `seed ${seed}: invariants broke`);
assert.ok(m.s.winner !== null || m.s.endReason === 'turnLimit');
}
});
test('a greedy human plays 40 full-length games through the controller', () => {
const stops: Stops[] = ['combat', 'all', 'never'];
for (let seed = 1; seed <= 40; seed++) {
const m = newMatch(seed, { checkInvariants: true, stops: stops[seed % 3] });
const me = greedyBot();
playOut(m, seed, undefined, (legal) => me.choose(m.view(), legal, m.human));
assert.deepEqual(m.errors, [], `seed ${seed}: invariants broke`);
}
});
test('End command ends your command, stopping only for your prompts and response windows', () => {
let used = 0;
for (let seed = 1; seed <= 40; seed++) {
const m = newMatch(seed, { stops: 'all' });
playOut(m, seed, (g) => {
if (g.s.active !== g.human || g.s.choice || openWindow(g.s) || g.s.turn % 3 !== 0) return false;
const turn = g.s.turn;
g.endCommand();
used++;
if (g.over) return true;
const stopped = g.humanToAct() && (g.s.choice !== null || openWindow(g.s) !== null);
assert.ok(g.s.turn !== turn || stopped || g.botToAct(), `seed ${seed}: End command stopped at step ${g.s.step}`);
// While the bot answers, End command keeps going, and finishes once the bot is done.
while (g.botToAct()) g.runBot();
if (!g.over && g.s.turn === turn && g.humanToAct()) {
assert.ok(g.s.choice !== null || openWindow(g.s) !== null, `seed ${seed}: End command left you in your own step`);
}
return true;
});
}
assert.ok(used > 100);
});
test('a game record replays to the same state', () => {
for (let seed = 1; seed <= 20; seed++) {
const m = newMatch(seed);
let n = 0;
playOut(m, seed, (g) => {
if (++n % 97 !== 0) return false;
const copy = Match.fromRecord(pool, g.record());
assert.equal(JSON.stringify(copy.s), JSON.stringify(g.s), `seed ${seed}: replay differs at move ${g.actions.length}`);
return false;
});
const copy = Match.fromRecord(pool, JSON.parse(JSON.stringify(m.record())));
assert.equal(JSON.stringify(copy.s), JSON.stringify(m.s));
}
});
test('undo goes back to your previous decision, dice included', () => {
for (let seed = 1; seed <= 10; seed++) {
const m = newMatch(seed, { undo: true });
let checked = 0;
playOut(m, seed, (g) => {
if (checked > 30 || g.s.turn % 5 !== 1) return false;
const before = JSON.stringify(g.s);
const actions = g.actions.length;
const legal = g.legal();
const a = legal[legal.length - 1];
g.act(a);
if (g.botToAct()) g.runBot();
const after = JSON.stringify(g.s);
assert.ok(g.undo());
assert.equal(JSON.stringify(g.s), before);
assert.equal(g.actions.length, actions);
// The same move again plays out the same way: the dice are part of the state.
g.act(a);
if (g.botToAct()) g.runBot();
assert.equal(JSON.stringify(g.s), after);
assert.ok(g.undo());
checked++;
return false;
});
assert.ok(checked > 0);
}
});
test('the combat readout sees every combat, even ones that resolve inside one move', () => {
for (let seed = 1; seed <= 20; seed++) {
const m = newMatch(seed, { stops: 'never' });
playOut(m, seed);
const started = m.s.log!.filter((l) => l.tag === 'combat' && l.text.startsWith('Combat at')).length;
assert.equal(m.combats.length, started, `seed ${seed}`);
for (const c of m.combats) {
assert.ok(c.done, `seed ${seed}: combat ${c.id} isn't finished`);
assert.ok(m.s.log![c.logFrom].text.startsWith(`Combat at ${c.loc}`), `seed ${seed}: combat ${c.id} starts at the wrong log line`);
if (c.rolled) assert.equal(c.attacker.dice.length, c.attacker.cores, `seed ${seed}: combat ${c.id} lost its dice`);
}
}
});