import test from "node:test"; import assert from "node:assert/strict"; import { GAIT } from "../../shared/protocol/constants.js"; import { makeCharacter, makeSimulation, pinEastSplit, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; function entityOn (simulation, entityId) { return simulation.entityRecord(entityId); } test("a player keeps nearby chunks simulating and surrounding regions resident", () => { const harness = makeSimulation({ workerCount: 1, graceMs: 30_000 }); try { const character = makeCharacter(harness.stateDb); const entityId = harness.simulation.spawnCharacter(1, character); const debug = harness.simulation.debugWorld(); assert.equal(debug.workerCount, 1); assert.ok(debug.simulatingChunks.includes("overworld|0,0")); assert.ok(debug.simulatingChunks.includes("overworld|1,0")); assert.ok(!debug.simulatingChunks.includes("overworld|3,0")); assert.ok(debug.regions.some((region) => region.regionX === 0 && region.regionY === 0 && region.refs > 0)); assert.ok(debug.regions.some((region) => region.regionX === 1 && region.regionY === 0 && region.refs > 0)); assert.equal(entityOn(harness.simulation, entityId).workerId, 0); } finally { harness.close(); } }); test("dormant chunks do not simulate movement", () => { const harness = makeSimulation({ workerCount: 1, graceMs: 30_000 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); const npcId = harness.simulation.spawnEntity({ position: { tileX: 50, tileY: 8 }, movement: { gait: GAIT.walk, path: [{ x: 51, y: 8 }], walkCarry: 0 }, appearance: { sex: "male", firstName: "Ned", lastName: "Still" } }); assert.ok(!harness.simulation.debugWorld().simulatingChunks.includes("overworld|3,0")); stepTimes(harness.simulation, walkTicks(4)); const npc = entityOn(harness.simulation, npcId); assert.equal(npc.tileX, 50); assert.equal(npc.tileY, 8); } finally { harness.close(); } }); test("regions unload after the grace period once the last ChunkLoader leaves", () => { const harness = makeSimulation({ workerCount: 1, graceMs: 10 }); try { const character = makeCharacter(harness.stateDb); harness.simulation.spawnCharacter(1, character); assert.ok(harness.simulation.debugWorld().regions.length > 0); harness.simulation.despawnCharacter(character.character_id); harness.simulation.step(harness.now()); assert.ok(harness.simulation.debugWorld().regions.length > 0, "still in grace"); harness.advance(10); harness.simulation.step(harness.now()); assert.equal(harness.simulation.debugWorld().regions.length, 0); } finally { harness.close(); } }); test("persistent entities are saved on unload and restored when the region returns", () => { const harness = makeSimulation({ workerCount: 1, graceMs: 0 }); try { const character = makeCharacter(harness.stateDb); harness.simulation.spawnCharacter(1, character); const npcId = harness.simulation.spawnEntity({ position: { tileX: 8, tileY: 9 }, appearance: { sex: "male", firstName: "Ned", lastName: "Keep" }, persistent: {} }); harness.simulation.despawnCharacter(character.character_id); harness.simulation.step(harness.now()); assert.equal(harness.simulation.entityRecord(npcId), null); assert.equal(harness.stateDb.listPersistentInRegion(0, 0, 63, 63).length, 1); harness.simulation.spawnCharacter(1, character); const restored = harness.simulation.entityRecord(npcId); assert.ok(restored); assert.equal(restored.tileX, 8); assert.equal(restored.tileY, 9); assert.equal(restored.firstName, "Ned"); } finally { harness.close(); } }); test("mutating a snapshot does not mutate worker memory", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); harness.simulation.spawnCharacter(1, character); const snapshot = harness.simulation.snapshotChunk(0, 0); snapshot.open = false; snapshot.entities.push({ entityId: 999 }); const again = harness.simulation.snapshotChunk(0, 0); assert.equal(again.open, true); assert.equal(again.entities.some((entity) => entity.entityId === 999), false); } finally { harness.close(); } });