import assert from 'node:assert/strict'; import { test } from 'node:test'; import { loadCards } from '../src/load.ts'; import { randomDeck } from '../src/deck.ts'; import { advanceTargets, anomalyCores, createGame, legalActions, viewFor } from '../src/engine.ts'; import { checkInvariants } from '../src/invariants.ts'; import { makeRng } from '../src/rng.ts'; import { DEFAULT_RULES, withRule, type RulesConfig } from '../src/rules.ts'; import { commanderOf, roundOf } from '../src/state.ts'; import { Table } from './table.ts'; const pool = loadCards(); test('setup follows rules.md', () => { const rng = makeRng(5); const decks = [randomDeck(pool, rng, DEFAULT_RULES), randomDeck(pool, rng, DEFAULT_RULES)] as const; const s = createGame({ pool, decks: [decks[0], decks[1]], seed: 3 }); for (const p of [0, 1] as const) { const pl = s.players[p]; assert.ok(pl.hand.length >= 7, 'opening hand'); assert.equal(pl.serverPile.length, 19); assert.ok(pl.bandwidth >= 4); assert.deepEqual(pl.anomalies.slice(0, 4).map((a) => a.faceUp), [true, false, false, false]); assert.ok(pl.anomalies.slice(4).every((a) => !a.faceUp && a.at === null), 'no copies yet'); assert.equal(s.locations[p === 0 ? 'S1' : 'S2'].owner, p); } assert.equal(s.turn, 1); assert.equal(s.active, s.firstPlayer); assert.deepEqual(checkInvariants(s), []); }); test('both players draw on their first turn (since v0.9); firstTurn.draw=false makes the first player skip it', () => { // The test table pins the old skip, so card tests start from a known hand. const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Additional Core'] } }); assert.equal(t.s.players[0].hand.length, 0); t.to('playAgain').end(); assert.equal(t.s.active, 1); assert.equal(t.s.players[1].hand.length, 1); const pool = loadCards(); const hand = (rules: RulesConfig) => { const s = createGame({ pool, decks: [randomDeck(pool, makeRng(1), rules), randomDeck(pool, makeRng(2), rules)], seed: 5, rules }); return s.players[s.firstPlayer].hand.length; }; assert.equal(DEFAULT_RULES.firstTurn.draw, true); assert.equal(hand(withRule(DEFAULT_RULES, 'firstTurn.draw=false')), hand(DEFAULT_RULES) - 1); }); test('rule variants: the second player draws extra in their first draw step only', () => { const t = new Table({ north: { main: ['Additional Core', 'Additional Core', 'Additional Core', 'Additional Core'] } }); t.s.rules = withRule(t.s.rules, 'secondPlayer.firstDraw=1'); t.to('playAgain').end(); assert.equal(t.s.players[1].hand.length, 2); t.to('playAgain').end().to('playAgain').end(); assert.equal(t.s.players[1].hand.length, 3); }); test('rule variants: more cards per draw step', () => { const t = new Table({ north: { main: ['Additional Core', 'Additional Core'] } }); t.s.rules = withRule(t.s.rules, 'drawPerTurn=2'); t.to('playAgain').end(); assert.equal(t.s.players[1].hand.length, 2); }); test('advancing captures an unowned location in step 5 and pays 4 Gb; Lv 1 unlocks nothing', () => { const t = new Table(); t.bandwidth(0, 4); assert.deepEqual(advanceTargets(t.s, 0), ['A2']); t.go({ type: 'place', slot: 1, to: 'S1' }).go({ type: 'advance', slot: 1, to: 'A2' }); t.to('attack'); assert.equal(t.s.locations.A2.owner, null); t.end(); assert.equal(t.s.locations.A2.owner, 0); assert.equal(t.anomaly(0, 1).at, 'A2'); assert.equal(t.s.players[0].bandwidth, 8, '4 Gb + 4 for the capture'); assert.equal(t.anomaly(0, 2).faceUp, false, 'A2 is Lv 1 for South'); assert.equal(t.s.step, 'playAgain'); }); test('the second row (Lv 2) unlocks Anomaly 2; the middle row (Lv 3) unlocks Anomalies 2 and 3', () => { for (const [from, to, unlocked] of [['A2', 'B1', [true, false]], ['B1', 'C2', [true, true]]] as const) { const t = new Table(); t.own(0, from); t.place(0, 1, from); t.go({ type: 'advance', slot: 1, to }).to('attack').end(); assert.equal(t.s.locations[to].owner, 0); assert.deepEqual([t.anomaly(0, 2).faceUp, t.anomaly(0, 3).faceUp], unlocked, to); } }); test('rule variants: a turn-1 capture can pay no Bandwidth and unlock nothing', () => { const t = new Table(); t.s.rules = withRule(withRule(t.s.rules, 'firstTurn.captureBandwidth=false'), 'firstTurn.unlock=false'); t.bandwidth(0, 4).own(0, 'A2'); t.place(0, 1, 'A2'); t.go({ type: 'advance', slot: 1, to: 'B1' }).to('attack').end(); assert.equal(t.s.locations.B1.owner, 0); assert.equal(t.s.players[0].bandwidth, 4); assert.equal(t.anomaly(0, 2).faceUp, false); }); test('rule variants: the first player can be barred from playing cards on turn 1', () => { const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Additional Core'] } }); t.s.rules = withRule(t.s.rules, 'firstTurn.cards=false'); t.hand(0, 'Additional Core'); t.bandwidth(0, 9).place(0, 1, 'S1'); t.to('playAgain'); assert.ok(!t.legal().some((a) => a.type === 'installHardware')); t.end(); t.hand(1, 'Additional Core'); t.bandwidth(1, 9).place(1, 1, 'S2'); t.to('playAgain'); assert.ok(t.legal().some((a) => a.type === 'installHardware'), 'the second player can on turn 2'); }); test('rule variants: the second player can start with extra cards and a location', () => { const rng = makeRng(5); const decks = [randomDeck(pool, rng, DEFAULT_RULES), randomDeck(pool, rng, DEFAULT_RULES)] as const; const rules = withRule(withRule(DEFAULT_RULES, 'secondPlayer.cards=2'), 'secondPlayer.locations=1'); const s = createGame({ pool, decks: [decks[0], decks[1]], seed: 3, rules }); const p2 = s.firstPlayer === 0 ? 1 : 0; assert.equal(s.players[p2].hand.length, rules.openingHand + 2); assert.equal(s.players[s.firstPlayer].hand.length, rules.openingHand + 1, 'the first player drew on turn 1'); assert.equal(s.locations[p2 === 0 ? 'A2' : 'E2'].owner, p2); assert.equal(s.players[p2].serverPile.length, 18); assert.deepEqual(checkInvariants(s), []); }); test('placement closes once an advance is declared', () => { const t = new Table(); t.go({ type: 'place', slot: 1, to: 'S1' }).go({ type: 'advance', slot: 1, to: 'A2' }); assert.ok(!t.legal().some((a) => a.type === 'place')); }); test('a Firewall blocks movement until someone pays to clear it', () => { const t = new Table(); t.own(0, 'A2'); t.s.firewalls['A1-A2'] = 2; t.bandwidth(0, 4).go({ type: 'place', slot: 1, to: 'S1' }); assert.ok(!advanceTargets(t.s, 0).includes('A1')); assert.ok(!t.can({ type: 'advance', slot: 1, to: 'A1' })); t.go({ type: 'clearFirewall', line: 'A1-A2' }); assert.equal(t.s.players[0].bandwidth, 0, 'clearing a Lv 2 Firewall costs 4'); assert.ok(advanceTargets(t.s, 0).includes('A1')); }); test('Firewalls go only on gateways, and upgrades cost the full price of the new level', () => { const t = new Table({ south: { servers: ['Upgraded Hardware'] } }); t.own(0, 'A2', 'Upgraded Hardware'); t.bandwidth(0, 20).to('play'); assert.ok(!t.can({ type: 'firewall', line: 'S1-A2', level: 1 })); t.go({ type: 'firewall', line: 'A1-A2', level: 1 }).go({ type: 'firewall', line: 'A1-A2', level: 2 }); assert.equal(t.s.players[0].bandwidth, 20 - 2 - 4); assert.equal(t.s.firewalls['A1-A2'], 2); }); test('combat: the loser goes back to its slot and loses its Hardware; the winner captures', () => { for (const attackerWins of [true, false]) { const t = new Table({ north: { main: ['Additional Core'] } }); t.own(0, 'B1'); t.own(1, 'C1'); t.place(0, 1, 'B1'); t.place(1, 1, 'C1'); const hw = t.hardware(1, 1, 'Additional Core'); t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }); assert.equal(anomalyCores(t.s, 0, 1, 'attack'), 4); assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 5, '4 Cores + Additional Core'); t.rig(...(attackerWins ? [6, 6, 6, 6, 1, 1, 1, 1, 1] : [1, 1, 1, 1, 6, 6, 6, 6, 6])); t.end(); if (attackerWins) { assert.equal(t.s.locations.C1.owner, 0); assert.equal(t.anomaly(0, 1).at, 'C1'); assert.equal(t.anomaly(1, 1).at, null); assert.deepEqual(t.anomaly(1, 1).hardware, []); assert.ok(t.s.players[1].discard.includes(hw)); assert.equal(t.s.stats.attacksWon[0], 1); } else { assert.equal(t.anomaly(0, 1).at, null); assert.equal(t.anomaly(1, 1).at, 'C1'); assert.deepEqual(t.anomaly(1, 1).hardware, [hw]); } assert.equal(t.s.stats.attacks[0], 1); } }); test('ties reroll every die', () => { const t = new Table(); t.own(0, 'B1'); t.own(1, 'C1'); t.place(0, 1, 'B1'); t.place(1, 1, 'C1'); t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }); t.rig(3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 1, 1, 1, 1); t.end(); assert.equal(t.s.locations.C1.owner, 0); assert.match(t.logText(), /after 1 tie reroll/); }); test('an attacker can only hit a defended location next to where it stands', () => { const t = new Table(); t.own(0, 'B1'); t.own(1, 'C1'); t.own(1, 'C3'); t.place(1, 1, 'C1'); t.place(1, 2, 'C3'); t.place(0, 1, 'B1'); t.to('attack'); assert.deepEqual( t.legal().filter((a) => a.type === 'attack'), [{ type: 'attack', slot: 1, to: 'C1' }], ); }); test('reaching the location target wins immediately', () => { const t = new Table(); for (const loc of ['A1', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3', 'D1', 'D2']) t.own(0, loc); t.place(0, 1, 'S1'); t.go({ type: 'advance', slot: 1, to: 'A2' }).to('attack').end(); assert.equal(t.s.over, true); assert.equal(t.s.winner, 0); assert.equal(t.s.endReason, 'win'); assert.deepEqual(legalActions(t.s), []); }); test('Salvage pays full printed Bandwidth, from hand only', () => { const t = new Table({ south: { main: ['Cooling System'] } }); const [card] = t.hand(0, 'Cooling System'); t.go({ type: 'salvage', card }); assert.equal(t.s.players[0].bandwidth, 6); assert.ok(t.s.players[0].discard.includes(card)); }); test('views hide the opponent hand, both decks, face-down Anomalies and the RNG', () => { const t = new Table({ south: { main: ['Additional Core'] }, north: { main: ['Additional Core'] } }); t.hand(0, 'Additional Core'); t.hand(1, 'Additional Core'); const v = viewFor(t.s, 0); assert.deepEqual(v.players[1].hand, [-1]); assert.deepEqual(v.players[0].hand, t.s.players[0].hand); assert.ok(v.players[0].deck.every((id) => id === -1)); assert.ok(v.players[1].serverPile.every((id) => id === -1)); assert.equal(v.players[0].anomalies[1].card, -1); assert.equal(v.players[1].anomalies[1].card, -1); assert.equal(v.rng.s, 0); assert.notEqual(t.s.rng.s, 0); }); test('the same seed replays the same game', () => { const rng = makeRng(9); const decks = [randomDeck(pool, rng, DEFAULT_RULES), randomDeck(pool, rng, DEFAULT_RULES)] as const; const a = createGame({ pool, decks: [decks[0], decks[1]], seed: 42 }); const b = createGame({ pool, decks: [decks[0], decks[1]], seed: 42 }); assert.deepEqual(a, b); }); test('an opponent with a response gets a window after a declaration; without one it passes itself', () => { const t = new Table({ north: { main: ['Execute: Ping Sweep'] } }); t.own(0, 'A2'); t.place(0, 1, 'S1'); t.place(0, 2, 'A2'); t.act({ type: 'advance', slot: 1, to: 'A1' }); assert.equal(t.decider, 0, 'North has no Bandwidth, so no window'); t.hand(1, 'Execute: Ping Sweep'); t.bandwidth(1, 4); t.act({ type: 'advance', slot: 2, to: 'A3' }); assert.equal(t.decider, 1, 'North may respond'); t.act({ type: 'pass' }); assert.equal(t.decider, 0); }); // --------------------------------------------------------------------------------------------- // Recruit rule (prototype): Anomalies in the deck, bought with Bandwidth /** A table under the recruit rule: Anomaly 1 stays, Anomalies 2–4 move to their owner's hand, priced at 6 Gb. */ function recruitTable(o: ConstructorParameters[0] = {}): Table { const t = new Table(o); t.s.rules = withRule(t.s.rules, 'recruit.on=true'); for (const p of [0, 1] as const) { const avs = t.s.players[p].anomalies; for (let i = 1; i < 4; i++) { const name = t.s.cards[avs[i].card].name; t.s.defs[name] = { ...t.s.defs[name], bandwidth: 6 }; t.s.players[p].hand.push(avs[i].card); avs[i].card = avs[i + 4].card = -1; avs[i].faceUp = false; } } return t; } const recruits = (t: Table) => t.legal().filter((a) => a.type === 'recruit'); test('recruit rule: buy an Anomaly from hand into an empty slot; it enters play on a later turn', () => { const t = recruitTable(); const card = t.s.players[0].hand[0]; t.bandwidth(0, 5).to('play'); assert.equal(recruits(t).length, 0, "can't afford 6 Gb"); t.bandwidth(0, 6); assert.deepEqual(recruits(t).map((a) => a.type === 'recruit' && a.slot), [2, 1, 2, 1, 2, 1], 'first empty slot, or over Anomaly 1'); t.go({ type: 'recruit', card, slot: 2 }); assert.equal(t.s.players[0].bandwidth, 0); assert.equal(t.anomaly(0, 2).card, card); assert.equal(t.anomaly(0, 2).faceUp, true); assert.ok(!t.s.players[0].hand.includes(card)); t.to('playAgain').end().to('playAgain').end(); assert.ok(t.can({ type: 'place', slot: 2, to: 'S1' })); }); test('recruit rule: captures unlock nothing, and a salvaged Anomaly pays its price', () => { const t = recruitTable(); t.own(0, 'B1'); t.place(0, 1, 'B1'); t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end(); assert.equal(t.s.locations.C2.owner, 0); assert.equal(t.anomaly(0, 2).card, -1); const bandwidth = t.s.players[0].bandwidth; t.go({ type: 'salvage', card: t.s.players[0].hand[0] }); assert.equal(t.s.players[0].bandwidth, bandwidth + 6); }); test('recruit rule: copies count toward the 4 Anomalies', () => { const t = recruitTable({ south: { anomalies: ['Carbon NanoBots', 'Broken Bits', 'Pixelated Picture', 'Corrupt Driver'] } }); t.own(0, 'A2'); t.place(0, 1, 'A2'); const [a, b] = t.s.players[0].hand; t.bandwidth(0, 30).to('play'); t.go({ type: 'recruit', card: a, slot: 2 }).go({ type: 'recruit', card: b, slot: 3 }); const replicate = t.legal().find((x) => x.type === 'activate' && x.name === 'Carbon NanoBots' && x.args.to === 'S1'); assert.ok(replicate, '3 Anomalies, so a copy fits'); t.go(replicate).settle(); assert.equal(t.anomaly(0, 5).at, 'S1'); assert.ok(!recruits(t).some((r) => r.type === 'recruit' && r.slot === 4), 'no room for a fourth with the copy out'); }); test('recruit rule: recruiting over an Anomaly retires it and its Hardware', () => { const t = recruitTable({ south: { main: ['Additional Core'] } }); t.own(0, 'A2'); const ref = t.place(0, 1, 'A2'); const old = t.anomaly(0, 1).card; const hw = t.hardware(ref.player, ref.slot, 'Additional Core'); const card = t.s.players[0].hand.find((id) => t.s.defs[t.s.cards[id].name].kind === 'anomaly')!; t.bandwidth(0, 6).to('play').go({ type: 'recruit', card, slot: 1 }); assert.equal(t.anomaly(0, 1).card, card); assert.equal(t.anomaly(0, 1).at, null); assert.deepEqual(t.anomaly(0, 1).hardware, []); assert.ok(t.s.players[0].discard.includes(old) && t.s.players[0].discard.includes(hw)); assert.equal(t.s.locations.A2.owner, 0, 'the location stays yours'); }); test('recruit rule: decks keep one Anomaly outside and shuffle the rest into the main deck', () => { const pool = loadCards(); const rules = withRule(withRule(DEFAULT_RULES, 'recruit.on=true'), 'recruit.anomaliesInDeck=5'); const deck = randomDeck(pool, makeRng(3), rules); assert.equal(deck.anomalies.length, 1); assert.equal(deck.main.length, 45); assert.equal(deck.main.filter((n) => pool.byName.get(n)!.kind === 'anomaly').length, 5); const s = createGame({ pool, decks: [deck, randomDeck(pool, makeRng(4), rules)], seed: 1, rules }); assert.deepEqual(s.players[0].anomalies.slice(0, 4).map((a) => a.card >= 0), [true, false, false, false]); assert.equal(checkInvariants(s).length, 0); }); // --------------------------------------------------------------------------------------------- // Buy-Anomalies rule (prototype): pay Bandwidth to flip the next Anomaly instead of unlocking by level test('buy rule: pay to flip the next face-down Anomaly; it enters play on a later turn', () => { const t = new Table(); t.s.rules = withRule(withRule(t.s.rules, 'buyAnomalies.on=true'), 'buyAnomalies.prices=8,12,16'); t.bandwidth(0, 7).to('play'); assert.ok(!t.can({ type: 'buyAnomaly', slot: 2 }), "can't afford 8 Gb"); t.bandwidth(0, 20).go({ type: 'buyAnomaly', slot: 2 }); assert.equal(t.anomaly(0, 2).faceUp, true); assert.equal(t.s.players[0].bandwidth, 12); assert.ok(t.can({ type: 'buyAnomaly', slot: 3 }), 'Anomaly 3 is next, at 12'); t.go({ type: 'buyAnomaly', slot: 3 }); assert.equal(t.s.players[0].bandwidth, 0); t.to('playAgain').end().to('playAgain').end(); assert.ok(t.can({ type: 'place', slot: 2, to: 'S1' })); }); test('buy rule: captures unlock nothing', () => { const t = new Table(); t.s.rules = withRule(t.s.rules, 'buyAnomalies.on=true'); t.own(0, 'B1'); t.place(0, 1, 'B1'); t.go({ type: 'advance', slot: 1, to: 'C2' }).to('attack').end(); assert.equal(t.s.locations.C2.owner, 0); assert.deepEqual([t.anomaly(0, 2).faceUp, t.anomaly(0, 3).faceUp], [false, false]); }); // --------------------------------------------------------------------------------------------- // Command rule (prototype): alternating commands, 2 per player per round /** Command rule with plain A B A B rounds and an alternating lead, so tests can predict whose command it is. */ function commandTable(): Table { const t = new Table(); for (const sp of ['commands.on=true', 'commands.snake=false', 'commands.leads=FS']) t.s.rules = withRule(t.s.rules, sp); return t; } test('command rule: players alternate commands, and the lead alternates by round', () => { const t = commandTable(); assert.deepEqual([1, 2, 3, 4, 5, 6, 7, 8, 9].map((turn) => commanderOf(t.s, turn)), [0, 1, 0, 1, 1, 0, 1, 0, 0]); assert.deepEqual([1, 4, 5, 8, 9].map((turn) => roundOf(t.s, turn)), [1, 1, 2, 2, 3]); t.to('playAgain').end(); assert.equal(t.s.active, 1); t.to('playAgain').end().to('playAgain').end().to('playAgain').end(); assert.equal(t.s.turn, 5); assert.equal(t.s.active, 1, 'North leads round 2'); }); test('command rule: the order of commands can follow a pattern of round leads, or snake', () => { const order = (...specs: string[]) => { const t = commandTable(); for (const sp of specs) t.s.rules = withRule(t.s.rules, sp); return Array.from({ length: 16 }, (_, i) => 'FS'[commanderOf(t.s, i + 1)]).join(''); }; assert.equal(order(), 'FSFS' + 'SFSF' + 'FSFS' + 'SFSF'); assert.equal(order('commands.leads=F'), 'FSFS'.repeat(4)); assert.equal(order('commands.leads=FFSS'), 'FSFS' + 'FSFS' + 'SFSF' + 'SFSF'); assert.equal(order('commands.leads=thue'), 'FSFS' + 'SFSF' + 'SFSF' + 'FSFS'); assert.equal(order('commands.snake=true', 'commands.leads=F'), 'FSSF'.repeat(4)); assert.equal(order('commands.snake=true'), 'FSSF' + 'SFFS' + 'FSSF' + 'SFFS', 'the v0.8 order'); assert.deepEqual(DEFAULT_RULES.commands, { on: true, perPlayer: 2, leads: 'F', snake: true }, 'the default since v0.9: the first player leads every round, snake order'); }); test('command rule: one advance or attack per command, each Anomaly once a round', () => { const t = commandTable(); t.own(0, 'A2'); t.own(0, 'A1'); t.place(0, 1, 'A2'); t.place(0, 2, 'A1'); t.go({ type: 'advance', slot: 1, to: 'A3' }); assert.ok(!t.legal().some((a) => a.type === 'advance'), 'the command is spent'); t.to('attack'); assert.ok(!t.legal().some((a) => a.type === 'attack')); t.to('playAgain').end().to('playAgain').end(); assert.equal(t.s.turn, 3); const movers = new Set(t.legal().filter((a) => a.type === 'advance').map((a) => a.type === 'advance' && a.slot)); assert.deepEqual([...movers], [2], 'Anomaly 1 already acted this round'); }); test('command rule: an Anomaly unlocked this round enters play next round', () => { const t = commandTable(); t.own(0, 'A2'); t.place(0, 1, 'A2'); t.go({ type: 'advance', slot: 1, to: 'B1' }).to('attack').end(); assert.equal(t.anomaly(0, 2).faceUp, true); t.to('playAgain').end().to('playAgain').end(); assert.equal(t.s.turn, 3); assert.ok(!t.legal().some((a) => a.type === 'place' && a.slot === 2)); t.to('playAgain').end().to('playAgain').end().to('playAgain').end(); assert.equal(t.s.turn, 6); assert.ok(t.legal().some((a) => a.type === 'place' && a.slot === 2)); }); test('command rule: each player draws at their first command of a round only', () => { const t = commandTable(); const hand = () => t.s.players[1].hand.length; t.to('playAgain').end(); assert.equal(hand(), 1, "North's first command of round 1"); t.to('playAgain').end().to('playAgain').end(); assert.equal(hand(), 1, 'no draw on the second command'); t.to('playAgain').end(); assert.equal(t.s.turn, 5); assert.equal(hand(), 2, 'round 2'); });