import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { makeCharacter, makeSimulation, pinEastSplit, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; test("walking into a foreign region transfers the only authoritative copy", () => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); assert.equal(harness.simulation.entityRecord(entityId).workerId, 0); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 }); const events = stepTimes(harness.simulation, walkTicks(2)); const record = harness.simulation.entityRecord(entityId); assert.equal(record.tileX, 64); assert.equal(record.tileY, 8); assert.equal(record.workerId, 1); assert.equal(harness.simulation.debugWorld().entities.filter((entity) => entity.entityId === entityId).length, 1); assert.ok(events.some((event) => event.type === "entity-move" && event.entityId === entityId && event.tileX === 64)); const onSource = harness.simulation.workers[0].call({ type: "listPublicEntities" }); const onDest = harness.simulation.workers[1].call({ type: "listPublicEntities" }); assert.equal(onSource.some((entity) => entity.entityId === entityId), false); assert.equal(onDest.some((entity) => entity.entityId === entityId), true); } finally { harness.close(); } }); test("a transferred entity keeps its entity id and receives a new local handle", () => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { harness.simulation.spawnEntity({ position: { tileX: 70, tileY: 8 }, appearance: { sex: "male", firstName: "Pad", lastName: "East" } }); const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); const sourceHandle = harness.simulation.workers[0].world.ecs.handleOf(entityId); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 }); stepTimes(harness.simulation, walkTicks(2)); const destHandle = harness.simulation.workers[1].world.ecs.handleOf(entityId); assert.equal(harness.simulation.entityRecord(entityId).workerId, 1); assert.equal(harness.simulation.workers[0].world.ecs.handleOf(entityId), null); assert.notEqual(destHandle, sourceHandle); assert.equal(harness.simulation.workers[1].world.ecs.entityIdOf(destHandle), entityId); } finally { harness.close(); } }); test("a blocked destination rejects the transfer and leaves one copy on the source worker", () => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const walker = makeCharacter(harness.stateDb, { tileX: 62, tileY: 8 }); const anchor = makeCharacter(harness.stateDb, { tileX: 70, tileY: 8, firstName: "Bob" }); const walkerId = harness.simulation.spawnCharacter(1, walker); harness.simulation.spawnCharacter(2, anchor); assert.equal(harness.simulation.entityRecord(walkerId).workerId, 0); const eastWorker = harness.simulation.workers[1]; const data = eastWorker.world.chunkData(4, 0); data.blocks[8 * 16 + 0] = { typeId: BlockType.stoneWall, state: null, config: null }; harness.simulation.enqueue(1, { type: "move-to", characterId: walker.character_id, tileX: 64, tileY: 8 }); stepTimes(harness.simulation, walkTicks(3)); const walking = harness.simulation.entityRecord(walkerId); assert.ok(walking.tileX < 64); assert.equal(walking.workerId, 0); assert.equal(harness.simulation.debugWorld().entities.filter((entity) => entity.entityId === walkerId).length, 1); } finally { harness.close(); } }); test("commands keep working after a transfer", () => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 }); stepTimes(harness.simulation, walkTicks(2)); assert.equal(harness.simulation.entityRecord(entityId).workerId, 1); harness.simulation.enqueue(1, { type: "set-gait", characterId: character.character_id, gait: "run" }); harness.simulation.step(); assert.equal(harness.simulation.entityRecord(entityId).gait, "run"); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 65, tileY: 8 }); stepTimes(harness.simulation, 6); assert.equal(harness.simulation.entityRecord(entityId).tileX, 65); assert.equal(harness.simulation.entityRecord(entityId).workerId, 1); } finally { harness.close(); } });