test
This commit is contained in:
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'])
|
||||
})
|
||||
Reference in New Issue
Block a user