// Property tests over random legal play (engine-b). Failures print a seed: rerun with PROP_SEED=. import test from 'node:test'; import assert from 'node:assert/strict'; import { DEFAULT_RULESET, FACTION_KEYWORD, PLAYERS, applyAction, createMatch, deserialize, isOver, legalActions, replay, result, serialize, starterDeck, viewFor, type Action, type Faction, type GameCard, type MatchState, } from '../shared/game/index.ts'; import { forAll, type Gen } from './game/property.ts'; const MAX_ACTIONS = 2000; const FACTIONS = Object.keys(FACTION_KEYWORD) as Faction[]; const STATS = ['attention', 'depth', 'reach'] as const; /** A starter deck with random factions, homes and trend, keywords per ยง10.1, so every keyword and Portal gets exercised. */ function randomDeck(g: Gen, tag: string): GameCard[] { return starterDeck(tag).map(card => { const faction = g.pick(FACTIONS); return { ...card, cardId: `${tag}:${card.cardId}`, faction, home: g.pick(STATS), trend: g.pick([-1, 0, 0, 1] as const), keyword: card.cost === 1 ? null : FACTION_KEYWORD[faction] }; }); } function decksFor(g: Gen, seed: number): [GameCard[], GameCard[]] { return [randomDeck(g, `a-${seed}`), randomDeck(g, `b-${seed}`)]; } /** Random legal play; commits come out at random so queues of every length happen. Concede is rare. */ function playOut(g: Gen, state: MatchState, check?: (s: MatchState) => void) { const log: Action[] = []; for (let steps = 0; !isOver(state); steps++) { assert.ok(steps <= MAX_ACTIONS, `match did not terminate within ${MAX_ACTIONS} actions`); const options = PLAYERS.flatMap(player => legalActions(state, player)); assert.ok(options.length > 0, `no legal actions in a live match (turn ${state.turn})`); const action: Action = g.bool(0.002) ? { type: 'concede', player: g.pick(PLAYERS) } : g.pick(options); log.push(action); state = applyAction(state, action); check?.(state); } return { state, log }; } test('random legal play never crashes, always terminates within the turn cap, and has a result', () => { forAll(200, (g, seed) => { const { state } = playOut(g, createMatch(DEFAULT_RULESET, decksFor(g, seed), seed)); const r = result(state); assert.ok(r, 'finished match has a result'); assert.ok(r.turns <= DEFAULT_RULESET.turns); if (r.reason !== 'concede' && r.reason !== 'afk') { const led = r.topicsLed; if (r.winner === null) assert.equal(led[0], led[1]); else assert.ok(led[r.winner] > led[1 - r.winner] || r.reason === 'totalPower'); } }); }); test('same seed, decks and actions give the same match (replay)', () => { forAll(50, (g, seed) => { const decks = decksFor(g, seed); const first = playOut(g, createMatch(DEFAULT_RULESET, decks, seed)); const again = replay(DEFAULT_RULESET, decks, seed, first.log); assert.deepEqual(again, first.state); }); }); test('createMatch is deterministic per seed and varies across seeds', () => { const decks = (): [GameCard[], GameCard[]] => [starterDeck('x'), starterDeck('y')]; assert.deepEqual(createMatch(DEFAULT_RULESET, decks(), 7), createMatch(DEFAULT_RULESET, decks(), 7)); const distinct = new Set(Array.from({ length: 20 }, (_, i) => JSON.stringify(createMatch(DEFAULT_RULESET, decks(), i)))); assert.ok(distinct.size > 1); }); test('serialize/deserialize round-trips after every action', () => { forAll(30, (g, seed) => { playOut(g, createMatch(DEFAULT_RULESET, decksFor(g, seed), seed), s => assert.deepEqual(deserialize(serialize(s)), s)); }); }); test('applyAction never mutates its input', () => { forAll(30, (g, seed) => { let state = createMatch(DEFAULT_RULESET, decksFor(g, seed), seed); for (let steps = 0; !isOver(state) && steps < MAX_ACTIONS; steps++) { const before = serialize(state); const next = applyAction(state, g.pick(PLAYERS.flatMap(player => legalActions(state, player)))); assert.equal(serialize(state), before); state = next; } }); }); test('every listed legal action is accepted; a finished match lists none', () => { forAll(30, (g, seed) => { const { state } = playOut(g, createMatch(DEFAULT_RULESET, decksFor(g, seed), seed), s => { for (const player of PLAYERS) for (const action of legalActions(s, player)) assert.doesNotThrow(() => applyAction(s, action)); }); for (const player of PLAYERS) assert.deepEqual(legalActions(state, player), []); }); }); test('lane power is never negative and lanes never exceed capacity', () => { forAll(50, (g, seed) => { playOut(g, createMatch(DEFAULT_RULESET, decksFor(g, seed), seed), s => { for (const player of PLAYERS) { const view = viewFor(s, player); for (const lane of view.lanes) { assert.ok(lane.power[0] >= 0 && lane.power[1] >= 0); for (const side of lane.cardPower) for (const p of side) assert.ok(p >= 0); } } for (const lane of s.lanes) for (const side of lane.cards) assert.ok(side.length <= s.ruleset.laneCapacity); }); }); }); test('a view never shows the opponent hand or queue', () => { forAll(30, (g, seed) => { playOut(g, createMatch(DEFAULT_RULESET, decksFor(g, seed), seed), s => { for (const player of PLAYERS) { const text = JSON.stringify(viewFor(s, player)); for (const card of s.players[1 - player].hand) assert.ok(!text.includes(`"${card.cardId}"`), `p${player} sees ${card.cardId}`); } }); }); });