8. Tests
Goal: test each mission with no page: won and lost as it says, the carried hero, the choice at the gate, and the computer playing a minute. npm test runs them (Vitest, tests/rules.test.ts).
Make a mission's world
ts
import { checkMap, createRegistry, createWorldFromMap, type World } from '@v12-engine/engine'
import { mapNamed, maps } from '../src/maps'
import { ENEMY, PLAYER, rules, onStart, UNIT } from '../src/rules'
const registry = createRegistry(rules)
/** A mission's world, started as the page starts it */
async function mission(name: string): Promise<World> {
const map = mapNamed(name)
const world = await createWorldFromMap({ map, rules, seed: 1 })
onStart(world, { map })
return world
}
/** Steps `seconds` of the game */
function play(world: World, seconds: number): void {
for (let i = 0; i < seconds * world.tickRate; i++) world.step()
}It calls the same onStart the page does.
Check every map
ts
describe('the maps', () => {
it.each(Object.keys(maps))('%s has nothing wrong with it', (name) => {
const problems = checkMap(mapNamed(name), registry).filter((problem) => problem.error)
expect(problems).toEqual([])
})
})checkMap is what the editor's Problems panel runs: duplicate names, unknown unit types, players with no start.
Win and lose a mission
ts
it('is won by burning every building of the camp, and lost when the hall falls', async () => {
const won = await mission('homestead')
play(won, 1)
for (const entity of [...won.index.ownedBy(ENEMY)]) if (entity.def.kind === 'building') won.kill(entity)
play(won, 1)
expect(won.objectives.get(PLAYER, 'campaign.raze').status).toBe('done')
expect(won.winner).toBe(won.player(PLAYER).team)
const lost = await mission('homestead')
for (const hall of standing(lost, PLAYER, UNIT.hall)) lost.kill(hall)
play(lost, 1)
expect(lost.objectives.get(PLAYER, 'campaign.hall').status).toBe('failed')
expect(lost.winner).not.toBe(lost.player(PLAYER).team)
})world.kill does in a line what a player does in ten minutes.
Carry a hero and make the choice
ts
const seasoned: HeroRecord = {
type: UNIT.knight,
name: 'hero',
level: 3,
xp: 0,
points: 0,
abilities: { holyLight: 2 },
items: [{ item: 'steelSword', charges: 0 }, null, null, null, null, null]
}
it('brings the carried hero in where the map has one, with what he earned', async () => {
const map = mapNamed('pass')
const world = await createWorldFromMap({ map, rules, seed: 1 })
const [hero] = carryIn(world, [seasoned], PLAYER, map.players[0].start!)
onStart(world, { map })
expect(hero.hero?.level).toBe(3)
expect(hero.abilities.find((ability) => ability.id === 'holyLight')?.level).toBe(2)
})
it('lets the player choose a duel: the guards stand down, Varga is rallied', async () => {
const world = await mission('pass')
play(world, 0.5)
world.issue({ type: 'cinematicChoice', player: PLAYER, cinematic: 'campaign.parley', question: 'How does this end?', choice: 'duel' })
play(world, 0.5)
for (const name of ['guard1', 'guard2', 'guard3']) expect(world.named(name)?.owner).toBe(MAP_NEUTRAL)
expect(world.named('warlord')?.buffs.some((buff) => buff.id === 'rallied')).toBe(true)
})carryIn does what the campaign does between missions. The choice is a player command, so the test issues it as the screen would.
Let the computer play a minute
ts
import { simulate } from '@v12-engine/engine/testing'
it('plays, the Red Hand’s computer thinking, with no error for a minute (simulate)', async () => {
const map = mapNamed('homestead')
// the mission seats the Red Hand's computer itself (`start`): simulate is told of none
const [game] = await simulate({ rules: rules, map, computers: [], seconds: 60, onStart: (world) => onStart(world, { map }) }, { seeds: [1] })
expect(game.failed).toBeUndefined()
expect(game.ticks).toBeGreaterThan(0)
})simulate plays whole games with no page, many seeds in worker threads if you like. See Test my rules.
Next: 9. Ship it.