import test from "node:test"; import assert from "node:assert/strict"; import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; test("reattachCharacterSession moves command ownership to a new session", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 12, tileY: 8 }); stepTimes(harness.simulation, walkTicks(5)); const before = harness.simulation.entityRecord(entityId); assert.equal(before.tileX, 12); const again = harness.simulation.reattachCharacterSession(character.character_id, 2); assert.equal(again, entityId); harness.simulation.enqueue(2, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); stepTimes(harness.simulation, walkTicks(3)); const after = harness.simulation.entityRecord(entityId); assert.equal(after.tileX, 10); assert.equal(harness.simulation.entityForCharacter(character.character_id), entityId); } finally { harness.close(); } }); test("reattachCharacterSession rejects commands from the old session", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.reattachCharacterSession(character.character_id, 2); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 12, tileY: 8 }); stepTimes(harness.simulation, walkTicks(5)); const stayed = harness.simulation.entityRecord(entityId); assert.equal(stayed.tileX, 8); assert.equal(stayed.tileY, 8); } finally { harness.close(); } });