test
This commit is contained in:
@@ -29,6 +29,58 @@ test('enters trigger with a draw effect', () => {
|
||||
assert.equal(ability.effects[0].op, 'draw')
|
||||
})
|
||||
|
||||
test('compound effects resolve "it" through the previous target', () => {
|
||||
const [ability] = creature('When this enters, tap target creature an opponent controls and put a stun counter on it.')
|
||||
assert.equal(ability.kind, 'triggered')
|
||||
if (ability.kind !== 'triggered') return
|
||||
assert.deepEqual(ability.effects[1], {
|
||||
op: 'put_counters',
|
||||
target: { kind: 'previous_target' },
|
||||
counter: 'stun',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
})
|
||||
})
|
||||
|
||||
test('compound effects share an explicit player subject', () => {
|
||||
const [ability] = spell('Target player draws two cards and loses 2 life.')
|
||||
assert.equal(ability.kind, 'spell')
|
||||
if (ability.kind !== 'spell') return
|
||||
assert.deepEqual(ability.effects[1], {
|
||||
op: 'life',
|
||||
player: 'target_player',
|
||||
amount: { kind: 'number', value: 2 },
|
||||
mode: 'lose',
|
||||
})
|
||||
})
|
||||
|
||||
test('damage triggers without a recipient use the event amount', () => {
|
||||
const [ability] = creature('Whenever this deals damage, you gain that much life.')
|
||||
assert.equal(ability.kind, 'triggered')
|
||||
if (ability.kind !== 'triggered') return
|
||||
assert.equal(ability.event.kind, 'deals_damage')
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'life',
|
||||
player: 'you',
|
||||
amount: { kind: 'event_amount' },
|
||||
mode: 'gain',
|
||||
})
|
||||
})
|
||||
|
||||
test('a later sentence can refer to a token created by an earlier one', () => {
|
||||
const [ability] = creature(
|
||||
'When this enters, create a 0/0 green and blue Fractal creature token. Put three +1/+1 counters on it.',
|
||||
)
|
||||
assert.equal(ability.kind, 'triggered')
|
||||
if (ability.kind !== 'triggered') return
|
||||
assert.equal(ability.effects[0].op, 'create_token')
|
||||
assert.deepEqual(ability.effects[1], {
|
||||
op: 'put_counters',
|
||||
target: { kind: 'previous_target' },
|
||||
counter: '+1/+1',
|
||||
amount: { kind: 'number', value: 3 },
|
||||
})
|
||||
})
|
||||
|
||||
test('activated mana ability', () => {
|
||||
const [ability] = creature('{T}: Add {G}.')
|
||||
assert.equal(ability.kind, 'activated')
|
||||
@@ -149,11 +201,16 @@ test('"it" refers to the triggering object when unambiguous', () => {
|
||||
assert.deepEqual(effect.target, { kind: 'trigger_subject' })
|
||||
})
|
||||
|
||||
test('ambiguous "it" is left unsupported instead of guessed', () => {
|
||||
test('a target introduced in one clause is the "it" of the next', () => {
|
||||
const [ability] = creature(
|
||||
'When this creature enters, tap target creature an opponent controls and put a stun counter on it.',
|
||||
)
|
||||
assert.equal(ability.kind, 'unsupported')
|
||||
assert.equal(ability.kind, 'triggered')
|
||||
if (ability.kind !== 'triggered') return
|
||||
const effect = ability.effects[1]
|
||||
assert.equal(effect.op, 'put_counters')
|
||||
if (effect.op !== 'put_counters') return
|
||||
assert.deepEqual(effect.target, { kind: 'previous_target' })
|
||||
})
|
||||
|
||||
test('modal text is unsupported', () => {
|
||||
@@ -275,7 +332,7 @@ test('token-doubling replacement lines compile and round-trip', () => {
|
||||
const [ability] = creature(text)
|
||||
assert.equal(ability.kind, 'static')
|
||||
if (ability.kind !== 'static') return
|
||||
assert.deepEqual(ability.effects[0], expected)
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(ability.effects[0])), expected)
|
||||
|
||||
const source = formatAbility(ability)
|
||||
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
|
||||
@@ -354,6 +411,172 @@ test('"When <subject>" triggers and bare "gain life" effects parse', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('"This ability triggers only once" marks the trigger', () => {
|
||||
const [ability] = creature('At the beginning of your second main phase, if this is tapped, put a flying counter on it. This ability triggers only once.')
|
||||
assert.equal(ability.kind, 'triggered')
|
||||
if (ability.kind !== 'triggered') return
|
||||
assert.deepEqual(ability.event, { kind: 'main_phase', who: 'you', phase: 'postcombat' })
|
||||
assert.equal(ability.oncePerTurn, true)
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'put_counters',
|
||||
target: { kind: 'self' },
|
||||
counter: 'flying',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
})
|
||||
})
|
||||
|
||||
test('impulse exiles the top cards and grants play permission', () => {
|
||||
const [ability] = spell('Exile the top three cards of your library. Until end of turn, you may play those cards.')
|
||||
assert.equal(ability.kind, 'spell')
|
||||
if (ability.kind !== 'spell') return
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'impulse',
|
||||
player: 'you',
|
||||
amount: { kind: 'number', value: 3 },
|
||||
faceDown: false,
|
||||
})
|
||||
})
|
||||
|
||||
test('a mana ability can produce the chosen color', () => {
|
||||
const [ability] = creature('{T}: Add one mana of the chosen color.')
|
||||
assert.equal(ability.kind, 'activated')
|
||||
if (ability.kind !== 'activated') return
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'add_mana',
|
||||
player: 'you',
|
||||
mana: {
|
||||
kind: 'any_color',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
oneColor: true,
|
||||
restriction: 'chosen color',
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test('doubling counters compiles to its own effect', () => {
|
||||
const [ability] = spell('Double the number of +1/+1 counters on target creature.')
|
||||
assert.equal(ability.kind, 'spell')
|
||||
if (ability.kind !== 'spell') return
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'double_counters',
|
||||
target: { kind: 'object', selector: { types: ['creature'] }, mode: 'target' },
|
||||
counter: '+1/+1',
|
||||
})
|
||||
})
|
||||
|
||||
test('damage prevention compiles with its shield and scope', () => {
|
||||
const [shield] = spell('Prevent the next 3 damage that would be dealt to target creature this turn.')
|
||||
assert.equal(shield.kind, 'spell')
|
||||
if (shield.kind !== 'spell') return
|
||||
assert.equal(shield.effects[0].op, 'prevent_damage')
|
||||
const first = shield.effects[0]
|
||||
if (first.op !== 'prevent_damage') return
|
||||
assert.deepEqual(first.target, { kind: 'object', selector: { types: ['creature'] }, mode: 'target' })
|
||||
assert.deepEqual(first.amount, { kind: 'number', value: 3 })
|
||||
assert.equal(first.duration, 'until_end_of_turn')
|
||||
assert.equal(first.global, undefined)
|
||||
|
||||
const [global] = spell('Prevent all combat damage that would be dealt this turn.')
|
||||
assert.equal(global.kind, 'spell')
|
||||
if (global.kind !== 'spell') return
|
||||
const second = global.effects[0]
|
||||
if (second.op !== 'prevent_damage') return
|
||||
assert.equal(second.amount, 'all')
|
||||
assert.equal(second.combatOnly, true)
|
||||
assert.equal(second.global, true)
|
||||
})
|
||||
|
||||
test('counter and energy replacement lines compile', () => {
|
||||
const [counters] = creature('If one or more +1/+1 counters would be put on a creature you control, twice that many +1/+1 counters are put on that creature instead.')
|
||||
assert.equal(counters.kind, 'static')
|
||||
if (counters.kind !== 'static') return
|
||||
assert.equal(counters.effects[0].op, 'counter_multiplier')
|
||||
if (counters.effects[0].op !== 'counter_multiplier') return
|
||||
assert.equal(counters.effects[0].counter, '+1/+1')
|
||||
assert.equal(counters.effects[0].multiplier, 2)
|
||||
|
||||
const [energy] = creature('If you would get one or more {E}, you get twice that many {E} instead.')
|
||||
assert.equal(energy.kind, 'static')
|
||||
if (energy.kind !== 'static') return
|
||||
assert.deepEqual(energy.effects[0], { op: 'replacement_amount', what: 'energy', multiplier: 2 })
|
||||
})
|
||||
|
||||
test('static source-filtered prevention compiles', () => {
|
||||
const [ability] = creature('Prevent all damage that would be dealt to this by artifact creatures.')
|
||||
assert.equal(ability.kind, 'static')
|
||||
if (ability.kind !== 'static') return
|
||||
assert.deepEqual(ability.effects[0], {
|
||||
op: 'prevent_damage',
|
||||
target: { kind: 'self' },
|
||||
amount: 'all',
|
||||
combatOnly: undefined,
|
||||
duration: 'permanent',
|
||||
sourceSelector: { types: ['artifact', 'creature'] },
|
||||
})
|
||||
})
|
||||
|
||||
test('source-scoped prevention compiles with its chosen source', () => {
|
||||
const [ability] = spell('Prevent all damage a red source of your choice would deal this turn.')
|
||||
assert.equal(ability.kind, 'spell')
|
||||
if (ability.kind !== 'spell') return
|
||||
const shield = ability.effects[0]
|
||||
assert.equal(shield.op, 'prevent_damage')
|
||||
if (shield.op !== 'prevent_damage') return
|
||||
assert.deepEqual(shield.source, { kind: 'object', selector: { colors: ['R'] }, mode: 'target' })
|
||||
assert.equal(shield.global, true)
|
||||
})
|
||||
|
||||
test('a prevention follow-up compiles into the shield', () => {
|
||||
const [ability] = spell('Prevent the next 3 damage that would be dealt to target creature this turn. If damage is prevented this way, this deals that much damage to any target.')
|
||||
assert.equal(ability.kind, 'spell')
|
||||
if (ability.kind !== 'spell') return
|
||||
const shield = ability.effects[0]
|
||||
assert.equal(shield.op, 'prevent_damage')
|
||||
if (shield.op !== 'prevent_damage') return
|
||||
assert.deepEqual(shield.then, [{
|
||||
op: 'damage',
|
||||
target: { kind: 'any' },
|
||||
amount: { kind: 'event_amount' },
|
||||
}])
|
||||
})
|
||||
|
||||
test('replacement amount lines compile', () => {
|
||||
const [life] = creature('If you would gain life, you gain twice that much life instead.')
|
||||
assert.equal(life.kind, 'static')
|
||||
if (life.kind !== 'static') return
|
||||
assert.deepEqual(life.effects[0], { op: 'replacement_amount', what: 'life_gain', multiplier: 2 })
|
||||
|
||||
const [plus] = creature('If you would gain life, you gain that much life plus 1 instead.')
|
||||
assert.equal(plus.kind, 'static')
|
||||
if (plus.kind !== 'static') return
|
||||
assert.deepEqual(plus.effects[0], { op: 'replacement_amount', what: 'life_gain', multiplier: 1, add: 1 })
|
||||
|
||||
const [draw] = creature('If you would draw a card, draw two cards instead.')
|
||||
assert.equal(draw.kind, 'static')
|
||||
if (draw.kind !== 'static') return
|
||||
assert.deepEqual(draw.effects[0], { op: 'replacement_amount', what: 'draw', multiplier: 2 })
|
||||
})
|
||||
|
||||
test('"skip your draw step" and attack-or-block restrictions compile', () => {
|
||||
const [skip] = creature('Skip your draw step.')
|
||||
assert.equal(skip.kind, 'static')
|
||||
if (skip.kind !== 'static') return
|
||||
assert.deepEqual(skip.effects[0], {
|
||||
op: 'restriction',
|
||||
target: { kind: 'player', who: 'you', mode: 'target' },
|
||||
rule: 'skip_draw_step',
|
||||
})
|
||||
|
||||
const [lock] = creature('Enchanted creature cannot attack or block, and its activated abilities cannot be activated.')
|
||||
assert.equal(lock.kind, 'static')
|
||||
if (lock.kind !== 'static') return
|
||||
assert.deepEqual(lock.effects.map(effect => effect.op === 'restriction' ? effect.rule : effect.op), [
|
||||
'cant_attack',
|
||||
'cant_block',
|
||||
'cant_activate',
|
||||
])
|
||||
})
|
||||
|
||||
test('a trailing condition becomes a static condition', () => {
|
||||
const [ability] = creature('This creature cannot attack if defending player controls an untapped land.')
|
||||
assert.equal(ability.kind, 'static')
|
||||
@@ -646,3 +869,51 @@ test('Role token creation compiles with the attachment target', () => {
|
||||
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
|
||||
}
|
||||
})
|
||||
|
||||
test('token-creation replacement lines compile and round-trip', () => {
|
||||
const cases: Array<[string, object]> = [
|
||||
[
|
||||
'If you would create a Food token, instead create a Food token and a Treasure token.',
|
||||
{ op: 'token_replacement', from: ['food'], tokens: [{ types: ['artifact'], subtypes: ['food'], colors: [] }, { types: ['artifact'], subtypes: ['treasure'], colors: [] }], who: 'you' },
|
||||
],
|
||||
[
|
||||
'If you would create a Clue, Food, or Treasure token, instead create one of each.',
|
||||
{ op: 'token_replacement', from: ['clue', 'food', 'treasure'], tokens: [{ name: 'Clue', types: ['artifact'], subtypes: ['clue'], colors: [] }, { name: 'Food', types: ['artifact'], subtypes: ['food'], colors: [] }, { name: 'Treasure', types: ['artifact'], subtypes: ['treasure'], colors: [] }], who: 'you' },
|
||||
],
|
||||
]
|
||||
for (const [text, expected] of cases) {
|
||||
const [ability] = creature(text)
|
||||
assert.equal(ability.kind, 'static')
|
||||
if (ability.kind !== 'static') return
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(ability.effects[0])), expected)
|
||||
|
||||
const source = formatAbility(ability)
|
||||
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
|
||||
}
|
||||
})
|
||||
|
||||
test('exile-instead-of-dying clauses compile', () => {
|
||||
const [damaged] = spell('This deals 3 damage to any target. If a creature dealt damage this way would die this turn, exile it instead.')
|
||||
assert.equal(damaged.kind, 'spell')
|
||||
if (damaged.kind !== 'spell') return
|
||||
assert.deepEqual(damaged.effects[1], { op: 'exile_instead_of_dying', target: { kind: 'damaged' } })
|
||||
|
||||
const [that] = spell('This deals 3 damage to target creature. If that creature would die this turn, exile it instead.')
|
||||
assert.equal(that.kind, 'spell')
|
||||
if (that.kind !== 'spell') return
|
||||
assert.deepEqual(that.effects[1], { op: 'exile_instead_of_dying', target: { kind: 'previous_target' } })
|
||||
})
|
||||
|
||||
test('equipped and enchanted conditions gate static abilities', () => {
|
||||
const [equipped] = creature('As long as this is equipped, it gets +2/+0 and has menace.')
|
||||
assert.equal(equipped.kind, 'static')
|
||||
if (equipped.kind !== 'static') return
|
||||
assert.equal(equipped.condition?.kind, 'source_equipped')
|
||||
assert.equal(equipped.effects[0].op, 'pump')
|
||||
assert.equal(equipped.effects[1].op, 'grant')
|
||||
|
||||
const [enchanted] = creature('As long as this is enchanted, it has flying.')
|
||||
assert.equal(enchanted.kind, 'static')
|
||||
if (enchanted.kind !== 'static') return
|
||||
assert.equal(enchanted.condition?.kind, 'source_enchanted')
|
||||
})
|
||||
|
||||
283
backend/test/game-core.test.ts
Normal file
283
backend/test/game-core.test.ts
Normal file
@@ -0,0 +1,283 @@
|
||||
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)
|
||||
})
|
||||
343
backend/test/game-effects.test.ts
Normal file
343
backend/test/game-effects.test.ts
Normal file
@@ -0,0 +1,343 @@
|
||||
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))
|
||||
})
|
||||
155
backend/test/game-mana.test.ts
Normal file
155
backend/test/game-mana.test.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
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('Exotic Orchard is limited to colors an opponent land could produce', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const orchard = game.addCard('p1', exoticOrchard(), 'battlefield')
|
||||
game.addCard('p2', land('Forest', 'G'), 'battlefield')
|
||||
const green = game.addCard('p1', spell('Green Spell', [], '{G}'), 'hand')
|
||||
const blue = game.addCard('p1', spell('Blue Spell', [], '{U}'), 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', green.id), true)
|
||||
assert.equal(game.canPlayCard('p1', blue.id), false)
|
||||
assert.throws(() => game.castSpell('p1', blue.id), GameError)
|
||||
assert.equal(orchard.tapped, false)
|
||||
assert.deepEqual(game.getPlayer('p1').manaPool, { W: 0, U: 0, B: 0, R: 0, G: 0, C: 0 })
|
||||
|
||||
game.castSpell('p1', green.id)
|
||||
assert.equal(orchard.tapped, true)
|
||||
assert.equal(game.getPlayer('p1').manaPool.G, 0)
|
||||
})
|
||||
|
||||
test('Exotic Orchard adds an opponent land color when activated manually', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const orchard = game.addCard('p1', exoticOrchard(), 'battlefield')
|
||||
game.addCard('p2', land('Island', 'U'), 'battlefield')
|
||||
|
||||
game.activateAbility('p1', orchard.id, 0)
|
||||
assert.equal(game.getPlayer('p1').manaPool.U, 1)
|
||||
assert.equal(game.getPlayer('p1').manaPool.W, 0)
|
||||
})
|
||||
|
||||
test('Exotic Orchard produces nothing without an opponent land', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const orchard = game.addCard('p1', exoticOrchard(), 'battlefield')
|
||||
|
||||
game.activateAbility('p1', orchard.id, 0)
|
||||
assert.equal(orchard.tapped, true)
|
||||
assert.deepEqual(game.getPlayer('p1').manaPool, { W: 0, U: 0, B: 0, R: 0, G: 0, C: 0 })
|
||||
})
|
||||
|
||||
test('a mana ability that makes one of several colors uses the chosen color', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const dual = game.addCard('p1', card('Dual Land', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: ['{W}', '{U}'], oneOf: true } }],
|
||||
optional: false,
|
||||
text: '{T}: Add {W} or {U}.',
|
||||
}],
|
||||
}), 'battlefield')
|
||||
|
||||
assert.deepEqual(game.manaColorChoices(dual, 0), ['W', 'U'])
|
||||
game.activateAbility('p1', dual.id, 0, { manaColor: 'U' })
|
||||
assert.equal(game.getPlayer('p1').manaPool.U, 1)
|
||||
assert.equal(game.getPlayer('p1').manaPool.W, 0)
|
||||
})
|
||||
|
||||
test('a mana color the ability cannot make is rejected before tapping', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const mono = game.addCard('p1', land('Mono Land', 'W'), 'battlefield')
|
||||
|
||||
assert.throws(() => game.activateAbility('p1', mono.id, 0, { manaColor: 'G' }), GameError)
|
||||
assert.equal(mono.tapped, false)
|
||||
assert.equal(game.getPlayer('p1').manaPool.G, 0)
|
||||
})
|
||||
|
||||
test('an any-color mana source produces the chosen color and offers all five', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const rainbow = game.addCard('p1', card('Rainbow Land', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'any_color', amount: { kind: 'number', value: 1 }, oneColor: false } }],
|
||||
optional: false,
|
||||
text: '{T}: Add one mana of any color.',
|
||||
}],
|
||||
}), 'battlefield')
|
||||
|
||||
assert.deepEqual(game.manaColorChoices(rainbow, 0), ['W', 'U', 'B', 'R', 'G'])
|
||||
game.activateAbility('p1', rainbow.id, 0, { manaColor: 'R' })
|
||||
assert.equal(game.getPlayer('p1').manaPool.R, 1)
|
||||
})
|
||||
|
||||
test('a restricted mana source is used before an unrestricted one', () => {
|
||||
const rainbow = card('Rainbow Land', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'any_color', amount: { kind: 'number', value: 1 }, oneColor: false } }],
|
||||
optional: false,
|
||||
text: '{T}: Add one mana of any color.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const rainbowLand = game.addCard('p1', rainbow, 'battlefield')
|
||||
const orchard = game.addCard('p1', exoticOrchard(), 'battlefield')
|
||||
game.addCard('p2', land('Forest', 'G'), 'battlefield')
|
||||
const gold = game.addCard('p1', spell('Gold Spell', [], '{G}{U}'), 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', gold.id), true)
|
||||
game.castSpell('p1', gold.id)
|
||||
assert.equal(orchard.tapped, true)
|
||||
assert.equal(rainbowLand.tapped, true)
|
||||
assert.deepEqual(game.getPlayer('p1').manaPool, { W: 0, U: 0, B: 0, R: 0, G: 0, C: 0 })
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
663
backend/test/game-rules.test.ts
Normal file
663
backend/test/game-rules.test.ts
Normal file
@@ -0,0 +1,663 @@
|
||||
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('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, 2)
|
||||
|
||||
assert.throws(() => game.activateAbility('p1', source.id, 0), GameError)
|
||||
assert.equal(source.counters.spore, 1)
|
||||
})
|
||||
|
||||
test('a free alternative cost needs a commander and skips the mana cost', () => {
|
||||
const maneuver = card('Flawless Maneuver', {
|
||||
typeLine: 'Instant',
|
||||
manaCost: '{2}{W}',
|
||||
manaValue: 3,
|
||||
abilities: [
|
||||
{
|
||||
kind: 'alternative_cost',
|
||||
costs: [],
|
||||
condition: { kind: 'you_control_commander' },
|
||||
text: 'If you control a commander, you may cast this spell without paying its mana cost.',
|
||||
},
|
||||
{
|
||||
kind: 'spell',
|
||||
effects: [{
|
||||
op: 'grant',
|
||||
target: { kind: 'object', selector: { controller: 'you', types: ['creature'] }, mode: 'each' },
|
||||
keyword: 'Indestructible',
|
||||
duration: 'until_end_of_turn',
|
||||
}],
|
||||
optional: false,
|
||||
text: 'Creatures you control gain indestructible until end of turn.',
|
||||
},
|
||||
],
|
||||
})
|
||||
const game = new Game({
|
||||
seed: 1,
|
||||
startingHandSize: 0,
|
||||
commander: true,
|
||||
players: [
|
||||
{ id: 'p1', deck: [card('Filler')], commander: creature('Test Commander', 2, 2) },
|
||||
{ id: 'p2', deck: [card('Filler')], commander: creature('Other Commander', 2, 2) },
|
||||
],
|
||||
})
|
||||
game.start()
|
||||
const spell = game.addCard('p1', maneuver, 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', spell.id), false)
|
||||
assert.throws(() => game.castSpell('p1', spell.id), GameError)
|
||||
|
||||
game.moveToZone(game.getPlayer('p1').commanderId!, 'battlefield', { silent: true })
|
||||
|
||||
assert.equal(game.canPlayCard('p1', spell.id), true)
|
||||
game.castSpell('p1', spell.id)
|
||||
assert.equal(game.stack.length, 1)
|
||||
assert.deepEqual(game.getPlayer('p1').manaPool, { W: 0, U: 0, B: 0, R: 0, G: 0, C: 0 })
|
||||
})
|
||||
|
||||
test('overload pays the overload cost and widens the spell target', () => {
|
||||
const rift = card('Cyclonic Rift', {
|
||||
typeLine: 'Instant',
|
||||
manaCost: '{U}',
|
||||
manaValue: 1,
|
||||
abilities: [
|
||||
{
|
||||
kind: 'spell',
|
||||
effects: [{
|
||||
op: 'return_to_hand',
|
||||
target: {
|
||||
kind: 'object',
|
||||
selector: { controller: 'opponent', types: ['permanent'], excludeTypes: ['land'] },
|
||||
mode: 'target',
|
||||
},
|
||||
}],
|
||||
optional: false,
|
||||
text: "Return target nonland permanent you don't control to its owner's hand.",
|
||||
},
|
||||
{ kind: 'overload', cost: '{1}{U}', text: 'Overload {1}{U}' },
|
||||
],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const island = game.addCard('p1', land('Island', 'U'), 'battlefield')
|
||||
const wastes = game.addCard('p1', land('Wastes', 'C'), 'battlefield')
|
||||
const ownCreature = game.addCard('p1', creature('Own Bear', 2, 2), 'battlefield')
|
||||
const theirCreature = game.addCard('p2', creature('Their Bear', 2, 2), 'battlefield')
|
||||
const theirArtifact = game.addCard('p2', card('Their Rock', {
|
||||
typeLine: 'Artifact',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
}), 'battlefield')
|
||||
const theirLand = game.addCard('p2', land('Forest', 'G'), 'battlefield')
|
||||
const spell = game.addCard('p1', rift, 'hand')
|
||||
|
||||
game.castSpell('p1', spell.id, { overload: true })
|
||||
|
||||
assert.equal(island.tapped, true)
|
||||
assert.equal(wastes.tapped, true)
|
||||
game.resolveStack()
|
||||
|
||||
assert.ok(game.getPlayer('p2').hand.includes(theirCreature.id))
|
||||
assert.ok(game.getPlayer('p2').hand.includes(theirArtifact.id))
|
||||
assert.ok(game.getPlayer('p2').battlefield.includes(theirLand.id))
|
||||
assert.ok(game.getPlayer('p1').battlefield.includes(ownCreature.id))
|
||||
})
|
||||
|
||||
test('a noncreature cost increase taxes spells but not creatures', () => {
|
||||
const thalia = card('Thalia, Guardian of Thraben', {
|
||||
typeLine: 'Legendary Creature — Human Soldier',
|
||||
power: 2,
|
||||
toughness: 1,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'cost_increase', amount: { kind: 'number', value: 1 }, spellType: 'noncreature' }],
|
||||
text: 'Noncreature spells cost {1} more to cast.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
game.addCard('p1', thalia, 'battlefield')
|
||||
const first = game.addCard('p1', land('Wastes', 'C'), 'battlefield')
|
||||
const instant = game.addCard('p1', spell('Ponder', [
|
||||
{ op: 'draw', player: 'you', amount: { kind: 'number', value: 1 } },
|
||||
], '{1}'), 'hand')
|
||||
const bears = game.addCard('p1', card('Grizzly Bears', {
|
||||
typeLine: 'Creature — Bear',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
manaCost: '{1}',
|
||||
}), 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', instant.id), false)
|
||||
assert.equal(game.canPlayCard('p1', bears.id), true)
|
||||
|
||||
game.addCard('p1', land('Wastes', 'C'), 'battlefield')
|
||||
assert.equal(game.canPlayCard('p1', instant.id), true)
|
||||
game.castSpell('p1', instant.id)
|
||||
assert.equal(first.tapped, true)
|
||||
})
|
||||
|
||||
test('spell cost reductions match the spell type and controller', () => {
|
||||
const baral = card('Baral, Chief of Compliance', {
|
||||
typeLine: 'Legendary Creature — Human Wizard',
|
||||
power: 1,
|
||||
toughness: 3,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{
|
||||
op: 'cost_reduction',
|
||||
target: { kind: 'object', selector: { controller: 'you', types: ['instant', 'sorcery'], match: 'any' }, mode: 'each' },
|
||||
amount: { kind: 'number', value: 1 },
|
||||
}],
|
||||
text: 'Instant and sorcery spells you cast cost {1} less to cast.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
game.addCard('p1', baral, 'battlefield')
|
||||
const wastes = game.addCard('p1', land('Wastes', 'C'), 'battlefield')
|
||||
const sorcery = game.addCard('p1', card('Divination', {
|
||||
typeLine: 'Sorcery',
|
||||
manaCost: '{2}',
|
||||
abilities: [{ kind: 'spell', effects: [{ op: 'draw', player: 'you', amount: { kind: 'number', value: 1 } }], optional: false, text: 'Draw a card.' }],
|
||||
}), 'hand')
|
||||
const bears = game.addCard('p1', card('Grizzly Bears', {
|
||||
typeLine: 'Creature — Bear',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
manaCost: '{2}',
|
||||
}), 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', sorcery.id), true)
|
||||
assert.equal(game.canPlayCard('p1', bears.id), false)
|
||||
|
||||
game.castSpell('p1', sorcery.id)
|
||||
assert.equal(wastes.tapped, true)
|
||||
})
|
||||
|
||||
test('a colored cost reduction removes matching pips and nothing else', () => {
|
||||
const edgewalker = card('Edgewalker', {
|
||||
typeLine: 'Creature — Human Cleric',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{
|
||||
op: 'cost_reduction',
|
||||
target: { kind: 'object', selector: { controller: 'you', types: ['creature'], match: 'any', subtypes: ['cleric', 'soldier'] }, mode: 'each' },
|
||||
symbols: '{W}{B}',
|
||||
}],
|
||||
text: 'Cleric spells you cast cost {W}{B} less to cast.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
game.addCard('p1', edgewalker, 'battlefield')
|
||||
game.addCard('p1', land('Plains', 'W'), 'battlefield')
|
||||
game.addCard('p1', land('Swamp', 'B'), 'battlefield')
|
||||
const cleric = game.addCard('p1', card('Cleric Test', {
|
||||
typeLine: 'Creature — Human Cleric',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
colors: ['W', 'B'],
|
||||
manaCost: '{1}{W}{B}',
|
||||
manaValue: 3,
|
||||
}), 'hand')
|
||||
const soldier = game.addCard('p1', card('Soldier Test', {
|
||||
typeLine: 'Creature — Human Soldier',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
colors: ['W', 'B'],
|
||||
manaCost: '{1}{W}{B}',
|
||||
manaValue: 3,
|
||||
}), 'hand')
|
||||
const bear = game.addCard('p1', card('Bear Test', {
|
||||
typeLine: 'Creature — Bear',
|
||||
power: 2,
|
||||
toughness: 2,
|
||||
colors: ['W', 'B'],
|
||||
manaCost: '{1}{W}{B}',
|
||||
manaValue: 3,
|
||||
}), 'hand')
|
||||
|
||||
assert.equal(game.canPlayCard('p1', cleric.id), true)
|
||||
assert.equal(game.canPlayCard('p1', soldier.id), true)
|
||||
assert.equal(game.canPlayCard('p1', bear.id), false)
|
||||
game.castSpell('p1', cleric.id)
|
||||
})
|
||||
|
||||
test('Linvala stops only opponents from activating creature abilities', () => {
|
||||
const linvala = card('Linvala, Keeper of Silence', {
|
||||
typeLine: 'Legendary Creature — Angel',
|
||||
power: 3,
|
||||
toughness: 4,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{
|
||||
op: 'restriction',
|
||||
target: { kind: 'object', selector: { controller: 'opponent', types: ['creature'] }, mode: 'each' },
|
||||
rule: 'cant_activate',
|
||||
}],
|
||||
text: "Activated abilities of creatures your opponents control can't be activated.",
|
||||
}],
|
||||
})
|
||||
const drawAbility = {
|
||||
kind: 'activated' as const,
|
||||
costs: [],
|
||||
effects: [{ op: 'draw' as const, player: 'you' as const, amount: { kind: 'number' as const, value: 1 } }],
|
||||
optional: false,
|
||||
text: 'Draw a card.',
|
||||
}
|
||||
const game = setup([{ id: 'p1', deck: [card('Filler'), card('Filler')] }, { id: 'p2' }])
|
||||
game.start()
|
||||
game.addCard('p1', linvala, 'battlefield')
|
||||
const own = game.addCard('p1', creature('Own Cleric', 1, 1, [drawAbility]), 'battlefield')
|
||||
const theirs = game.addCard('p2', creature('Their Cleric', 1, 1, [drawAbility]), 'battlefield')
|
||||
|
||||
game.activateAbility('p1', own.id, 0)
|
||||
assert.equal(game.stack.length, 1)
|
||||
|
||||
game.resolveStack()
|
||||
game.pass('p1')
|
||||
assert.throws(() => game.activateAbility('p2', theirs.id, 0), /cannot activate/)
|
||||
})
|
||||
|
||||
test('Aven Mindcensor limits an opponent search to the top four cards', () => {
|
||||
const aven = card('Aven Mindcensor', {
|
||||
typeLine: 'Creature — Bird Wizard',
|
||||
power: 2,
|
||||
toughness: 1,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'search_restriction', who: 'opponent', amount: { kind: 'number', value: 4 } }],
|
||||
text: 'If an opponent would search a library, that player searches the top four cards of that library instead.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2', deck: [] }])
|
||||
game.start()
|
||||
game.addCard('p1', aven, 'battlefield')
|
||||
for (const name of ['Forest', 'Island', 'Mountain', 'Plains', 'Swamp', 'Wastes']) {
|
||||
game.addCard('p2', land(name, 'C'), 'library')
|
||||
}
|
||||
const topFour = game.getPlayer('p2').library.slice(0, 4)
|
||||
const growth = game.addCard('p2', spell('Rampant Growth', [{
|
||||
op: 'search',
|
||||
player: 'you',
|
||||
selector: { types: ['land'] },
|
||||
amount: { kind: 'number', value: 1 },
|
||||
destination: 'battlefield',
|
||||
shuffle: true,
|
||||
}]), 'hand')
|
||||
|
||||
game.pass('p1')
|
||||
game.castSpell('p2', growth.id)
|
||||
game.resolveStack()
|
||||
|
||||
assert.deepEqual(
|
||||
game.searchCandidates('p2').map(object => object.id).sort(),
|
||||
[...topFour].sort(),
|
||||
)
|
||||
})
|
||||
|
||||
test('Rest in Peace exiles all graveyards and replaces later graveyard moves', () => {
|
||||
const rest = card('Rest in Peace', {
|
||||
typeLine: 'Enchantment',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [
|
||||
{
|
||||
kind: 'triggered',
|
||||
event: { kind: 'enters', subject: { kind: 'self' } },
|
||||
effects: [{ op: 'exile_graveyards' }],
|
||||
optional: false,
|
||||
text: 'When this enchantment enters, exile all graveyards.',
|
||||
},
|
||||
{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'exile_instead_of_graveyard' }],
|
||||
text: 'If a card or token would be put into a graveyard from anywhere, exile it instead.',
|
||||
},
|
||||
],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const oldSpell = game.addCard('p1', spell('Old Spell', []), 'graveyard')
|
||||
const oldCreature = game.addCard('p2', creature('Old Bear', 2, 2), 'graveyard')
|
||||
const doomed = game.addCard('p1', creature('Doomed Bear', 2, 2), 'battlefield')
|
||||
|
||||
game.addCard('p1', rest, 'battlefield', { silent: false })
|
||||
game.resolveStack()
|
||||
|
||||
assert.ok(game.getPlayer('p1').exile.includes(oldSpell.id))
|
||||
assert.ok(game.getPlayer('p2').exile.includes(oldCreature.id))
|
||||
assert.deepEqual(game.getPlayer('p1').graveyard, [])
|
||||
assert.deepEqual(game.getPlayer('p2').graveyard, [])
|
||||
|
||||
game.moveToZone(doomed, 'graveyard')
|
||||
|
||||
assert.equal(doomed.zone, 'exile')
|
||||
assert.ok(game.getPlayer('p1').exile.includes(doomed.id))
|
||||
})
|
||||
|
||||
test("Leyline of the Void exiles only opponents' cards and starts on the battlefield", () => {
|
||||
const leyline = card('Leyline of the Void', {
|
||||
typeLine: 'Enchantment',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [
|
||||
{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'restriction', target: { kind: 'self' }, rule: 'begin_on_battlefield' }],
|
||||
text: 'If this card is in your opening hand, you may begin the game with it on the battlefield.',
|
||||
},
|
||||
{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'exile_instead_of_graveyard', owner: 'opponent' }],
|
||||
text: "If a card would be put into an opponent's graveyard from anywhere, exile it instead.",
|
||||
},
|
||||
],
|
||||
})
|
||||
const game = new Game({
|
||||
seed: 3,
|
||||
startingHandSize: 0,
|
||||
players: [{ id: 'p1', deck: [card('Filler')] }, { id: 'p2', deck: [card('Filler')] }],
|
||||
})
|
||||
const leylineObject = game.addCard('p1', leyline, 'hand')
|
||||
game.start()
|
||||
|
||||
assert.ok(game.getPlayer('p1').battlefield.includes(leylineObject.id))
|
||||
assert.ok(!game.getPlayer('p1').hand.includes(leylineObject.id))
|
||||
|
||||
const theirs = game.addCard('p2', creature('Their Bear', 2, 2), 'battlefield')
|
||||
const own = game.addCard('p1', creature('Own Bear', 2, 2), 'battlefield')
|
||||
game.moveToZone(theirs, 'graveyard')
|
||||
game.moveToZone(own, 'graveyard')
|
||||
|
||||
assert.equal(theirs.zone, 'exile')
|
||||
assert.equal(own.zone, 'graveyard')
|
||||
})
|
||||
|
||||
test('Field of the Dead makes a Zombie at seven differently named lands', () => {
|
||||
const field = card('Field of the Dead', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'triggered',
|
||||
event: {
|
||||
kind: 'enters',
|
||||
subject: {
|
||||
kind: 'object',
|
||||
selector: { other: true, controller: 'you', types: ['land'] },
|
||||
mode: 'each',
|
||||
includeSelf: true,
|
||||
},
|
||||
},
|
||||
condition: { kind: 'distinct_names', selector: { types: ['land'] }, atLeast: 7 },
|
||||
effects: [{
|
||||
op: 'create_token',
|
||||
player: 'you',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
token: {
|
||||
types: ['creature'],
|
||||
subtypes: ['zombie'],
|
||||
colors: ['B'],
|
||||
power: { kind: 'number', value: 2 },
|
||||
toughness: { kind: 'number', value: 2 },
|
||||
},
|
||||
}],
|
||||
optional: false,
|
||||
text: 'Whenever this or another land you control enters, if you control seven or more lands with different names, create a 2/2 black Zombie creature token.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
game.addCard('p1', field, 'battlefield')
|
||||
for (const name of ['Forest', 'Island', 'Mountain', 'Plains']) {
|
||||
game.addCard('p1', land(name, 'C'), 'battlefield')
|
||||
}
|
||||
|
||||
game.addCard('p1', land('Swamp', 'C'), 'battlefield', { silent: false })
|
||||
game.resolveStack()
|
||||
assert.equal(game.getPlayer('p1').battlefield
|
||||
.map(id => game.getObject(id)).filter(object => object.isToken).length, 0)
|
||||
|
||||
game.addCard('p1', land('Wastes', 'C'), 'battlefield', { silent: false })
|
||||
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.typeLine.toLowerCase().includes('zombie'), true)
|
||||
})
|
||||
|
||||
test('predefined Treasure, Gold, Powerstone, Heartwood, and Vibranium tokens produce mana', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
|
||||
const treasure = makeToken(game, { types: ['artifact'], subtypes: ['treasure'], colors: [] })
|
||||
assert.equal(treasure.definition.name, 'Treasure')
|
||||
game.activateAbility('p1', treasure.id, 0, { manaColor: 'G' })
|
||||
assert.equal(game.getPlayer('p1').manaPool.G, 1)
|
||||
assert.equal(treasure.zone, 'graveyard')
|
||||
|
||||
const gold = makeToken(game, { types: ['artifact'], subtypes: ['gold'], colors: [] })
|
||||
game.activateAbility('p1', gold.id, 0, { manaColor: 'R' })
|
||||
assert.equal(game.getPlayer('p1').manaPool.R, 1)
|
||||
|
||||
const powerstone = makeToken(game, { types: ['artifact'], subtypes: ['powerstone'], colors: [] })
|
||||
game.activateAbility('p1', powerstone.id, 0)
|
||||
assert.equal(game.getPlayer('p1').manaPool.C, 1)
|
||||
|
||||
const heartwood = makeToken(game, { types: ['artifact'], subtypes: ['heartwood'], colors: [] })
|
||||
game.activateAbility('p1', heartwood.id, 0, { manaColor: 'G' })
|
||||
assert.equal(game.getPlayer('p1').manaPool.G, 2)
|
||||
|
||||
const vibranium = makeToken(game, { types: ['artifact'], subtypes: ['vibranium'], colors: [] })
|
||||
assert.equal(game.hasKeyword(vibranium, 'Indestructible'), true)
|
||||
game.activateAbility('p1', vibranium.id, 0)
|
||||
assert.equal(game.getPlayer('p1').manaPool.C, 2)
|
||||
})
|
||||
|
||||
test('predefined Food, Clue, Blood, and Shard tokens sacrifice for their effects', () => {
|
||||
const game = setup([{
|
||||
id: 'p1',
|
||||
deck: Array.from({ length: 6 }, (_, index) => card(`Filler ${index}`)),
|
||||
}, { id: 'p2' }])
|
||||
game.start()
|
||||
|
||||
const food = makeToken(game, { types: ['artifact'], subtypes: ['food'], colors: [] })
|
||||
addMana(game, 'G', 'G')
|
||||
game.activateAbility('p1', food.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(game.getPlayer('p1').life, 43)
|
||||
assert.equal(food.zone, 'graveyard')
|
||||
|
||||
game.addCard('p1', card('Discard Fodder'), 'hand')
|
||||
const discard = game.getObject(game.getPlayer('p1').hand[0])
|
||||
const handAfterFood = game.getPlayer('p1').hand.length
|
||||
const blood = makeToken(game, { types: ['artifact'], subtypes: ['blood'], colors: [] })
|
||||
addMana(game, 'G')
|
||||
game.activateAbility('p1', blood.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(discard.zone, 'graveyard')
|
||||
assert.equal(game.getPlayer('p1').hand.length, handAfterFood)
|
||||
|
||||
const clue = makeToken(game, { types: ['artifact'], subtypes: ['clue'], colors: [] })
|
||||
addMana(game, 'G', 'G')
|
||||
game.activateAbility('p1', clue.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(game.getPlayer('p1').hand.length, handAfterFood + 1)
|
||||
|
||||
const shard = makeToken(game, { types: ['enchantment'], subtypes: ['shard'], colors: [] })
|
||||
addMana(game, 'G', 'G')
|
||||
game.activateAbility('p1', shard.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(game.getPlayer('p1').hand.length, handAfterFood + 2)
|
||||
})
|
||||
|
||||
test('Walker and Incubator tokens use their predefined characteristics', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
|
||||
const walker = makeToken(game, { types: ['artifact'], subtypes: ['walker'], colors: [] })
|
||||
assert.equal(walker.definition.name, 'Walker')
|
||||
assert.equal(game.getPower(walker), 2)
|
||||
assert.equal(game.getToughness(walker), 2)
|
||||
assert.deepEqual(walker.definition.colors, ['B'])
|
||||
|
||||
const maker = game.addCard('p1', card('Incubator Maker', {
|
||||
typeLine: 'Artifact',
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'keyword_action', action: 'incubate', amount: { kind: 'number', value: 2 } }],
|
||||
optional: false,
|
||||
text: '{T}: Incubate 2.',
|
||||
}],
|
||||
}), 'battlefield')
|
||||
game.activateAbility('p1', maker.id, 0)
|
||||
game.resolveStack()
|
||||
|
||||
const incubator = game.getPlayer('p1').battlefield
|
||||
.map(id => game.getObject(id))
|
||||
.find(object => object.isToken && object.definition.name === 'Incubator')
|
||||
assert.ok(incubator)
|
||||
assert.equal(incubator.counters['+1/+1'], 2)
|
||||
|
||||
addMana(game, 'G', 'G')
|
||||
game.activateAbility('p1', incubator.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(incubator.definition.name, 'Phyrexian')
|
||||
assert.equal(game.getPower(incubator), 2)
|
||||
assert.equal(game.getToughness(incubator), 2)
|
||||
})
|
||||
|
||||
test('predefined Map, Lander, and Mutagen tokens use their activated abilities', () => {
|
||||
const mapGame = setup([{ id: 'p1', deck: [card('Filler')] }, { id: 'p2' }])
|
||||
mapGame.start()
|
||||
const topCard = mapGame.addCard('p1', card('Top Card'), 'library')
|
||||
const bear = mapGame.addCard('p1', creature('Bear', 2, 2), 'battlefield')
|
||||
addMana(mapGame, 'G')
|
||||
const map = makeToken(mapGame, { types: ['artifact'], subtypes: ['map'], colors: [] })
|
||||
mapGame.activateAbility('p1', map.id, 0, { targets: [{ kind: 'object', id: bear.id }] })
|
||||
mapGame.resolveStack()
|
||||
assert.equal(bear.counters['+1/+1'], 1)
|
||||
assert.equal(topCard.zone, 'graveyard')
|
||||
|
||||
addMana(mapGame, 'G')
|
||||
const mutagen = makeToken(mapGame, { types: ['artifact'], subtypes: ['mutagen'], colors: [] })
|
||||
mapGame.activateAbility('p1', mutagen.id, 0, { targets: [{ kind: 'object', id: bear.id }] })
|
||||
mapGame.resolveStack()
|
||||
assert.equal(bear.counters['+1/+1'], 2)
|
||||
|
||||
const landGame = setup([{ id: 'p1', deck: [card('Filler')] }, { id: 'p2' }])
|
||||
landGame.start()
|
||||
const forest = landGame.addCard('p1', land('Forest', 'G'), 'library')
|
||||
addMana(landGame, 'G', 'G')
|
||||
const lander = makeToken(landGame, { types: ['artifact'], subtypes: ['lander'], colors: [] })
|
||||
landGame.activateAbility('p1', lander.id, 0)
|
||||
landGame.resolveStack()
|
||||
assert.equal(forest.zone, 'battlefield')
|
||||
assert.equal(forest.tapped, true)
|
||||
})
|
||||
204
backend/test/game-search.test.ts
Normal file
204
backend/test/game-search.test.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
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('a search that shuffles and puts the chosen card on top leaves it on top', () => {
|
||||
const bear = creature('Bear', 2, 2)
|
||||
const elf = creature('Elf', 1, 1)
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const bearObject = game.addCard('p1', bear, 'library')
|
||||
const elfObject = game.addCard('p1', elf, 'library')
|
||||
const worldlyTutor = game.addCard('p1', spell('Worldly Tutor', [{
|
||||
op: 'search',
|
||||
player: 'you',
|
||||
selector: { types: ['creature'] },
|
||||
amount: { kind: 'number', value: 1 },
|
||||
destination: 'top',
|
||||
tapped: false,
|
||||
shuffle: true,
|
||||
}]), 'hand')
|
||||
|
||||
game.castSpell('p1', worldlyTutor.id)
|
||||
game.resolveStack()
|
||||
assert.equal(game.pendingSearch?.playerId, 'p1')
|
||||
|
||||
game.chooseSearchCard('p1', elfObject.id)
|
||||
|
||||
assert.equal(game.getPlayer('p1').library[0], elfObject.id)
|
||||
assert.ok(game.getPlayer('p1').library.includes(bearObject.id))
|
||||
})
|
||||
|
||||
test('as-enters creature type choices pause until the controller picks', () => {
|
||||
const cavern = card('Cavern of Souls', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'choose', what: 'creature_type' }],
|
||||
text: 'As this enters, choose a creature type.',
|
||||
}],
|
||||
})
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const object = game.addCard('p1', cavern, 'hand')
|
||||
game.playLand('p1', object.id)
|
||||
|
||||
assert.equal(game.pendingChoice?.sourceId, object.id)
|
||||
assert.equal(game.pendingChoice?.playerId, 'p1')
|
||||
assert.throws(() => game.pass('p1'), GameError)
|
||||
assert.throws(() => game.chooseCreatureType('p2', 'Elf'), GameError)
|
||||
assert.throws(() => game.chooseCreatureType('p1', 'Not A Type'), GameError)
|
||||
|
||||
game.chooseCreatureType('p1', 'elf')
|
||||
assert.equal(object.chosenCreatureType, 'Elf')
|
||||
assert.equal(game.pendingChoice, undefined)
|
||||
|
||||
game.moveToZone(object, 'graveyard')
|
||||
assert.equal(object.chosenCreatureType, undefined)
|
||||
game.moveToZone(object, 'battlefield', { silent: false })
|
||||
assert.equal(game.pendingChoice?.sourceId, object.id)
|
||||
})
|
||||
|
||||
test('a split search sends the first pick to the battlefield and the next to hand', () => {
|
||||
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 cultivate = game.addCard('p1', spell('Cultivate', [{
|
||||
op: 'search',
|
||||
player: 'you',
|
||||
selector: { types: ['land'], supertypes: ['basic'] },
|
||||
amount: { kind: 'number', value: 2 },
|
||||
destination: 'battlefield',
|
||||
tapped: true,
|
||||
shuffle: true,
|
||||
split: { destination: 'hand', tapped: false },
|
||||
}]), 'hand')
|
||||
|
||||
game.castSpell('p1', cultivate.id)
|
||||
game.resolveStack()
|
||||
|
||||
game.chooseSearchCard('p1', islandObject.id)
|
||||
assert.equal(game.pendingSearch?.destination, 'hand')
|
||||
game.chooseSearchCard('p1', forestObject.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').hand.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)
|
||||
})
|
||||
|
||||
function shockLand(): CardDefinition {
|
||||
return card('Steam Vents', {
|
||||
typeLine: 'Land — Island Mountain',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'enters_tapped', target: { kind: 'self' }, unlessCost: { kind: 'pay_life', amount: { kind: 'number', value: 2 } } }],
|
||||
text: "As this land enters, you may pay 2 life. If you don't, it enters tapped.",
|
||||
}, {
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: ['{U}', '{R}'], oneOf: true } }],
|
||||
optional: false,
|
||||
text: '{T}: Add {U} or {R}.',
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
test('a land can pay life to enter untapped or enter tapped', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const vents = game.addCard('p1', shockLand(), 'hand')
|
||||
game.playLand('p1', vents.id)
|
||||
|
||||
assert.equal(vents.tapped, false)
|
||||
assert.equal(game.pendingChoice?.what, 'pay_cost')
|
||||
assert.equal(game.pendingChoice?.amount, 2)
|
||||
assert.throws(() => game.chooseCostPayment('p2', true), GameError)
|
||||
|
||||
game.chooseCostPayment('p1', true)
|
||||
assert.equal(game.getPlayer('p1').life, 38)
|
||||
assert.equal(vents.tapped, false)
|
||||
assert.equal(game.pendingChoice, undefined)
|
||||
|
||||
const second = game.addCard('p1', shockLand(), 'hand')
|
||||
game.moveToZone(second, 'battlefield', { controller: 'p1', silent: false })
|
||||
assert.equal(game.pendingChoice?.what, 'pay_cost')
|
||||
|
||||
game.chooseCostPayment('p1', false)
|
||||
assert.equal(second.tapped, true)
|
||||
assert.equal(game.getPlayer('p1').life, 38)
|
||||
})
|
||||
|
||||
test('a choice mana ability adds only the color the spell needs', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const vents = game.addCard('p1', shockLand(), 'battlefield')
|
||||
const red = game.addCard('p1', spell('Red Spell', [], '{R}'), 'hand')
|
||||
|
||||
game.castSpell('p1', red.id)
|
||||
|
||||
assert.equal(vents.tapped, true)
|
||||
assert.equal(game.getPlayer('p1').manaPool.R, 0)
|
||||
assert.equal(game.getPlayer('p1').manaPool.U, 0)
|
||||
})
|
||||
520
backend/test/game-targets.test.ts
Normal file
520
backend/test/game-targets.test.ts
Normal file
@@ -0,0 +1,520 @@
|
||||
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)
|
||||
})
|
||||
224
backend/test/game-tokens.test.ts
Normal file
224
backend/test/game-tokens.test.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
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('token doublers stack and only apply to their controller', () => {
|
||||
const goblin: TokenSpec = {
|
||||
types: ['creature'],
|
||||
subtypes: ['goblin'],
|
||||
colors: ['R'],
|
||||
power: { kind: 'number', value: 1 },
|
||||
toughness: { kind: 'number', value: 1 },
|
||||
}
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const p1Maker = game.addCard('p1', tokenMaker('P1 Maker', goblin), 'battlefield')
|
||||
game.addCard('p1', tokenDoubler('Parallel Lives', 2), 'battlefield')
|
||||
game.addCard('p1', tokenDoubler('Anointed Procession', 2), 'battlefield')
|
||||
const p2Maker = game.addCard('p2', tokenMaker('P2 Maker', goblin), 'battlefield')
|
||||
game.addCard('p2', tokenDoubler('Adrix and Nev', 2), 'battlefield')
|
||||
const tokensOf = (playerId: string): number => game.getPlayer(playerId).battlefield
|
||||
.map(id => game.getObject(id)).filter(object => object.isToken).length
|
||||
|
||||
game.activateAbility('p1', p1Maker.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(tokensOf('p1'), 4)
|
||||
assert.equal(tokensOf('p2'), 0)
|
||||
|
||||
game.endTurn('p1')
|
||||
game.activateAbility('p2', p2Maker.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(tokensOf('p2'), 2)
|
||||
})
|
||||
|
||||
test('Primal Vigor doubles tokens for every player', () => {
|
||||
const goblin: TokenSpec = {
|
||||
types: ['creature'],
|
||||
subtypes: ['goblin'],
|
||||
colors: ['R'],
|
||||
power: { kind: 'number', value: 1 },
|
||||
toughness: { kind: 'number', value: 1 },
|
||||
}
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const p1Maker = game.addCard('p1', tokenMaker('P1 Maker', goblin), 'battlefield')
|
||||
game.addCard('p1', tokenDoubler('Primal Vigor', 2, 'each_player'), 'battlefield')
|
||||
const p2Maker = game.addCard('p2', tokenMaker('P2 Maker', goblin), 'battlefield')
|
||||
const tokensOf = (playerId: string): number => game.getPlayer(playerId).battlefield
|
||||
.map(id => game.getObject(id)).filter(object => object.isToken).length
|
||||
|
||||
game.activateAbility('p1', p1Maker.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(tokensOf('p1'), 2)
|
||||
|
||||
game.endTurn('p1')
|
||||
game.activateAbility('p2', p2Maker.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(tokensOf('p2'), 2)
|
||||
})
|
||||
|
||||
test('Ojer Taq triples creature tokens but not other tokens', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const creatureMaker = game.addCard('p1', tokenMaker('Creature Maker', {
|
||||
types: ['creature'],
|
||||
subtypes: ['goblin'],
|
||||
colors: ['R'],
|
||||
power: { kind: 'number', value: 1 },
|
||||
toughness: { kind: 'number', value: 1 },
|
||||
}), 'battlefield')
|
||||
const treasureMaker = game.addCard('p1', tokenMaker('Treasure Maker', {
|
||||
types: ['artifact'], subtypes: ['treasure'], colors: [],
|
||||
}), 'battlefield')
|
||||
game.addCard('p1', tokenDoubler('Ojer Taq', 3, 'you', true), 'battlefield')
|
||||
|
||||
game.activateAbility('p1', creatureMaker.id, 0)
|
||||
game.resolveStack()
|
||||
game.activateAbility('p1', treasureMaker.id, 0)
|
||||
game.resolveStack()
|
||||
|
||||
const tokens = game.getPlayer('p1').battlefield.map(id => game.getObject(id)).filter(object => object.isToken)
|
||||
assert.equal(tokens.filter(object => object.definition.typeLine.toLowerCase().includes('creature')).length, 3)
|
||||
assert.equal(tokens.filter(object => object.definition.typeLine.toLowerCase().includes('treasure')).length, 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('Junk exiles the top card and lets its controller cast it that turn', () => {
|
||||
const game = setup([{ id: 'p1', deck: [card('Filler')] }, { id: 'p2' }])
|
||||
game.start()
|
||||
const exiled = game.addCard('p1', spell('Top Spell', [], ''), 'library')
|
||||
const junk = makeToken(game, { types: ['artifact'], subtypes: ['junk'], colors: [] })
|
||||
|
||||
game.activateAbility('p1', junk.id, 0)
|
||||
game.resolveStack()
|
||||
assert.equal(exiled.zone, 'exile')
|
||||
assert.equal(game.canPlayCard('p1', exiled.id), true)
|
||||
|
||||
game.castSpell('p1', exiled.id)
|
||||
game.resolveStack()
|
||||
assert.equal(exiled.zone, 'graveyard')
|
||||
})
|
||||
|
||||
test('Role tokens attach to the target creature and apply their abilities', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const bear = game.addCard('p1', creature('Bear', 2, 2), 'battlefield')
|
||||
const maker = game.addCard('p1', roleMaker('Monster'), 'battlefield')
|
||||
|
||||
assert.deepEqual(game.legalTargetsForAbility('p1', maker.id, 0), [{ kind: 'object', id: bear.id }])
|
||||
game.activateAbility('p1', maker.id, 0, { targets: [{ kind: 'object', id: bear.id }] })
|
||||
game.resolveStack()
|
||||
|
||||
const role = game.getPlayer('p1').battlefield
|
||||
.map(id => game.getObject(id))
|
||||
.find(object => object.isToken)
|
||||
assert.ok(role)
|
||||
assert.equal(role.definition.name, 'Monster Role')
|
||||
assert.match(role.definition.typeLine.toLowerCase(), /enchantment — aura role/)
|
||||
assert.equal(role.attachedTo, bear.id)
|
||||
assert.equal(game.getPower(bear), 3)
|
||||
assert.equal(game.getToughness(bear), 3)
|
||||
assert.equal(game.hasKeyword(bear, 'Trample'), true)
|
||||
})
|
||||
|
||||
test('Young Hero Role triggers when the enchanted creature attacks with toughness 3 or less', () => {
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
const bear = game.addCard('p1', creature('Bear', 2, 2), 'battlefield')
|
||||
game.start()
|
||||
const maker = game.addCard('p1', roleMaker('Young Hero'), 'battlefield')
|
||||
game.activateAbility('p1', maker.id, 0, { targets: [{ kind: 'object', id: bear.id }] })
|
||||
game.resolveStack()
|
||||
|
||||
game.declareAttackers('p1', [{ attackerId: bear.id, defenderId: 'p2' }])
|
||||
game.resolveStack()
|
||||
assert.equal(bear.counters['+1/+1'], 1)
|
||||
assert.equal(game.getPower(bear), 3)
|
||||
})
|
||||
|
||||
test('token creation replacement swaps the created token', () => {
|
||||
const food: TokenSpec = {
|
||||
name: 'Food',
|
||||
types: ['artifact'],
|
||||
subtypes: ['food'],
|
||||
colors: [],
|
||||
abilities: [{ kind: 'activated', costs: [{ kind: 'tap' }, { kind: 'mana', cost: '{2}' }], effects: [] }],
|
||||
}
|
||||
const treasure: TokenSpec = { name: 'Treasure', types: ['artifact'], subtypes: ['treasure'], colors: [] }
|
||||
const game = setup([{ id: 'p1' }, { id: 'p2' }])
|
||||
game.start()
|
||||
const maker = game.addCard('p1', tokenMaker('Food Maker', food), 'battlefield')
|
||||
game.addCard('p1', card('Token Replacer', {
|
||||
typeLine: 'Enchantment',
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'token_replacement', from: ['food'], tokens: [food, treasure], who: 'you' }],
|
||||
text: 'If you would create a Food token, instead create a Food token and a Treasure token.',
|
||||
}],
|
||||
}), 'battlefield')
|
||||
|
||||
game.activateAbility('p1', maker.id, 0)
|
||||
game.resolveStack()
|
||||
const tokens = game.getPlayer('p1').battlefield.map(id => game.getObject(id)).filter(object => object.isToken)
|
||||
assert.deepEqual(tokens.map(token => token.definition.name).sort(), ['Food', 'Treasure'])
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
159
backend/test/helpers.ts
Normal file
159
backend/test/helpers.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
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'
|
||||
|
||||
export function card(name: string, overrides: Partial<CardDefinition> = {}): CardDefinition {
|
||||
return {
|
||||
oracleId: name.toLowerCase().replace(/[^a-z0-9]+/g, '-'),
|
||||
name,
|
||||
typeLine: 'Creature — Test',
|
||||
manaValue: 0,
|
||||
colors: [],
|
||||
abilities: [],
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
export function creature(name: string, power: number, toughness: number, abilities: Ability[] = []): CardDefinition {
|
||||
return card(name, { typeLine: 'Creature — Test', power, toughness, abilities })
|
||||
}
|
||||
|
||||
export function spell(name: string, effects: Effect[], manaCost = ''): CardDefinition {
|
||||
return card(name, { typeLine: 'Instant', manaCost, manaValue: 0, abilities: [{ kind: 'spell', effects, optional: false, text: name }] })
|
||||
}
|
||||
|
||||
export 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}}.`,
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
export function exoticOrchard(): CardDefinition {
|
||||
return card('Exotic Orchard', {
|
||||
typeLine: 'Land',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{
|
||||
op: 'add_mana',
|
||||
player: 'you',
|
||||
mana: {
|
||||
kind: 'any_color',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
oneColor: false,
|
||||
restriction: 'a land an opponent controls could produce',
|
||||
},
|
||||
}],
|
||||
optional: false,
|
||||
text: '{T}: Add one mana of any color that a land an opponent controls could produce.',
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
export 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
|
||||
}
|
||||
|
||||
export function tokenMaker(name: string, token: TokenSpec): CardDefinition {
|
||||
return card(name, {
|
||||
typeLine: 'Artifact',
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'create_token', player: 'you', amount: { kind: 'number', value: 1 }, token }],
|
||||
optional: false,
|
||||
text: '{T}: Create a token.',
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
export function tokenDoubler(
|
||||
name: string,
|
||||
multiplier: number,
|
||||
who: 'you' | 'each_player' = 'you',
|
||||
creatureOnly = false,
|
||||
): CardDefinition {
|
||||
return card(name, {
|
||||
typeLine: 'Enchantment',
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'token_multiplier', multiplier, who, ...(creatureOnly ? { creatureOnly: true } : {}) }],
|
||||
text: 'Token replacement.',
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
export function makeToken(game: Game, token: TokenSpec, playerId = 'p1'): GameObject {
|
||||
const maker = game.addCard(playerId, tokenMaker('Token Maker', token), 'battlefield')
|
||||
game.activateAbility(playerId, maker.id, 0)
|
||||
game.resolveStack()
|
||||
const tokens = game.getPlayer(playerId).battlefield
|
||||
.map(id => game.getObject(id))
|
||||
.filter(object => object.isToken)
|
||||
return tokens[tokens.length - 1]
|
||||
}
|
||||
|
||||
export function addMana(game: Game, ...symbols: string[]): void {
|
||||
for (const symbol of symbols) {
|
||||
const source = game.addCard('p1', land('Mana Land', symbol), 'battlefield')
|
||||
game.activateAbility('p1', source.id, 0)
|
||||
}
|
||||
}
|
||||
|
||||
export function roleMaker(name: string): CardDefinition {
|
||||
return card(`${name} Role Maker`, {
|
||||
typeLine: 'Enchantment',
|
||||
abilities: [{
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{
|
||||
op: 'create_token',
|
||||
player: 'you',
|
||||
amount: { kind: 'number', value: 1 },
|
||||
token: { name, types: ['enchantment'], subtypes: ['aura', 'role'], colors: [] },
|
||||
attachTo: { kind: 'object', selector: { types: ['creature'], controller: 'you' }, mode: 'target' },
|
||||
}],
|
||||
optional: false,
|
||||
text: `{T}: Create a ${name} Role token attached to target creature you control.`,
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export function shockLand(): CardDefinition {
|
||||
return card('Steam Vents', {
|
||||
typeLine: 'Land — Island Mountain',
|
||||
power: undefined,
|
||||
toughness: undefined,
|
||||
abilities: [{
|
||||
kind: 'static',
|
||||
effects: [{ op: 'enters_tapped', target: { kind: 'self' }, unlessCost: { kind: 'pay_life', amount: { kind: 'number', value: 2 } } }],
|
||||
text: "As this land enters, you may pay 2 life. If you don't, it enters tapped.",
|
||||
}, {
|
||||
kind: 'activated',
|
||||
costs: [{ kind: 'tap' }],
|
||||
effects: [{ op: 'add_mana', player: 'you', mana: { kind: 'symbols', symbols: ['{U}', '{R}'], oneOf: true } }],
|
||||
optional: false,
|
||||
text: '{T}: Add {U} or {R}.',
|
||||
}],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user