import assert from 'node:assert/strict' import test from 'node:test' import { Game, GameError } from '../src/game/game.js' import { compileText } from '../src/effects/compile.js' import type { Ability, Effect, TokenSpec } from '../src/effects/types.js' import type { CardDefinition, GameObject } from '../src/game/types.js' import { card, creature, spell, land, exoticOrchard, setup, tokenMaker, tokenDoubler, makeToken, addMana, roleMaker, shockLand } from './helpers.js' test('temporary pump effects expire at end of turn', () => { const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 3 }, () => card('Filler')) }]) game.start() const bear = game.addCard('p1', creature('Grizzly Bears', 2, 2), 'battlefield') const growth = game.addCard('p1', spell('Giant Growth', [{ op: 'pump', target: { kind: 'object', selector: { types: ['creature'] }, mode: 'target' }, power: { kind: 'number', value: 3 }, toughness: { kind: 'number', value: 3 }, duration: 'until_end_of_turn', }]), 'hand') game.castSpell('p1', growth.id, { targets: [{ kind: 'object', id: bear.id }] }) game.resolveStack() assert.equal(game.getPower(bear), 5) assert.equal(game.getToughness(bear), 5) game.endTurn('p1') assert.equal(game.getPower(bear), 2) }) test('the engine exposes legal creature targets for a card', () => { const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 3 }, () => card('Filler')) }]) game.start() const bear = game.addCard('p2', creature('Grizzly Bears', 2, 2), 'battlefield') const growth = game.addCard('p1', spell('Giant Growth', [{ op: 'pump', target: { kind: 'object', selector: { types: ['creature'] }, mode: 'target' }, power: { kind: 'number', value: 3 }, toughness: { kind: 'number', value: 3 }, duration: 'until_end_of_turn', }]), 'hand') assert.deepEqual(game.legalTargetsForCard('p1', growth.id), [{ kind: 'object', id: bear.id }]) }) test('excluded colors filter legal targets', () => { const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 3 }, () => card('Filler')) }]) game.start() game.addCard('p2', card('Black Bear', { typeLine: 'Creature — Bear', colors: ['B'], power: 2, toughness: 2 }), 'battlefield') const green = game.addCard('p2', card('Green Bear', { typeLine: 'Creature — Bear', colors: ['G'], power: 2, toughness: 2 }), 'battlefield') const doomBlade = game.addCard('p1', spell('Doom Blade', [{ op: 'destroy', target: { kind: 'object', selector: { types: ['creature'], excludeColors: ['B'] }, mode: 'target' }, }]), 'hand') assert.deepEqual(game.legalTargetsForCard('p1', doomBlade.id), [{ kind: 'object', id: green.id }]) }) test('the engine exposes legal targets for an activated ability', () => { const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 3 }, () => card('Filler')) }]) game.start() const bear = game.addCard('p2', creature('Grizzly Bears', 2, 2), 'battlefield') const pinger = game.addCard('p1', card('Pinger', { typeLine: 'Creature — Test', power: 1, toughness: 1, abilities: [{ kind: 'activated', costs: [], optional: false, text: '{T}: This creature deals 1 damage to target creature an opponent controls.', effects: [{ op: 'damage', target: { kind: 'object', selector: { controller: 'opponent', types: ['creature'] }, mode: 'target' }, amount: { kind: 'number', value: 1 }, }], }], }), 'battlefield') assert.deepEqual(game.legalTargetsForAbility('p1', pinger.id, 0), [{ kind: 'object', id: bear.id }]) }) test('"until the end of your next turn" lasts through the controller next turn', () => { const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 3 }, () => card('Filler')) }]) game.start() const bear = game.addCard('p1', creature('Grizzly Bears', 2, 2), 'battlefield') const growth = game.addCard('p1', spell('Growth', [{ op: 'pump', target: { kind: 'object', selector: { types: ['creature'] }, mode: 'target' }, power: { kind: 'number', value: 3 }, toughness: { kind: 'number', value: 3 }, duration: 'until_end_of_your_next_turn', }]), 'hand') game.castSpell('p1', growth.id, { targets: [{ kind: 'object', id: bear.id }] }) game.resolveStack() assert.equal(game.getPower(bear), 5) game.endTurn('p1') assert.equal(game.getPower(bear), 5) game.endTurn('p2') assert.equal(game.getPower(bear), 5) game.endTurn('p1') assert.equal(game.getPower(bear), 2) }) test('counter replacement doubles counters put on matching creatures', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Hardened Scales Test', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'counter_multiplier', target: { kind: 'object', selector: { types: ['creature'], controller: 'you' }, mode: 'each' }, counter: '+1/+1', multiplier: 2, }], text: 'If one or more +1/+1 counters would be put on a creature you control, twice that many are put on it instead.', }], }), 'battlefield') game.start() const bear = game.addCard('p1', creature('Counter Bear', 2, 2), 'battlefield') const growth = game.addCard('p1', spell('Growth', [{ op: 'put_counters', target: { kind: 'object', selector: { types: ['creature'] } }, counter: '+1/+1', amount: { kind: 'number', value: 2 }, }]), 'hand') game.castSpell('p1', growth.id, { targets: [{ kind: 'object', id: bear.id }] }) game.resolveStack() assert.equal(bear.counters['+1/+1'], 4) }) test('energy replacement doubles energy gained', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Energy Doubler', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'replacement_amount', what: 'energy', multiplier: 2 }], text: 'If you would get one or more {E}, you get twice that many {E} instead.', }], }), 'battlefield') game.start() const spark = game.addCard('p1', spell('Spark', [ { op: 'energy', player: 'you', amount: { kind: 'number', value: 2 } }, ]), 'hand') game.castSpell('p1', spark.id) game.resolveStack() assert.equal(game.getPlayer('p1').energy, 4) }) test('a static source-filtered prevention stops only matching damage', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) const protectedCreature = game.addCard('p1', card('Ward Golem', { typeLine: 'Artifact Creature — Golem', power: 2, toughness: 5, abilities: [{ kind: 'static', effects: [{ op: 'prevent_damage', target: { kind: 'self' }, amount: 'all', duration: 'permanent', sourceSelector: { types: ['creature'] }, }], text: 'Prevent all damage that would be dealt to this by creatures.', }], }), 'battlefield') const plain = game.addCard('p1', creature('Plain Bear', 2, 5), 'battlefield') const pinger = game.addCard('p2', card('Pinger', { typeLine: 'Creature — Test', power: 1, toughness: 1, abilities: [{ kind: 'activated', costs: [], effects: [{ op: 'damage', target: { kind: 'object', selector: { types: ['creature'] }, mode: 'target' }, amount: { kind: 'number', value: 3 }, }], optional: false, text: '{0}: Deal 3 damage to target creature.', }], }), 'battlefield') game.start() while (game.priority !== 'p2') game.pass(game.priority) game.activateAbility('p2', pinger.id, 0, { targets: [{ kind: 'object', id: protectedCreature.id }] }) game.resolveStack() assert.equal(protectedCreature.damage, 0) while (game.priority !== 'p2') game.pass(game.priority) game.activateAbility('p2', pinger.id, 0, { targets: [{ kind: 'object', id: plain.id }] }) game.resolveStack() assert.equal(plain.damage, 3) }) test('a source-scoped shield only stops the chosen source', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) const pinger = (name: string) => card(name, { typeLine: 'Creature — Test', power: 1, toughness: 1, abilities: [{ kind: 'activated', costs: [], effects: [{ op: 'damage', target: { kind: 'player', who: 'opponent', mode: 'target' }, amount: { kind: 'number', value: 3 }, }], optional: false, text: '{0}: Deal 3 damage to target opponent.', }], }) const chosen = game.addCard('p2', pinger('Chosen Pinger'), 'battlefield') const other = game.addCard('p2', pinger('Other Pinger'), 'battlefield') game.start() const shield = spell('Source Shield', [{ op: 'prevent_damage', target: { kind: 'any' }, amount: 'all', duration: 'until_end_of_turn', global: true, source: { kind: 'object', selector: {}, mode: 'target' }, }]) const shieldCard = game.addCard('p1', shield, 'hand') game.castSpell('p1', shieldCard.id, { targets: [{ kind: 'object', id: chosen.id }] }) game.resolveStack() while (game.priority !== 'p2') game.pass(game.priority) game.activateAbility('p2', chosen.id, 0, { targets: [{ kind: 'player', id: 'p1' }] }) game.resolveStack() assert.equal(game.getPlayer('p1').life, 40) while (game.priority !== 'p2') game.pass(game.priority) game.activateAbility('p2', other.id, 0, { targets: [{ kind: 'player', id: 'p1' }] }) game.resolveStack() assert.equal(game.getPlayer('p1').life, 37) }) test('a prevention follow-up runs with the prevented amount', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const target = game.addCard('p1', creature('Shielded Bear', 2, 5), 'battlefield') const shield = spell('Shield', [{ op: 'prevent_damage', target: { kind: 'object', selector: { types: ['creature'] } }, amount: { kind: 'number', value: 3 }, duration: 'until_end_of_turn', then: [{ op: 'damage', target: { kind: 'player', who: 'opponent', mode: 'target' }, amount: { kind: 'event_amount' }, }], }]) const bolt = spell('Bolt', [{ op: 'damage', target: { kind: 'object', selector: { types: ['creature'] } }, amount: { kind: 'number', value: 5 }, }]) const shieldCard = game.addCard('p1', shield, 'hand') game.castSpell('p1', shieldCard.id, { targets: [{ kind: 'object', id: target.id }, { kind: 'player', id: 'p2' }], }) game.resolveStack() const boltCard = game.addCard('p1', bolt, 'hand') game.castSpell('p1', boltCard.id, { targets: [{ kind: 'object', id: target.id }] }) game.resolveStack() assert.equal(target.damage, 2) assert.equal(game.getPlayer('p2').life, 37) }) test('color-specific damage replacement only boosts matching sources', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Red Enhancer', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'replacement_amount', what: 'damage', multiplier: 1, add: 1, sourceColor: 'R' }], text: 'If a red source would deal damage, it deals that much damage plus 1 instead.', }], }), 'battlefield') game.start() const redBolt = game.addCard('p1', card('Red Bolt', { typeLine: 'Instant', colors: ['R'], abilities: [{ kind: 'spell', effects: [{ op: 'damage', target: { kind: 'player', who: 'opponent', mode: 'target' }, amount: { kind: 'number', value: 3 } }], optional: false, text: 'Deal 3 damage.', }], }), 'hand') game.castSpell('p1', redBolt.id, { targets: [{ kind: 'player', id: 'p2' }] }) game.resolveStack() assert.equal(game.getPlayer('p2').life, 36) }) test('life-gain replacement doubles the life gained', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Life Doubler', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'replacement_amount', what: 'life_gain', multiplier: 2 }], text: 'If you would gain life, you gain twice that much life instead.', }], }), 'battlefield') game.start() const heal = game.addCard('p1', spell('Heal', [ { op: 'life', player: 'you', amount: { kind: 'number', value: 3 }, mode: 'gain' }, ]), 'hand') game.castSpell('p1', heal.id) game.resolveStack() assert.equal(game.getPlayer('p1').life, 46) }) test('damage replacement doubles damage from sources you control', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Furnace', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'replacement_amount', what: 'damage', multiplier: 2 }], text: 'If a source you control would deal damage, it deals double that damage instead.', }], }), 'battlefield') game.start() const bolt = game.addCard('p1', spell('Bolt', [ { op: 'damage', target: { kind: 'player', who: 'opponent', mode: 'target' }, amount: { kind: 'number', value: 3 } }, ]), 'hand') game.castSpell('p1', bolt.id, { targets: [{ kind: 'player', id: 'p2' }] }) game.resolveStack() assert.equal(game.getPlayer('p2').life, 34) }) test('draw replacement doubles each draw', () => { const deck = [card('Filler One'), card('Filler Two'), card('Filler Three'), card('Filler Four')] const game = setup([{ id: 'p1', deck }, { id: 'p2', deck }]) game.addCard('p1', card('Draw Doubler', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'replacement_amount', what: 'draw', multiplier: 2 }], text: 'If you would draw a card, draw two cards instead.', }], }), 'battlefield') game.start() assert.equal(game.getPlayer('p1').hand.length, 2) assert.equal(game.getPlayer('p2').hand.length, 0) }) test('"skip your draw step" stops its controller drawing', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.addCard('p1', card('Necro Test', { typeLine: 'Enchantment', abilities: [{ kind: 'static', effects: [{ op: 'restriction', target: { kind: 'player', who: 'you', mode: 'target' }, rule: 'skip_draw_step' }], text: 'Skip your draw step.', }], }), 'battlefield') game.start() assert.equal(game.getPlayer('p1').hand.length, 0) assert.equal(game.getPlayer('p1').library.length, 1) game.endTurn('p1') game.endTurn('p2') assert.equal(game.activePlayer, 'p1') assert.equal(game.getPlayer('p1').hand.length, 0) assert.equal(game.getPlayer('p1').library.length, 1) }) test('an unblockable attacker rejects declared blockers', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) const attacker = game.addCard('p1', card('Slippery Otter', { typeLine: 'Creature — Otter', power: 2, toughness: 2, abilities: [{ kind: 'static', effects: [{ op: 'restriction', target: { kind: 'self' }, rule: 'cant_be_blocked' }], text: 'This creature cannot be blocked.', }], }), 'battlefield') const blocker = game.addCard('p2', creature('Blocker', 2, 2), 'battlefield') game.start() game.declareAttackers('p1', [{ attackerId: attacker.id, defenderId: 'p2' }]) assert.throws( () => game.declareBlockers('p2', [{ blockerId: blocker.id, attackerId: attacker.id }]), /cannot be blocked/, ) }) test('a "can\'t be countered" spell survives a counterspell', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const uncounterable = card('Uncounterable', { typeLine: 'Instant', abilities: [ { kind: 'spell', effects: [{ op: 'life', player: 'you', amount: { kind: 'number', value: 3 }, mode: 'gain' }], optional: false, text: 'Gain 3 life.' }, { kind: 'static', effects: [{ op: 'restriction', target: { kind: 'self' }, rule: 'cant_be_countered' }], text: 'This spell cannot be countered.' }, ], }) const counterspell = spell('Counterspell', [{ op: 'counter', what: 'spell', target: { kind: 'spell', selector: {}, mode: 'target' } }]) const target = game.addCard('p1', uncounterable, 'hand') const counterCard = game.addCard('p2', counterspell, 'hand') game.castSpell('p1', target.id) game.castSpell('p2', counterCard.id, { targets: [{ kind: 'spell', id: game.stack[game.stack.length - 1].id }] }) game.resolveStack() assert.equal(game.getPlayer('p1').life, 43) assert.equal(target.zone, 'graveyard') }) test('combat damage kills creatures and moves them to the graveyard', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) const attacker = game.addCard('p1', creature('Attacker', 2, 2), 'battlefield') const blocker = game.addCard('p2', creature('Blocker', 2, 2), 'battlefield') game.start() game.declareAttackers('p1', [{ attackerId: attacker.id, defenderId: 'p2' }]) assert.equal(game.step, 'declare_blockers') assert.equal(game.priority, 'p2') assert.equal(attacker.tapped, true) game.declareBlockers('p2', [{ blockerId: blocker.id, attackerId: attacker.id }]) game.resolveCombatDamage() assert.equal(game.getPlayer('p2').life, 40) assert.equal(game.step, 'end_combat') assert.ok(game.getPlayer('p1').graveyard.includes(attacker.id)) assert.ok(game.getPlayer('p2').graveyard.includes(blocker.id)) }) test('unblocked attackers deal damage to the defending player', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) const attacker = game.addCard('p1', creature('Attacker', 3, 3), 'battlefield') game.start() game.declareAttackers('p1', [{ attackerId: attacker.id, defenderId: 'p2' }]) game.declareBlockers('p2', []) game.resolveCombatDamage() assert.equal(game.getPlayer('p2').life, 37) }) test('counterspell counters a spell on the stack', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() game.addCard('p1', land('Mountain', 'R'), 'battlefield') game.addCard('p2', land('Island', 'U'), 'battlefield') game.addCard('p2', land('Island', 'U'), 'battlefield') const bolt = game.addCard('p1', spell('Lightning Bolt', [ { op: 'damage', target: { kind: 'any' }, amount: { kind: 'number', value: 3 } }, ], '{R}'), 'hand') const counterspell = game.addCard('p2', spell('Counterspell', [ { op: 'counter', what: 'spell', target: { kind: 'spell', selector: { types: [] }, mode: 'target' } }, ], '{U}{U}'), 'hand') game.castSpell('p1', bolt.id, { targets: [{ kind: 'player', id: 'p2' }] }) game.castSpell('p2', counterspell.id) assert.equal(game.stack.length, 2) game.resolveStack() assert.equal(game.getPlayer('p2').life, 40) assert.equal(game.stack.length, 0) assert.ok(game.getPlayer('p1').graveyard.includes(bolt.id)) assert.ok(game.getPlayer('p2').graveyard.includes(counterspell.id)) }) test('activated abilities create tokens that enter the battlefield', () => { const maker = card('Goblin Maker', { typeLine: 'Artifact', power: undefined, toughness: undefined, abilities: [{ kind: 'activated', costs: [{ kind: 'tap' }], effects: [{ op: 'create_token', player: 'you', amount: { kind: 'number', value: 1 }, token: { name: 'Goblin', types: ['creature'], subtypes: ['goblin'], colors: ['R'], power: { kind: 'number', value: 1 }, toughness: { kind: 'number', value: 1 } }, }], optional: false, text: '{T}: Create a 1/1 Goblin.', }], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const source = game.addCard('p1', maker, 'battlefield') game.activateAbility('p1', source.id, 0) assert.equal(game.stack.length, 1) game.resolveStack() const tokens = game.getPlayer('p1').battlefield.map(id => game.getObject(id)).filter(object => object.isToken) assert.equal(tokens.length, 1) assert.equal(tokens[0].definition.name, 'Goblin') assert.equal(game.getPower(tokens[0]), 1) })