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('impulse exiles the top cards and lets the player cast them this turn', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const impulseSpell = game.addCard('p1', spell('Impulse Test', [ { op: 'impulse', player: 'you', amount: { kind: 'number', value: 2 } }, ]), 'hand') const top = game.addCard('p1', spell('Top Card', []), 'library') const second = game.addCard('p1', spell('Second Card', []), 'library') game.castSpell('p1', impulseSpell.id) game.resolveStack() assert.equal(top.zone, 'exile') assert.equal(second.zone, 'exile') assert.equal(top.playableFromExileTurn, game.turn) assert.equal(second.playableFromExileTurn, game.turn) assert.doesNotThrow(() => game.castSpell('p1', top.id)) }) test('hideaway exiles a card and its linked ability plays it for free once the condition is met', () => { const hideawayLand = card('Hideaway Test', { typeLine: 'Land', abilities: [ { kind: 'triggered', event: { kind: 'enters', subject: { kind: 'self' } }, effects: [{ op: 'hideaway', amount: { kind: 'number', value: 4 } }], optional: false, text: 'Hideaway 4', }, { kind: 'activated', costs: [], effects: [{ op: 'conditional', condition: { kind: 'total_power', controller: 'you', atLeast: 10 }, effects: [{ op: 'play_exiled', player: 'you', free: true }], }], optional: false, text: 'Play the exiled card without paying its mana cost if creatures you control have total power 10 or greater.', }, ], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const hidden = game.addCard('p1', creature('Hidden Bear', 2, 2), 'library') for (const name of ['Filler One', 'Filler Two', 'Filler Three']) { game.addCard('p1', creature(name, 1, 1), 'library') } const library = game.getPlayer('p1').library library.splice(library.indexOf(hidden.id), 1) library.unshift(hidden.id) const land = game.addCard('p1', hideawayLand, 'hand') game.playLand('p1', land.id) game.resolveStack() assert.equal(game.pendingChoice?.what, 'hideaway') assert.equal(game.pendingChoice?.playerId, 'p1') assert.equal(game.pendingChoice?.cardIds?.length, 4) assert.ok(game.pendingChoice?.cardIds?.includes(hidden.id)) assert.throws(() => game.chooseHideawayCard('p2', hidden.id), GameError) const looked = [...game.getPlayer('p1').library.slice(0, 4)] game.chooseHideawayCard('p1', hidden.id) assert.equal(game.pendingChoice, undefined) assert.equal(hidden.zone, 'exile') assert.equal(hidden.faceDown, true) assert.deepEqual(land.linkedExile, [hidden.id]) assert.deepEqual( game.getPlayer('p1').library.slice(-3).sort(), looked.filter(id => id !== hidden.id).sort(), ) game.activateAbility('p1', land.id, 1) game.resolveStack() assert.equal(hidden.zone, 'exile') game.addCard('p1', creature('Big Bear', 10, 10), 'battlefield') game.activateAbility('p1', land.id, 1) game.resolveStack() assert.equal(hidden.zone, 'battlefield') assert.equal(hidden.faceDown, undefined) assert.ok(game.getPlayer('p1').battlefield.includes(hidden.id)) }) test('playing a card hidden by hideaway uses the land drop', () => { const hideawayLand = card('Hideaway Land', { typeLine: 'Land', abilities: [ { kind: 'triggered', event: { kind: 'enters', subject: { kind: 'self' } }, effects: [{ op: 'hideaway', amount: { kind: 'number', value: 1 } }], optional: false, text: 'Hideaway 1', }, { kind: 'activated', costs: [], effects: [{ op: 'conditional', condition: { kind: 'total_power', controller: 'you', atLeast: 0 }, effects: [{ op: 'play_exiled', player: 'you', free: true }], }], optional: false, text: 'Play the exiled card without paying its mana cost.', }, ], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const hidden = game.addCard('p1', land('Island', 'U'), 'library') const library = game.getPlayer('p1').library library.splice(library.indexOf(hidden.id), 1) library.unshift(hidden.id) const source = game.addCard('p1', hideawayLand, 'hand') game.playLand('p1', source.id) game.resolveStack() assert.equal(hidden.zone, 'exile') assert.equal(hidden.faceDown, true) game.getPlayer('p1').landPlayedThisTurn = true game.activateAbility('p1', source.id, 1) game.resolveStack() assert.equal(hidden.zone, 'exile') game.getPlayer('p1').landPlayedThisTurn = false game.activateAbility('p1', source.id, 1) game.resolveStack() assert.equal(hidden.zone, 'battlefield') assert.equal(game.getPlayer('p1').landPlayedThisTurn, true) }) test('enters triggers from compiled card code fire and resolve through the stack', () => { const soulWarden = card('Soul Warden', { typeLine: 'Creature — Human Cleric', power: 1, toughness: 1, colors: ['W'], abilities: [{ kind: 'triggered', event: { kind: 'enters', subject: { kind: 'object', selector: { other: true, types: ['creature'] }, mode: 'each' }, }, effects: [{ op: 'life', player: 'you', amount: { kind: 'number', value: 1 }, mode: 'gain' }], optional: false, text: 'Whenever another creature enters, you gain 1 life.', }], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() game.addCard('p1', soulWarden, 'battlefield') const elves = game.addCard('p1', creature('Llanowar Elves', 1, 1), 'hand') game.castSpell('p1', elves.id) game.resolveStack() assert.equal(game.getPlayer('p1').life, 41) assert.ok(game.getPlayer('p1').battlefield.includes(elves.id)) }) test('activated mana abilities produce mana and creatures have summoning sickness', () => { const solRing = card('Sol Ring', { typeLine: 'Artifact', power: undefined, toughness: undefined, abilities: [{ kind: 'activated', costs: [{ kind: 'tap' }], effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: ['{C}', '{C}'] } }], optional: false, text: '{T}: Add {C}{C}.', }], }) const game = setup([{ id: 'p1', deck: Array.from({ length: 5 }, () => card('Filler')) }, { id: 'p2', deck: Array.from({ length: 5 }, () => card('Filler')) }]) game.start() const ring = game.addCard('p1', solRing, 'battlefield') const elves = game.addCard('p1', creature('Llanowar Elves', 1, 1, [{ kind: 'activated', costs: [{ kind: 'tap' }], effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: ['{G}'] } }], optional: false, text: '{T}: Add {G}.', }]), 'battlefield') game.activateAbility('p1', ring.id, 0) assert.deepEqual(game.getPlayer('p1').manaPool, { W: 0, U: 0, B: 0, R: 0, G: 0, C: 2 }) assert.equal(ring.tapped, true) assert.throws(() => game.activateAbility('p1', elves.id, 0), GameError) game.endTurn('p1') game.endTurn('p2') assert.equal(elves.tapped, false) game.activateAbility('p1', elves.id, 0) assert.equal(game.getPlayer('p1').manaPool.G, 1) }) test('a player who runs out of cards loses and the last player wins', () => { const game = new Game({ seed: 3, startingHandSize: 0, players: [ { id: 'p1', deck: [card('Filler')] }, { id: 'p2', deck: [] }, ], }) game.start() game.endTurn('p1') assert.equal(game.status, 'finished') assert.equal(game.winner, 'p1') assert.equal(game.getPlayer('p2').lost, true) }) test('lethal damage to a player ends the game', () => { const game = setup([{ id: 'p1' }, { id: 'p2', life: 3 }]) game.start() game.addCard('p1', land('Mountain', 'R'), 'battlefield') const bolt = game.addCard('p1', spell('Lightning Bolt', [ { op: 'damage', target: { kind: 'any' }, amount: { kind: 'number', value: 3 } }, ], '{R}'), 'hand') game.castSpell('p1', bolt.id, { targets: [{ kind: 'player', id: 'p2' }] }) game.resolveStack() assert.equal(game.status, 'finished') assert.equal(game.winner, 'p1') assert.equal(game.getPlayer('p2').life, 0) }) test('the legend rule leaves one legendary permanent per name under one controller', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const legend = card('Kiora, the Rising Tide', { typeLine: 'Legendary Creature — Merfolk Wizard', power: 1, toughness: 1, }) const first = game.addCard('p1', legend, 'battlefield') const second = game.addCard('p1', legend, 'battlefield') const forest = game.addCard('p1', land('Forest', 'G'), 'hand') game.playLand('p1', forest.id) assert.equal(first.zone, 'battlefield') assert.equal(second.zone, 'graveyard') assert.ok(game.getPlayer('p1').graveyard.includes(second.id)) }) test('the legend rule does not apply across controllers', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const legend = card('Kiora, the Rising Tide', { typeLine: 'Legendary Creature — Merfolk Wizard', power: 1, toughness: 1, }) const mine = game.addCard('p1', legend, 'battlefield') const theirs = game.addCard('p2', legend, 'battlefield') const forest = game.addCard('p1', land('Forest', 'G'), 'hand') game.playLand('p1', forest.id) assert.equal(mine.zone, 'battlefield') assert.equal(theirs.zone, 'battlefield') }) test('a failed mana payment leaves mana sources untapped and the pool unchanged', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const mountain = game.addCard('p1', land('Mountain', 'R'), 'battlefield') const expensive = game.addCard('p1', spell('Expensive Spell', [], '{R}{R}'), 'hand') assert.throws(() => game.castSpell('p1', expensive.id), GameError) assert.equal(mountain.tapped, false) assert.equal(game.getPlayer('p1').manaPool.R, 0) assert.ok(game.getPlayer('p1').hand.includes(expensive.id)) }) test('phyrexian mana can be paid with exactly enough life', () => { const game = setup([{ id: 'p1', life: 2 }, { id: 'p2' }]) game.start() const spellCard = game.addCard('p1', spell('Gitaxian Probe', [], '{W/P}'), 'hand') assert.doesNotThrow(() => game.castSpell('p1', spellCard.id)) assert.equal(game.getPlayer('p1').life, 0) assert.equal(game.status, 'finished') assert.equal(game.winner, 'p2') }) test('an unless cost auto-taps the mana sources of the payer', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = game.addCard('p2', creature('Grizzly Bears', 2, 2), 'battlefield') const forests = [ game.addCard('p2', land('Forest', 'G'), 'battlefield'), game.addCard('p2', land('Forest', 'G'), 'battlefield'), ] const edict = game.addCard('p1', spell('Edict', [{ op: 'sacrifice', player: 'opponent', target: { kind: 'object', selector: { types: ['creature'] } }, unless: { kind: 'mana', symbols: '{2}' }, }]), 'hand') game.castSpell('p1', edict.id, { targets: [{ kind: 'player', id: 'p2' }, { kind: 'object', id: bear.id }], }) game.resolveStack() assert.equal(bear.zone, 'battlefield') assert.ok(forests.every(forest => forest.tapped)) assert.equal(game.getPlayer('p2').manaPool.G, 0) }) test('damage followed by exile-instead-of-dying sends the damaged creature to exile', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = game.addCard('p2', creature('Doomed Bear', 2, 2), 'battlefield') const burn = game.addCard('p1', spell('Exiling Burn', [ { op: 'damage', target: { kind: 'any' }, amount: { kind: 'number', value: 3 } }, { op: 'exile_instead_of_dying', target: { kind: 'damaged' } }, ]), 'hand') game.castSpell('p1', burn.id, { targets: [{ kind: 'object', id: bear.id }] }) game.resolveStack() assert.equal(bear.zone, 'exile') assert.ok(game.getPlayer('p2').exile.includes(bear.id)) assert.ok(!game.getPlayer('p2').graveyard.includes(bear.id)) })