BlueGit

timfa questward

Files

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

worker-transfer.test.js

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