Files
commander/backend/test/effects.test.ts
2026-10-27 19:14:33 +01:00

550 lines
22 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 { parsePseudoCard } from '../src/effects/pseudo.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('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('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('ambiguous "it" is left unsupported instead of guessed', () => {
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')
})
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('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 pseudocode', () => {
const [ability] = creature('When this creature enters, draw a card.')
const text = formatAbility(ability)
assert.match(text, /on enters\(this\)/)
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), 'on enters(each other nontoken dragon creature you control):\n create_token(you, 1, copy of the triggering object, not legendary)')
})
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('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 pseudo = formatAbility(ability)
assert.match(pseudo, /until the end of your next turn/)
assert.equal(formatAbility(parsePseudoCard(`# Test [supported]\n${pseudo}`).abilities[0]), pseudo)
}
})
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 pseudocode', () => {
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 pseudo = formatAbility(self)
assert.match(pseudo, /remove 1 \+1\/\+1 counter\(s\) from this/)
assert.equal(formatAbility(parsePseudoCard(`# Test [supported]\n${pseudo}`).abilities[0]), pseudo)
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 pseudocode', () => {
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 pseudo = formatAbility(ability)
const reparsed = parsePseudoCard(`# Test [supported]\n${pseudo}`).abilities[0]
assert.ok(reparsed)
assert.equal(formatAbility(reparsed), pseudo)
}
})