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('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, 8) assert.equal(game.getPlayer('p1').library.length, 2) 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('mulligans draw a new seven and bottom one more card each time', () => { const game = new Game({ seed: 3, startingHandSize: 7, mulligans: true, players: [ { id: 'p1', deck: Array.from({ length: 40 }, (_, index) => card(`P1 Card ${index}`)) }, { id: 'p2', deck: Array.from({ length: 40 }, (_, index) => card(`P2 Card ${index}`)) }, ], }) game.start() assert.equal(game.status, 'mulligan') assert.equal(game.getPlayer('p1').hand.length, 7) game.keepHand('p2') game.mulligan('p1') assert.equal(game.getPlayer('p1').hand.length, 7) assert.equal(game.mulligans.get('p1')?.count, 1) assert.equal(game.mulligans.get('p1')?.bottom, 0) game.mulligan('p1') assert.equal(game.mulligans.get('p1')?.count, 2) assert.equal(game.mulligans.get('p1')?.bottom, 1) assert.throws(() => game.keepHand('p1'), /bottom/) const bottomCard = game.getPlayer('p1').hand[0] game.putOnBottom('p1', bottomCard) assert.equal(game.getPlayer('p1').hand.length, 6) assert.equal(game.getPlayer('p1').library.at(-1), bottomCard) assert.equal(game.mulligans.get('p1')?.bottom, 0) game.mulligan('p1') assert.equal(game.mulligans.get('p1')?.count, 3) assert.equal(game.mulligans.get('p1')?.bottom, 2) for (const cardId of [...game.getPlayer('p1').hand.slice(0, 2)]) game.putOnBottom('p1', cardId) assert.equal(game.getPlayer('p1').hand.length, 5) game.keepHand('p1') assert.equal(game.status, 'active') assert.equal(game.turn, 1) assert.equal(game.step, 'main') assert.equal(game.getPlayer('p1').hand.length, 6) }) 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('doubling counters doubles each kind on the target', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const target = game.addCard('p1', creature('Counter Bear', 2, 3), 'battlefield') target.counters['+1/+1'] = 2 target.counters['charge'] = 1 const doubler = spell('Doubler', [{ op: 'double_counters', target: { kind: 'object', selector: { types: ['creature'] } }, }]) const cardInHand = game.addCard('p1', doubler, 'hand') game.castSpell('p1', cardInHand.id, { targets: [{ kind: 'object', id: target.id }] }) game.resolveStack() assert.equal(target.counters['+1/+1'], 4) assert.equal(target.counters.charge, 2) }) test('damage prevention shields absorb the next damage', () => { const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = creature('Bear', 2, 3) const target = game.addCard('p1', bear, 'battlefield') const bolt = spell('Bolt', [{ op: 'damage', target: { kind: 'object', selector: { types: ['creature'] } }, amount: { kind: 'number', value: 5 } }]) const shield = spell('Shield', [{ op: 'prevent_damage', target: { kind: 'object', selector: { types: ['creature'] } }, amount: { kind: 'number', value: 3 }, duration: 'until_end_of_turn', }]) const shieldCard = game.addCard('p1', shield, 'hand') game.castSpell('p1', shieldCard.id, { targets: [{ kind: 'object', id: target.id }] }) 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(target.zone, 'battlefield') }) test('a "you may" spell asks its controller and honors the choice', () => { const optionalSpell = card('Optional Test', { typeLine: 'Instant', abilities: [{ kind: 'spell', effects: [{ op: 'life', player: 'you', amount: { kind: 'number', value: 3 }, mode: 'gain' }], optional: true, text: 'You may gain 3 life.', }], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const accept = game.addCard('p1', optionalSpell, 'hand') game.castSpell('p1', accept.id) game.resolveStack() assert.equal(game.pendingChoice?.what, 'optional') const before = game.getPlayer('p1').life game.chooseOptional('p1', true) assert.equal(game.getPlayer('p1').life, before + 3) const decline = game.addCard('p1', optionalSpell, 'hand') game.castSpell('p1', decline.id) game.resolveStack() assert.equal(game.pendingChoice?.what, 'optional') const after = game.getPlayer('p1').life game.chooseOptional('p1', false) assert.equal(game.getPlayer('p1').life, after) assert.equal(decline.zone, 'graveyard') }) test('declining a "you may" runs the "if you don\'t" branch', () => { const optionalSpell = card('Optional Branch', { typeLine: 'Instant', abilities: [{ kind: 'spell', effects: [ { op: 'life', player: 'you', amount: { kind: 'number', value: 2 }, mode: 'gain' }, { op: 'conditional', condition: { kind: 'if_you_dont' }, effects: [{ op: 'life', player: 'you', amount: { kind: 'number', value: 1 }, mode: 'lose' }], }, ], optional: true, text: 'You may gain 2 life. If you don\'t, you lose 1 life.', }], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const spellCard = game.addCard('p1', optionalSpell, 'hand') game.castSpell('p1', spellCard.id) game.resolveStack() const before = game.getPlayer('p1').life game.chooseOptional('p1', false) assert.equal(game.getPlayer('p1').life, before - 1) }) test('a compiled "you may" enters trigger asks and applies the choice', () => { const abilities = compileText( 'When this creature enters, you may gain 2 life.', 'Optional Bear', [], 'Creature — Bear', ) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const bear = game.addCard('p1', creature('Optional Bear', 2, 2, abilities), 'hand') game.castSpell('p1', bear.id) game.resolveStack() assert.equal(game.pendingChoice?.what, 'optional') const before = game.getPlayer('p1').life game.chooseOptional('p1', true) assert.equal(game.getPlayer('p1').life, before + 2) }) test('a land that chooses a color produces that color', () => { const landDef = card('Chosen Color Land', { typeLine: 'Land', abilities: [ { kind: 'static', effects: [{ op: 'choose', what: 'color' }], text: 'As this enters, choose a color.', }, { kind: 'activated', costs: [{ kind: 'tap' }], effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'any_color', amount: { kind: 'number', value: 1 }, oneColor: true, restriction: 'chosen color' }, }], optional: false, text: '{T}: Add one mana of the chosen color.', }, ], }) const game = setup([{ id: 'p1' }, { id: 'p2' }]) game.start() const land = game.addCard('p1', landDef, 'hand') game.playLand('p1', land.id) assert.equal(game.pendingChoice?.what, 'color') game.chooseColor('p1', 'Blue') game.activateAbility('p1', land.id, 1) game.resolveStack() assert.equal(game.getPlayer('p1').manaPool.U, 1) })