Files
wikideck/tests/game-property.test.ts

101 lines
4.1 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
createMatch, legalActions, applyAction, isOver, result, serialize, deserialize, starterDeck, PLAYERS,
type Action, type GameCard, type MatchState,
} from '../shared/game/index.ts';
import { forAll, type Gen } from './game/property.ts';
import { testRules } from './game/build.ts';
type State = MatchState;
/** Every keyword enabled, so property runs exercise all effects. */
const RULES = testRules({ laneModifiers: { pool: ['plain', 'portal', 'narrow'], revealTurns: [1, 2, 3],
params: { portal: { categoryIndex: 3, amount: 1 }, narrow: { maxCost: 3 } } } });
const KEYWORD_IDS = Object.keys(RULES.keywords);
/** Generous bound: a match must finish well within this many actions. */
const MAX_ACTIONS = 2000;
/** Starter deck with random keywords sprinkled in (shared pageIds so Edition can fire). */
function randomDeck(g: Gen, tag: string): GameCard[] {
return starterDeck(tag, RULES).map(c => ({ ...c, keyword: g.bool(0.6) ? g.pick(KEYWORD_IDS) : null,
category: g.pick(['Space', 'Nature', 'People', 'Science', 'History', 'Culture'] as const) }));
}
function randomMatch(g: Gen, seed: number) {
return createMatch(RULES, [randomDeck(g, `a-${seed}`), randomDeck(g, `b-${seed}`)], `match-${seed}`);
}
/** Plays random legal actions until the match ends; returns the final state and the action log. */
function playOut(g: Gen, state: State, check?: (s: State) => void) {
const log: unknown[] = [];
let steps = 0;
while (!isOver(state)) {
const options = PLAYERS.flatMap(player => legalActions(state, player));
assert.ok(options.length > 0, `no legal actions in a live match at step ${steps}`);
const action = g.pick(options);
log.push(action);
state = applyAction(state, action);
check?.(state);
assert.ok(++steps <= MAX_ACTIONS, `match did not terminate within ${MAX_ACTIONS} actions`);
}
return { state, log };
}
test('random legal play never crashes and always terminates with a result', () => {
forAll(200, (g, seed) => {
const { state } = playOut(g, randomMatch(g, seed));
const outcome = result(state);
assert.ok(outcome, 'finished match has a result');
});
});
test('same seed and same actions give the same result', () => {
forAll(50, (g, seed) => {
const decks: [GameCard[], GameCard[]] = [randomDeck(g, `a-${seed}`), randomDeck(g, `b-${seed}`)];
const first = playOut(g, createMatch(RULES, decks, `m-${seed}`));
// Replay the exact action log on a fresh match from the same seed.
let replay = createMatch(RULES, decks, `m-${seed}`);
for (const action of first.log) replay = applyAction(replay, action as Action);
assert.deepEqual(replay, first.state);
assert.deepEqual(result(replay), result(first.state));
});
});
test('createMatch is deterministic for a seed and varies across seeds', () => {
const a = createMatch(RULES, [starterDeck('x'), starterDeck('y')], 'same');
const b = createMatch(RULES, [starterDeck('x'), starterDeck('y')], 'same');
assert.deepEqual(a, b);
const distinct = new Set(Array.from({ length: 20 }, (_, i) =>
JSON.stringify(createMatch(RULES, [starterDeck('x'), starterDeck('y')], `seed-${i}`))));
assert.ok(distinct.size > 1, 'different seeds should shuffle differently');
});
test('serialize/deserialize round-trips after every action', () => {
forAll(30, (g, seed) => {
playOut(g, randomMatch(g, seed), s => {
assert.deepEqual(deserialize(serialize(s)), s);
});
});
});
test('applyAction never mutates its input state', () => {
forAll(30, (g, seed) => {
let state = randomMatch(g, seed);
let steps = 0;
while (!isOver(state) && steps++ < MAX_ACTIONS) {
const before = JSON.stringify(state);
const action = g.pick(PLAYERS.flatMap(player => legalActions(state, player)));
const next = applyAction(state, action);
assert.equal(JSON.stringify(state), before);
state = next;
}
});
});
test('a finished match offers no legal actions', () => {
forAll(30, (g, seed) => {
const { state } = playOut(g, randomMatch(g, seed));
for (const player of PLAYERS) assert.deepEqual(legalActions(state, player), []);
});
});