BlueGit

timfa questward

Files

questward / server / tests / character-session-reattach.test.js

character-session-reattach.test.js

character-session-reattach.test.js 1.9 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js";
4
5test("reattachCharacterSession moves command ownership to a new session", () =>
6{
7 const harness = makeSimulation({ workerCount: 1 });
8 try
9 {
10 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
11 const entityId = harness.simulation.spawnCharacter(1, character);
12 harness.simulation.enqueue(1, {
13 type: "move-to",
14 characterId: character.character_id,
15 tileX: 12,
16 tileY: 8
17 });
18 stepTimes(harness.simulation, walkTicks(5));
19 const before = harness.simulation.entityRecord(entityId);
20 assert.equal(before.tileX, 12);
21
22 const again = harness.simulation.reattachCharacterSession(character.character_id, 2);
23 assert.equal(again, entityId);
24
25 harness.simulation.enqueue(2, {
26 type: "move-to",
27 characterId: character.character_id,
28 tileX: 10,
29 tileY: 8
30 });
31 stepTimes(harness.simulation, walkTicks(3));
32 const after = harness.simulation.entityRecord(entityId);
33 assert.equal(after.tileX, 10);
34 assert.equal(harness.simulation.entityForCharacter(character.character_id), entityId);
35 }
36 finally
37 { harness.close(); }
38});
39
40test("reattachCharacterSession rejects commands from the old session", () =>
41{
42 const harness = makeSimulation({ workerCount: 1 });
43 try
44 {
45 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
46 const entityId = harness.simulation.spawnCharacter(1, character);
47 harness.simulation.reattachCharacterSession(character.character_id, 2);
48
49 harness.simulation.enqueue(1, {
50 type: "move-to",
51 characterId: character.character_id,
52 tileX: 12,
53 tileY: 8
54 });
55 stepTimes(harness.simulation, walkTicks(5));
56
57 const stayed = harness.simulation.entityRecord(entityId);
58 assert.equal(stayed.tileX, 8);
59 assert.equal(stayed.tileY, 8);
60 }
61 finally
62 { harness.close(); }
63});