| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { OVERWORLD_ID } from "../../shared/world/dimensions.js"; |
| 4 | import { chunkKey, tileToChunk } from "../src/world/coordinates.js"; |
| 5 | import { eventInterestKeys, resolveEventDimensionId } from "../src/gateway/eventInterest.js"; |
| 6 | |
| 7 | const CAVE = "cave-fx"; |
| 8 | |
| 9 | test("resolveEventDimensionId prefers event.dimensionId then attacker/caster ids", () => |
| 10 | { |
| 11 | assert.equal( |
| 12 | resolveEventDimensionId({ dimensionId: CAVE }, () => OVERWORLD_ID), |
| 13 | CAVE |
| 14 | ); |
| 15 | assert.equal( |
| 16 | resolveEventDimensionId( |
| 17 | { type: "combat-swing", attackerId: 7 }, |
| 18 | (id) => (id === 7 ? CAVE : OVERWORLD_ID) |
| 19 | ), |
| 20 | CAVE |
| 21 | ); |
| 22 | assert.equal( |
| 23 | resolveEventDimensionId( |
| 24 | { type: "spell-projectile", casterId: 3 }, |
| 25 | (id) => (id === 3 ? CAVE : null) |
| 26 | ), |
| 27 | CAVE |
| 28 | ); |
| 29 | assert.equal( |
| 30 | resolveEventDimensionId({ type: "combat-swing", attackerId: 1 }, () => null), |
| 31 | OVERWORLD_ID |
| 32 | ); |
| 33 | }); |
| 34 | |
| 35 | test("eventInterestKeys scope combat and chunk-delta to the event dimension", () => |
| 36 | { |
| 37 | const swing = eventInterestKeys( |
| 38 | { |
| 39 | type: "combat-swing", |
| 40 | attackerId: 1, |
| 41 | fromTileX: 8, |
| 42 | fromTileY: 8, |
| 43 | tileX: 9, |
| 44 | tileY: 8, |
| 45 | dimensionId: CAVE |
| 46 | }, |
| 47 | () => OVERWORLD_ID |
| 48 | ); |
| 49 | assert.equal(swing.dimensionId, CAVE); |
| 50 | assert.equal( |
| 51 | swing.toKey, |
| 52 | chunkKey(tileToChunk(9), tileToChunk(8), CAVE) |
| 53 | ); |
| 54 | assert.notEqual( |
| 55 | swing.toKey, |
| 56 | chunkKey(tileToChunk(9), tileToChunk(8), OVERWORLD_ID) |
| 57 | ); |
| 58 | |
| 59 | const delta = eventInterestKeys( |
| 60 | { |
| 61 | type: "chunk-delta", |
| 62 | chunkX: 0, |
| 63 | chunkY: 0, |
| 64 | dimensionId: CAVE |
| 65 | }, |
| 66 | () => OVERWORLD_ID |
| 67 | ); |
| 68 | assert.equal(delta.toKey, chunkKey(0, 0, CAVE)); |
| 69 | assert.equal(delta.fromKey, null); |
| 70 | }); |