import assert from 'node:assert/strict'; import { test } from 'node:test'; import { loadCards } from '../src/cards.ts'; import { randomDeck } from '../src/deck.ts'; import { advanceTargets, avatarCores, createGame, legalActions, viewFor } from '../src/engine.ts'; import { checkInvariants } from '../src/invariants.ts'; import { makeRng } from '../src/rng.ts'; import { DEFAULT_RULES, withRule } from '../src/rules.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.ram >= 4); assert.deepEqual(pl.avatars.slice(0, 4).map((a) => a.faceUp), [true, false, false, false]); assert.ok(pl.avatars.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('the first player skips the draw; the second player draws', () => { 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); }); test('advancing captures an unowned location in step 5 and pays 4 RAM; Lv 1 unlocks nothing', () => { const t = new Table(); t.ram(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.avatar(0, 1).at, 'A2'); assert.equal(t.s.players[0].ram, 8, '4 RAM + 4 for the capture'); assert.equal(t.avatar(0, 2).faceUp, false, 'A2 is Lv 1 for South'); assert.equal(t.s.step, 'playAgain'); }); test('the second row (Lv 2) unlocks Avatar 2; the middle row (Lv 3) unlocks Avatars 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.avatar(0, 2).faceUp, t.avatar(0, 3).faceUp], unlocked, to); } }); test('rule variants: a turn-1 capture can pay no RAM and unlock nothing', () => { const t = new Table(); t.s.rules = withRule(withRule(t.s.rules, 'firstTurn.captureRam=false'), 'firstTurn.unlock=false'); t.ram(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].ram, 4); assert.equal(t.avatar(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.ram(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.ram(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, s.players[s.firstPlayer].hand.length + 2); 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.ram(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].ram, 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.ram(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].ram, 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(avatarCores(t.s, 0, 1, 'attack'), 4); assert.equal(avatarCores(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.avatar(0, 1).at, 'C1'); assert.equal(t.avatar(1, 1).at, null); assert.deepEqual(t.avatar(1, 1).hardware, []); assert.ok(t.s.players[1].discard.includes(hw)); assert.equal(t.s.stats.attacksWon[0], 1); } else { assert.equal(t.avatar(0, 1).at, null); assert.equal(t.avatar(1, 1).at, 'C1'); assert.deepEqual(t.avatar(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 RAM, 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].ram, 6); assert.ok(t.s.players[0].discard.includes(card)); }); test('views hide the opponent hand, both decks, face-down Avatars 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].avatars[1].card, -1); assert.equal(v.players[1].avatars[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 RAM, so no window'); t.hand(1, 'Execute: Ping Sweep'); t.ram(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); });