forked from WikiDeck/wikideck
125 lines
5.6 KiB
TypeScript
125 lines
5.6 KiB
TypeScript
/**
|
||
* Golden test: DESIGN.md §12 worked example (PR #17 @ cf2fceb), encoded exactly.
|
||
* The Topic order, hands and initiative are injected; each player's per-turn
|
||
* commit list is the table's, in the listed queue order. Owner: engine-b.
|
||
*
|
||
* Passes against night/engine/core e0ac157 (frozen §3–§7 rules).
|
||
*/
|
||
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import {
|
||
RULESET_V1_0, applyAction, createMatch, isOver, lanePower, result, starterDeck, topicsLed,
|
||
type Action, type GameCard, type MatchState, type PlayerId, type Ruleset, type Stat, type Faction,
|
||
} from '../shared/game/index.ts';
|
||
|
||
const opts = {};
|
||
|
||
/** §9 defaults the example depends on. */
|
||
const FROZEN = {
|
||
laneCapacity: 3, powerByCost: [0, 1, 2, 4, 6, 8, 10], onTopicBonus: [0, 1, 1, 2, 2, 3, 3],
|
||
vanillaBonus: 1, energyCarryMax: 1, underdog: false, portalReveal: [2, 3, 4],
|
||
};
|
||
|
||
const c = (owner: string, title: string, faction: Faction, cost: number, power: number, home: Stat, keyword: string | null): GameCard =>
|
||
({ cardId: `${owner}:${title}`, pageId: title.length * 1000 + cost, title, faction, rarity: 'Common',
|
||
cost, power, home, trend: 0, keyword, source: 'loaner' });
|
||
|
||
const ANA = [
|
||
c('ana', 'Octopus', 'Nature', 1, 2, 'depth', null),
|
||
c('ana', 'Moon', 'Space', 2, 2, 'reach', 'translated'),
|
||
c('ana', 'Coral reef', 'Nature', 2, 2, 'depth', 'rising'),
|
||
c('ana', 'Albert Einstein', 'People', 4, 6, 'attention', 'local'),
|
||
c('ana', 'Ancient Rome', 'History', 5, 8, 'depth', 'nostalgic'),
|
||
c('ana', 'Library of Alexandria', 'History', 4, 6, 'depth', 'nostalgic'),
|
||
c('ana', 'Periodic table', 'Science', 3, 4, 'depth', 'encyclopedic'),
|
||
];
|
||
const BO = [
|
||
c('bo', 'Haiku', 'Culture', 1, 2, 'attention', null),
|
||
c('bo', 'Mars', 'Space', 2, 2, 'reach', 'translated'),
|
||
c('bo', 'Jazz', 'Culture', 2, 2, 'attention', 'viral'),
|
||
c('bo', 'Pop music', 'Culture', 4, 6, 'attention', 'viral'),
|
||
c('bo', 'Jupiter', 'Space', 5, 8, 'reach', 'translated'),
|
||
c('bo', 'Fern', 'Nature', 3, 4, 'depth', 'rising'),
|
||
c('bo', 'Mona Lisa', 'Culture', 3, 4, 'attention', 'viral'),
|
||
];
|
||
|
||
const T1 = 0, T2 = 1, T3 = 2;
|
||
/** [player, title, lane] in queue order, per turn (§12 table). */
|
||
const PLAN: [PlayerId, string, number][][] = [
|
||
[[0, 'Octopus', T1], [1, 'Haiku', T2]],
|
||
[[0, 'Moon', T3]], // Bo passes to carry 1
|
||
[[0, 'Coral reef', T1], [1, 'Mars', T3], [1, 'Jazz', T2]],
|
||
[[1, 'Pop music', T3], [0, 'Albert Einstein', T2]],
|
||
[[0, 'Ancient Rome', T3], [1, 'Jupiter', T1]],
|
||
[[1, 'Fern', T1], [0, 'Library of Alexandria', T1], [1, 'Mona Lisa', T2], [0, 'Periodic table', T2]],
|
||
];
|
||
|
||
function setup(): MatchState {
|
||
const rules: Ruleset = structuredClone(RULESET_V1_0); // §12 is pinned to the v1.0 ruleset
|
||
const state = createMatch(rules, [starterDeck('golden-a'), starterDeck('golden-b')], 1);
|
||
const pool = state.ruleset.topicPool;
|
||
const find = (id: string) => pool.find(t => t.id === id) ?? assert.fail(`topic ${id} missing from the default pool`);
|
||
const topics = [find('archive'), find('main-page'), find('translations')];
|
||
state.lanes = topics.map(topic => ({ topic, portalRevealed: false, cards: [[], []] }));
|
||
// Inject: every card the example plays starts in hand, nothing left to draw.
|
||
state.players[0].hand = structuredClone(ANA);
|
||
state.players[1].hand = structuredClone(BO);
|
||
for (const p of state.players) p.deck = [];
|
||
state.initiative = 0; // Ana has initiative on odd turns
|
||
return state;
|
||
}
|
||
|
||
const id = (player: PlayerId, title: string) => `${player === 0 ? 'ana' : 'bo'}:${title}`;
|
||
|
||
function playTurn(state: MatchState, plays: [PlayerId, string, number][]): MatchState {
|
||
for (const [player, title, lane] of plays) state = applyAction(state, { type: 'play', player, cardId: id(player, title), lane } as Action);
|
||
state = applyAction(state, { type: 'commit', player: 0 });
|
||
return applyAction(state, { type: 'commit', player: 1 });
|
||
}
|
||
|
||
test('§12 golden: RULESET_V1_0 matches the frozen §9 v1.0 values', opts, () => {
|
||
for (const [key, value] of Object.entries(FROZEN)) assert.deepEqual((RULESET_V1_0 as unknown as Record<string, unknown>)[key], value, key);
|
||
});
|
||
|
||
test('§12 golden: final Topic powers 19–15, 15–19, 13–10; Ana (p0) wins 2–1', opts, () => {
|
||
let state = setup();
|
||
const energyAtStart: [number, number][] = [];
|
||
const snapshots: MatchState[] = [];
|
||
for (const plays of PLAN) {
|
||
energyAtStart.push([state.players[0].energy, state.players[1].energy]);
|
||
state = playTurn(state, plays);
|
||
snapshots.push(state);
|
||
}
|
||
assert.ok(isOver(state));
|
||
const r = result(state)!;
|
||
assert.deepEqual(r.lanePower, [[19, 15], [15, 19], [13, 10]]);
|
||
assert.equal(r.winner, 0);
|
||
assert.deepEqual(r.topicsLed, [2, 1]);
|
||
|
||
// Carry-over: Bo 4 at the start of turn 3; Ana 5, 6, 7 at the start of turns 4, 5, 6.
|
||
assert.equal(energyAtStart[2][1], 4);
|
||
assert.deepEqual(energyAtStart.slice(3).map(e => e[0]), [5, 6, 7]);
|
||
|
||
// After turn 5: T2 is 9–9 and Bo leads it by consensus (2 on-topic cards to 1).
|
||
const afterFive = snapshots[4];
|
||
assert.deepEqual(lanePower(afterFive, T2), [9, 9]);
|
||
assert.deepEqual(lanePower(afterFive, T1), [9, 8]);
|
||
assert.deepEqual(lanePower(afterFive, T3), [13, 10]);
|
||
assert.deepEqual(topicsLed(afterFive), [2, 1]);
|
||
});
|
||
|
||
test('§12 golden: per-turn Topic powers match the table', opts, () => {
|
||
const expected = [
|
||
[[3, 0], [0, 3], [0, 0]],
|
||
[[4, 0], [0, 3], [3, 0]],
|
||
[[8, 0], [0, 8], [3, 3]],
|
||
[[9, 0], [9, 8], [3, 9]],
|
||
[[9, 8], [9, 9], [13, 10]],
|
||
];
|
||
let state = setup();
|
||
PLAN.slice(0, 5).forEach((plays, turn) => {
|
||
state = playTurn(state, plays);
|
||
assert.deepEqual(state.lanes.map((_, lane) => lanePower(state, lane)), expected[turn], `after turn ${turn + 1}`);
|
||
});
|
||
});
|