forked from WikiDeck/wikideck
mulberry32 uint32 seed with the §4 draw order, Topics with stats and Portals, staggered reveal, initiative and underdog rules, queue/undo/commit/timeout actions, §9 flat RulesConfig with zod bounds, viewFor hidden info, replay. cards.ts left to engine-b; placeholder loaners in starter.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
7.7 KiB
TypeScript
143 lines
7.7 KiB
TypeScript
// Core-loop tests owned by engine-a. Keyword, property and golden tests: engine-b (tests/game-*.test.ts).
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
DEFAULT_RULESET, IllegalActionError, Rng, applyAction, cardPower, createMatch, deserialize, isOver, legalActions,
|
|
loanerCard, parseRuleset, placeCard, replay, result, revealTopic, serialize, starterDeck, STARTER_POOL, topicsLed, viewFor,
|
|
type Action, type GameCard, type MatchState, type PlayerId, type Ruleset,
|
|
} from '../shared/game/index.ts';
|
|
|
|
const decks = (seed = 'd'): [GameCard[], GameCard[]] => [starterDeck(`${seed}-a`), starterDeck(`${seed}-b`)];
|
|
const commitBoth = (s: MatchState) => applyAction(applyAction(s, { type: 'commit', player: 0 }), { type: 'commit', player: 1 });
|
|
const vanilla = (cost: number, overrides: Partial<GameCard> = {}): GameCard =>
|
|
({ ...loanerCard(STARTER_POOL.find(s => s[3] === cost && s[5] === null)!), keyword: null, ...overrides });
|
|
|
|
function randomPlay(seed: number, ruleset: Ruleset = DEFAULT_RULESET): MatchState {
|
|
let state = createMatch(ruleset, decks(String(seed)), seed);
|
|
const chooser = Rng.fromSeed(`bot:${seed}`);
|
|
for (let steps = 0; !isOver(state); steps++) {
|
|
assert.ok(steps < 2000, 'match must terminate');
|
|
const player = chooser.int(2) as PlayerId;
|
|
const actions = legalActions(state, player);
|
|
if (actions.length) state = applyAction(state, chooser.pick(actions));
|
|
}
|
|
return state;
|
|
}
|
|
|
|
test('mulberry32 matches the reference sequence and is seed-deterministic', () => {
|
|
// Reference: mulberry32(1) first output = 0.6270739405881613.
|
|
assert.equal(new Rng(1).next(), 0.6270739405881613);
|
|
const seq = (r: Rng) => Array.from({ length: 8 }, () => r.next());
|
|
assert.deepEqual(seq(Rng.fromSeed(42)), seq(Rng.fromSeed(42)));
|
|
assert.notDeepEqual(seq(Rng.fromSeed(42)), seq(Rng.fromSeed(43)));
|
|
assert.equal(Rng.fromSeed('lobby-1').state, Rng.fromSeed('lobby-1').state);
|
|
});
|
|
|
|
test('DEFAULT_RULESET matches DESIGN §9 and parses; bad rulesets are rejected', () => {
|
|
assert.deepEqual(parseRuleset(DEFAULT_RULESET), DEFAULT_RULESET);
|
|
assert.equal(DEFAULT_RULESET.turns, 6);
|
|
assert.deepEqual(DEFAULT_RULESET.powerByCost, [0, 1, 2, 4, 5, 7, 9]);
|
|
for (const bad of [{ turns: 13 }, { turnSeconds: 60 }, { energyByTurn: [1, 2] }, { revealSchedule: [1, 2] },
|
|
{ keywordParams: { nope: {} } }, { extra: 1 }, { mulligan: true }, { costThresholds: [90, 1, 2, 3, 4] }])
|
|
assert.throws(() => parseRuleset({ ...DEFAULT_RULESET, ...bad }), JSON.stringify(bad));
|
|
});
|
|
|
|
test('setup: one Topic per stat, all face-down except Topic 1 on turn 1, hands of 4, energy 1', () => {
|
|
const state = createMatch(DEFAULT_RULESET, decks(), 7);
|
|
assert.equal(state.turn, 1);
|
|
assert.deepEqual(state.lanes.map(l => l.topic.stat).sort(), ['attention', 'depth', 'reach']);
|
|
assert.deepEqual(state.lanes.map(l => l.revealed), [true, false, false]);
|
|
for (const p of state.players) { assert.equal(p.hand.length, 4); assert.equal(p.deck.length, 8); assert.equal(p.energy, 1); }
|
|
assert.deepEqual(viewFor(state, 0).lanes.map(l => l.topic === null), [false, true, true]);
|
|
});
|
|
|
|
test('decks are validated: size, copies per page, unknown keyword', () => {
|
|
const [a, b] = decks();
|
|
assert.throws(() => createMatch(DEFAULT_RULESET, [a.slice(0, 11), b], 1), IllegalActionError);
|
|
const triple = [...a.slice(0, 9), ...[0, 1, 2].map(n => ({ ...a[0], cardId: `x${n}` }))];
|
|
assert.throws(() => createMatch(DEFAULT_RULESET, [triple, b], 1), /copies/);
|
|
assert.throws(() => createMatch(DEFAULT_RULESET, [[{ ...a[0], keyword: 'nope' }, ...a.slice(1)], b], 1), /unknown keyword/);
|
|
});
|
|
|
|
test('queued plays are hidden from the opponent until reveal; undo refunds', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 11);
|
|
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, 0).energyLeft, 0);
|
|
assert.equal(viewFor(state, 1).opponent.queuedCount, 1);
|
|
assert.equal(JSON.stringify(viewFor(state, 1)).includes(`"${play.cardId}"`), false);
|
|
const undone = applyAction(state, { type: 'undo', player: 0, cardId: play.cardId });
|
|
assert.equal(viewFor(undone, 0).energyLeft, 1);
|
|
state = commitBoth(state);
|
|
assert.equal(state.turn, 2);
|
|
assert.equal(viewFor(state, 1).lanes[play.lane].cards[1][0].card.cardId, play.cardId);
|
|
assert.throws(() => applyAction(state, { type: 'play', player: 0, cardId: play.cardId, lane: 0 }), IllegalActionError);
|
|
});
|
|
|
|
test('energy cap and lane capacity are enforced when queuing', () => {
|
|
const state = createMatch(DEFAULT_RULESET, decks(), 3);
|
|
for (const a of legalActions(state, 0)) if (a.type === 'play') assert.ok(state.players[0].hand.find(c => c.cardId === a.cardId)!.cost <= 1);
|
|
let full = state;
|
|
for (let i = 0; i < 4; i++) full = placeCard(full, 0, 0, vanilla(1, { cardId: `f${i}` }));
|
|
assert.ok(legalActions(full, 0).every(a => a.type !== 'play' || a.lane !== 0));
|
|
});
|
|
|
|
test('lane power: base + on-topic bonus (revealed only) + trend, floored at 0', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 5);
|
|
const lane = 1; // face-down on turn 1
|
|
const stat = state.lanes[lane].topic.stat;
|
|
state = placeCard(state, 0, lane, vanilla(3, { cardId: 'on', home: stat, trend: 1 }));
|
|
const card = () => state.lanes[lane].cards[0][0];
|
|
assert.equal(cardPower(state, card()), 4 + 1);
|
|
state = revealTopic(state, lane);
|
|
assert.equal(cardPower(state, card()), 4 + 2 + 1);
|
|
state = placeCard(state, 1, lane, vanilla(1, { cardId: 'neg', home: stat === 'depth' ? 'reach' : 'depth', trend: -1, power: 0 }));
|
|
assert.equal(cardPower(state, state.lanes[lane].cards[1][0]), 0);
|
|
});
|
|
|
|
test('initiative: leader reveals first next turn; even board alternates', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 9);
|
|
const first = state.initiative;
|
|
state = commitBoth(state); // nobody played: even
|
|
assert.equal(state.initiative, first === 0 ? 1 : 0);
|
|
});
|
|
|
|
test('underdog: +1 energy for a player leading 0 Topics while the opponent leads 2+', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 13);
|
|
state = placeCard(state, 1, 0, vanilla(1, { cardId: 'u1' }));
|
|
state = placeCard(state, 1, 1, vanilla(1, { cardId: 'u2' }));
|
|
state = commitBoth(state);
|
|
assert.deepEqual(topicsLed(state), [0, 2]);
|
|
assert.equal(state.players[0].energy, 3);
|
|
assert.equal(state.players[1].energy, 2);
|
|
});
|
|
|
|
test('timeouts with nothing queued concede after afkTurns; concede ends the match', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 17);
|
|
state = applyAction(applyAction(state, { type: 'timeout', player: 0 }), { type: 'commit', player: 1 });
|
|
assert.equal(isOver(state), false);
|
|
state = applyAction(state, { type: 'timeout', player: 0 });
|
|
assert.equal(result(state)?.winner, 1);
|
|
assert.equal(result(state)?.reason, 'afk');
|
|
const conceded = applyAction(createMatch(DEFAULT_RULESET, decks(), 1), { type: 'concede', player: 1 });
|
|
assert.equal(result(conceded)?.winner, 0);
|
|
assert.deepEqual(legalActions(conceded, 0), []);
|
|
});
|
|
|
|
test('two passing players draw after 6 turns', () => {
|
|
let state = createMatch(DEFAULT_RULESET, decks(), 21);
|
|
while (!isOver(state)) state = commitBoth(state);
|
|
assert.deepEqual({ winner: result(state)!.winner, reason: result(state)!.reason, turns: result(state)!.turns }, { winner: null, reason: 'draw', turns: 6 });
|
|
});
|
|
|
|
test('random legal play terminates, replays exactly, and survives serialisation after every action', () => {
|
|
for (let seed = 0; seed < 30; seed++) {
|
|
const end = randomPlay(seed);
|
|
assert.ok(result(end)!.turns <= DEFAULT_RULESET.turns);
|
|
assert.deepEqual(randomPlay(seed), end);
|
|
assert.deepEqual(replay(DEFAULT_RULESET, decks(String(seed)), seed, end.actions), end);
|
|
assert.deepEqual(deserialize(serialize(end)), end);
|
|
}
|
|
});
|