import assert from 'node:assert/strict'; import { test } from 'node:test'; import { addBugs } from '../src/cards/util.ts'; import { anomalyCores, canWear, executionActions, executionCost, programCost } from '../src/engine.ts'; import { healthOf, slotsFree } from '../src/query.ts'; import { card } from './card.ts'; import { Table, type Side } from './table.ts'; function scene(o: { south?: Side; north?: Side; first?: 0 | 1 } = {}) { const t = new Table(o); t.own(0, 'B1'); t.own(1, 'C1'); t.place(0, 1, 'B1'); t.place(1, 1, 'C1'); return t; } const attackC1 = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }); const attackerDice = (t: Table) => t.logText().match(/Roll: Broken Bits \[([\d,]*)\]/)![1].split(',').length; card('RAM Expansion', () => { const t = scene({ south: { main: ['RAM Expansion'] } }); t.hardware(0, 1, 'RAM Expansion'); assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 4, 'no Cores'); assert.equal(healthOf(t.s, { player: 0, slot: 1 }), 9 + 2, 'Broken Bits: 9 Health, +2'); }); card('Additional Core', () => { const t = scene({ south: { main: ['Additional Core'] } }); t.hardware(0, 1, 'Additional Core'); assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 5); }); card('Overclocked CPU', () => { const t = scene({ south: { main: ['Overclocked CPU', 'Stack Overflow'] } }); t.hardware(0, 1, 'Overclocked CPU'); const so = t.program(0, 'B1', 'Stack Overflow'); assert.equal(anomalyCores(t.s, 0, 1, 'attack'), 6, '+2 in combat'); attackC1(t); t.rig(6, 6, 6, 6, 6, 6, 1, 1, 1, 1).end(); assert.equal(attackerDice(t), 6); assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'a Program where it fought from is disabled'); t.to('playAgain').end(); assert.ok(t.s.cards[so].disabledUntil > t.s.clock, 'through the next turn'); t.to('playAgain').end(); assert.ok(t.s.cards[so].disabledUntil <= t.s.clock, 'back after 2 turns'); }); card('Solid-State Cache', () => { const t = new Table({ south: { main: ['Solid-State Cache', 'Stack Overflow', 'Hotfix'] } }); t.own(0, 'B1'); t.place(0, 1, 'B1'); t.hardware(0, 1, 'Solid-State Cache'); const [so, hf] = t.hand(0, 'Stack Overflow', 'Hotfix'); assert.equal(programCost(t.s, 0, so, 'B1'), 4); assert.equal(programCost(t.s, 0, so, 'S1'), 7, 'only on the occupied Server'); t.bandwidth(0, 4).to('play').go({ type: 'installProgram', card: so, at: 'B1' }); assert.equal(programCost(t.s, 0, hf, 'B1'), 7, 'first each turn'); }); card('Redundant Power Supply', () => { const t = scene({ south: { main: ['Redundant Power Supply', 'Additional Core'] } }); const rps = t.hardware(0, 1, 'Redundant Power Supply'); const ac = t.hardware(0, 1, 'Additional Core'); attackC1(t); t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end(); assert.equal(t.anomaly(0, 1).at, null); assert.deepEqual(t.anomaly(0, 1).hardware, [rps, ac], 'kept, in its slot'); // The second defeat loses it. t.to('playAgain').end().to('playAgain').end(); t.go({ type: 'place', slot: 1, to: 'B1' }); attackC1(t); t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end(); assert.deepEqual(t.anomaly(0, 1).hardware, []); }); card('Network Interface Upgrade', () => { const t = new Table({ south: { anomalies: ['Beta Version 1.0', 'Broken Bits', 'Code Zero', 'Pixelated Picture'], main: ['Additional Core', 'RAM Expansion', 'Network Interface Upgrade', 'Packet Accelerator'] } }); const beta = { player: 0 as const, slot: 1 }; t.place(0, 1, 'S1'); t.hardware(0, 1, 'Additional Core'); t.hardware(0, 1, 'RAM Expansion'); const [niu, pa] = t.hand(0, 'Network Interface Upgrade', 'Packet Accelerator'); assert.equal(slotsFree(t.s, beta), 0); assert.ok(!canWear(t.s, pa, beta)); assert.ok(canWear(t.s, niu, beta), 'fits with both Expansion Slots full'); t.bandwidth(0, 4).to('play').go({ type: 'installHardware', card: niu, slot: 1 }); assert.equal(slotsFree(t.s, beta), 1, 'and adds an Expansion Slot'); assert.ok(canWear(t.s, pa, beta)); }); card('Dedicated GPU', () => { const t = new Table({ south: { main: ['Dedicated GPU', 'Execute: Ping Sweep'] } }); t.place(0, 1, 'S1'); t.hardware(0, 1, 'Dedicated GPU'); t.hand(0, 'Execute: Ping Sweep'); t.bandwidth(0, 4).play(0, 'Execute: Ping Sweep', null, {}, 'S1').settle(); assert.equal(t.s.players[0].hand.length, 3, 'performed by the Server it stands on: draws 2 + 1'); }); card('ECC Memory', () => { const t = scene({ south: { main: ['ECC Memory', 'Additional Core'] }, north: { main: ['Bloatware', 'Stock Rice'] } }); t.hardware(0, 1, 'ECC Memory'); t.hardware(0, 1, 'Additional Core'); t.program(1, 'C1', 'Bloatware'); t.program(1, 'C1', 'Stock Rice'); attackC1(t); t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1).end(); assert.equal(attackerDice(t), 5, "neither Bug Program touches it"); assert.equal(addBugs(t.s, { player: 0, slot: 1 }, 1, 'a Bug card', 1), false, "and opponents can't give it Bug counters"); assert.equal(t.anomaly(0, 1).bugs, 0); assert.equal(addBugs(t.s, { player: 1, slot: 1 }, 1, 'a Bug card', 0), true, 'Anomalies without it can get them'); }); card('Cooling System', () => { const t = new Table({ south: { main: ['Cooling System', 'Solid-State Cache', 'Stack Overflow', 'Hotfix', 'Compatibility Layer'] } }); t.own(0, 'B1'); t.place(0, 1, 'B1'); t.hardware(0, 1, 'Cooling System'); t.hardware(0, 1, 'Solid-State Cache'); const [so, hf] = t.hand(0, 'Stack Overflow', 'Hotfix'); t.bandwidth(0, 20).to('play'); t.go({ type: 'installProgram', card: so, at: 'B1' }); assert.equal(programCost(t.s, 0, hf, 'B1'), 4, 'Solid-State Cache works twice'); t.go({ type: 'installProgram', card: hf, at: 'B1' }); assert.equal(t.s.players[0].bandwidth, 20 - 4 - 4); }); card('RAID Array', () => { const t = scene({ south: { main: ['RAID Array', 'Additional Core'] } }); t.hardware(0, 1, 'RAID Array'); t.hardware(0, 1, 'Additional Core'); t.bandwidth(0, 5); attackC1(t); t.rig(1, 1, 1, 1, 1, 6, 6, 6, 6).end(); assert.equal(t.s.choice?.kind, 'RAID Array:keep'); t.choose('Keep').settle(); assert.equal(t.anomaly(0, 1).hardware.length, 2); assert.equal(t.s.players[0].bandwidth, 0, 'half of 5 + 5'); }); card('Packet Accelerator', () => { const t = scene({ south: { main: ['Packet Accelerator'] } }); t.hardware(0, 1, 'Packet Accelerator'); assert.equal(anomalyCores(t.s, 0, 1, 'attack'), 5); assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 4); }); card('Firewall Appliance', () => { const t = scene({ south: { main: ['Execute: Sabotage'] }, north: { main: ['Firewall Appliance'] } }); const fa = t.hardware(1, 1, 'Firewall Appliance'); const [sab] = t.hand(0, 'Execute: Sabotage'); t.bandwidth(0, 10); attackC1(t); t.act({ type: 'end' }); assert.equal(executionCost(t.s, 0, sab, 'C1', { card: fa, mode: 'destroy' }), 10, '+3 for the first hostile Execution'); t.play(0, 'Execute: Sabotage', 'C1', { card: fa, mode: 'destroy' }); assert.equal(t.s.players[0].bandwidth, 0); }); card('Hardware Sandbox', () => { const t = new Table({ first: 1, north: { main: ['Hardware Sandbox', 'Stack Overflow', 'Hotfix'] }, south: { main: ['Execute: Kill Process', 'Execute: Root Access'] } }); t.own(1, 'C1'); t.place(1, 1, 'C1'); const so = t.program(1, 'C1', 'Stack Overflow'); const hf = t.program(1, 'C1', 'Hotfix'); const [sandbox] = t.hand(1, 'Hardware Sandbox'); t.bandwidth(1, 7).to('play').act({ type: 'installHardware', card: sandbox, slot: 1 }); t.choose('Stack Overflow').settle(); t.to('playAgain').end(); t.hand(0, 'Execute: Kill Process', 'Execute: Root Access'); t.bandwidth(0, 9); const kills = () => executionActions(t.s, 0, 'now').filter((a) => a.type === 'play' && t.s.cards[a.card].name === 'Execute: Kill Process').map((a) => a.type === 'play' && a.args.card); assert.deepEqual(kills(), [hf], 'Stack Overflow is protected'); t.play(0, 'Execute: Root Access', 'C1').settle(); assert.deepEqual(kills().sort(), [so, hf].sort(), 'Root Access lifts it'); }); card('Diagnostic Module', () => { const t = new Table({ first: 1, south: { main: ['Diagnostic Module', 'Additional Core', 'Cooling System'] } }); t.place(0, 1, 'S1'); t.hardware(0, 1, 'Diagnostic Module'); const [a, b] = t.top(0, 'Additional Core', 'Cooling System'); t.to('playAgain').end(); assert.equal(t.s.choice?.kind, 'Diagnostic Module:bottom'); t.choose('bottom').settle(); assert.deepEqual(t.s.players[0].hand, [b]); assert.equal(t.s.players[0].deck.at(-1), a); }); card('Hot-Swap Bay', () => { const t = new Table({ south: { main: ['Hot-Swap Bay', 'Additional Core', 'Overclocked CPU'] } }); t.place(0, 1, 'S1'); const hsb = t.hardware(0, 1, 'Hot-Swap Bay'); const ac = t.hardware(0, 1, 'Additional Core'); const [oc] = t.hand(0, 'Overclocked CPU'); t.bandwidth(0, 1).use(hsb, { card: ac, cards: [oc] }).settle(); assert.deepEqual(t.anomaly(0, 1).hardware, [hsb, oc]); assert.deepEqual(t.s.players[0].hand, [ac]); assert.equal(t.s.players[0].bandwidth, 0, 'pays the 1 Gb difference'); }); test('Hardware must still fit its Expansion Slots when Network Interface Upgrade leaves', () => { const t = new Table({ south: { anomalies: ['Beta Version 1.0', 'Broken Bits', 'Code Zero', 'Pixelated Picture'], main: ['Network Interface Upgrade', 'Hot-Swap Bay', 'Additional Core', 'Packet Accelerator', 'RAM Expansion', 'Execute: Rollback'], }, }); t.place(0, 1, 'S1'); const niu = t.hardware(0, 1, 'Network Interface Upgrade'); const hsb = t.hardware(0, 1, 'Hot-Swap Bay'); const ac = t.hardware(0, 1, 'Additional Core'); const pa = t.hardware(0, 1, 'Packet Accelerator'); t.hand(0, 'RAM Expansion', 'Execute: Rollback'); t.bandwidth(0, 20); // Beta Version 1.0 has 2 Expansion Slots, +1 from the NIU, and all 3 are in use. const swaps = t.legal().filter((a) => a.type === 'activate' && a.card === hsb); assert.ok(!swaps.some((a) => a.type === 'activate' && a.args.card === niu), "Hot-Swap Bay can't swap the NIU for a card that needs an Expansion Slot"); assert.ok(swaps.some((a) => a.type === 'activate' && a.args.card === ac)); // Rollback returns the NIU: the newest Hardware no longer fits and is discarded. t.s.cards[niu].installedTurn = t.s.turn; t.play(0, 'Execute: Rollback', 'S1', { card: niu }).settle(); assert.deepEqual(t.anomaly(0, 1).hardware, [hsb, ac]); assert.ok(t.s.players[0].discard.includes(pa)); }); card('Quantum Coprocessor', () => { const t = new Table({ south: { main: ['Quantum Coprocessor', 'Execute: Kill Process'] }, north: { main: ['Stack Overflow', 'Hotfix'] } }); t.own(1, 'C1'); const so = t.program(1, 'C1', 'Stack Overflow'); const hf = t.program(1, 'C1', 'Hotfix'); t.place(0, 1, 'S1'); const qc = t.hardware(0, 1, 'Quantum Coprocessor'); t.hand(0, 'Execute: Kill Process'); t.bandwidth(0, 5).play(0, 'Execute: Kill Process', 'C1', { card: so }, 'S1').settle(); assert.equal(t.s.choice?.kind, 'Quantum Coprocessor:copy'); t.choose('Hotfix').settle(); assert.ok(t.s.cards[so].disabledUntil > t.s.clock); assert.ok(t.s.cards[hf].disabledUntil > t.s.clock, 'the copy hit a second Program'); assert.ok(t.s.cards[qc].disabledUntil > t.s.clock, 'then it disables itself'); });