Files
commander/backend/test/commander.test.ts
2026-10-29 20:30:29 +01:00

200 lines
6.7 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import type { Card } from '../src/cards.js'
import { canBeCommander, importDeckText, validateCommanderDeck } from '../src/game/commander.js'
import { Game } from '../src/game/game.js'
import type { CardDefinition } from '../src/game/types.js'
function card(name: string, overrides: Partial<Card> = {}): Card {
return {
oracle_id: name.toLowerCase().replace(/[^a-z0-9]+/g, '-'),
name,
layout: 'normal',
cmc: 0,
type_line: 'Creature — Test',
colors: [],
color_identity: [],
keywords: [],
...overrides,
}
}
function commanderCard(name: string, overrides: Partial<Card> = {}): Card {
return card(name, {
type_line: 'Legendary Creature — Test',
color_identity: ['G'],
...overrides,
})
}
function basicLand(name: string): Card {
return card(name, { type_line: 'Basic Land — Forest', color_identity: ['G'] })
}
function commanderDefinition(name: string, overrides: Partial<CardDefinition> = {}): CardDefinition {
return {
oracleId: name.toLowerCase().replace(/[^a-z0-9]+/g, '-'),
name,
typeLine: 'Legendary Creature — Test',
manaCost: '{1}',
manaValue: 1,
colors: ['G'],
colorIdentity: ['G'],
power: 2,
toughness: 2,
abilities: [],
...overrides,
}
}
test('commander deck validation enforces size, singleton, identity, and commander type', () => {
const commander = commanderCard('Test Commander')
const legal = Array.from({ length: 99 }, () => basicLand('Forest'))
assert.deepEqual(validateCommanderDeck(commander, legal), [])
assert.match(validateCommanderDeck(commander, legal.slice(1)).join(' '), /exactly 100/)
const duplicate = [...legal.slice(1), card('Sol Ring', { color_identity: [] }), card('Sol Ring', { color_identity: [] })]
assert.match(validateCommanderDeck(commander, duplicate).join(' '), /only basic lands may repeat/)
const offColor = [...legal.slice(1), card('Counterspell', { color_identity: ['U'] })]
assert.match(validateCommanderDeck(commander, offColor).join(' '), /outside the commander's color identity/)
assert.match(validateCommanderDeck(card('Not Legendary'), legal).join(' '), /cannot be a commander/)
assert.equal(canBeCommander(card('Planeswalker Commander', { oracle_text: 'This can be your commander.' })), true)
})
test('decklist import reads sections, quantities, and set suffixes', () => {
const known = new Map<string, Card>([
['Test Commander', commanderCard('Test Commander')],
['Sol Ring', card('Sol Ring')],
['Forest', basicLand('Forest')],
])
const result = importDeckText(`
Commander
1 Test Commander (TST) 1
Deck
1 Sol Ring (TST) 2
2 Forest (TST) 3 *F*
`, name => known.get(name))
assert.equal(result.commander?.name, 'Test Commander')
assert.equal(result.cards.length, 3)
assert.equal(result.cards.filter(entry => entry.name === 'Forest').length, 2)
assert.match(result.errors.join(' '), /exactly 100/)
})
test('a commander identity mana source only offers colors in the identity', () => {
const game = new Game({
seed: 1,
startingHandSize: 0,
commander: true,
players: [
{ id: 'p1', deck: [basicLand('Forest')], commander: commanderDefinition('Test Commander', { colors: ['U', 'R'], colorIdentity: ['U', 'R'] }) },
{ id: 'p2', deck: [basicLand('Forest')], commander: commanderDefinition('Other Commander') },
],
})
game.start()
const tower = game.addCard('p1', {
oracleId: 'command-tower',
name: 'Command Tower',
typeLine: 'Land',
abilities: [{
kind: 'activated',
costs: [{ kind: 'tap' }],
effects: [{
op: 'add_mana',
player: 'you',
mana: {
kind: 'any_color',
amount: { kind: 'number', value: 1 },
oneColor: false,
restriction: "commander's color identity",
},
}],
optional: false,
text: "{T}: Add one mana of any color in your commander's color identity.",
}],
}, 'battlefield')
assert.deepEqual(game.manaColorChoices(tower, 0), ['U', 'R'])
assert.throws(() => game.activateAbility('p1', tower.id, 0, { manaColor: 'G' }), /cannot produce/)
assert.equal(tower.tapped, false)
game.activateAbility('p1', tower.id, 0, { manaColor: 'R' })
assert.equal(game.getPlayer('p1').manaPool.R, 1)
})
test('a commander starts in the command zone, pays tax, and returns there from the graveyard', () => {
const commander = commanderDefinition('Test Commander')
const game = new Game({
seed: 1,
startingHandSize: 0,
commander: true,
players: [
{ id: 'p1', deck: [basicLand('Forest')], commander },
{ id: 'p2', deck: [basicLand('Forest')], commander: commanderDefinition('Other Commander') },
],
})
game.start()
const forest = {
oracleId: 'forest',
name: 'Forest',
typeLine: 'Basic Land — Forest',
abilities: [{
kind: 'activated' as const,
costs: [{ kind: 'tap' as const }],
effects: [{ op: 'add_mana' as const, player: 'you' as const, mana: { kind: 'symbols' as const, symbols: ['{G}'] } }],
optional: false,
text: '{T}: Add {G}.',
}],
}
const player = game.getPlayer('p1')
assert.equal(player.command.length, 1)
const object = game.getObject(player.command[0])
assert.equal(object.isCommander, true)
for (let index = 0; index < 4; index++) game.addCard('p1', forest, 'battlefield')
game.castSpell('p1', object.id)
game.resolveStack()
assert.equal(object.zone, 'battlefield')
assert.equal(player.commanderCasts[object.id], 1)
game.moveToZone(object, 'graveyard')
assert.equal(object.zone, 'command')
assert.equal(player.graveyard.length, 0)
game.castSpell('p1', object.id)
game.resolveStack()
assert.equal(object.zone, 'battlefield')
assert.equal(player.commanderCasts[object.id], 2)
assert.equal(player.battlefield.map(id => game.getObject(id)).filter(land => land.tapped).length, 4)
})
test('twenty-one commander damage loses the game', () => {
const commander = commanderDefinition('Big Commander', { power: 21, manaCost: '{0}', manaValue: 0 })
const game = new Game({
seed: 1,
startingHandSize: 0,
commander: true,
players: [
{ id: 'p1', deck: [basicLand('Forest')], commander },
{ id: 'p2', deck: [basicLand('Forest')], commander: commanderDefinition('Other Commander') },
],
})
game.start()
const object = game.getObject(game.getPlayer('p1').command[0])
game.castSpell('p1', object.id)
game.resolveStack()
object.enteredTurn = 0
game.declareAttackers('p1', [{ attackerId: object.id, defenderId: 'p2' }])
game.declareBlockers('p2', [])
game.resolveCombatDamage()
assert.equal(game.getPlayer('p2').commanderDamage[object.id], 21)
assert.equal(game.getPlayer('p2').lost, true)
assert.equal(game.winner, 'p1')
})