| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | buildDoorwayGraph, |
| 5 | findDimensionPath, |
| 6 | planPortalLegs, |
| 7 | buildFallbackLegs, |
| 8 | ensureCourierLegs, |
| 9 | advanceAbstractTravel, |
| 10 | poseOnCourierLeg, |
| 11 | estimateAbstractDistance |
| 12 | } from "../../shared/courier/doorwayGraph.js"; |
| 13 | import { OVERWORLD_ID } from "../../shared/world/dimensions.js"; |
| 14 | |
| 15 | test("doorway graph BFS finds multi-hop dimension path and reconstructs edges", () => |
| 16 | { |
| 17 | const graph = buildDoorwayGraph([ |
| 18 | { |
| 19 | tileX: 10, tileY: 10, dimensionId: OVERWORLD_ID, |
| 20 | targetDimensionId: "cave", targetTileX: 2, targetTileY: 2 |
| 21 | }, |
| 22 | { |
| 23 | tileX: 4, tileY: 4, dimensionId: "cave", |
| 24 | targetDimensionId: "cellar", targetTileX: 8, targetTileY: 1 |
| 25 | }, |
| 26 | { |
| 27 | tileX: 99, tileY: 99, dimensionId: "island", |
| 28 | targetDimensionId: OVERWORLD_ID, targetTileX: 0, targetTileY: 0 |
| 29 | } |
| 30 | ]); |
| 31 | assert.equal(graph.edges.length, 3); |
| 32 | assert.equal(graph.byFromDim.get(OVERWORLD_ID).length, 1); |
| 33 | |
| 34 | const path = findDimensionPath(graph, OVERWORLD_ID, "cellar"); |
| 35 | assert.ok(path); |
| 36 | assert.equal(path.length, 2); |
| 37 | assert.equal(path[0].toDimensionId, "cave"); |
| 38 | assert.equal(path[1].toDimensionId, "cellar"); |
| 39 | assert.equal(path[1].fromTileX, 4); |
| 40 | |
| 41 | assert.equal(findDimensionPath(graph, OVERWORLD_ID, OVERWORLD_ID).length, 0); |
| 42 | assert.equal(findDimensionPath(graph, "cellar", OVERWORLD_ID), null); |
| 43 | }); |
| 44 | |
| 45 | test("planPortalLegs builds fly/portal/fly for cross-dimension path", () => |
| 46 | { |
| 47 | const graph = buildDoorwayGraph([ |
| 48 | { |
| 49 | tileX: 10, tileY: 8, dimensionId: OVERWORLD_ID, |
| 50 | targetDimensionId: "cave", targetTileX: 3, targetTileY: 3 |
| 51 | } |
| 52 | ]); |
| 53 | const planned = planPortalLegs({ |
| 54 | fromX: 1.5, fromY: 8.5, fromDimensionId: OVERWORLD_ID, |
| 55 | toX: 5.5, toY: 3.5, toDimensionId: "cave", |
| 56 | graph |
| 57 | }); |
| 58 | assert.equal(planned.mode, "portal"); |
| 59 | assert.equal(planned.legs.length, 3); |
| 60 | assert.equal(planned.legs[0].kind, "fly"); |
| 61 | assert.equal(planned.legs[0].toX, 10.5); |
| 62 | assert.equal(planned.legs[1].kind, "portal"); |
| 63 | assert.equal(planned.legs[1].toDimensionId, "cave"); |
| 64 | assert.equal(planned.legs[1].toX, 3.5); |
| 65 | assert.equal(planned.legs[2].kind, "fly"); |
| 66 | assert.equal(planned.legs[2].toX, 5.5); |
| 67 | }); |
| 68 | |
| 69 | test("planPortalLegs returns fallback mode when no doorway path", () => |
| 70 | { |
| 71 | const graph = buildDoorwayGraph([]); |
| 72 | const planned = planPortalLegs({ |
| 73 | fromX: 1, fromY: 1, fromDimensionId: "cave", |
| 74 | toX: 20, toY: 8, toDimensionId: OVERWORLD_ID, |
| 75 | graph |
| 76 | }); |
| 77 | assert.equal(planned.mode, "fallback"); |
| 78 | assert.equal(planned.legs, null); |
| 79 | }); |
| 80 | |
| 81 | test("buildFallbackLegs produces fade-out, abstract, fade-in", () => |
| 82 | { |
| 83 | const simulating = new Set(["cave:1,1", "cave:0,1", "overworld:20,8", "overworld:19,8"]); |
| 84 | const isSimulating = (tx, ty, dim) => simulating.has(`${dim}:${tx},${ty}`); |
| 85 | const { mode, legs } = buildFallbackLegs({ |
| 86 | fromX: 1.5, fromY: 1.5, fromDimensionId: "cave", |
| 87 | toX: 20.5, toY: 8.5, toDimensionId: OVERWORLD_ID, |
| 88 | isSimulating |
| 89 | }); |
| 90 | assert.equal(mode, "fallback"); |
| 91 | assert.ok(legs.length >= 2); |
| 92 | assert.equal(legs[0].kind, "fly"); |
| 93 | assert.equal(legs[0].fromDimensionId, "cave"); |
| 94 | const abstract = legs.find((leg) => leg.kind === "abstract"); |
| 95 | assert.ok(abstract); |
| 96 | assert.ok(abstract.distance >= 0); |
| 97 | assert.equal(legs[legs.length - 1].kind, "fly"); |
| 98 | assert.equal(legs[legs.length - 1].toDimensionId, OVERWORLD_ID); |
| 99 | assert.equal(legs[legs.length - 1].toX, 20.5); |
| 100 | }); |
| 101 | |
| 102 | test("ensureCourierLegs migrates legacy overworld rows", () => |
| 103 | { |
| 104 | const transit = { |
| 105 | originX: 2.5, |
| 106 | originY: 8.5, |
| 107 | destX: 20.5, |
| 108 | destY: 8.5, |
| 109 | traveled: 3 |
| 110 | }; |
| 111 | ensureCourierLegs(transit); |
| 112 | assert.equal(transit.originDimensionId, OVERWORLD_ID); |
| 113 | assert.equal(transit.destDimensionId, OVERWORLD_ID); |
| 114 | assert.equal(transit.legs.length, 1); |
| 115 | assert.equal(transit.legs[0].kind, "fly"); |
| 116 | assert.equal(transit.legIndex, 0); |
| 117 | assert.equal(transit.traveled, 3); |
| 118 | }); |
| 119 | |
| 120 | test("advanceAbstractTravel and poseOnCourierLeg helpers", () => |
| 121 | { |
| 122 | const advanced = advanceAbstractTravel({ |
| 123 | traveled: 0, |
| 124 | distance: 16, |
| 125 | dt: 1, |
| 126 | settings: { pigeonCruiseTilesPerSec: 8 } |
| 127 | }); |
| 128 | assert.equal(advanced.speed, 8); |
| 129 | assert.equal(advanced.traveled, 8); |
| 130 | assert.equal(advanced.arrived, false); |
| 131 | |
| 132 | const flyPose = poseOnCourierLeg({ |
| 133 | kind: "fly", |
| 134 | fromDimensionId: OVERWORLD_ID, |
| 135 | fromX: 0, fromY: 0, toX: 10, toY: 0 |
| 136 | }, 5); |
| 137 | assert.ok(Math.abs(flyPose.x - 5) < 1e-6); |
| 138 | assert.equal(poseOnCourierLeg({ kind: "abstract", distance: 10 }, 3), null); |
| 139 | assert.ok(estimateAbstractDistance({ |
| 140 | fromX: 0, fromY: 0, toX: 10, toY: 0, crossDimension: true |
| 141 | }) >= 32); |
| 142 | }); |