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('"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 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 " 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('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 } }, }]) })