Files
commander/backend/test/effects.test.ts
2026-11-04 17:10:22 +01:00

920 lines
37 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { compileText } from '../src/effects/compile.js'
import { formatAbility } from '../src/effects/format.js'
import { parseCardSource } from '../src/effects/source.js'
import type { Ability, Effect, Target } from '../src/effects/types.js'
function creature(text: string, keywords: string[] = []): Ability[] {
return compileText(text, 'Test Creature', keywords, 'Creature — Human')
}
function spell(text: string): Ability[] {
return compileText(text, 'Test Spell', [], 'Instant')
}
test('keyword lines become keyword abilities', () => {
const [ability] = creature('Flying, vigilance, lifelink')
assert.equal(ability.kind, 'keywords')
if (ability.kind !== 'keywords') return
assert.deepEqual(ability.keywords.map(keyword => keyword.keyword), ['Flying', 'Vigilance', 'Lifelink'])
})
test('enters trigger with a draw effect', () => {
const [ability] = creature('When this creature enters, draw a card.')
assert.equal(ability.kind, 'triggered')
if (ability.kind !== 'triggered') return
assert.deepEqual(ability.event, { kind: 'enters', subject: { kind: 'self' } })
assert.equal(ability.effects.length, 1)
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')
if (ability.kind !== 'activated') return
assert.deepEqual(ability.costs, [{ kind: 'tap' }])
assert.deepEqual(ability.effects[0], {
op: 'add_mana',
player: 'you',
mana: { kind: 'symbols', symbols: ['{G}'] },
})
})
test('"or" in a mana ability marks the symbols as a choice', () => {
const [ability] = creature('{T}: Add {G} or {U}.')
assert.equal(ability.kind, 'activated')
if (ability.kind !== 'activated') return
assert.deepEqual(ability.effects[0], {
op: 'add_mana',
player: 'you',
mana: { kind: 'symbols', symbols: ['{G}', '{U}'], oneOf: true },
})
})
test('paying life to enter untapped compiles to an enters-tapped replacement', () => {
const [ability] = compileText(
"As this land enters, you may pay 2 life. If you don't, it enters tapped.",
'Steam Vents',
[],
'Land — Island Mountain',
)
assert.equal(ability.kind, 'static')
if (ability.kind !== 'static') return
assert.deepEqual(ability.effects[0], {
op: 'enters_tapped',
target: { kind: 'self' },
unlessCost: { kind: 'pay_life', amount: { kind: 'number', value: 2 } },
})
})
test('search effect keeps destination, tapped, and shuffle', () => {
const [ability] = spell('Search your library for a basic land card, put it onto the battlefield tapped, then shuffle.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
assert.deepEqual(ability.effects[0], {
op: 'search',
player: 'you',
selector: { types: ['land'], supertypes: ['basic'] },
amount: { kind: 'number', value: 1 },
destination: 'battlefield',
tapped: true,
shuffle: true,
})
})
test('search can put the chosen card on top of the library', () => {
const [ability] = spell('Search your library for a creature card, reveal it, then shuffle and put the card on top.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
assert.deepEqual(ability.effects[0], {
op: 'search',
player: 'you',
selector: { types: ['creature'] },
amount: { kind: 'number', value: 1 },
destination: 'top',
tapped: false,
shuffle: true,
})
})
test('a two-card search can split between the battlefield and hand', () => {
const [ability] = spell('Search your library for up to two basic land cards, reveal those cards, put one onto the battlefield tapped and the other into your hand, then shuffle.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
assert.deepEqual(ability.effects[0], {
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 },
})
})
test('"if you search your library this way, shuffle" keeps the search', () => {
const [ability] = spell('Search your library for a card named Sol Ring, reveal it, and put it into your hand. If you search your library this way, shuffle.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
assert.deepEqual(ability.effects[0], {
op: 'search',
player: 'you',
selector: { name: 'sol ring', types: [] },
amount: { kind: 'number', value: 1 },
destination: 'hand',
tapped: false,
shuffle: true,
})
})
test('search selectors support an exact mana value', () => {
const [ability] = spell('Search your library for an artifact card with mana value 3, reveal it, put it into your hand, then shuffle.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
const effect = ability.effects[0]
assert.equal(effect.op, 'search')
if (effect.op !== 'search') return
assert.deepEqual(effect.selector, { types: ['artifact'], manaValue: { atLeast: 3, atMost: 3 } })
})
test('"it" refers to the triggering object when unambiguous', () => {
const [ability] = creature('Whenever another creature enters, put a +1/+1 counter on it.')
assert.equal(ability.kind, 'triggered')
if (ability.kind !== 'triggered') return
const effect = ability.effects[0]
assert.equal(effect.op, 'put_counters')
if (effect.op !== 'put_counters') return
assert.deepEqual(effect.target, { kind: 'trigger_subject' })
})
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, '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', () => {
const [ability] = creature('Choose one —')
assert.equal(ability.kind, 'unsupported')
if (ability.kind !== 'unsupported') return
assert.equal(ability.reason, 'modal')
})
test('static anthem', () => {
const [ability] = creature('Creatures you control get +1/+1.')
assert.equal(ability.kind, 'static')
if (ability.kind !== 'static') return
assert.equal(ability.effects[0].op, 'pump')
if (ability.effects[0].op !== 'pump') return
assert.equal(ability.effects[0].power.kind, 'number')
const target = ability.effects[0].target as Extract<Target, { kind: 'object' }>
assert.equal(target.mode, 'each')
assert.equal(target.selector.controller, 'you')
})
test('negative colors become an excluded color filter', () => {
const [ability] = spell('Destroy target nonblack creature.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
assert.deepEqual(ability.effects[0], {
op: 'destroy',
target: {
kind: 'object',
selector: { types: ['creature'], excludeColors: ['B'] },
mode: 'target',
},
})
const source = formatAbility(ability)
assert.match(source, /withoutColor\('black'\)/)
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
})
test('enters-with-counters is a static replacement effect', () => {
const [ability] = creature('This creature enters with X +1/+1 counters on it.')
assert.equal(ability.kind, 'static')
if (ability.kind !== 'static') return
assert.deepEqual(ability.effects[0], {
op: 'enters_with_counters',
target: { kind: 'self' },
counter: '+1/+1',
amount: { kind: 'x' },
})
})
test('trigger condition cards in graveyard', () => {
const [ability] = creature(
'Threshold — As long as there are seven or more cards in your graveyard, this creature gets +1/+1 and has first strike.',
)
assert.equal(ability.kind, 'static')
if (ability.kind !== 'static') return
assert.deepEqual(ability.condition, { kind: 'cards_in_graveyard', player: 'you', atLeast: 7 })
})
test('formatter renders card source', () => {
const [ability] = creature('When this creature enters, draw a card.')
const text = formatAbility(ability)
assert.match(text, /triggered\(when\.enters\(self\)/)
assert.match(text, /draw\(you, 1\)/)
})
test('token copy of the triggering nontoken Dragon loses legendary', () => {
const [ability] = creature(
"Whenever another nontoken Dragon you control enters, create a token that's a copy of it, except the token isn't legendary.",
)
assert.equal(ability.kind, 'triggered')
if (ability.kind !== 'triggered') return
assert.deepEqual(ability.event, {
kind: 'enters',
subject: {
kind: 'object',
selector: {
other: true,
controller: 'you',
token: false,
types: ['creature'],
subtypes: ['dragon'],
},
mode: 'each',
},
})
const effect = ability.effects[0]
assert.equal(effect.op, 'create_token')
if (effect.op !== 'create_token') return
assert.deepEqual(effect.copyOf, { kind: 'trigger_subject' })
assert.equal(effect.notLegendary, true)
assert.equal(
formatAbility(ability),
"triggered(when.enters(each(permanents.other().nontoken().withSubtype('dragon').withType('creature').youControl())), () => createToken(you, 1, copyOf(triggerSubject, { notLegendary: true })))",
)
})
test('token-doubling replacement lines compile and round-trip', () => {
const cases: Array<[string, object]> = [
[
'If an effect would create one or more tokens under your control, it creates twice that many of those tokens instead.',
{ op: 'token_multiplier', multiplier: 2, who: 'you' },
],
[
'If one or more tokens would be created under your control, twice that many of those tokens are created instead.',
{ op: 'token_multiplier', multiplier: 2, who: 'you' },
],
[
'If one or more tokens would be created, twice that many of those tokens are created instead.',
{ op: 'token_multiplier', multiplier: 2, who: 'each_player' },
],
[
'If one or more creature tokens would be created under your control, three times that many of those tokens are created instead.',
{ op: 'token_multiplier', multiplier: 3, who: 'you', creatureOnly: true },
],
]
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('all effects are typed', () => {
const [ability] = spell('Draw two cards, then discard a card.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
const effects: Effect[] = ability.effects
assert.deepEqual(effects.map(effect => effect.op), ['draw', 'discard'])
})
test('a leading duration applies to the effect', () => {
const [ability] = spell('Until end of turn, target creature gets +2/+0 and gains first strike.')
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
const [pump, grant] = ability.effects
assert.equal(pump.op, 'pump')
assert.equal(grant.op, 'grant')
if (pump.op !== 'pump' || grant.op !== 'grant') return
assert.equal(pump.duration, 'until_end_of_turn')
assert.equal(grant.duration, 'until_end_of_turn')
})
test('"When you do" becomes a reflexive condition and energy costs are payable', () => {
const [ability] = creature(
'Whenever a creature attacks, you may pay {3}. When you do, put a +1/+1 counter on that creature.',
)
assert.equal(ability.kind, 'triggered')
if (ability.kind !== 'triggered') return
const conditional = ability.effects[1]
assert.equal(conditional.op, 'conditional')
if (conditional.op !== 'conditional') return
assert.deepEqual(conditional.condition, { kind: 'if_you_do' })
const [activated] = creature('{T}, Pay {E}{E}: This creature deals 1 damage to target creature.')
assert.equal(activated.kind, 'activated')
if (activated.kind !== 'activated') return
assert.deepEqual(activated.costs, [
{ kind: 'tap' },
{ kind: 'energy', amount: 2 },
])
})
test('for-each pump keeps the zero stat', () => {
const [ability] = creature('This creature gets +1/+0 for each artifact you control.')
assert.equal(ability.kind, 'static')
if (ability.kind !== 'static') return
const effect = ability.effects[0]
assert.equal(effect.op, 'pump')
if (effect.op !== 'pump') return
assert.deepEqual(effect.power, { kind: 'count', selector: { types: ['artifact'], controller: 'you' } })
assert.deepEqual(effect.toughness, { kind: 'number', value: 0 })
})
test('"When <subject>" triggers and bare "gain life" effects parse', () => {
const [dies] = creature('When enchanted creature dies, return this to its owner\'s hand.')
assert.equal(dies.kind, 'triggered')
if (dies.kind !== 'triggered') return
assert.deepEqual(dies.event, {
kind: 'dies',
subject: { kind: 'object', selector: { attached: 'enchanted', types: ['creature'] }, mode: 'each' },
})
const [upkeep] = creature('At the beginning of your upkeep, you may gain 1 life.')
assert.equal(upkeep.kind, 'triggered')
if (upkeep.kind !== 'triggered') return
assert.equal(upkeep.optional, true)
assert.deepEqual(upkeep.effects[0], {
op: 'life',
player: 'you',
amount: { kind: 'number', value: 1 },
mode: 'gain',
})
})
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')
if (ability.kind !== 'static') return
assert.deepEqual(ability.condition, {
kind: 'defending_player_controls',
selector: { tapped: false, types: ['land'] },
})
})
test('"until the end of your next turn" gets its own duration', () => {
for (const text of [
'Target creature gets +2/+2 until the end of your next turn.',
'Until the end of your next turn, target creature gets +2/+2.',
]) {
const [ability] = spell(text)
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
const effect = ability.effects[0]
assert.equal(effect.op, 'pump')
if (effect.op !== 'pump') return
assert.equal(effect.duration, 'until_end_of_your_next_turn')
const source = formatAbility(ability)
assert.match(source, /untilEndOfYourNextTurn/)
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
}
})
test('parameterized keywords accept an em dash', () => {
const [escape] = creature('Escape—{2}{R}, Exile three other cards from your graveyard.')
assert.equal(escape.kind, 'keyword')
if (escape.kind !== 'keyword') return
assert.equal(escape.keyword, 'Escape')
const [fabricate] = creature('Fabricate 1')
assert.deepEqual(fabricate, { kind: 'keyword', keyword: 'Fabricate', value: '1' })
})
test('counter-removal costs parse and round-trip through card source', () => {
const [self] = creature('{2}{U}, Remove a +1/+1 counter from this: Draw a card, then discard a card.')
assert.equal(self.kind, 'activated')
if (self.kind !== 'activated') return
assert.deepEqual(self.costs, [
{ kind: 'mana', symbols: '{2}{u}' },
{ kind: 'remove_counters', counter: '+1/+1', amount: { kind: 'number', value: 1 } },
])
const source = formatAbility(self)
assert.match(source, /removeCountersCost\('\+1\/\+1', 1\)/)
assert.equal(formatAbility(parseCardSource(`export default [${source}]`).abilities[0]), source)
const [other] = creature('Remove three spore counters from a creature you control: Create a 1/1 green Saproling creature token.')
assert.equal(other.kind, 'activated')
if (other.kind !== 'activated') return
assert.deepEqual(other.costs, [{
kind: 'remove_counters',
counter: 'spore',
amount: { kind: 'number', value: 3 },
selector: { controller: 'you', types: ['creature'] },
}])
})
test('hideaway lands exile a card and gate playing it on their condition', () => {
const lands: Array<[string, string, string]> = [
["Clive's Hideaway", `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
{T}: Add {C}.
{2}, {T}: You may play the exiled card without paying its mana cost if you control four or more legendary creatures.`, 'you_control'],
['Howltooth Hollow', `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
This land enters tapped.
{T}: Add {B}.
{B}, {T}: You may play the exiled card without paying its mana cost if each player has no cards in hand.`, 'cards_in_hand_at_most'],
['Mosswort Bridge', `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
This land enters tapped.
{T}: Add {G}.
{G}, {T}: You may play the exiled card without paying its mana cost if creatures you control have total power 10 or greater.`, 'total_power'],
['Shelldock Isle', `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
This land enters tapped.
{T}: Add {U}.
{U}, {T}: You may play the exiled card without paying its mana cost if a library has twenty or fewer cards in it.`, 'library_at_most'],
['Spinerock Knoll', `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
This land enters tapped.
{T}: Add {R}.
{R}, {T}: You may play the exiled card without paying its mana cost if an opponent was dealt 7 or more damage this turn.`, 'opponent_damaged_this_turn'],
['Windbrisk Heights', `Hideaway 4 (When this land enters, look at the top four cards of your library, exile one face down, then put the rest on the bottom in a random order.)
This land enters tapped.
{T}: Add {W}.
{W}, {T}: You may play the exiled card without paying its mana cost if you attacked with three or more creatures this turn.`, 'attacked_with_creatures'],
]
for (const [name, text, condition] of lands) {
const abilities = compileText(text, name, [], 'Land')
assert.equal(abilities.filter(ability => ability.kind === 'unsupported').length, 0, name)
const hideaway = abilities.find(ability => ability.kind === 'triggered' && ability.effects.some(effect => effect.op === 'hideaway'))
assert.equal(hideaway?.kind, 'triggered', name)
if (hideaway?.kind !== 'triggered') continue
assert.deepEqual(hideaway.effects, [{ op: 'hideaway', amount: { kind: 'number', value: 4 } }], name)
const activated = abilities.at(-1)
assert.equal(activated?.kind, 'activated', name)
if (activated?.kind !== 'activated') continue
const [effect] = activated.effects
assert.equal(effect.op, 'conditional', name)
if (effect.op !== 'conditional') continue
assert.equal(effect.condition.kind, condition, name)
assert.deepEqual(effect.effects, [{ op: 'play_exiled', player: 'you', free: true }], name)
}
})
test('a trigger returns its own card from the graveyard', () => {
const [ability] = creature('When this dies, return this from your graveyard to the battlefield with a finality counter on it.')
assert.equal(ability.kind, 'triggered')
if (ability.kind !== 'triggered') return
assert.deepEqual(ability.effects, [{
op: 'return_to_battlefield',
target: { kind: 'self' },
tapped: false,
transformed: undefined,
counters: { counter: 'finality', amount: { kind: 'number', value: 1 } },
}])
})
test('Commander-batch oracle lines compile to dedicated nodes', () => {
const [free] = spell('If you control a commander, you may cast this without paying its mana cost.')
assert.equal(free.kind, 'alternative_cost')
if (free.kind !== 'alternative_cost') return
assert.deepEqual(free.condition, { kind: 'you_control_commander' })
assert.deepEqual(free.costs, [])
const [overload] = spell('Overload {6}{U}')
assert.deepEqual(overload, { kind: 'overload', cost: '{6}{U}', text: 'Overload {6}{U}' })
const [graves] = creature('When this enters, exile all graveyards.')
assert.equal(graves.kind, 'triggered')
if (graves.kind !== 'triggered') return
assert.deepEqual(graves.effects, [{ op: 'exile_graveyards' }])
const [rest] = creature('If a card or token would be put into a graveyard from anywhere, exile it instead.')
assert.equal(rest.kind, 'static')
if (rest.kind !== 'static') return
assert.deepEqual(rest.effects, [{ op: 'exile_instead_of_graveyard' }])
const [leyline] = creature("If a card would be put into an opponent's graveyard from anywhere, exile it instead.")
assert.equal(leyline.kind, 'static')
if (leyline.kind !== 'static') return
assert.deepEqual(leyline.effects, [{ op: 'exile_instead_of_graveyard', owner: 'opponent' }])
const [thalia] = creature('Noncreature spells cost {1} more to cast.')
assert.equal(thalia.kind, 'static')
if (thalia.kind !== 'static') return
assert.deepEqual(thalia.effects, [{
op: 'cost_increase',
amount: { kind: 'number', value: 1 },
spellType: 'noncreature',
}])
const [linvala] = creature("Activated abilities of creatures your opponents control can't be activated.")
assert.equal(linvala.kind, 'static')
if (linvala.kind !== 'static') return
assert.deepEqual(linvala.effects, [{
op: 'restriction',
target: { kind: 'object', selector: { controller: 'opponent', types: ['creature'] }, mode: 'each' },
rule: 'cant_activate',
}])
const [aven] = creature('If an opponent would search a library, that player searches the top four cards of that library instead.')
assert.equal(aven.kind, 'static')
if (aven.kind !== 'static') return
assert.deepEqual(aven.effects, [{
op: 'search_restriction',
who: 'opponent',
amount: { kind: 'number', value: 4 },
}])
const [field] = compileText(
'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.',
'Field of the Dead',
[],
'Land',
)
assert.equal(field.kind, 'triggered')
if (field.kind !== 'triggered') return
assert.deepEqual(field.condition, {
kind: 'distinct_names',
selector: { types: ['land'] },
atLeast: 7,
})
})
test('spell cost reductions compile their conditions and mana symbols', () => {
const [bears] = creature('Creature spells you cast cost {1} less to cast.')
assert.equal(bears.kind, 'static')
if (bears.kind !== 'static') return
assert.deepEqual(bears.effects, [{
op: 'cost_reduction',
target: {
kind: 'object',
selector: { controller: 'you', types: ['creature'] },
mode: 'each',
},
amount: { kind: 'number', value: 1 },
}])
const [cantrips] = creature('Instant and sorcery spells cost {2} less to cast.')
assert.equal(cantrips.kind, 'static')
if (cantrips.kind !== 'static') return
assert.deepEqual(cantrips.effects, [{
op: 'cost_reduction',
target: {
kind: 'object',
selector: { match: 'any', types: ['instant', 'sorcery'] },
mode: 'each',
},
amount: { kind: 'number', value: 2 },
}])
const [legendary] = creature('Legendary spells you cast cost {R}{G} less to cast. This effect reduces only the amount of colored mana you pay.')
assert.equal(legendary.kind, 'static')
if (legendary.kind !== 'static') return
assert.deepEqual(legendary.effects, [{
op: 'cost_reduction',
target: {
kind: 'object',
selector: { controller: 'you', supertypes: ['legendary'], types: [] },
mode: 'each',
},
symbols: '{R}{G}',
}])
const [tribal] = creature("Each spell you cast that's a Demon, Horror, or Nightmare costs {1} less to cast.")
assert.equal(tribal.kind, 'static')
if (tribal.kind !== 'static') return
assert.deepEqual(tribal.effects, [{
op: 'cost_reduction',
target: {
kind: 'object',
selector: { controller: 'you', match: 'any', subtypes: ['demon', 'horror', 'nightmare'], types: [] },
mode: 'each',
},
amount: { kind: 'number', value: 1 },
}])
})
test('new abilities, effects and conditions round-trip through card source', () => {
const abilities: Ability[] = [
...spell('If you control a commander, you may cast this without paying its mana cost.'),
...spell('Overload {6}{U}'),
...creature('When this enters, exile all graveyards.'),
...creature('If a card or token would be put into a graveyard from anywhere, exile it instead.'),
...creature("If a card would be put into an opponent's graveyard from anywhere, exile it instead."),
...creature('Noncreature spells cost {1} more to cast.'),
...creature('Creature spells you cast cost {1} less to cast.'),
...creature('Legendary spells you cast cost {R}{G} less to cast.'),
...creature("Each spell you cast that's a Demon, Horror, or Nightmare costs {1} less to cast."),
...creature("Activated abilities of creatures your opponents control can't be activated."),
...creature('If an opponent would search a library, that player searches the top four cards of that library instead.'),
...compileText(
'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.',
'Field of the Dead',
[],
'Land',
),
]
for (const ability of abilities) {
const source = formatAbility(ability)
const reparsed = parseCardSource(`export default [${source}]`).abilities[0]
assert.ok(reparsed)
assert.equal(formatAbility(reparsed), source)
}
})
test('Role token creation compiles with the attachment target', () => {
for (const role of ['Cursed', 'Monster', 'Royal', 'Sorcerer', 'Virtuous', 'Wicked', 'Young Hero']) {
const [ability] = spell(`Create a ${role} Role token attached to target creature you control.`)
assert.equal(ability.kind, 'spell')
if (ability.kind !== 'spell') return
const effect = ability.effects[0]
assert.equal(effect.op, 'create_token')
if (effect.op !== 'create_token') return
assert.equal(effect.token.name, role)
assert.deepEqual(effect.token.subtypes, ['aura', 'role'])
assert.deepEqual(effect.attachTo, {
kind: 'object',
selector: { controller: 'you', types: ['creature'] },
mode: 'target',
})
const source = formatAbility(ability)
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')
})