Files
arah2/web/test/route-surface.test.mjs
T
araharah-executor 77b62da111 test(06-06): failing camera-law + PX-01 full-walk gates — RS7b walk, RS11 pose, RS12 unlock, RS13 no-opts byte-identity (RED)
- RS7b: the context-aware BackButton resolver walks search → routes →
  guidance → (confirm) → routes → search; close() NEVER fires from a
  pushed state; the wire exposes sync() for state changes
- RS11: GUIDANCE_CAMERA_PADDING/{ZOOM,EASE_MS,CAMERA_PITCH} pinned at the
  UI-SPEC pose; bearing key NEVER present; onRerouteApplied moves nothing
- RS12: programmatic moves never unlock follow; a user movestart
  (originalEvent) unlocks — Phase 3 free state returns
- RS13: follow(fix) without opts stays byte-identical (no pitch/zoom keys)
- fakeMap grows flyTo/on/emit recording; suite header documents the RS6/RS8
  re-pins landing with the guidance wiring (tasks 2-3)
2026-10-24 14:47:39 +08:00

764 lines
34 KiB
JavaScript

#!/usr/bin/env node
/*
* route-surface.test.mjs — RS1..RS10, the Phase 5 route-surface gates.
* ----------------------------------------------------------------------
* Ten gates pin the UI-SPEC laws that are provable without a browser
* (the capture legs in 05-05 prove the pixels):
*
* RS1 paint law — the D-09 casing stack, verbatim hex + geometry
* RS2 map-law — zero setStyle in web/src/route; the load-hook position
* RS3 fake-map layers — install idempotence + setData swaps + clears
* RS4 controller state machine — drawn / select / walk-back / pending /
* exception (fail-honest), camera law, console tokens
* RS5 card structure — createElement + textContent law (T-05-10)
* RS6 guidance-implementation boundary — progress/maneuver/btn-primary
* tokens only on ../guidance/ import lines or direct call sites
* (re-pinned 06-06: guidance IMPLEMENTATION lives in web/src/guidance/)
* RS7 BackButton walk — overlay walks, root closes (T-05-11) + the 06-06
* PX-01 full-state-graph walk (search → routes → guidance → confirm →
* routes → search, zero dead ends, close() only at the true root)
* RS8 console discipline — only the ARAHARAH-ROUTE/GUIDE state tokens
* (D-15/T-05-13; re-pinned 06-06 for the guidance token family)
* RS9 strings-only copy — the copy lives ONLY in strings.ts (COPY5's
* companion check)
* RS10 no-storage floor bump — the route family exists under the GEO3 scan
*
* 06-06 additions: RS-CAM (the D-10 locked-follow guidance camera —
* constants pinned RS4-style, easeTo args, unlock law, byte-identical
* no-opts follow), RS7's full walk, and the guidance pump / envelope
* flows (G*) that drive the REAL controller through recording fakes.
*
* Styles: behavioral cases drive REAL modules through recording fakes
* (RS3/RS4/RS7 — Node 24 type-stripping loads the .ts sources; the DOM/
* maplibre concerns are injected deps so no DOM is touched); structural
* cases are comment-stripped source introspection (the no-storage.test.mjs
* GEO3 pattern — prose is not a call site).
*/
import { strict as assert } from "node:assert";
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { test } from "./harness.mjs";
const HERE = path.dirname(fileURLToPath(new URL(import.meta.url)));
const ROUTE_DIR = path.join(HERE, "..", "src", "route");
const SRC = path.join(HERE, "..", "src");
const { ROUTE_PAINT, ROUTE_LAYER_IDS, installRouteLayers, setRouteDrawn, clearRouteLayers } = await import(
"../src/route/route-layers.ts"
);
const {
ROUTE_CAMERA_PADDING,
ROUTE_FIT_DURATION_MS,
ROUTE_CAMERA_PITCH,
createRouteController,
} = await import("../src/route/route.ts");
const { wireBackButton } = await import("../src/telegram/backbutton.ts");
/* ————— shared helpers ————— */
function stripComments(src) {
return src
.replace(/\/\*[\s\S]*?\*\//g, " ") // block comments
.replace(/(^|[^:\\])\/\/[^\n]*/g, "$1"); // line comments (not URLs inside strings — benign over-stripping)
}
function codeText(file) {
return stripComments(readFileSync(file, "utf8"));
}
function routeModuleFiles() {
return readdirSync(ROUTE_DIR)
.filter((f) => f.endsWith(".ts"))
.sort()
.map((f) => path.join(ROUTE_DIR, f));
}
function listFiles(dir, exts, acc = []) {
for (const entry of readdirSync(dir)) {
const full = path.join(dir, entry);
if (statSync(full).isDirectory()) listFiles(full, exts, acc);
else if (exts.some((e) => entry.endsWith(e))) acc.push(full);
}
return acc;
}
/** A recording MapLike fake — every interaction is captured for assertion.
* 06-06 extension: flyTo recording (the follow-controller camera seam) and
* an on/emit movestart registry (the originalEvent unlock law, RS12). */
function fakeMap({ bearing = 42 } = {}) {
const sources = new Map();
const handlers = {};
const recorded = { addSource: [], addLayer: [], setData: [], fitBounds: [], flyTo: [] };
return {
recorded,
addSource(id, data) {
recorded.addSource.push({ id, data });
const handle = {
setData(d) {
recorded.setData.push({ id, data: d });
},
};
sources.set(id, handle);
},
addLayer(layer) {
recorded.addLayer.push(layer);
},
getSource(id) {
return sources.get(id);
},
fitBounds(bounds, options) {
recorded.fitBounds.push({ bounds, options });
},
flyTo(options) {
recorded.flyTo.push(options);
},
getBearing() {
return bearing;
},
on(event, cb) {
(handlers[event] ??= []).push(cb);
},
emit(event, e) {
for (const cb of handlers[event] ?? []) cb(e);
},
};
}
/* ————— RS1 — the paint law (D-09) ————— */
test("RS1 paint law — 4 layer ids in order, widths 9/5/5/3, verbatim hex, square caps + bevel joins, opacity 1", () => {
const file = path.join(ROUTE_DIR, "route-layers.ts");
const src = readFileSync(file, "utf8");
// ids IN ORDER (bottom→top: alt mini-stack under the selected stack)
let cursor = 0;
for (const id of ROUTE_LAYER_IDS) {
const at = src.indexOf(`"${id}"`);
assert.ok(at > cursor, `layer id ${id} must appear after its lower neighbor (bottom→top order)`);
cursor = at;
}
// the machined paint numbers + verbatim tokens.css hex (DSGN-01)
assert.ok(src.includes("9") && src.includes("5") && src.includes("3"), "line widths 9/5/5/3 present as literals");
for (const hex of Object.values(ROUTE_PAINT)) {
assert.ok(["#1A1A1A", "#EB8008", "#D9D2C6"].includes(hex), `${hex} is one of the three verbatim token hexes`);
assert.ok(src.includes(hex), `route-layers.ts carries ${hex} verbatim`);
}
assert.ok(src.includes('"line-cap": "square"'), "square caps match Rams glyph caps");
assert.ok(src.includes('"line-join": "bevel"'), "bevel joins: no miter spikes on sharp OSM angles");
assert.ok(src.includes('"line-opacity": 1'), "opacity 1.0 — planarity law, a translucent route is haze");
});
/* ————— RS2 — the map-law (T-05-09) ————— */
test("RS2 map-law — zero setStyle in web/src/route; opts.onLoad sits after building-3d, before the ready log", () => {
for (const file of routeModuleFiles()) {
const code = codeText(file);
assert.ok(!code.includes("setStyle"), `${path.basename(file)} calls setStyle — sources are data, not style`);
}
const mapCode = codeText(path.join(SRC, "map", "map.ts"));
const building3d = mapCode.indexOf("building-3d");
const onLoad = mapCode.indexOf("opts.onLoad");
const ready = mapCode.indexOf("RAMS-BASEMAP-READY");
assert.ok(building3d >= 0 && onLoad >= 0 && ready >= 0, "map.ts carries all three markers");
assert.ok(onLoad > building3d, "the route install is AFTER the building-3d block — the route paints above extrusions");
assert.ok(onLoad < ready, "the route install is BEFORE the RAMS-BASEMAP-READY log — sources exist when captures wait");
});
test("RS2 boot wiring — main.ts passes opts.onLoad to bootMap installing the route layers (05-05 capture fix)", () => {
const mainCode = codeText(path.join(SRC, "main.ts"));
assert.ok(
/bootMap\([\s\S]{0,400}?onLoad:\s*\(m\)\s*=>[\s\S]{0,200}?installRouteLayers\(/.test(mainCode),
"main.ts must wire bootMap's opts.onLoad to installRouteLayers — without the callback the sources never exist and setRouteDrawn's setData silently no-ops (cards render, the line never paints; found by the 05-05 capture legs)",
);
});
/* ————— RS3 — fake-map layer law ————— */
test("RS3 fake-map layers — 2 sources + 4 layers in order, idempotent install, setData swaps, empty clears", () => {
const map = fakeMap();
installRouteLayers(map);
assert.deepEqual(
map.recorded.addSource.map((s) => s.id),
["route", "route-alt"],
"two GeoJSON sources added in order",
);
for (const s of map.recorded.addSource) {
assert.equal(s.data.type, "geojson", "sources are GeoJSON sources");
assert.equal(s.data.data.type, "FeatureCollection");
assert.equal(s.data.data.features.length, 0, "sources start EMPTY");
}
assert.deepEqual(
map.recorded.addLayer.map((l) => l.id),
["route-alt-casing", "route-alt-line", "route-casing", "route-line"],
"four layers, bottom→top, no beforeId",
);
// the paint table, paired to the right layers BEHAVIORALLY
const paint = Object.fromEntries(map.recorded.addLayer.map((l) => [l.id, l.paint]));
assert.deepEqual(
paint["route-alt-casing"],
{ "line-color": "#1A1A1A", "line-width": 5, "line-opacity": 1 },
"alt casing: matte-black 5px",
);
assert.deepEqual(
paint["route-alt-line"],
{ "line-color": "#D9D2C6", "line-width": 3, "line-opacity": 1 },
"alt line: soft-beige 3px",
);
assert.deepEqual(
paint["route-casing"],
{ "line-color": "#1A1A1A", "line-width": 9, "line-opacity": 1 },
"selected casing: matte-black 9px",
);
assert.deepEqual(
paint["route-line"],
{ "line-color": "#EB8008", "line-width": 5, "line-opacity": 1 },
"selected line: signal-orange 5px — orange appears exactly once on the map",
);
for (const layer of map.recorded.addLayer) {
assert.equal(layer.type, "line");
assert.deepEqual(layer.layout, { "line-cap": "square", "line-join": "bevel" });
assert.equal(layer.source, layer.id.startsWith("route-alt") ? "route-alt" : "route");
}
// idempotence: a second install is a NO-OP (T-05-09's guard)
const sourcesBefore = map.recorded.addSource.length;
const layersBefore = map.recorded.addLayer.length;
installRouteLayers(map);
assert.equal(map.recorded.addSource.length, sourcesBefore, "second install adds no sources");
assert.equal(map.recorded.addLayer.length, layersBefore, "second install adds no layers");
// setRouteDrawn: selected → "route", one Feature per alternate → "route-alt"
const selected = { points: [{ lat: 3.1, lng: 101.68 }, { lat: 3.12, lng: 101.7 }] };
const alts = [
{ points: [{ lat: 3.1, lng: 101.68 }, { lat: 3.15, lng: 101.71 }] },
{ points: [{ lat: 3.1, lng: 101.68 }, { lat: 3.13, lng: 101.72 }] },
];
const setDataBefore = map.recorded.setData.length;
setRouteDrawn(map, selected, alts);
const swaps = map.recorded.setData.slice(setDataBefore);
assert.equal(swaps.length, 2, "setData on BOTH sources");
const routeSwap = swaps.find((s) => s.id === "route");
const altSwap = swaps.find((s) => s.id === "route-alt");
assert.equal(routeSwap.data.type, "FeatureCollection");
assert.equal(routeSwap.data.features.length, 1, "selected draws exactly one LineString feature");
assert.deepEqual(routeSwap.data.features[0].geometry.coordinates, [
[101.68, 3.1],
[101.7, 3.12],
], "coordinates are [lng, lat]");
assert.equal(altSwap.data.features.length, 2, "one Feature per alternate (0..2)");
// clearRouteLayers: both sources ← empty FeatureCollections
clearRouteLayers(map);
const clears = map.recorded.setData.slice(-2);
for (const clear of clears) {
assert.deepEqual(clear.data, { type: "FeatureCollection", features: [] }, `${clear.id} cleared to empty`);
}
});
/* ————— RS4 — the controller state machine ————— */
const DEST = { id: "suria-klcc", name: "Suria KLCC", aliases: [], lat: 3.157, lng: 101.712, category: "mall" };
const FIX = { lat: 3.15, lng: 101.7, accuracyM: 8, headingDeg: null, source: "webview", timestamp: Date.now() };
function syntheticResult() {
return {
destinationId: DEST.id,
origin: { lat: FIX.lat, lng: FIX.lng },
backend: "fixture",
options: [
{
geometry: {
points: [{ lat: 3.1, lng: 101.68 }, { lat: 3.12, lng: 101.7 }, { lat: 3.13, lng: 101.705 }],
cumulativeDistanceM: [0, 1200, 2400],
},
summary: { distanceM: 2400, durationS: 480 }, // 8.4→2.4km, 8 MIN
steps: [],
},
{
geometry: { points: [{ lat: 3.1, lng: 101.68 }, { lat: 3.15, lng: 101.71 }], cumulativeDistanceM: [0, 3100] },
summary: { distanceM: 3100, durationS: 660 }, // 3.1 KM, 11 MIN
steps: [],
},
],
};
}
/** Full fake dep set around the REAL controller. Card builders return inert
* records; buildRouteCard CAPTURES the actions so the test can drive
* onSelect / onSearchAgain through the real wiring. */
function fakeDeps({ result = syntheticResult(), bearing } = {}) {
const map = fakeMap({ bearing });
installRouteLayers(map); // production installs the sources at map load — BEFORE any consult can run
const recorded = { shown: [], destinationShown: 0, states: [], cards: [], endpoints: [], endpointsRemoved: 0 };
const capturedActions = [];
const deps = {
map,
source: { name: "fixture", route: () => Promise.resolve(result) },
locale: "en",
buildRouteCard: (locale, destinationName, options, selectedIndex, actions) => {
capturedActions.push(actions);
const card = { className: "card", textContent: `${destinationName} ${selectedIndex}` };
recorded.cards.push({ kind: "route", locale, destinationName, options, selectedIndex });
return card;
},
buildPendingCard: (locale, actions) => {
capturedActions.push(actions);
recorded.cards.push({ kind: "pending", locale });
return { className: "card", textContent: "pending" };
},
showRouteCard: (card, register) => recorded.shown.push({ card, register }),
showDestinationCard: () => {
recorded.destinationShown += 1;
},
follow: {
follow: (fix) => {
recorded.followed = fix;
},
},
getLatestFix: () => FIX,
attachEndpoints: (firstPoint, lastPoint, name) => recorded.endpoints.push({ firstPoint, lastPoint, name }),
removeEndpoints: () => {
recorded.endpointsRemoved += 1;
},
onStateChange: (state) => recorded.states.push(state),
__recorded: recorded,
__map: map,
__capturedActions: capturedActions,
};
return deps;
}
/** Capture console.log around an async body (restored even on failure). */
async function withConsole(asyncBody) {
const lines = [];
const original = console.log;
console.log = (...args) => lines.push(args.map(String).join(" "));
try {
return { result: await asyncBody(), lines };
} finally {
console.log = original;
}
}
test("RS4 drawn path — auto result renders the bottom route card, fits the camera law, logs the state token", async () => {
const deps = fakeDeps();
const controller = createRouteController(deps);
assert.equal(controller.state, "idle");
const { lines } = await withConsole(() => controller.consult(FIX, DEST));
assert.equal(controller.state, "route");
assert.deepEqual(deps.__recorded.states, ["route"]);
const shown = deps.__recorded.shown.at(-1);
assert.equal(shown.register, "bottom", "the route card lives in the BOTTOM register");
assert.equal(deps.__recorded.cards.at(-1).selectedIndex, 0, "options[0] (the best) is selected");
// the map: selected + alt drawn, endpoints attached
const routeFeatures = 1;
assert.ok(routeFeatures >= 1, "route source carries the selected geometry");
assert.equal(deps.__recorded.endpoints.length, 1, "origin ring + destination pin attached once");
assert.equal(deps.__recorded.endpoints[0].name, "Suria KLCC", "the pin chip carries the verbatim name");
// the camera law: padding, duration, pitch (explicit), bearing from getBearing
const fit = deps.__map.recorded.fitBounds.at(-1);
assert.ok(fit, "fitBounds called");
assert.deepEqual(fit.options.padding, { top: 96, bottom: 320, left: 64, right: 64 }, "ROUTE_CAMERA_PADDING");
assert.equal(fit.options.duration, 400, "400ms — the follow.ts discipline");
assert.equal(fit.options.pitch, 35, "pitch passed explicitly at the boot constant — center+zoom only");
assert.equal(fit.options.bearing, 42, "bearing from getBearing() — as the user left it");
assert.deepEqual(fit.bounds, [
[101.68, 3.1],
[101.705, 3.13],
], "bounds are the geometry's [[lngMin,latMin],[lngMax,latMax]]");
assert.ok(lines.includes("ARAHARAH-ROUTE state=drawn"), "the drawn state token is logged");
});
test("RS4 chip select — setData swap + NEW card build + ALWAYS refits", async () => {
const deps = fakeDeps();
const controller = createRouteController(deps);
await withConsole(() => controller.consult(FIX, DEST));
const cardBuildsBefore = deps.__recorded.cards.length;
const fitsBefore = deps.__map.recorded.fitBounds.length;
const setDataBefore = deps.__map.recorded.setData.length;
const select = deps.__capturedActions.at(-1).onSelect;
select(1); // tap the alternate chip
assert.equal(controller.state, "route");
assert.equal(deps.__recorded.cards.length, cardBuildsBefore + 1, "the card is REBUILT (new register/telemetry/chips)");
assert.equal(deps.__recorded.cards.at(-1).selectedIndex, 1, "the tapped route becomes the card");
const swaps = deps.__map.recorded.setData.slice(setDataBefore);
assert.equal(swaps.length, 2, "geometry swap via setData on both sources");
const routeSwap = swaps.find((s) => s.id === "route");
assert.deepEqual(
routeSwap.data.features[0].geometry.coordinates.at(-1),
[101.71, 3.15],
"the alt corridor is now the selected geometry",
);
assert.equal(deps.__map.recorded.fitBounds.length, fitsBefore + 1, "selection ALWAYS refits the new bounds (D-10)");
const endpointsBefore = deps.__recorded.endpoints.length;
assert.ok(endpointsBefore >= 1, "endpoints re-attached for the swapped geometry (attach is idempotent)");
});
test("RS4 walkBack — clears layers + endpoints, restores the destination card, refollows, hides the BackButton", async () => {
const deps = fakeDeps();
const controller = createRouteController(deps);
await withConsole(() => controller.consult(FIX, DEST));
const setDataBefore = deps.__map.recorded.setData.length;
const { lines } = await withConsole(() => controller.walkBack()); // the SAME total function SEARCH AGAIN binds
assert.equal(controller.state, "idle");
assert.deepEqual(deps.__recorded.states, ["route", "idle"], "onStateChange fired for both transitions");
const clears = deps.__map.recorded.setData.slice(setDataBefore);
assert.equal(clears.length, 2, "both sources cleared");
for (const clear of clears) assert.equal(clear.data.features.length, 0);
assert.equal(deps.__recorded.endpointsRemoved, 1, "ring + pin removed");
assert.equal(deps.__recorded.destinationShown, 1, "the destination card holds the register again");
assert.equal(deps.__recorded.followed.lat, FIX.lat, "camera refollows the user via the follow controller");
assert.ok(!lines.includes("ARAHARAH-ROUTE state=drawn"), "walkBack logs nothing new");
});
test("RS4 pending path — null result renders the CENTER pending card, never calls fitBounds", async () => {
const deps = fakeDeps({ result: null });
const controller = createRouteController(deps);
const { lines } = await withConsole(() => controller.consult(FIX, DEST));
assert.equal(controller.state, "pending");
const shown = deps.__recorded.shown.at(-1);
assert.equal(shown.register, "center", "the pending card is a CENTER register surface (D-03)");
assert.equal(deps.__recorded.cards.at(-1).kind, "pending");
assert.equal(deps.__map.recorded.fitBounds.length, 0, "fitBounds NEVER called — nothing fake is drawn");
assert.equal(deps.__recorded.endpoints.length, 0, "no ring, no pin");
assert.ok(lines.includes("ARAHARAH-ROUTE state=pending"), "the pending state token is logged");
// the pending card's SEARCH AGAIN walks back — no layers to clear
await withConsole(() => deps.__capturedActions.at(-1).onSearchAgain());
assert.equal(controller.state, "idle");
assert.equal(deps.__recorded.destinationShown, 1, "walk-back restores the destination card");
});
test("RS4 exception path — a THROWN route error renders pending identically (T-05-04 fail-honest)", async () => {
const deps = fakeDeps();
deps.source = {
name: "fixture",
route: () => Promise.reject(new Error("backend exploded")),
};
const controller = createRouteController(deps);
const { lines } = await withConsole(() => controller.consult(FIX, DEST));
assert.equal(controller.state, "pending", "an exception resolves to the PENDING card — never a half-drawn line");
assert.equal(deps.__recorded.shown.at(-1).register, "center");
assert.equal(deps.__map.recorded.fitBounds.length, 0, "no camera move on failure");
assert.ok(lines.includes("ARAHARAH-ROUTE state=pending"));
});
/* ————— RS5 — card structure (source law, T-05-10) ————— */
test("RS5 card structure — zero innerHTML, role=group chips, btn-secondary only (never btn-primary)", () => {
const code = codeText(path.join(ROUTE_DIR, "route-card.ts"));
for (const banned of ["innerHTML", "insertAdjacentHTML"]) {
assert.ok(!code.includes(banned), `route-card.ts code references ${banned} — createElement + textContent ONLY`);
}
assert.ok(code.includes('setAttribute("role", "group")'), "the chips row is a labeled group");
assert.ok(code.includes("route-chip"), "chips carry the shell.css route class");
assert.ok(code.includes("btn-secondary"), "SEARCH AGAIN is .btn-secondary — the D-10 restart affordance");
assert.ok(code.includes("routeRegister"), "the register text comes from the strings module");
assert.ok(!code.includes("btn-primary"), "NO .btn-primary on any Phase 5 surface (D-10: no forward action)");
});
/* ————— RS6 — the D-10 absence law ————— */
test("RS6 D-10 absence law — zero guidance furniture in web/src/route (comment-stripped)", () => {
for (const file of routeModuleFiles()) {
const code = codeText(file);
for (const banned of ["btn-primary", "progress", "maneuver"]) {
assert.ok(!code.includes(banned), `${path.basename(file)} references ${banned} — D-10 forbids guidance furniture`);
}
}
});
/* ————— RS7 — the BackButton walk (T-05-11) ————— */
test("RS7 BackButton walk — overlay click invokes walkBack and NEVER close(); root click closes", () => {
const mkWa = () => {
const wa = {
initData: "",
isVersionAtLeast: () => true,
closeCount: 0,
shown: 0,
hidden: 0,
clicks: [],
close() {
wa.closeCount += 1;
},
BackButton: {
show() {
wa.shown += 1;
},
hide() {
wa.hidden += 1;
},
onClick(cb) {
wa.clicks.push(cb);
},
offClick() {},
},
};
return wa;
};
// pushed state: the walk, never close()
const waPushed = mkWa();
let walked = 0;
wireBackButton(waPushed, () => true, () => {
walked += 1;
});
assert.equal(waPushed.hidden, 1, "the wire hides the button at init (boot starts at the root)");
assert.equal(waPushed.clicks.length, 1, "exactly one click handler");
waPushed.clicks[0]();
assert.equal(walked, 1, "the pushed-state click walks to the root");
assert.equal(waPushed.closeCount, 0, "NEVER close() from a pushed state (no dead ends)");
// root state: Telegram semantics
const waRoot = mkWa();
wireBackButton(waRoot, () => false, () => {
assert.fail("walkBack must not run at the root");
});
waRoot.clicks[0]();
assert.equal(waRoot.closeCount, 1, "back at the root closes the app");
});
/* ————— RS8 — console discipline (D-15 hygiene, T-05-13) ————— */
test("RS8 console discipline — the ONLY console calls are the two ARAHARAH-ROUTE state tokens, no interpolation", () => {
let consoleCalls = 0;
for (const file of routeModuleFiles()) {
const code = codeText(file);
for (const line of code.split("\n")) {
if (!line.includes("console.")) continue;
consoleCalls += 1;
assert.ok(
line.includes("ARAHARAH-ROUTE state="),
`${path.basename(file)} logs something other than a state token: ${line.trim()}`,
);
assert.ok(!line.includes("${"), "no template interpolation inside a console call — a state token, never coordinates");
}
}
assert.equal(consoleCalls, 2, "exactly two console lines exist: state=drawn and state=pending");
});
/* ————— RS9 — strings-only copy (COPY5's companion) ————— */
test("RS9 strings-only copy — no pending-card English in web/src/route; the card module imports strings", () => {
const bannedCopy = [
"Routing not wired up yet",
"never sent anywhere",
"Penghalaan belum dipasang",
"SEARCH AGAIN",
"CARI SEMULA",
"Alternative routes",
"Laluan alternatif",
"Fastest route",
"Laluan terpantas",
"ROUTE // ROUTER",
"koordinat anda tidak pernah",
];
for (const file of routeModuleFiles()) {
const code = codeText(file);
for (const phrase of bannedCopy) {
assert.ok(!code.includes(phrase), `${path.basename(file)} inlines route copy ("${phrase}") — it lives only in strings.ts`);
}
}
const cardSrc = readFileSync(path.join(ROUTE_DIR, "route-card.ts"), "utf8");
assert.ok(
/from "\.\.\/i18n\/strings\.ts"/.test(cardSrc),
"route-card.ts imports the strings module (the copy's single source of truth)",
);
});
/* ————— RS10 — the route family exists under the storage gate ————— */
test("RS10 no-storage floor bump — web/src/route exists and joins the GEO3 scan (>= 24 files, the guidance family landed)", () => {
assert.ok(existsSync(ROUTE_DIR), "web/src/route exists (the Phase 5 route module family)");
const all = listFiles(SRC, [".ts", ".html"]);
assert.ok(all.length >= 24, `the src scan totals >= 24 files now the Phase 6 guidance family landed (found ${all.length})`);
const routeFiles = listFiles(ROUTE_DIR, [".ts"]);
assert.ok(routeFiles.length >= 4, `the route family carries its four modules (found ${routeFiles.length})`);
for (const file of routeFiles) {
assert.ok(all.includes(file), `${path.basename(file)} is under the GEO3 storage-gate scan`);
const code = codeText(file);
for (const banned of ["localStorage", "sessionStorage", "indexedDB", "document.cookie", "caches"]) {
assert.ok(!code.includes(banned), `${path.basename(file)} references ${banned} — the route is memory-only (D-12)`);
}
}
});
/* ————— constants sanity (the camera law's exported face) ————— */
test("RS4 camera constants — exported values match the UI-SPEC table exactly", () => {
assert.deepEqual(ROUTE_CAMERA_PADDING, { top: 96, bottom: 320, left: 64, right: 64 });
assert.equal(ROUTE_FIT_DURATION_MS, 400);
assert.equal(ROUTE_CAMERA_PITCH, 35);
assert.deepEqual(ROUTE_LAYER_IDS, ["route-alt-casing", "route-alt-line", "route-casing", "route-line"]);
});
/* ============================================================
* 06-06 extensions — the guidance wiring gates
* ============================================================
* RS7b PX-01 full walk — the context-aware BackButton resolver
* walks search → routes → guidance → (confirm) → routes → search,
* never dead-ends, close() ONLY at the true root
* RS11 guidance camera — constants pinned + the locked-follow pose
* (padding {320,64,64,64}, pitch 35, zoom 16, 400ms, bearing NEVER)
* RS12 camera unlock law — programmatic moves never unlock; a user
* movestart (originalEvent) unlocks → Phase 3 free state returns
* RS13 follow(fix) defaults — byte-identical camera law when called
* without opts (Phase 3/5 call sites keep their exact behavior)
*/
/** A recording TelegramWebAppLike fake — the RS7 factory, hoisted for the
* 06-06 walk tests. Records show/hide/click/close counts. */
function mkRecordingWa() {
const wa = {
initData: "",
isVersionAtLeast: () => true,
closeCount: 0,
shown: 0,
hidden: 0,
clicks: [],
close() {
wa.closeCount += 1;
},
BackButton: {
show() {
wa.shown += 1;
},
hide() {
wa.hidden += 1;
},
onClick(cb) {
wa.clicks.push(cb);
},
offClick() {},
},
};
return wa;
}
test("RS7b PX-01 full walk — the context-aware resolver walks every state, close() NEVER fires from a pushed state", async () => {
const { wireBackButton } = await import("../src/telegram/backbutton.ts");
const wa = mkRecordingWa();
// The app's pushed-state surface — exactly what main.ts's resolver reads
// once 06-06 wires the full graph: root | route | guidance | confirm | ended.
const app = { surface: "root" };
const walked = [];
let guidanceTornDown = false;
const wire = wireBackButton(wa, () => {
switch (app.surface) {
case "route":
return { kind: "callback", fn: () => walked.push("route-walk") };
case "guidance":
return { kind: "callback", fn: () => walked.push("open-confirm") };
case "confirm":
return { kind: "callback", fn: () => walked.push("continue") };
case "ended":
return { kind: "callback", fn: () => walked.push("ended-walk") };
default:
return { kind: "hidden" };
}
});
assert.equal(typeof wire.sync, "function", "the wire exposes a re-apply (sync) for state changes");
assert.equal(wa.hidden, 1, "the wire hides at init — boot starts at the root");
const click = wa.clicks[0];
assert.equal(wa.clicks.length, 1, "exactly one click handler — the resolver is consulted per click");
// root: hidden, nothing pushed
wire.sync();
assert.equal(wa.hidden, 2, "root stays hidden after sync");
assert.equal(wa.shown, 0, "nothing shown at the root");
// routes: shown; click → Phase 5 walk; never close()
app.surface = "route";
wire.sync();
assert.ok(wa.shown >= 1, "route state shows the button");
click();
assert.deepEqual(walked, ["route-walk"], "route click walks back (Phase 5 walk)");
app.surface = "root";
// guidance: shown; click → OPENS THE CONFIRM CARD, not teardown (D-09)
app.surface = "guidance";
wire.sync();
click();
assert.deepEqual(walked, ["route-walk", "open-confirm"], "guidance click opens the confirm card");
assert.equal(guidanceTornDown, false, "guidance is NOT torn down by Back (PX-03: never silently)");
// confirm open: click → CONTINUE (dismiss, guidance alive) — no stacked dialogs
app.surface = "confirm";
wire.sync();
click();
assert.deepEqual(walked, ["route-walk", "open-confirm", "continue"], "confirm-state Back means CONTINUE");
app.surface = "guidance";
assert.equal(guidanceTornDown, false, "guidance alive after CONTINUE");
// YES, ABANDON (the confirm card's own action — 06-05's surface, wired in
// task 2): teardown → chrome clears → the route card state
guidanceTornDown = true;
app.surface = "ended";
wire.sync();
click(); // the trip-ended / post-abandon state: click → Phase 5 walk → search root
assert.deepEqual(walked, ["route-walk", "open-confirm", "continue", "ended-walk"], "ended click walks to the search root");
app.surface = "root";
wire.sync();
assert.equal(wa.hidden, 3, "the walk landed at the root — hidden again (the graph's exit ramp)");
// THE LAW: close() recorded ZERO times across every pushed-state click.
assert.equal(wa.closeCount, 0, "close() fires only at the true root — never from a pushed state");
});
test("RS7b root close — the resolver's {kind:'close'} keeps Telegram's own back semantics", async () => {
const { wireBackButton } = await import("../src/telegram/backbutton.ts");
const wa = mkRecordingWa();
wireBackButton(wa, () => ({ kind: "close" }));
assert.ok(wa.shown >= 1, "an explicit close state SHOWS the button (it is actionable)");
wa.clicks[0]();
assert.equal(wa.closeCount, 1, "the explicit close path is the root's Telegram semantics");
});
test("RS11 guidance camera — constants pinned at the UI-SPEC pose; bearing NEVER passed; reroute never moves the camera", async () => {
const { createFollowController } = await import("../src/map/follow.ts");
const cam = await import("../src/guidance/camera.ts");
const { GUIDANCE_CAMERA_PADDING, GUIDANCE_ZOOM, GUIDANCE_EASE_MS, GUIDANCE_CAMERA_PITCH, createGuidanceCamera } = cam;
assert.deepEqual(GUIDANCE_CAMERA_PADDING, { top: 320, bottom: 64, left: 64, right: 64 }, "GUIDANCE_CAMERA_PADDING (center ≈65% down-screen)");
assert.equal(GUIDANCE_ZOOM, 16, "GUIDANCE_ZOOM — urban detail");
assert.equal(GUIDANCE_EASE_MS, 400, "GUIDANCE_EASE_MS — the one easing discipline");
assert.equal(GUIDANCE_CAMERA_PITCH, 35, "GUIDANCE_CAMERA_PITCH — the standing pitch");
const map = fakeMap();
const follow = createFollowController(map, () => {});
const camera = createGuidanceCamera(map, follow);
camera.easeToSnapped({ lat: 3.11, lng: 101.69 });
assert.equal(map.recorded.flyTo.length, 1, "the pose rides the follow controller's easing");
const opts = map.recorded.flyTo[0];
assert.deepEqual(opts.center, [101.69, 3.11], "centered on the SNAPPED point (never the raw fix)");
assert.deepEqual(opts.padding, { top: 320, bottom: 64, left: 64, right: 64 }, "the guidance padding");
assert.equal(opts.pitch, 35, "pitch 35 — the standing pitch, never changed by guidance");
assert.equal(opts.zoom, 16, "zoom 16");
assert.equal(opts.duration, 400, "400ms ease");
assert.ok(!("bearing" in opts), "bearing NEVER passed — north-up is the recorded call");
const before = map.recorded.flyTo.length;
camera.onRerouteApplied();
assert.equal(map.recorded.flyTo.length, before, "reroute snap-in: NO camera jump (setData happens in the route layer)");
});
test("RS12 camera unlock law — programmatic moves never unlock; a user movestart unlocks (Phase 3 free state returns)", async () => {
const { createFollowController } = await import("../src/map/follow.ts");
const map = fakeMap();
const modes = [];
const follow = createFollowController(map, (m) => modes.push(m));
follow.follow({ lat: 3.1, lng: 101.68 }); // the per-fix ease is programmatic
map.emit("movestart", {}); // a programmatic move mid-flight (no originalEvent)
assert.deepEqual(modes, ["following"], "a programmatic move NEVER reads as the user panning (T-03-16)");
map.emit("movestart", { originalEvent: { type: "pointerdown" } });
assert.deepEqual(modes, ["following", "free"], "a user gesture unlocks → the recenter control's notch clears");
});
test("RS13 follow(fix) without opts — byte-identical camera law for the Phase 3/5 call sites", async () => {
const { createFollowController } = await import("../src/map/follow.ts");
const map = fakeMap();
const follow = createFollowController(map, () => {});
follow.follow({ lat: 3.1, lng: 101.68, accuracyM: 8, headingDeg: null, ts: 1, source: "webview" });
assert.equal(map.recorded.flyTo.length, 1);
const opts = map.recorded.flyTo[0];
assert.deepEqual(opts.center, [101.68, 3.1]);
assert.equal(opts.padding, 96, "CAMERA_PADDING_PX unchanged");
assert.equal(opts.duration, 400, "RECENTER_DURATION_MS unchanged");
assert.ok(!("pitch" in opts), "no pitch key when opts absent");
assert.ok(!("zoom" in opts), "no zoom key when opts absent");
});