| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; |
| 4 | |
| 5 | test("joining and leaving emit spawn and despawn replication instead of extra snapshots", () => |
| 6 | { |
| 7 | const harness = makeSimulation({ workerCount: 1 }); |
| 8 | try |
| 9 | { |
| 10 | const first = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 11 | const firstId = harness.simulation.spawnCharacter(1, first); |
| 12 | assert.ok(harness.simulation.drainReplication().some((event) => |
| 13 | event.type === "entity-spawn" && event.entityId === firstId && event.firstName === "Ada" |
| 14 | )); |
| 15 | |
| 16 | const second = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Ben" }); |
| 17 | const secondId = harness.simulation.spawnCharacter(2, second); |
| 18 | const spawned = harness.simulation.drainReplication(); |
| 19 | assert.ok(spawned.some((event) => event.type === "entity-spawn" && event.entityId === secondId)); |
| 20 | assert.equal(spawned.some((event) => event.sessionId != null), false); |
| 21 | |
| 22 | harness.simulation.despawnCharacter(second.character_id); |
| 23 | const gone = harness.simulation.drainReplication(); |
| 24 | assert.ok(gone.some((event) => |
| 25 | event.type === "entity-despawn" && event.entityId === secondId && event.tileX === 9 && event.tileY === 8 |
| 26 | )); |
| 27 | } |
| 28 | finally |
| 29 | { harness.close(); } |
| 30 | }); |
| 31 | |
| 32 | test("movement replication carries tile and gait only", () => |
| 33 | { |
| 34 | const harness = makeSimulation({ workerCount: 1 }); |
| 35 | try |
| 36 | { |
| 37 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 38 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 39 | harness.simulation.drainReplication(); |
| 40 | harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); |
| 41 | const events = stepTimes(harness.simulation, walkTicks(3)); |
| 42 | const move = events.find((event) => event.type === "entity-move" && event.entityId === entityId); |
| 43 | assert.ok(move); |
| 44 | assert.equal(move.tileX, 9); |
| 45 | assert.equal(move.gait, "walk"); |
| 46 | assert.equal(move.firstName, undefined); |
| 47 | assert.equal(move.sex, undefined); |
| 48 | assert.equal(move.characterId, undefined); |
| 49 | } |
| 50 | finally |
| 51 | { harness.close(); } |
| 52 | }); |