First commit
This commit is contained in:
92
backend/test/pseudo.test.ts
Normal file
92
backend/test/pseudo.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import test from 'node:test'
|
||||
import type { Card } from '../src/cards.js'
|
||||
import { formatCard } from '../src/effects/format.js'
|
||||
import {
|
||||
compileImplementation,
|
||||
deleteOverride,
|
||||
readAllOverrides,
|
||||
readOverride,
|
||||
writeOverride,
|
||||
} from '../src/effects/implementations.js'
|
||||
import { parsePseudoCard, PseudoParseError } from '../src/effects/pseudo.js'
|
||||
|
||||
const CARD: Card = {
|
||||
oracle_id: '00000000-0000-4000-8000-000000000001',
|
||||
name: 'Test Card',
|
||||
layout: 'normal',
|
||||
cmc: 1,
|
||||
type_line: 'Creature — Human',
|
||||
colors: [],
|
||||
color_identity: [],
|
||||
keywords: [],
|
||||
}
|
||||
|
||||
const SOURCE = `on enters(this):
|
||||
choose 1:
|
||||
1. exile(target other creature you own); schedule(next_end_step):
|
||||
return_to_battlefield(the chosen target)
|
||||
static:
|
||||
grant_ability(each enchanted land):
|
||||
on upkeep(you) (may):
|
||||
pay({W}{W})
|
||||
if you do:
|
||||
gain_life(you, 1)`
|
||||
|
||||
test('pseudocode round-trips through the compiler and formatter', () => {
|
||||
const compiled = compileImplementation(CARD, SOURCE)
|
||||
assert.equal(compiled.manual, true)
|
||||
assert.equal(compiled.status, 'supported')
|
||||
|
||||
const rendered = formatCard(compiled)
|
||||
const reparsed = compileImplementation(CARD, rendered)
|
||||
assert.equal(formatCard(reparsed), rendered)
|
||||
assert.equal(reparsed.abilities.length, 2)
|
||||
|
||||
const [triggered] = reparsed.abilities
|
||||
assert.equal(triggered.kind, 'triggered')
|
||||
if (triggered.kind !== 'triggered') return
|
||||
assert.equal(triggered.effects[0].op, 'modal')
|
||||
if (triggered.effects[0].op !== 'modal') return
|
||||
assert.equal(triggered.effects[0].modes[0][1].op, 'schedule')
|
||||
|
||||
const staticAbility = reparsed.abilities[1]
|
||||
assert.equal(staticAbility.kind, 'static')
|
||||
if (staticAbility.kind !== 'static') return
|
||||
assert.equal(staticAbility.effects[0].op, 'grant_ability')
|
||||
if (staticAbility.effects[0].op !== 'grant_ability') return
|
||||
assert.equal(staticAbility.effects[0].ability.kind, 'triggered')
|
||||
})
|
||||
|
||||
test('invalid pseudocode reports its line', () => {
|
||||
assert.throws(
|
||||
() => parsePseudoCard('spell:\n draw(you)'),
|
||||
(error: unknown) => error instanceof PseudoParseError && error.line === 2,
|
||||
)
|
||||
})
|
||||
|
||||
test('manual implementations persist one file per card', async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'commander-overrides-'))
|
||||
const previous = process.env.CARD_OVERRIDES_DIR
|
||||
process.env.CARD_OVERRIDES_DIR = dir
|
||||
t.after(async () => {
|
||||
if (previous === undefined) delete process.env.CARD_OVERRIDES_DIR
|
||||
else process.env.CARD_OVERRIDES_DIR = previous
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
await writeOverride(CARD.oracle_id, SOURCE)
|
||||
const stored = await readOverride(CARD.oracle_id)
|
||||
assert.equal(stored?.source, `${SOURCE}\n`)
|
||||
assert.equal((await readAllOverrides()).get(CARD.oracle_id), `${SOURCE}\n`)
|
||||
|
||||
const compiled = compileImplementation(CARD, stored!.source)
|
||||
assert.equal(compiled.manual, true)
|
||||
assert.equal(compiled.status, 'supported')
|
||||
|
||||
await deleteOverride(CARD.oracle_id)
|
||||
assert.equal(await readOverride(CARD.oracle_id), undefined)
|
||||
})
|
||||
Reference in New Issue
Block a user