216 lines
7.0 KiB
TypeScript
216 lines
7.0 KiB
TypeScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import {
|
|
connectNodes,
|
|
graphFromSource,
|
|
nextNodeId,
|
|
parseCall,
|
|
parseGraph,
|
|
serializeGraph,
|
|
shapeFromText,
|
|
sourceFromGraph,
|
|
syncTexts,
|
|
} from '../app/utils/cardGraph.ts'
|
|
import type { GraphModel } from '../app/utils/cardGraph.ts'
|
|
|
|
const SOURCE = `# Soul Warden [supported]
|
|
on enters(each other creature):
|
|
gain_life(you, 1)
|
|
|
|
on upkeep(you):
|
|
if you control at least 3 artifacts:
|
|
draw(you, 1)
|
|
create_token(you, 1, 1/1 white soldier creature)`
|
|
|
|
test('parseGraph builds ability and nested effect nodes', () => {
|
|
const roots = parseGraph(SOURCE)
|
|
|
|
assert.equal(roots.length, 3)
|
|
assert.equal(roots[0].kind, 'raw')
|
|
assert.equal(roots[1].kind, 'ability')
|
|
assert.equal(roots[1].text, 'on enters(each other creature):')
|
|
assert.deepEqual(roots[1].children.map(node => node.text), ['gain_life(you, 1)'])
|
|
|
|
const upkeep = roots[2]
|
|
assert.equal(upkeep.children[0].text, 'if you control at least 3 artifacts:')
|
|
assert.deepEqual(upkeep.children[0].children.map(node => node.text), ['draw(you, 1)'])
|
|
assert.equal(upkeep.children[1].text, 'create_token(you, 1, 1/1 white soldier creature)')
|
|
})
|
|
|
|
test('serializeGraph round-trips formatter output and normalizes indentation', () => {
|
|
assert.equal(serializeGraph(parseGraph(SOURCE)), SOURCE.replace('\n\n', '\n'))
|
|
assert.equal(
|
|
serializeGraph(parseGraph('on dies(this):\n draw(you, 1)')),
|
|
'on dies(this):\n draw(you, 1)',
|
|
)
|
|
})
|
|
|
|
test('graph conversion round-trips abilities, chains and nested bodies', () => {
|
|
const model = graphFromSource(SOURCE)
|
|
const result = sourceFromGraph(model)
|
|
|
|
assert.equal(result.source, SOURCE.replace('\n\n', '\n'))
|
|
assert.equal(result.unlinked, 0)
|
|
assert.deepEqual(result.problems, [])
|
|
})
|
|
|
|
test('connectNodes replaces an input link and rejects cycles', () => {
|
|
const model = graphFromSource(SOURCE)
|
|
const ability = model.nodes.find(node => node.kind === 'ability' && node.text.startsWith('on enters'))
|
|
const first = model.nodes.find(node => node.text === 'gain_life(you, 1)')
|
|
const second = model.nodes.find(node => node.text === 'create_token(you, 1, 1/1 white soldier creature)')
|
|
assert.ok(ability && first && second)
|
|
|
|
assert.equal(connectNodes(model, ability.id, 'next', second.id), true)
|
|
assert.equal(sourceFromGraph(model).unlinked, 1)
|
|
|
|
assert.equal(connectNodes(model, first.id, 'next', second.id), true)
|
|
assert.equal(connectNodes(model, second.id, 'next', first.id), false)
|
|
})
|
|
|
|
test('shapeFromText exposes trigger and effect arguments', () => {
|
|
const trigger = shapeFromText('on attacks(this):')
|
|
assert.equal(trigger?.name, 'on attacks')
|
|
assert.deepEqual(trigger?.slots, [{ label: 'subject', text: 'this' }])
|
|
|
|
const damage = shapeFromText('damage(target creature, 3)')
|
|
assert.deepEqual(damage?.slots.map(slot => slot.label), ['target', 'amount'])
|
|
})
|
|
|
|
test('shapeFromText adds dropdown choices for constant slots', () => {
|
|
const enters = shapeFromText('on enters(each other nontoken dragon creature you control):')
|
|
assert.deepEqual(enters?.slots[0]?.options, [
|
|
'this',
|
|
'each other creature',
|
|
'each creature you control',
|
|
'each other nontoken dragon creature you control',
|
|
"each nontoken angel you don't control",
|
|
])
|
|
|
|
const copy = shapeFromText("create_token(you, 1, copy of the triggering object, not legendary)")
|
|
assert.deepEqual(copy?.slots.map(slot => slot.label), ['player', 'amount', 'token', 'options'])
|
|
assert.ok(copy?.slots[2]?.options?.includes('copy of the triggering object'))
|
|
assert.deepEqual(copy?.slots[3]?.options, ['not legendary'])
|
|
})
|
|
|
|
test('value nodes fill trigger and effect argument sockets', () => {
|
|
const model = graphFromSource(`# Test [supported]
|
|
on attacks(this):
|
|
damage(target creature, 3)`)
|
|
const trigger = model.nodes.find(node => node.kind === 'ability')
|
|
const damage = model.nodes.find(node => node.text.startsWith('damage('))
|
|
assert.ok(trigger && damage)
|
|
|
|
const player = {
|
|
id: nextNodeId(),
|
|
kind: 'value' as const,
|
|
text: 'target player',
|
|
literal: 'target player',
|
|
valueLabel: 'Players',
|
|
x: 0,
|
|
y: 0,
|
|
}
|
|
model.nodes.push(player)
|
|
assert.equal(connectNodes(model, player.id, 'value', trigger.id, 0), true)
|
|
|
|
const artifact = {
|
|
id: nextNodeId(),
|
|
kind: 'value' as const,
|
|
text: 'target artifact',
|
|
literal: 'target artifact',
|
|
valueLabel: 'Targets',
|
|
x: 0,
|
|
y: 0,
|
|
}
|
|
model.nodes.push(artifact)
|
|
assert.equal(connectNodes(model, artifact.id, 'value', damage.id, 0), true)
|
|
|
|
assert.equal(sourceFromGraph(model).source, `# Test [supported]
|
|
on attacks(target player):
|
|
damage(target artifact, 3)`)
|
|
})
|
|
|
|
test('or and and value nodes combine selector slots', () => {
|
|
const model = graphFromSource(`# Test [supported]
|
|
spell:
|
|
search(you, creature, 1, to hand, shuffle)`)
|
|
const search = model.nodes.find(node => node.text.startsWith('search('))
|
|
assert.ok(search)
|
|
|
|
const combo: GraphModel['nodes'][number] = {
|
|
id: nextNodeId(),
|
|
kind: 'value',
|
|
text: '',
|
|
combine: 'or',
|
|
valueLabel: 'Logic',
|
|
x: 0,
|
|
y: 0,
|
|
shape: {
|
|
head: '',
|
|
tail: '',
|
|
separator: ' or ',
|
|
slots: [
|
|
{ label: 'left', text: 'creature' },
|
|
{ label: 'right', text: 'artifact' },
|
|
],
|
|
},
|
|
}
|
|
model.nodes.push(combo)
|
|
assert.equal(connectNodes(model, combo.id, 'value', search.id, 1), true)
|
|
|
|
assert.match(sourceFromGraph(model).source, /search\(you, creature or artifact, 1, to hand, shuffle\)/)
|
|
|
|
syncTexts(model)
|
|
assert.equal(combo.text, 'creature or artifact')
|
|
|
|
combo.combine = 'and'
|
|
combo.shape!.slots[0].text = 'red'
|
|
combo.shape!.slots[1].text = 'creature'
|
|
syncTexts(model)
|
|
assert.equal(combo.text, 'red and creature')
|
|
})
|
|
|
|
test('structured selector value nodes render their slots', () => {
|
|
const model = graphFromSource(`# Test [supported]
|
|
on enters(this):`)
|
|
const trigger = model.nodes.find(node => node.kind === 'ability')
|
|
assert.ok(trigger)
|
|
|
|
const selector: GraphModel['nodes'][number] = {
|
|
id: nextNodeId(),
|
|
kind: 'value',
|
|
text: '',
|
|
valueLabel: 'Selector',
|
|
x: 0,
|
|
y: 0,
|
|
shape: {
|
|
head: '',
|
|
tail: '',
|
|
separator: ' ',
|
|
slots: [
|
|
{ label: 'prefix', text: 'each' },
|
|
{ label: 'state', text: 'nontoken' },
|
|
{ label: 'type', text: 'angel' },
|
|
{ label: 'controller', text: "you don't control" },
|
|
],
|
|
},
|
|
}
|
|
model.nodes.push(selector)
|
|
assert.equal(connectNodes(model, selector.id, 'value', trigger.id, 0), true)
|
|
|
|
syncTexts(model)
|
|
assert.equal(selector.text, "each nontoken angel you don't control")
|
|
assert.equal(sourceFromGraph(model).source, `# Test [supported]
|
|
on enters(each nontoken angel you don't control):`)
|
|
})
|
|
|
|
test('parseCall splits arguments outside nested groups', () => {
|
|
assert.deepEqual(parseCall('draw(you, 1)'), { name: 'draw', args: ['you', '1'] })
|
|
assert.deepEqual(parseCall('proliferate()'), { name: 'proliferate', args: [] })
|
|
assert.deepEqual(parseCall('damage(divided(any number, creature), 3)'), {
|
|
name: 'damage',
|
|
args: ['divided(any number, creature)', '3'],
|
|
})
|
|
assert.equal(parseCall('if you control at least 3 artifacts:'), undefined)
|
|
})
|