// #82: `tools/seed.mjs` over the real wire. It signs in as GM, creates deterministic NPC tokens, // skips labels on a rerun and `--replace` clears the labelled set first. import test from 'node:test' import assert from 'node:assert/strict' import path from 'node:path' import { spawnSync } from 'node:child_process' import { projectRoot, sleep } from './lib.mjs' import { openAs, withGmServer } from './withserver.mjs' function runSeed(args) { const r = spawnSync(process.execPath, [path.join(projectRoot, 'tools', 'seed.mjs'), ...args], { cwd: projectRoot, encoding: 'utf8' }) return { status: r.status, stdout: r.stdout, stderr: r.stderr } } /** The GM's current token set, deduped by id from a fresh connection's resync. */ async function collectTokens(origin, gmKey) { const gm = await openAs(origin, gmKey) const hello = await gm.ws.waitText('hello') await gm.ws.waitText('walls') await sleep(400) const byId = new Map() for (const message of gm.ws.messages) { if (message.t === 'token' && typeof message.id === 'string') byId.set(message.id, message) } gm.ws.close() await gm.ws.closed return { scene: hello.scene, tokens: [...byId.values()] } } const byName = tokens => [...tokens].sort((a, b) => a.name.localeCompare(b.name)) test('seed: creates N deterministic NPC tokens, skips a rerun, and --replace starts over', { timeout: 180000 }, async () => { await withGmServer(async (server, gmKey) => { const url = server.origin const args = [`--url=${url}`, `--key=${gmKey}`, '--tokens=6', '--vision=2', '--seed=7'] const first = runSeed(args) assert.equal(first.status, 0, first.stderr) assert.match(first.stdout, /seeded 6 tokens \(6 created, 0 already present, 0 removed\)/) const firstSet = await collectTokens(url, gmKey) assert.equal(firstSet.tokens.length, 6) const tokens = byName(firstSet.tokens) assert.deepEqual(tokens.map(t => t.name), ['Seed 01', 'Seed 02', 'Seed 03', 'Seed 04', 'Seed 05', 'Seed 06']) for (const token of tokens) { assert.equal(token.kind, 'npc') assert.equal(token.controller, null, 'an NPC stays GM-controlled') assert.equal(token.gm_only, false) assert.ok(token.pos[0] >= 1 && token.pos[0] < firstSet.scene.w) assert.ok(token.pos[1] >= 1 && token.pos[1] < firstSet.scene.h) } assert.equal(tokens[0].flags.vision, true, 'the first token has vision') assert.equal(tokens[0].vision_radius, 400) assert.equal(tokens[1].flags.vision, true) assert.equal(tokens[2].flags.vision, undefined, 'the rest keep the DESIGN 8 {} default') const positions = tokens.map(t => t.pos.join(',')) assert.equal(new Set(positions).size, 6, 'positions are distinct') // Same seed on a second fresh scene: same layout (ids differ, positions do not). await withGmServer(async (other, otherKey) => { const second = runSeed([`--url=${other.origin}`, `--key=${otherKey}`, '--tokens=6', '--seed=7']) assert.equal(second.status, 0, second.stderr) const otherSet = await collectTokens(other.origin, otherKey) assert.deepEqual(byName(otherSet.tokens).map(t => t.pos), byName(tokens).map(t => t.pos)) }) // Rerun without --replace: no duplicates. const again = runSeed(args) assert.equal(again.status, 0, again.stderr) assert.match(again.stdout, /seeded 6 tokens \(0 created, 6 already present, 0 removed\)/) assert.equal((await collectTokens(url, gmKey)).tokens.length, 6) // A smaller run without --replace leaves the extra labelled tokens alone; --replace clears // every labelled token (even one outside the new count) and rebuilds exactly N. const small = runSeed([`--url=${url}`, `--key=${gmKey}`, '--tokens=4', '--seed=7']) assert.equal(small.status, 0, small.stderr) assert.equal((await collectTokens(url, gmKey)).tokens.length, 6) const replaced = runSeed([`--url=${url}`, `--key=${gmKey}`, '--tokens=4', '--seed=7', '--replace']) assert.equal(replaced.status, 0, replaced.stderr) assert.match(replaced.stdout, /4 created, 0 already present, 6 removed/) const after = await collectTokens(url, gmKey) assert.equal(after.tokens.length, 4) assert.deepEqual(byName(after.tokens).map(t => t.name), ['Seed 01', 'Seed 02', 'Seed 03', 'Seed 04']) }) })