import assert from 'node:assert/strict' import test from 'node:test' import { Game, GameError } from '../src/game/game.js' import type { Ability, Effect } from '../src/effects/types.js' import type { CardDefinition } from '../src/game/types.js' function card(name: string, overrides: Partial = {}): CardDefinition { return { oracleId: name.toLowerCase().replace(/[^a-z0-9]+/g, '-'), name, typeLine: 'Creature — Test', manaValue: 0, colors: [], abilities: [], ...overrides, } } function creature(name: string, power: number, toughness: number, abilities: Ability[] = []): CardDefinition { return card(name, { typeLine: 'Creature — Test', power, toughness, abilities }) } function spell(name: string, effects: Effect[], manaCost = ''): CardDefinition { return card(name, { typeLine: 'Instant', manaCost, manaValue: 0, abilities: [{ kind: 'spell', effects, optional: false, text: name }] }) } function land(name: string, color: string): CardDefinition { return card(name, { typeLine: `Basic Land — ${name}`, power: undefined, toughness: undefined, abilities: [{ kind: 'activated', costs: [{ kind: 'tap' }], effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: [`{${color}}`] } }], optional: false, text: `{T}: Add {${color}}.`, }], }) } function setup(players: { id: string; deck?: CardDefinition[]; life?: number }[]): Game { const game = new Game({ seed: 7, startingHandSize: 0, players: players.map(player => ({ id: player.id, life: player.life, deck: player.deck ?? [card('Filler')] })), }) return game } test('turns start at the first main phase, draw opening hands, and pass to the next player', () => { const forest = land('Forest', 'G') const game = new Game({ seed: 1, startingHandSize: 7, players: [ { id: 'p1', deck: Array.from({ length: 10 }, () => forest) }, { id: 'p2', deck: Array.from({ length: 10 }, () => forest) }, ], }) game.start() assert.equal(game.turn, 1) assert.equal(game.activePlayer, 'p1') assert.equal(game.phase, 'precombat_main') assert.equal(game.step, 'main') assert.equal(game.priority, 'p1') assert.equal(game.getPlayer('p1').life, 40) assert.equal(game.getPlayer('p1').hand.length, 7) assert.equal(game.getPlayer('p1').library.length, 3) game.endTurn('p1') assert.equal(game.turn, 2) assert.equal(game.activePlayer, 'p2') assert.equal(game.getPlayer('p2').hand.length, 8) assert.equal(game.getPlayer('p1').hand.length, 7) }) test('passing priority twice advances from the main phase into combat', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() game.pass('p1') assert.equal(game.priority, 'p2') game.pass('p2') assert.equal(game.phase, 'combat') assert.equal(game.step, 'declare_attackers') assert.equal(game.priority, 'p1') }) test('a spell goes on the stack, auto-taps mana, and resolves its compiled effect', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const mountain = 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' }] }) assert.equal(game.stack.length, 1) assert.equal(mountain.tapped, true) assert.equal(game.priority, 'p2') game.resolveStack() assert.equal(game.stack.length, 0) assert.equal(game.getPlayer('p2').life, 37) assert.ok(game.getPlayer('p1').graveyard.includes(bolt.id)) }) test('permanents can be repositioned by their controller and positions are cleared on zone changes', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = game.addCard('p1', creature('Grizzly Bears', 2, 2), 'battlefield') game.movePermanent('p1', bear.id, 0.25, 0.75) assert.deepEqual(bear.position, { x: 0.25, y: 0.75 }) game.movePermanent('p1', bear.id, -1, 2) assert.deepEqual(bear.position, { x: 0, y: 1 }) assert.throws(() => game.movePermanent('p2', bear.id, 0.5, 0.5), /control/) game.moveToZone(bear, 'graveyard') assert.equal(bear.position, undefined) }) 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('static pump effects apply to matching creatures only', () => { const anthem = card('Glorious Anthem', { typeLine: 'Enchantment', power: undefined, toughness: undefined, abilities: [{ kind: 'static', effects: [{ op: 'pump', target: { kind: 'object', selector: { controller: 'you', types: ['creature'] }, mode: 'each' }, power: { kind: 'number', value: 1 }, toughness: { kind: 'number', value: 1 }, }], text: 'Creatures you control get +1/+1.', }], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const anthemObject = game.addCard('p1', anthem, 'battlefield') const bear = game.addCard('p1', creature('Grizzly Bears', 2, 2), 'battlefield') const enemy = game.addCard('p2', creature('Enemy Bear', 2, 2), 'battlefield') assert.equal(game.getPower(bear), 3) assert.equal(game.getToughness(bear), 3) assert.equal(game.getPower(enemy), 2) assert.equal(game.getPower(anthemObject), 0) }) 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('"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('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) }) test('search effects find cards in the library and put them onto the battlefield tapped', () => { const forest = land('Forest', 'G') const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const found = game.addCard('p1', forest, 'library') const rampantGrowth = game.addCard('p1', spell('Rampant Growth', [{ op: 'search', player: 'you', selector: { types: ['land'], supertypes: ['basic'] }, amount: { kind: 'number', value: 1 }, destination: 'battlefield', tapped: true, shuffle: true, }]), 'hand') game.castSpell('p1', rampantGrowth.id) game.resolveStack() assert.ok(game.getPlayer('p1').battlefield.includes(found.id)) assert.equal(found.tapped, true) }) test('search effects let the player pick among multiple matching cards', () => { const forest = land('Forest', 'G') const island = land('Island', 'U') const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const forestObject = game.addCard('p1', forest, 'library') const islandObject = game.addCard('p1', island, 'library') const rampantGrowth = game.addCard('p1', spell('Rampant Growth', [{ op: 'search', player: 'you', selector: { types: ['land'], supertypes: ['basic'] }, amount: { kind: 'number', value: 1 }, destination: 'battlefield', tapped: true, shuffle: true, }]), 'hand') game.castSpell('p1', rampantGrowth.id) game.resolveStack() assert.equal(game.pendingSearch?.playerId, 'p1') assert.deepEqual( game.searchCandidates('p1').map(object => object.id).sort(), [forestObject.id, islandObject.id].sort(), ) assert.equal(forestObject.zone, 'library') assert.throws(() => game.pass('p1'), GameError) game.chooseSearchCard('p1', islandObject.id) assert.equal(game.pendingSearch, undefined) assert.ok(game.getPlayer('p1').battlefield.includes(islandObject.id)) assert.equal(islandObject.tapped, true) assert.ok(game.getPlayer('p1').library.includes(forestObject.id)) }) test('autoAdvance stops instead of passing while a search needs a pick', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() game.addCard('p1', land('Forest', 'G'), 'library') game.addCard('p1', land('Island', 'U'), 'library') const rampantGrowth = game.addCard('p1', spell('Rampant Growth', [{ op: 'search', player: 'you', selector: { types: ['land'], supertypes: ['basic'] }, amount: { kind: 'number', value: 1 }, destination: 'battlefield', tapped: true, shuffle: true, }]), 'hand') game.castSpell('p1', rampantGrowth.id) game.autoAdvance() assert.equal(game.pendingSearch?.playerId, 'p1') assert.equal(game.priority, 'p1') }) test('static replacement effects make permanents enter tapped with counters', () => { const slow = creature('Slow Bear', 2, 2, [{ kind: 'static', effects: [{ op: 'enters_tapped', target: { kind: 'self' } }], text: 'This enters tapped.', }, { kind: 'static', effects: [{ op: 'enters_with_counters', target: { kind: 'self' }, counter: '+1/+1', amount: { kind: 'number', value: 1 }, }], text: 'This enters with a +1/+1 counter.', }]) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = game.addCard('p1', slow, 'hand') game.castSpell('p1', bear.id) game.resolveStack() assert.equal(bear.tapped, true) assert.equal(bear.counters['+1/+1'], 1) assert.equal(game.getPower(bear), 3) assert.equal(game.getToughness(bear), 3) }) 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('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('canPlayCard reports legal lands and spells for the player with priority', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const forest = game.addCard('p1', land('Forest', 'G'), 'hand') const bear = game.addCard('p1', card('Grizzly Bears', { typeLine: 'Creature — Bear', power: 2, toughness: 2, manaCost: '{G}', }), 'hand') assert.equal(game.canPlayCard('p1', forest.id), true) assert.equal(game.canPlayCard('p1', bear.id), false) assert.equal(game.canPlayCard('p2', forest.id), false) game.playLand('p1', forest.id) assert.equal(game.canPlayCard('p1', bear.id), true) }) test('autoAdvance skips empty phases and stops when someone can act', () => { const expensive = card('Expensive', { manaCost: '{5}' }) const game = new Game({ seed: 3, startingHandSize: 0, players: [ { id: 'p1', deck: [expensive] }, { id: 'p2', deck: [expensive] }, ], }) game.start() game.addCard('p1', land('Forest', 'G'), 'battlefield') // The only action is a mana ability, which should not hold the phase open. game.autoAdvance() assert.equal(game.turn, 2) assert.equal(game.activePlayer, 'p2') assert.equal(game.phase, 'precombat_main') assert.equal(game.priority, 'p2') const hasty = creature('Hasty', 2, 2, [{ kind: 'keyword', keyword: 'Haste' }]) game.addCard('p2', hasty, 'battlefield') game.autoAdvance() assert.equal(game.turn, 2) assert.equal(game.phase, 'combat') assert.equal(game.step, 'declare_attackers') assert.equal(game.priority, 'p2') }) test('every defending player declares blocks before combat damage in multiplayer', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }, { id: 'p3' }]) game.start() const first = game.addCard('p1', creature('First Attacker', 2, 2), 'battlefield') const second = game.addCard('p1', creature('Second Attacker', 3, 3), 'battlefield') first.enteredTurn = 0 second.enteredTurn = 0 game.declareAttackers('p1', [ { attackerId: first.id, defenderId: 'p2' }, { attackerId: second.id, defenderId: 'p3' }, ]) assert.equal(game.step, 'declare_blockers') game.declareBlockers('p2', []) assert.equal(game.step, 'declare_blockers') assert.equal(game.priority, 'p3') game.declareBlockers('p3', []) assert.equal(game.step, 'combat_damage') game.resolveCombatDamage() assert.equal(game.getPlayer('p2').life, 38) assert.equal(game.getPlayer('p3').life, 37) }) test('counter-removal costs are paid and fail when counters run out', () => { const vessel = card('Spore Vessel', { typeLine: 'Artifact', power: undefined, toughness: undefined, abilities: [{ kind: 'activated', costs: [{ kind: 'remove_counters', counter: 'spore', amount: { kind: 'number', value: 2 } }], effects: [{ op: 'draw', player: 'you', amount: { kind: 'number', value: 1 } }], optional: false, text: 'Remove two spore counters from this: Draw a card.', }], }) const game = setup([{ id: 'p1', deck: Array.from({ length: 3 }, () => card('Filler')) }, { id: 'p2' }]) game.start() const source = game.addCard('p1', vessel, 'battlefield') source.counters.spore = 3 game.activateAbility('p1', source.id, 0) game.resolveStack() assert.equal(source.counters.spore, 1) assert.equal(game.getPlayer('p1').hand.length, 1) assert.throws(() => game.activateAbility('p1', source.id, 0), GameError) assert.equal(source.counters.spore, 1) })