This commit is contained in:
Forrest 2026-10-01 17:20:33 -04:00
parent 5368464042
commit 937f44defe
100 changed files with 10185 additions and 2042 deletions

148
app/test/match.test.ts Normal file
View file

@ -0,0 +1,148 @@
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`);
}
}
});

59
app/test/moves.test.ts Normal file
View file

@ -0,0 +1,59 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { greedyBot } from '../../sim/src/bots/greedy.ts';
import { describeAction } from '../../sim/src/describe.ts';
import { actionKey } from '../../sim/src/engine.ts';
import { loadCards } from '../../sim/src/load.ts';
import { LINE_BY_ID, LOCATIONS } from '../../sim/src/map.ts';
import { makeRng, nextFloat, pick } from '../../sim/src/rng.ts';
import type { GameState, PlayerId } from '../../sim/src/types.ts';
import { Match, randomDecks, rulesFrom } from '../src/match.ts';
import { movesOf, refOfKey, type Key } from '../src/moves.ts';
const pool = loadCards();
/** Whether the screen has something to click for `key`: a hand card, a token or slot, a location, a gateway. */
function onScreen(view: GameState, me: PlayerId, key: Key, asSource: boolean): boolean {
if (key.startsWith('hand:')) return view.players[me].hand.some((id) => view.cards[id].name === key.slice(5));
if (key.startsWith('loc:')) return LOCATIONS.includes(key.slice(4));
// A gateway is drawn when it has a Firewall or is marked as a source.
if (key.startsWith('gate:')) return LINE_BY_ID.get(key.slice(5))?.gateway === true && (asSource || !!view.firewalls[key.slice(5)]);
const r = refOfKey(key);
if (r) {
const av = view.players[r.player].anomalies[r.slot - 1];
// Tokens for Anomalies in play; panel rows for slots 1–4 (and copies in play).
return av.at !== null || r.slot <= 4;
}
return false;
}
function check(m: Match): void {
const view = m.view();
const legal = m.legal();
const moves = movesOf(view, legal, m.human);
const labels = new Set(moves.map((x) => x.label));
for (const a of legal) assert.ok(labels.has(describeAction(view, a)), `no move for ${actionKey(a)}`);
for (const x of moves) {
if (x.source === 'bar') continue;
assert.ok(onScreen(view, m.human, x.source, true), `nothing to click for the source of "${x.label}" (${x.source})`);
if (x.target !== 'none') assert.ok(onScreen(view, m.human, x.target, false), `nothing to click for the target of "${x.label}" (${x.target})`);
}
}
test('every legal move can be reached by clicking: a source on screen, then a target on screen or its menu', () => {
for (let seed = 1; seed <= 60; seed++) {
const rules = seed % 10 === 0 ? ['board=grid4'] : [];
const m = new Match(pool, { seed, human: (seed % 2) as PlayerId, decks: randomDecks(pool, seed, rulesFrom(rules)), rules }, { stops: 'all', autoSkip: false });
const me = greedyBot();
const rng = makeRng(seed);
for (let n = 0; !m.over && n < 5000; n++) {
if (m.botToAct()) {
m.runBot();
continue;
}
check(m);
const legal = m.legal();
m.act(seed % 3 === 0 && nextFloat(rng) < 0.5 ? pick(rng, legal) : me.choose(m.view(), legal, m.human));
}
}
});