BlueGit

timfa questward

Files

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

worker-transfer.test.js

worker-transfer.test.js 4.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 makeCharacter,
5 makeSimulation,
6 pinEastSplit,
7 stepTimes,
8 walkTicks
9} from "./helpers/simulationHarness.js";
10
11test("walking into a foreign region transfers the only authoritative copy", () =>
12{
13 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
14 try
15 {
16 const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 });
17 const entityId = harness.simulation.spawnCharacter(1, character);
18 assert.equal(harness.simulation.entityRecord(entityId).workerId, 0);
19
20 harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 });
21 const events = stepTimes(harness.simulation, walkTicks(2));
22 const record = harness.simulation.entityRecord(entityId);
23 assert.equal(record.tileX, 64);
24 assert.equal(record.tileY, 8);
25 assert.equal(record.workerId, 1);
26 assert.equal(harness.simulation.debugWorld().entities.filter((entity) => entity.entityId === entityId).length, 1);
27 assert.ok(events.some((event) => event.type === "entity-move" && event.entityId === entityId && event.tileX === 64));
28
29 const onSource = harness.simulation.workers[0].call({ type: "listPublicEntities" });
30 const onDest = harness.simulation.workers[1].call({ type: "listPublicEntities" });
31 assert.equal(onSource.some((entity) => entity.entityId === entityId), false);
32 assert.equal(onDest.some((entity) => entity.entityId === entityId), true);
33 }
34 finally
35 { harness.close(); }
36});
37
38test("a transferred entity keeps its entity id and receives a new local handle", () =>
39{
40 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
41 try
42 {
43 harness.simulation.spawnEntity({
44 position: { tileX: 70, tileY: 8 },
45 appearance: { sex: "male", firstName: "Pad", lastName: "East" }
46 });
47 const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 });
48 const entityId = harness.simulation.spawnCharacter(1, character);
49 const sourceHandle = harness.simulation.workers[0].world.ecs.handleOf(entityId);
50 harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 });
51 stepTimes(harness.simulation, walkTicks(2));
52 const destHandle = harness.simulation.workers[1].world.ecs.handleOf(entityId);
53 assert.equal(harness.simulation.entityRecord(entityId).workerId, 1);
54 assert.equal(harness.simulation.workers[0].world.ecs.handleOf(entityId), null);
55 assert.notEqual(destHandle, sourceHandle);
56 assert.equal(harness.simulation.workers[1].world.ecs.entityIdOf(destHandle), entityId);
57 }
58 finally
59 { harness.close(); }
60});
61
62test("a blocked destination rejects the transfer and leaves one copy on the source worker", () =>
63{
64 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
65 try
66 {
67 const walker = makeCharacter(harness.stateDb, { tileX: 62, tileY: 8 });
68 const blocker = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8, firstName: "Bob" });
69 const walkerId = harness.simulation.spawnCharacter(1, walker);
70 const blockerId = harness.simulation.spawnCharacter(2, blocker);
71 assert.equal(harness.simulation.entityRecord(walkerId).workerId, 0);
72 assert.equal(harness.simulation.entityRecord(blockerId).workerId, 1);
73
74 harness.simulation.enqueue(1, { type: "move-to", characterId: walker.character_id, tileX: 64, tileY: 8 });
75 stepTimes(harness.simulation, walkTicks(3));
76 const walking = harness.simulation.entityRecord(walkerId);
77 assert.ok(walking.tileX < 64);
78 assert.equal(walking.workerId, 0);
79 assert.equal(harness.simulation.entityRecord(blockerId).tileX, 64);
80 assert.equal(harness.simulation.debugWorld().entities.filter((entity) => entity.entityId === walkerId).length, 1);
81 }
82 finally
83 { harness.close(); }
84});
85
86test("commands keep working after a transfer", () =>
87{
88 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
89 try
90 {
91 const character = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 });
92 const entityId = harness.simulation.spawnCharacter(1, character);
93 harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 64, tileY: 8 });
94 stepTimes(harness.simulation, walkTicks(2));
95 assert.equal(harness.simulation.entityRecord(entityId).workerId, 1);
96
97 harness.simulation.enqueue(1, { type: "set-gait", characterId: character.character_id, gait: "run" });
98 harness.simulation.step();
99 assert.equal(harness.simulation.entityRecord(entityId).gait, "run");
100 harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 65, tileY: 8 });
101 stepTimes(harness.simulation, 6);
102 assert.equal(harness.simulation.entityRecord(entityId).tileX, 65);
103 assert.equal(harness.simulation.entityRecord(entityId).workerId, 1);
104 }
105 finally
106 { harness.close(); }
107});