Files
wikideck/tests/game-engine.test.ts
claudeandClaude Opus 5.5 7c86ed35f5 Game engine scaffold: seeded RNG, types, Ruleset schema, core loop
Pure deterministic off-chain engine in shared/game: createMatch, legalActions,
applyAction, isOver, result, viewFor, serialize/deserialize, replay. Placeholder
Snap-style lanes rules until DESIGN.md v1; keyword hook registry for engine-b.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 22:50:45 -05:00

96 lines
4.4 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
DEFAULT_RULESET, IllegalActionError, Rng, applyAction, createMatch, deserialize, isOver, legalActions,
parseRuleset, replay, result, serialize, starterDeck, viewFor, type Action, type GameCard, type MatchState, type PlayerId,
} from '../shared/game/index.ts';
const decks = (seed = 'd'): [GameCard[], GameCard[]] => [starterDeck(`${seed}-a`), starterDeck(`${seed}-b`)];
function randomPlay(seed: string, ruleset = DEFAULT_RULESET): MatchState {
let state = createMatch(ruleset, decks(seed), seed);
const chooser = Rng.fromSeed(`bot:${seed}`);
for (let steps = 0; !isOver(state); steps++) {
assert.ok(steps < 1000, 'match must terminate');
const player = chooser.int(2) as PlayerId;
const actions = legalActions(state, player);
if (!actions.length) continue;
state = applyAction(state, chooser.pick(actions));
}
return state;
}
test('rng is deterministic and seed-sensitive', () => {
const a = Rng.fromSeed('x'), b = Rng.fromSeed('x'), c = Rng.fromSeed('y');
const seq = (r: Rng) => Array.from({ length: 8 }, () => r.nextUint32());
assert.deepEqual(seq(a), seq(b));
assert.notDeepEqual(seq(Rng.fromSeed('x')), seq(c));
const r = Rng.fromSeed('bounds');
for (let i = 0; i < 1000; i++) { const n = r.int(7); assert.ok(n >= 0 && n < 7); }
});
test('default ruleset validates; bad rulesets are rejected', () => {
assert.deepEqual(parseRuleset(DEFAULT_RULESET), DEFAULT_RULESET);
assert.throws(() => parseRuleset({ ...DEFAULT_RULESET, maxTurns: 99 }));
assert.throws(() => parseRuleset({ ...DEFAULT_RULESET, keywords: { nope: {} } }));
assert.throws(() => parseRuleset({ ...DEFAULT_RULESET, extra: 1 }));
});
test('starter decks are legal, deterministic and non-NFT', () => {
const deck = starterDeck('s');
assert.equal(deck.length, 12);
assert.deepEqual(deck, starterDeck('s'));
assert.ok(deck.every(card => card.source === 'starter'));
assert.deepEqual(deck.map(c => c.cost).sort().join(''), '112233445566');
});
test('match setup: hands, energy, turn 1', () => {
const state = createMatch(DEFAULT_RULESET, decks(), 'setup');
assert.equal(state.turn, 1);
for (const p of state.players) { assert.equal(p.hand.length, 4); assert.equal(p.deck.length, 8); assert.equal(p.energy, 1); }
assert.throws(() => createMatch(DEFAULT_RULESET, [decks()[0].slice(0, 5), decks()[1]], 's'), IllegalActionError);
});
test('staging is hidden from the opponent until reveal', () => {
let state = createMatch(DEFAULT_RULESET, decks(), 'hidden');
const play = legalActions(state, 0).find(a => a.type === 'play') as Extract<Action, { type: 'play' }>;
assert.ok(play, 'turn 1 should have a 1-cost play');
state = applyAction(state, play);
assert.equal(viewFor(state, 1).opponent.stagedCount, 1);
assert.equal(viewFor(state, 1).lanes[play.lane].cards[1].length, 0);
assert.equal(JSON.stringify(viewFor(state, 1)).includes(`"${play.cardId}"`), false);
state = applyAction(state, { type: 'endTurn', player: 0 });
state = applyAction(state, { type: 'endTurn', player: 1 });
assert.equal(state.turn, 2);
assert.equal(viewFor(state, 1).lanes[play.lane].cards[1][0].card.id, play.cardId);
assert.throws(() => applyAction(state, { type: 'play', player: 0, cardId: play.cardId, lane: 0 }), IllegalActionError);
});
test('unstage refunds energy', () => {
let state = createMatch(DEFAULT_RULESET, decks(), 'unstage');
const play = legalActions(state, 0).find(a => a.type === 'play') as Extract<Action, { type: 'play' }>;
state = applyAction(state, play);
state = applyAction(state, { type: 'unstage', player: 0, cardId: play.cardId });
assert.equal(state.players[0].energy, 1);
assert.equal(state.players[0].staged.length, 0);
});
test('concede ends the match for the other player', () => {
const state = applyAction(createMatch(DEFAULT_RULESET, decks(), 'c'), { type: 'concede', player: 1 });
assert.equal(isOver(state), true);
assert.equal(result(state)?.winner, 0);
assert.deepEqual(legalActions(state, 0), []);
});
test('random legal play terminates within maxTurns, and replays and serialises exactly', () => {
for (let i = 0; i < 40; i++) {
const seed = `prop-${i}`;
const end = randomPlay(seed);
const r = result(end)!;
assert.ok(r.turns <= DEFAULT_RULESET.maxTurns);
assert.deepEqual(randomPlay(seed), end);
assert.deepEqual(replay(DEFAULT_RULESET, decks(seed), seed, end.actions), end);
assert.deepEqual(deserialize(serialize(end)), end);
}
});