| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { EcsWorld } from "../src/ecs/world.js"; |
| 4 | import { deserializeEntity, normalizeMobileComponents, serializeEntity } from "../src/ecs/serialize.js"; |
| 5 | |
| 6 | test("worker-local handles recycle independently of global entity ids", () => |
| 7 | { |
| 8 | const world = new EcsWorld(); |
| 9 | const first = world.spawn(100); |
| 10 | assert.equal(first, 1); |
| 11 | assert.equal(world.entityIdOf(first), 100); |
| 12 | world.destroy(first); |
| 13 | const second = world.spawn(200); |
| 14 | assert.equal(second, 1); |
| 15 | assert.equal(world.entityIdOf(second), 200); |
| 16 | assert.equal(world.handleOf(100), null); |
| 17 | assert.equal(world.hasEntity(200), true); |
| 18 | assert.equal(world.hasEntity(100), false); |
| 19 | }); |
| 20 | |
| 21 | test("legacy movement.path and character.sessionId become Path and NetworkIdentity", () => |
| 22 | { |
| 23 | const normalized = normalizeMobileComponents({ |
| 24 | movement: { gait: "walk", path: [{ x: 1, y: 2 }], walkCarry: 0 }, |
| 25 | character: { characterId: 9, accountId: 3, sessionId: 4 } |
| 26 | }); |
| 27 | assert.deepEqual(normalized.movement, { gait: "walk", walkCarry: 0 }); |
| 28 | assert.deepEqual(normalized.path, { tiles: [{ x: 1, y: 2 }] }); |
| 29 | assert.deepEqual(normalized.character, { characterId: 9, accountId: 3 }); |
| 30 | assert.deepEqual(normalized.networkIdentity, { sessionId: 4 }); |
| 31 | }); |
| 32 | |
| 33 | test("query returns both handle and entity id", () => |
| 34 | { |
| 35 | const world = new EcsWorld(); |
| 36 | const handle = world.spawn(50); |
| 37 | world.add(handle, "position", { tileX: 1, tileY: 2 }); |
| 38 | const [row] = world.query("position"); |
| 39 | assert.equal(row.handle, handle); |
| 40 | assert.equal(row.entityId, 50); |
| 41 | assert.deepEqual(row.position, { tileX: 1, tileY: 2 }); |
| 42 | }); |
| 43 | |
| 44 | test("deserialize adopts the same entity id with a new local handle", () => |
| 45 | { |
| 46 | const source = new EcsWorld(); |
| 47 | source.add(source.spawn(7), "position", { tileX: 4, tileY: 5 }); |
| 48 | const dest = new EcsWorld(); |
| 49 | dest.spawn(1); |
| 50 | const restored = deserializeEntity(dest, serializeEntity(source, 7)); |
| 51 | assert.equal(restored, 7); |
| 52 | assert.notEqual(dest.handleOf(7), source.handleOf(7)); |
| 53 | assert.equal(dest.entityIdOf(dest.handleOf(7)), 7); |
| 54 | }); |