BlueGit

timfa questward

Files

questward / server / tests / worker-lifecycle.test.js

worker-lifecycle.test.js

worker-lifecycle.test.js 4.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { GAIT } from "../../shared/protocol/constants.js";
4import {
5 makeCharacter,
6 makeSimulation,
7 pinEastSplit,
8 stepTimes,
9 walkTicks
10} from "./helpers/simulationHarness.js";
11
12function entityOn (simulation, entityId)
13{
14 return simulation.entityRecord(entityId);
15}
16
17test("a player keeps nearby chunks simulating and surrounding regions resident", () =>
18{
19 const harness = makeSimulation({ workerCount: 1, graceMs: 30_000 });
20 try
21 {
22 const character = makeCharacter(harness.stateDb);
23 const entityId = harness.simulation.spawnCharacter(1, character);
24 const debug = harness.simulation.debugWorld();
25 assert.equal(debug.workerCount, 1);
26 assert.ok(debug.simulatingChunks.includes("0,0"));
27 assert.ok(debug.simulatingChunks.includes("1,0"));
28 assert.ok(!debug.simulatingChunks.includes("3,0"));
29 assert.ok(debug.regions.some((region) => region.regionX === 0 && region.regionY === 0 && region.refs > 0));
30 assert.ok(debug.regions.some((region) => region.regionX === 1 && region.regionY === 0 && region.refs > 0));
31 assert.equal(entityOn(harness.simulation, entityId).workerId, 0);
32 }
33 finally
34 { harness.close(); }
35});
36
37test("dormant chunks do not simulate movement", () =>
38{
39 const harness = makeSimulation({ workerCount: 1, graceMs: 30_000 });
40 try
41 {
42 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
43 harness.simulation.spawnCharacter(1, character);
44 const npcId = harness.simulation.spawnEntity({
45 position: { tileX: 50, tileY: 8 },
46 movement: { gait: GAIT.walk, path: [{ x: 51, y: 8 }], walkCarry: 0 },
47 appearance: { sex: "male", firstName: "Ned", lastName: "Still" }
48 });
49 assert.ok(!harness.simulation.debugWorld().simulatingChunks.includes("3,0"));
50 stepTimes(harness.simulation, walkTicks(4));
51 const npc = entityOn(harness.simulation, npcId);
52 assert.equal(npc.tileX, 50);
53 assert.equal(npc.tileY, 8);
54 }
55 finally
56 { harness.close(); }
57});
58
59test("regions unload after the grace period once the last ChunkLoader leaves", () =>
60{
61 const harness = makeSimulation({ workerCount: 1, graceMs: 10 });
62 try
63 {
64 const character = makeCharacter(harness.stateDb);
65 harness.simulation.spawnCharacter(1, character);
66 assert.ok(harness.simulation.debugWorld().regions.length > 0);
67 harness.simulation.despawnCharacter(character.character_id);
68 harness.simulation.step(harness.now());
69 assert.ok(harness.simulation.debugWorld().regions.length > 0, "still in grace");
70 harness.advance(10);
71 harness.simulation.step(harness.now());
72 assert.equal(harness.simulation.debugWorld().regions.length, 0);
73 }
74 finally
75 { harness.close(); }
76});
77
78test("persistent entities are saved on unload and restored when the region returns", () =>
79{
80 const harness = makeSimulation({ workerCount: 1, graceMs: 0 });
81 try
82 {
83 const character = makeCharacter(harness.stateDb);
84 harness.simulation.spawnCharacter(1, character);
85 const npcId = harness.simulation.spawnEntity({
86 position: { tileX: 8, tileY: 9 },
87 appearance: { sex: "male", firstName: "Ned", lastName: "Keep" },
88 persistent: {}
89 });
90 harness.simulation.despawnCharacter(character.character_id);
91 harness.simulation.step(harness.now());
92 assert.equal(harness.simulation.entityRecord(npcId), null);
93 assert.equal(harness.stateDb.listPersistentInRegion(0, 0, 63, 63).length, 1);
94
95 harness.simulation.spawnCharacter(1, character);
96 const restored = harness.simulation.entityRecord(npcId);
97 assert.ok(restored);
98 assert.equal(restored.tileX, 8);
99 assert.equal(restored.tileY, 9);
100 assert.equal(restored.firstName, "Ned");
101 }
102 finally
103 { harness.close(); }
104});
105
106test("mutating a snapshot does not mutate worker memory", () =>
107{
108 const harness = makeSimulation({ workerCount: 1 });
109 try
110 {
111 const character = makeCharacter(harness.stateDb);
112 harness.simulation.spawnCharacter(1, character);
113 const snapshot = harness.simulation.snapshotChunk(0, 0);
114 snapshot.open = false;
115 snapshot.entities.push({ entityId: 999 });
116 const again = harness.simulation.snapshotChunk(0, 0);
117 assert.equal(again.open, true);
118 assert.equal(again.entities.some((entity) => entity.entityId === 999), false);
119 }
120 finally
121 { harness.close(); }
122});