import test from "node:test"; import assert from "node:assert/strict"; import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; test("joining and leaving emit spawn and despawn replication instead of extra snapshots", () => { const harness = makeSimulation({ workerCount: 1 }); try { const first = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const firstId = harness.simulation.spawnCharacter(1, first); assert.ok(harness.simulation.drainReplication().some((event) => event.type === "entity-spawn" && event.entityId === firstId && event.firstName === "Ada" )); const second = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Ben" }); const secondId = harness.simulation.spawnCharacter(2, second); const spawned = harness.simulation.drainReplication(); assert.ok(spawned.some((event) => event.type === "entity-spawn" && event.entityId === secondId)); assert.equal(spawned.some((event) => event.sessionId != null), false); harness.simulation.despawnCharacter(second.character_id); const gone = harness.simulation.drainReplication(); assert.ok(gone.some((event) => event.type === "entity-despawn" && event.entityId === secondId && event.tileX === 9 && event.tileY === 8 )); } finally { harness.close(); } }); test("movement replication carries tile and gait only", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.drainReplication(); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); const events = stepTimes(harness.simulation, walkTicks(3)); const move = events.find((event) => event.type === "entity-move" && event.entityId === entityId); assert.ok(move); assert.equal(move.tileX, 9); assert.equal(move.gait, "walk"); assert.equal(move.firstName, undefined); assert.equal(move.sex, undefined); assert.equal(move.characterId, undefined); } finally { harness.close(); } });