import test from "node:test"; import assert from "node:assert/strict"; import { EcsWorld } from "../src/ecs/world.js"; import { deserializeEntity, normalizeMobileComponents, serializeEntity } from "../src/ecs/serialize.js"; test("worker-local handles recycle independently of global entity ids", () => { const world = new EcsWorld(); const first = world.spawn(100); assert.equal(first, 1); assert.equal(world.entityIdOf(first), 100); world.destroy(first); const second = world.spawn(200); assert.equal(second, 1); assert.equal(world.entityIdOf(second), 200); assert.equal(world.handleOf(100), null); assert.equal(world.hasEntity(200), true); assert.equal(world.hasEntity(100), false); }); test("legacy movement.path and character.sessionId become Path and NetworkIdentity", () => { const normalized = normalizeMobileComponents({ movement: { gait: "walk", path: [{ x: 1, y: 2 }], walkCarry: 0 }, character: { characterId: 9, accountId: 3, sessionId: 4 } }); assert.deepEqual(normalized.movement, { gait: "walk", walkCarry: 0 }); assert.deepEqual(normalized.path, { tiles: [{ x: 1, y: 2 }] }); assert.deepEqual(normalized.character, { characterId: 9, accountId: 3 }); assert.deepEqual(normalized.networkIdentity, { sessionId: 4 }); }); test("query returns both handle and entity id", () => { const world = new EcsWorld(); const handle = world.spawn(50); world.add(handle, "position", { tileX: 1, tileY: 2 }); const [row] = world.query("position"); assert.equal(row.handle, handle); assert.equal(row.entityId, 50); assert.deepEqual(row.position, { tileX: 1, tileY: 2 }); }); test("deserialize adopts the same entity id with a new local handle", () => { const source = new EcsWorld(); source.add(source.spawn(7), "position", { tileX: 4, tileY: 5 }); const dest = new EcsWorld(); dest.spawn(1); const restored = deserializeEntity(dest, serializeEntity(source, 7)); assert.equal(restored, 7); assert.notEqual(dest.handleOf(7), source.handleOf(7)); assert.equal(dest.entityIdOf(dest.handleOf(7)), 7); });