files-tcg/sim/test/executions.test.ts
2026-10-01 17:20:33 -04:00

239 lines
10 KiB
TypeScript

import assert from 'node:assert/strict';
import { anomalyCores, execBlocked, executionActions, openWindow } from '../src/engine.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;
}
/** South attacks C1 and the game stops at the first response window in the combat. */
const fight = (t: Table) => t.to('attack').go({ type: 'attack', slot: 1, to: 'C1' }).act({ type: 'end' });
const attackerDice = (t: Table) => t.logText().match(/Roll: Broken Bits \[([\d,]*)\]/)![1].split(',').length;
const defenderDice = (t: Table) => t.logText().match(/Roll: .* vs Broken Bits \[([\d,]*)\]/)![1].split(',').length;
card('Execute: Disconnect', () => {
// A defender slips away before the roll: the attacker walks in, the defender keeps its Hardware.
const t = scene({ north: { main: ['Execute: Disconnect', 'Additional Core'] } });
t.own(1, 'D1');
const hw = t.hardware(1, 1, 'Additional Core');
t.hand(1, 'Execute: Disconnect');
t.bandwidth(1, 4);
fight(t);
assert.equal(t.decider, 1);
t.play(1, 'Execute: Disconnect', 'C1', { ref: { player: 1, slot: 1 }, to: 'D1' }).settle();
assert.equal(t.anomaly(1, 1).at, 'D1');
assert.deepEqual(t.anomaly(1, 1).hardware, [hw]);
assert.equal(t.s.locations.C1.owner, 0, 'the attacker takes the empty location');
});
card('Execute: Disconnect', () => {
// An attacker retreats after a bad roll and isn't defeated.
const t = scene({ south: { main: ['Execute: Disconnect'] } });
t.hand(0, 'Execute: Disconnect');
t.bandwidth(0, 4);
t.rig(1, 1, 1, 1, 6, 6, 6, 6);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'preRoll');
t.act({ type: 'pass' });
assert.equal(openWindow(t.s)?.kind, 'postRoll');
t.play(0, 'Execute: Disconnect', 'C1', { ref: { player: 0, slot: 1 }, to: 'B1' }).settle();
assert.equal(t.anomaly(0, 1).at, 'B1');
assert.equal(t.s.locations.C1.owner, 1);
assert.match(t.logText(), /combat at C1 ends/);
});
card('Execute: Sabotage', () => {
const t = scene({ south: { main: ['Execute: Sabotage'] }, north: { main: ['Additional Core'] } });
const hw = t.hardware(1, 1, 'Additional Core');
t.hand(0, 'Execute: Sabotage');
t.bandwidth(0, 7);
fight(t);
t.rig(6, 6, 6, 6, 1, 1, 1, 1);
t.play(0, 'Execute: Sabotage', 'C1', { card: hw, mode: 'destroy' }).settle();
assert.equal(defenderDice(t), 4);
assert.ok(t.s.players[1].discard.includes(hw));
});
card('Execute: Kill Process', () => {
const t = scene({ south: { main: ['Execute: Kill Process'] }, north: { main: ['Stack Overflow'] } });
const so = t.program(1, 'C1', 'Stack Overflow');
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 5);
t.hand(0, 'Execute: Kill Process');
t.bandwidth(0, 5).play(0, 'Execute: Kill Process', 'C1', { card: so }).settle();
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 4);
t.to('playAgain').end();
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 5, "back on at its controller's next turn");
});
card('Execute: Force Reboot', () => {
const t = scene({ south: { main: ['Execute: Force Reboot', 'Additional Core', 'Dedicated GPU', 'Execute: Ping Sweep'] }, north: { main: ['Bloatware'] } });
t.program(1, 'C1', 'Bloatware');
t.hardware(0, 1, 'Additional Core');
t.hardware(0, 1, 'Dedicated GPU');
t.hand(0, 'Execute: Force Reboot', 'Execute: Ping Sweep');
const fromB1 = () => executionActions(t.s, 0, 'any').some((a) => a.type === 'play' && a.from === 'B1');
assert.ok(fromB1(), 'B1 can perform Executions');
t.bandwidth(0, 5);
fight(t);
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1);
t.play(0, 'Execute: Force Reboot', 'C1', { ref: { player: 0, slot: 1 } }).settle();
assert.equal(attackerDice(t), 5, "Bloatware's penalty is gone");
assert.ok(execBlocked(t.s, 'B1'), 'the Server it stands on is blocked for the turn');
assert.ok(!fromB1());
});
card('Execute: Fork', () => {
const t = scene({ south: { main: ['Execute: Fork', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.hand(0, 'Execute: Fork');
t.bandwidth(0, 7).play(0, 'Execute: Fork', 'B1', { card: so }).settle();
assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 6, 'two Stack Overflow effects, +1 each');
t.to('playAgain').end();
assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 5, 'until end of turn');
});
card('Execute: Ping Sweep', () => {
const t = new Table({ south: { main: ['Execute: Ping Sweep'] } });
t.hand(0, 'Execute: Ping Sweep');
t.bandwidth(0, 4).play(0, 'Execute: Ping Sweep', null).settle();
assert.equal(t.s.players[0].hand.length, 2);
});
card('Execute: Trace Route', () => {
const t = scene({ south: { main: ['Execute: Trace Route'] } });
t.hand(0, 'Execute: Trace Route');
t.rig(6, 6, 6, 6, 6, 1, 1, 1, 1);
t.bandwidth(0, 5).to('play').play(0, 'Execute: Trace Route', 'C1', { ref: { player: 0, slot: 1 } }).settle();
assert.equal(attackerDice(t), 5, 'attacks right away with +1 Core');
assert.equal(t.s.locations.C1.owner, 0);
assert.equal(t.anomaly(0, 1).at, 'C1');
});
card('Execute: Override', () => {
const t = scene({ south: { main: ['Execute: Override'] }, north: { main: ['Stack Overflow'] } });
const so = t.program(1, 'C1', 'Stack Overflow');
t.hand(0, 'Execute: Override');
t.bandwidth(0, 5).play(0, 'Execute: Override', 'C1', { card: so }).settle();
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 4);
t.to('playAgain').end();
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 5, 'until end of turn');
});
card('Execute: Memory Dump', () => {
const t = new Table({ south: { main: ['Execute: Memory Dump', 'Execute: Memory Dump'] } });
t.hand(0, 'Execute: Memory Dump', 'Execute: Memory Dump');
t.bandwidth(0, 8).play(0, 'Execute: Memory Dump', 'C1');
assert.equal(t.s.choice?.kind, 'Execute: Memory Dump:discard', "not South's: draw 2, discard 1");
t.choose('Network Interface Upgrade').settle();
assert.equal(t.s.players[0].hand.length, 1 + 2 - 1);
t.play(0, 'Execute: Memory Dump', 'S1').settle();
assert.equal(t.s.choice, null);
assert.equal(t.s.players[0].hand.length, 1 + 3, "South's own: draw 3, no discard");
});
card('Execute: Rollback', () => {
const t = new Table({ first: 1, north: { main: ['Stack Overflow'] }, south: { main: ['Execute: Rollback'] } });
t.own(1, 'C1');
const [so] = t.hand(1, 'Stack Overflow');
t.hand(0, 'Execute: Rollback');
t.bandwidth(0, 4);
t.bandwidth(1, 7).to('play').act({ type: 'installProgram', card: so, at: 'C1' });
assert.equal(t.decider, 0, 'South may respond to the install');
t.play(0, 'Execute: Rollback', 'C1', { card: so }).settle();
assert.deepEqual(t.s.players[1].hand, [so]);
assert.deepEqual(t.s.locations.C1.programs, []);
});
card('Execute: Root Access', () => {
const t = new Table({ south: { main: ['Execute: Root Access'] } });
t.hand(0, 'Execute: Root Access');
t.bandwidth(0, 4).play(0, 'Execute: Root Access', 'C1').settle();
assert.ok(t.s.locations.C1.rootAccess[0] > t.s.clock);
t.to('playAgain').end();
assert.ok(t.s.locations.C1.rootAccess[0] <= t.s.clock, 'until end of turn');
});
card('Execute: Packet Flood', () => {
const t = new Table({ north: { main: ['Execute: Packet Flood'] } });
t.own(0, 'B1');
t.place(0, 1, 'B1');
t.hand(1, 'Execute: Packet Flood');
t.bandwidth(1, 5);
t.act({ type: 'advance', slot: 1, to: 'C2' });
assert.equal(t.decider, 1);
t.play(1, 'Execute: Packet Flood', 'B1', { to: 'B1' }).settle();
t.to('attack').end();
assert.equal(t.s.locations.C2.owner, null, "the advance fails: it can't move");
assert.equal(t.anomaly(0, 1).at, 'B1');
});
card('Execute: Emergency Shutdown', () => {
const t = scene({ north: { main: ['Execute: Emergency Shutdown'] } });
t.hand(1, 'Execute: Emergency Shutdown');
t.bandwidth(1, 6);
t.rig(6, 6, 6, 6, 1, 1, 1, 1);
fight(t);
assert.equal(openWindow(t.s)?.kind, 'preRoll');
t.act({ type: 'pass' });
assert.equal(openWindow(t.s)?.kind, 'postRoll', 'North waits to see the dice');
t.play(1, 'Execute: Emergency Shutdown', 'C1').settle();
assert.equal(t.s.locations.C1.owner, 1);
assert.equal(t.anomaly(0, 1).at, 'B1');
assert.equal(t.anomaly(1, 1).at, 'C1');
assert.ok(t.anomaly(0, 1).disabledUntil > t.s.clock);
});
card('Execute: Process Clone', () => {
const t = scene({ south: { main: ['Execute: Process Clone', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.hand(0, 'Execute: Process Clone');
t.bandwidth(0, 7).play(0, 'Execute: Process Clone', 'B1', { card: so }).settle();
assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 4 + 2 + 2, 'two copies, each counting 2 Programs');
});
card('Execute: Defragment', () => {
const t = scene({ south: { main: ['Execute: Defragment', 'Stack Overflow'] } });
const so = t.program(0, 'B1', 'Stack Overflow');
t.s.cards[so].disabledUntil = 1e9;
t.hand(0, 'Execute: Defragment');
t.bandwidth(0, 4).play(0, 'Execute: Defragment', 'B1', { card: so }).settle();
assert.equal(t.s.cards[so].disabledUntil, 0);
assert.equal(anomalyCores(t.s, 0, 1, 'defend'), 5);
});
card('Execute: Remote Access', () => {
const t = scene({ first: 1, north: { main: ['Execute: Remote Access', 'Stack Overflow'] } });
t.own(1, 'D1');
const so = t.program(1, 'D1', 'Stack Overflow');
t.hand(1, 'Execute: Remote Access');
t.bandwidth(1, 6).play(1, 'Execute: Remote Access', 'C1', { card: so }).settle();
assert.equal(anomalyCores(t.s, 1, 1, 'defend'), 5, 'Stack Overflow runs on C1 too');
});
card('Execute: Purge', () => {
const t = scene({ south: { main: ['Execute: Purge', 'Additional Core'] } });
const hw = t.hardware(0, 1, 'Additional Core');
t.s.cards[hw].disabledUntil = 1e9;
Object.assign(t.anomaly(0, 1), { infection: 2, permanentInfection: 1 });
t.hand(0, 'Execute: Purge');
t.bandwidth(0, 4).play(0, 'Execute: Purge', 'B1', { to: 'B1' }).settle();
assert.equal(t.anomaly(0, 1).infection + t.anomaly(0, 1).permanentInfection, 0);
assert.equal(t.s.cards[hw].disabledUntil, 0);
});
card('Execute: Hard Reset', () => {
const t = new Table({ south: { main: ['Execute: Hard Reset', 'Stack Overflow', 'Hotfix'] } });
t.own(0, 'B1');
const so = t.program(0, 'B1', 'Stack Overflow');
const hf = t.program(0, 'B1', 'Hotfix');
t.hand(0, 'Execute: Hard Reset');
t.bandwidth(0, 6).play(0, 'Execute: Hard Reset', 'B1', { cards: [so, hf] }).settle();
assert.deepEqual(t.s.locations.B1.programs, []);
assert.deepEqual(t.s.players[0].hand.sort(), [so, hf].sort());
assert.equal(t.s.players[0].bandwidth, 2);
});