mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-22 17:58:06 +00:00
The repeater planner (#2391) gets a fourth tab, **Coverage**: put a marker where you are thinking of placing a repeater, set its antenna height, and see on the map who would hear it. It answers the siting question "what does a repeater reach if we put it here", with two layers: handheld users and other repeaters. ## What it shows - **Users layer:** the chance that a handheld at 1.5 m hears the site, in 4 bands (10-25%, 25-50%, 50-75%, 75% and up; below 10% is not painted). The chance comes from a distance curve `P(d) = 1 / (1 + exp((ln d - ln d50) / slope))`, fitted on drive-test receptions of 41 Belgian repeaters (d50 2.7 km, slope 0.30). Terrain only lowers a cell by 2 bands where a 1.5 m receiver sits deeper than 1 first-Fresnel radius under the terrain (`cmd/server/planner_viewshed.go:19-21`). Antenna height does not change the curve: in the drive tests, handheld reach barely changed between 1, 7 and 15 m. The panel says so in one line. - **Repeaters layer:** first-Fresnel clearance from the site to every cell, for a receiver at "Repeaters at" height (default `planner.existingAntennaM`). 3 classes: line of sight (clearance 0% or more), slightly blocked (-20% to 0%), blocked (not painted). Every known repeater within range gets its link computed with the existing planner link code, a dot in its class colour, a line for line of sight and slightly blocked, and an entry in a list. - **Compute button:** changing site, height or range moves the marker and updates the URL hash right away, and dims an existing result with an "Outdated" note. Only "Compute coverage" starts a job. A shared link that carries a site computes once on open. The planner has 1 heavy-job slot shared with Area and allows 6 jobs per minute per IP, so recomputing on every change would burn both. State is in the hash: `#/tools/planner?mode=coverage&site=51.06,4.41&h=10&rx=5&range=30&layer=repeaters`. This builds on the repeater planner @zfouts wrote in #2391: the terrain tiles, the first-Fresnel analysis, the link code, the job protocol and the Area module pattern are all reused here. Thanks for that work, it made this tab a small addition instead of a new subsystem. ## Why the Repeaters classes are not the route LOS verdicts The route verdicts (clear 100%+, clear60 60%+, marginal 0%+, blocked) almost never show on Shuttle Radar Topography Mission (SRTM) terrain at mast heights. At 10 m / 5 m, 30 km around Mechelen, 109 of 70,980 cells reach clear60 or clear. SRTM includes trees and roofs, so the line between 2 masts rarely clears 60% of the Fresnel zone. I checked the clearance against one Belgian repeater (28 m, receivers at 5 m) and the repeaters live data shows it actually hears: | Distance | Heard, clearance >= 0 | Heard, clearance < 0 | |---|---|---| | 0-10 km | 9 of 11 | none in range | | 10-20 km | 3 of 4 | 1 of 8 | | 20-30 km | 0 of 6 | 3 of 35 | | 30-60 km | 1 of 2 | 0 of 84 | Every link heard within 16 km had a clearance between 2% and 54%, so "marginal" is where working links live. The map classes follow that (`repeaterClass`, `cmd/server/planner_viewshed.go:75-89`). Chain, Relay and Area keep the shared verdicts; `losVerdict` is unchanged. This rests on 17 heard links from 1 repeater, which is why the -20% band is narrow and the panel says line of sight stops being enough beyond about 20 km. ## Server - `GET /api/planner/viewshed?site=&h=&rx=&range=` (`cmd/server/planner_api.go:603`, route `cmd/server/routes.go:393`). Same job protocol, result cache, rate limits and opt-in as the other planner endpoints: 404 while `planner.enabled` is false. - One byte per cell: low nibble Users class 1-5, high nibble Repeaters class 1-3, 0 for no data. The grid is about 300 x 300 at every range: 0.2 km cells up to 30 km, `range/150` above (`planner_viewshed.go:47`). - Links skip rooms, stale repeaters and a repeater within 100 m of the site (that is the site itself). They carry no `elev_m` profile. - `truncated` is true only when the 300-node cap may have dropped a repeater within range (`viewshedTruncated`, `planner_api.go:694`). - Tile cap: `Viewshed` in the planner tile limits, sized from the largest offered range (60 km). - New config: `planner.userReach.{d50Km,slope}` with a `_comment` in `config.example.json`. Bounds 0.2-50 km and 0.05-2. - No writes, no new `map[string]interface{}`; response types `PlannerViewshedResponse/Params/Link` in `planner_types.go`. ## Frontend - `public/planner-coverage.js` + `.css`, mounted like `planner-area.js`. `planner.js` gets a `MODULES` table (`public/planner.js:31`) so Area and Coverage share one enter/leave path. - Canvas raster in an `L.imageOverlay`; the layer switch repaints locally without a new request. Requests go through `fetchJob` / `CSApi`. - Colours are CSS variables `--pl-cov-user-2..5` and `--pl-cov-rep-1..3` in `:root` and both dark blocks of `style.css`; the Repeaters ones point at `--status-*`, so a theme that changes those changes the map. ## Performance The job is opt-in and runs in the existing planner job slot, never in a hot path. Measured on real SRTM tiles, local build, 304 Belgian repeaters: | Site | Range | Mast | Job time | |---|---|---|---| | Maasvallei (hilly) | 30 km | 10 m | 2.07 s | | Maasvallei | 30 km | 30 m | 2.57 s | | Maasvallei | 60 km | 10 m | 3.98 s | | Mechelen (flat) | 30 km | 10 m | 2.61 s | On a staging instance (ARM64, cold tile cache) a 30 km job took 6.8 s including the tile downloads. Response size at 60 km with 115 links: 239,942 bytes with link elevation profiles, 149,984 without (what this PR ships). `BenchmarkViewshedMaxLeg` covers the 60 km worst case on a synthetic terrain view. ## Verification - Go: 26 viewshed tests and benchmarks in `planner_viewshed_test.go`, `planner_api_test.go`, `planner_config_test.go` and `planner_tiles_test.go`, among them: flat terrain equals the plain curve and `analyzeLOS`, a ridge shadows only its far side, missing terrain is "no data", the `truncated` rule (proven to fail on the old rule), links without `elev_m`, the repeater at the site left out. `go test ./...` in `cmd/server` passes. - Frontend: `tests/unit/test-planner-coverage.js` (23 tests, in `test-all.sh`): grid decode, class-to-colour mapping, unpainted classes, hash round trip, no fetch on parameter change, 1 fetch per click, deep-link autorun once, stale responses ignored, link classes at the boundaries, escaped repeater names. - E2E: 5 Coverage cases in `tests/e2e/test-planner-e2e.js` (stubbed viewshed): click and compute, layer switch without a request, outdated then recompute, deep link, leaving the tab removes the overlay. Planner E2E 25/25 locally. - eslint 0 errors. Browser: light and dark theme, Users and Repeaters on flat, hilly and coastal sites, deep links, no console errors; also on a staging instance with live data. ## Not in this PR - The `--pl-cov-*` colours are not in the customizer's `THEME_CSS_MAP` yet. They follow `--status-*` for the Repeaters layer; the Users colours need their own entries (AGENTS.md rule 8, later milestone). - In Flanders the Users layer is close to a circle: 0.3% of cells differ from the plain curve around Mechelen, 42% in the Maasvallei, 64% in the Ardennes. The terrain correction is untested in hilly areas, the drive tests had no repeater there. - The Repeaters calibration uses 1 repeater. Other operators with drive tests or neighbour data can tighten it. - A link's class comes from `clearance_pct`, rounded to 0.1%, while the grid uses the raw clearance: within 0.05% of a bound a link can land 1 class off the cell under it. List, line and dot always agree with each other. - An OS dark-mode switch without the theme toggle does not repaint the overlay until the next compute, the same as the planner's tile layer. --------- Co-authored-by: zfouts <zachary.fouts@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
629 lines
38 KiB
JavaScript
629 lines
38 KiB
JavaScript
/**
|
||
* Repeater planner (#/tools/planner): loads the REAL public/los.js,
|
||
* public/los-panel.js and public/planner.js in a vm context with a stub
|
||
* window, location/history, fake timers and a stub fetch.
|
||
*
|
||
* Covers the window.MeshPlanner surface the area module builds on (fetchJob's
|
||
* job protocol, params/setParams, fmt*, esc) and the pure helpers in
|
||
* _internals: point (de)serialisation, hash parsing per mode, legsFromPoints,
|
||
* chipFor, and that every HTML builder keeps hostile node names inert. The
|
||
* page itself (map, panels, profile panel) is covered in the browser.
|
||
*/
|
||
'use strict';
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const vm = require('vm');
|
||
const { loadApiBase } = require('./api-base-sandbox');
|
||
const REPO_ROOT = path.resolve(__dirname, '..', '..');
|
||
|
||
let passed = 0, failed = 0;
|
||
function assert(cond, msg) {
|
||
if (cond) { passed++; console.log(' ✓ ' + msg); }
|
||
else { failed++; console.error(' ✗ ' + msg); }
|
||
}
|
||
const read = f => fs.readFileSync(path.join(REPO_ROOT, 'public', f), 'utf8');
|
||
const LOS_SRC = read('los.js');
|
||
const PANEL_SRC = read('los-panel.js');
|
||
const PLANNER_SRC = read('planner.js');
|
||
const flush = () => new Promise(r => setImmediate(r));
|
||
const eq = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
||
|
||
// ─── Fake timers: setTimeout only queues; the test runs them ─────────────
|
||
|
||
function fakeTimers() {
|
||
let seq = 0;
|
||
const q = [];
|
||
return {
|
||
setTimeout(fn, ms) { const t = { id: ++seq, fn, ms }; q.push(t); return t.id; },
|
||
clearTimeout(id) { const i = q.findIndex(t => t.id === id); if (i >= 0) q.splice(i, 1); },
|
||
pending: () => q.slice(),
|
||
runNext() { const t = q.shift(); if (t) t.fn(); return t; }
|
||
};
|
||
}
|
||
|
||
// ─── Stub responses ───────────────────────────────────────────────────────
|
||
|
||
function res(status, body, headers) {
|
||
const h = headers || {};
|
||
return {
|
||
status, ok: status >= 200 && status < 300,
|
||
json: () => (body === undefined ? Promise.reject(new Error('no body')) : Promise.resolve(body)),
|
||
headers: { get: k => (Object.prototype.hasOwnProperty.call(h, k) ? h[k] : null) }
|
||
};
|
||
}
|
||
|
||
function makeFetch(responses) {
|
||
const calls = [];
|
||
const f = (url, opts) => {
|
||
calls.push({ url, opts });
|
||
const next = responses.shift();
|
||
if (typeof next === 'function') return next(url, opts);
|
||
return next instanceof Error ? Promise.reject(next) : Promise.resolve(next);
|
||
};
|
||
f.calls = calls;
|
||
return f;
|
||
}
|
||
|
||
// ─── Context loader ───────────────────────────────────────────────────────
|
||
|
||
function load(opts) {
|
||
opts = opts || {};
|
||
const timers = opts.timers || fakeTimers();
|
||
const replaced = [];
|
||
const ctx = {
|
||
console: { log() {}, error() {}, warn() {} },
|
||
setTimeout: timers.setTimeout, clearTimeout: timers.clearTimeout,
|
||
location: { hash: opts.hash || '#/tools/planner' },
|
||
history: { state: null, replaceState(s, t, u) { replaced.push(u); ctx.location.hash = u; } },
|
||
fetch: opts.fetch || makeFetch([]),
|
||
AbortController, Map, Promise, Date
|
||
};
|
||
Object.assign(ctx, opts.globals || {});
|
||
ctx.window = ctx;
|
||
vm.createContext(ctx);
|
||
loadApiBase(ctx, opts.deployment);
|
||
vm.runInContext(LOS_SRC, ctx);
|
||
vm.runInContext(PANEL_SRC, ctx);
|
||
vm.runInContext(PLANNER_SRC, ctx);
|
||
return { ctx, MP: ctx.MeshPlanner, I: ctx.MeshPlanner._internals, LOS: ctx.MeshLOS, timers, replaced };
|
||
}
|
||
|
||
const N = 100;
|
||
const flat = h => Array.from({ length: N }, () => h);
|
||
const ridge = (base, peak) => Array.from({ length: N }, (_, i) => base + peak * Math.exp(-Math.pow((i - 50) / 6, 2)));
|
||
|
||
async function main() {
|
||
const { MP, I, LOS } = load();
|
||
|
||
console.log('\n=== MeshPlanner surface ===');
|
||
['fetchJob', 'showProfiles', 'esc', 'fmtKm', 'fmtPct', 'params', 'setParams'].forEach(k => {
|
||
assert(typeof MP[k] === 'function', 'MeshPlanner.' + k + ' is a function');
|
||
});
|
||
['parsePts', 'serializePts', 'parseParams', 'legsFromPoints', 'chipFor'].forEach(k => {
|
||
assert(typeof I[k] === 'function', '_internals.' + k + ' is a function');
|
||
});
|
||
assert(MP.esc('<a href="x">\'&') === '<a href="x">'&', 'esc escapes & < > " \'');
|
||
assert(MP.fmtKm(3.14159) === '3.14 km' && MP.fmtKm(42.26) === '42.3 km' && MP.fmtKm(123.4) === '123 km', 'fmtKm: 2 / 1 / 0 decimals by size');
|
||
assert(MP.fmtKm(null) === '—' && MP.fmtKm('x') === '—', 'fmtKm: unknown → —');
|
||
assert(MP.fmtPct(62.4) === '62%' && MP.fmtPct(-45.2) === '−45%' && MP.fmtPct(5000) === '>999%', 'fmtPct: rounds, true minus, caps');
|
||
assert(MP.fmtPct(null) === '—' && MP.fmtPct(NaN) === '—', 'fmtPct: unknown → —');
|
||
|
||
console.log('\n=== fmtClearance ===');
|
||
{
|
||
const f = MP.fmtClearance;
|
||
assert(f(999) === 'full clearance' && f(1500) === 'full clearance', 'fmtClearance: >= 999 → full clearance');
|
||
assert(f(998.9) === '999%', 'fmtClearance: 998.9 → 999%');
|
||
assert(f(0) === '0%' && f(62.4) === '62%', 'fmtClearance: ordinary values');
|
||
assert(f(-99.9) === '−100%', 'fmtClearance: -99.9 → −100%');
|
||
assert(f(-100) === 'terrain blocks the path' && f(-999) === 'terrain blocks the path', 'fmtClearance: <= -100 → terrain blocks the path');
|
||
assert(f(null) === '—' && f('') === '—' && f(NaN) === '—', 'fmtClearance: unknown → —');
|
||
assert(MP.fmtPct(-999) === '−999%' && MP.fmtPct(5000) === '>999%', 'fmtPct unchanged');
|
||
const area = {};
|
||
area.window = area;
|
||
vm.createContext(area);
|
||
vm.runInContext(read('planner-area.js'), area);
|
||
const fb = area.MeshPlannerArea._internals.fmtClearance;
|
||
const samples = [999, 1500, 998.9, 62.4, 0, -0.4, -45.2, -99.9, -100, -999, null, '', NaN, 'x', '12'];
|
||
assert(samples.every(x => fb(x) === f(x)), 'planner-area fallback fmtClearance matches the page helper');
|
||
const dh = I.directHtml({ distance_km: 41.3, clearance_pct: -999, verdict: 'blocked' }, 'Point A', 'Point B');
|
||
assert(/Point A → Point B · 41\.3 km · terrain blocks the path/.test(dh), 'direct link line wording');
|
||
assert(!/999/.test(dh), 'no sentinel number in the direct link');
|
||
assert(I.directHtml({ distance_km: 5, clearance_pct: 62, verdict: 'clear' }, 'A', 'B').includes('F1 clearance 62%'), 'ordinary direct clearance keeps its label');
|
||
const row = I.optionRowHtml({ kind: 'open', lat: 1, lon: 2, leg_a: { verdict: 'clear', clearance_pct: 999 }, leg_b: { verdict: 'clear', clearance_pct: 999 }, bottleneck_pct: 999 }, 'open', 0, false);
|
||
assert(row.includes('full clearance') && !row.includes('>999'), 'option score wording');
|
||
assert(I.chipFor(I.linkAnalysis({ verdict: 'blocked', clearance_pct: -999 })).title.indexOf('-999') < 0 && I.chipFor(I.linkAnalysis({ verdict: 'blocked', clearance_pct: -999 })).title.indexOf('999') < 0, 'chip title has no sentinel');
|
||
const prof = I.profileSectionHtml({ a: { name: 'A' }, b: { name: 'B' }, error: 'no_terrain', distanceKm: 3 }, 0);
|
||
assert(prof.includes('No terrain data for this leg'), 'no_terrain leg wording');
|
||
const w = I.warningsHtml(['terrain missing for 1 of 4 tiles (open sea or unavailable)', '<b>x</b>']);
|
||
assert(w.includes('mc-pl-warnings') && w.includes('terrain missing for 1 of 4 tiles') && w.includes('<b>'), 'relay warnings render, escaped');
|
||
assert(I.warningsHtml([]) === '' && I.warningsHtml(undefined) === '', 'no warnings → nothing');
|
||
assert(/el\.innerHTML = html \+ warningsHtml\(res\.warnings\)/.test(PLANNER_SRC), 'relay panel renders warnings');
|
||
}
|
||
|
||
console.log('\n=== direct link without terrain ===');
|
||
{
|
||
// The server could not measure it: error, no verdict, zero numbers, no profile.
|
||
const nt = { distance_km: 12.727, clearance_pct: 0, verdict: '', worst_km: 0, obstruction_m: 0, error: 'no_terrain' };
|
||
const an = I.linkAnalysis(nt);
|
||
assert(an.error === 'no_terrain' && !an.verdict, 'linkAnalysis keeps the link error');
|
||
const chip = I.chipFor(an);
|
||
assert(chip.tone === 'muted' && !chip.loading, 'unmeasured link → muted chip (the map draws the direct leg in this tone)');
|
||
const dh = I.directHtml(nt, 'Point A', 'Point B');
|
||
assert(/mc-los-tone-muted[^>]*>.*Not measured<\/span>/.test(dh), 'direct card: muted "Not measured" chip');
|
||
assert(dh.includes('No terrain data along the direct link, so it can't be judged. The relay options below are still measured.'), 'direct card: says why it is not judged');
|
||
assert(/Point A → Point B · 12\.7 km<\/p>/.test(dh), 'direct card: distance, no clearance figure');
|
||
assert(!/blocks|F1 clearance|Blocked|0%/.test(dh), 'direct card: no verdict or clearance from unknown terrain');
|
||
assert(!dh.includes('View profile'), 'direct card: no profile button');
|
||
assert(!I.directHtml(Object.assign({}, nt, { elev_m: [1, 2, 3] }), 'A', 'B').includes('View profile'), 'no profile button even with a stray elev_m');
|
||
const other = I.directHtml({ distance_km: 3, clearance_pct: 0, verdict: '', error: 'whatever' }, 'A', 'B');
|
||
assert(other.includes('Not measured') && other.includes('could not be measured') && !/blocks/.test(other), 'unknown link error: not measured, no verdict');
|
||
const empty = I.directHtml({ distance_km: 3, clearance_pct: 0, verdict: '' }, 'A', 'B');
|
||
assert(empty.includes('Not measured') && !/blocks/.test(empty), 'empty verdict: not measured, no verdict');
|
||
// Empty or odd verdicts never throw and never borrow a prototype key.
|
||
let threw = false;
|
||
try {
|
||
['', 'constructor', '__proto__', 'toString'].forEach(v => {
|
||
const c = I.chipFor(I.linkAnalysis({ verdict: v }));
|
||
if (c.tone !== 'muted' || c.text !== 'n/a') throw new Error(v + ' → ' + JSON.stringify(c));
|
||
});
|
||
I.chipFor(I.linkAnalysis({}));
|
||
I.chipFor(I.linkAnalysis(null));
|
||
I.chipFor({ error: 'constructor' });
|
||
} catch (e) { threw = e; }
|
||
assert(!threw, 'linkAnalysis/chipFor: empty or prototype-named verdicts → muted n/a, no throw' + (threw ? ' (' + threw.message + ')' : ''));
|
||
assert(/failed/.test(I.chipFor({ error: 'constructor' }).title), 'prototype-named error code → generic reason');
|
||
const measured = I.directHtml({ distance_km: 41.3, clearance_pct: -45, verdict: 'blocked', obstruction_m: 30, worst_km: 9, elev_m: [1, 2, 3] }, 'A', 'B');
|
||
assert(measured.includes('Terrain blocks the direct link') && measured.includes('View profile') && !measured.includes('Not measured'), 'a measured direct link is unchanged');
|
||
}
|
||
|
||
console.log('\n=== parsePts / serializePts ===');
|
||
{
|
||
const pts = [{ lat: 37.77493, lon: -122.41942 }, { lat: -33.8688, lon: 151.2093 }, { lat: 0, lon: 0 }];
|
||
const s = I.serializePts(pts);
|
||
assert(s === '37.77493,-122.41942,-33.8688,151.2093,0,0', 'serialize: lat,lon pairs, trailing zeros trimmed (got ' + s + ')');
|
||
assert(eq(I.parsePts(s), pts), 'round trip parse(serialize(pts)) === pts');
|
||
assert(I.serializePts(I.parsePts(s)) === s, 'round trip serialize(parse(s)) === s');
|
||
const r = I.parsePts('37.1234567,-122.0000049');
|
||
assert(r.length === 1 && r[0].lat === 37.12346 && r[0].lon === -122, 'parse rounds to 5 decimals (~1 m)');
|
||
assert(I.serializePts([{ lat: -0.000001, lon: 5 }]) === '0,5', 'no "-0" in the URL');
|
||
assert(I.parsePts('').length === 0 && I.parsePts(null).length === 0 && I.parsePts(undefined).length === 0, 'empty / null → []');
|
||
assert(I.parsePts('abc').length === 0 && I.parsePts('a,b,c,d').length === 0, 'non-numeric → []');
|
||
assert(eq(I.parsePts('1,2,3'), [{ lat: 1, lon: 2 }]), 'trailing odd value ignored');
|
||
assert(eq(I.parsePts('91,0,10,20'), [{ lat: 10, lon: 20 }]), 'out-of-range latitude pair dropped');
|
||
assert(I.parsePts('10,181').length === 0 && I.parsePts('-90.5,0').length === 0, 'out-of-range longitude / latitude dropped');
|
||
assert(I.parsePts('0x10,5').length === 0 && I.parsePts('1e1,5').length === 0 && I.parsePts('Infinity,5').length === 0, 'hex, exponent and Infinity are not decimal degrees');
|
||
assert(eq(I.parsePts(' 10 , 20 '), [{ lat: 10, lon: 20 }]), 'whitespace around numbers tolerated');
|
||
assert(I.parsePts('1,1,2,2,3,3', 2).length === 2, 'max caps the count');
|
||
assert(I.serializePts([{ lat: 'x', lon: 1 }, null, { lat: 1, lon: 2 }, { lat: 95, lon: 0 }]) === '1,2', 'serialize skips invalid points');
|
||
assert(I.serializePts([]) === '' && I.serializePts(null) === '', 'serialize empty → ""');
|
||
assert(I.parsePoint('37.5,-122.25').lat === 37.5 && I.parsePoint('1,2,3,4') === null && I.parsePoint('x,y') === null, 'parsePoint wants exactly one pair');
|
||
}
|
||
|
||
console.log('\n=== parseParams ===');
|
||
{
|
||
const c = I.parseParams('#/tools/planner?mode=chain&pts=37.1,-122.1,37.2,-122.2,37.3,-122.3&h=10,5,x');
|
||
assert(c.mode === 'chain', 'chain: mode');
|
||
assert(c.chain.pts.length === 3 && c.chain.pts[2].lon === -122.3, 'chain: pts');
|
||
assert(eq(c.chain.h, [10, 5, null]), 'chain: h per point, bad entry → null (default applies)');
|
||
|
||
const r = I.parseParams('#/tools/planner?mode=relay&a=37.1,-122.1&b=37.2,-122.2&ha=12.5&hb=5&hr=20&range=30&sel=e2');
|
||
assert(r.mode === 'relay', 'relay: mode');
|
||
assert(r.relay.a.lat === 37.1 && r.relay.b.lon === -122.2, 'relay: a and b');
|
||
assert(r.relay.ha === 12.5 && r.relay.hb === 5 && r.relay.hr === 20 && r.relay.range === 30, 'relay: heights and range');
|
||
assert(eq(r.relay.sel, { list: 'existing', idx: 1 }), 'relay: sel=e2 → existing #2');
|
||
assert(eq(I.parseParams('?mode=relay&sel=o1').relay.sel, { list: 'open', idx: 0 }), 'relay: sel=o1 → open #1');
|
||
|
||
const bad = I.parseParams('#/tools/planner?mode=relay&a=1,2,3&b=x,y&ha=500&hb=-1&hr=abc&range=nope&sel=x9');
|
||
assert(bad.relay.a === null && bad.relay.b === null, 'relay: malformed a/b → null');
|
||
assert(bad.relay.ha === null && bad.relay.hb === null && bad.relay.hr === null, 'relay: heights outside 0–100 m or non-numeric → null');
|
||
assert(bad.relay.range === null && bad.relay.sel === null, 'relay: bad range / sel → null');
|
||
|
||
const a = I.parseParams('#/tools/planner?mode=area&bbox=37,-123,38,-122&sites=3');
|
||
assert(a.mode === 'area' && a.raw.bbox === '37,-123,38,-122' && a.raw.sites === '3', 'area: mode, its params left raw for the area module');
|
||
|
||
assert(I.parseParams('#/tools/planner').mode === 'chain', 'no mode → chain');
|
||
assert(I.parseParams('#/tools/planner?mode=bogus').mode === 'chain', 'unknown mode → chain');
|
||
assert(I.parseParams('').mode === 'chain' && I.parseParams(null).chain.pts.length === 0, 'empty / null hash');
|
||
assert(I.parseParams('#/tools/planner?pts=1%2C2%2C3%2C4').chain.pts.length === 2, 'percent-encoded commas decode');
|
||
assert(I.parseParams('mode=relay&a=1,2').relay.a.lon === 2, 'a bare query string works too');
|
||
const proto = I.queryOf('?__proto__=x&constructor=y&mode=relay&mode=chain');
|
||
assert(!Object.prototype.hasOwnProperty.call(proto, '__proto__') && proto.mode === 'relay', 'queryOf: prototype keys ignored, first occurrence wins');
|
||
assert(I.buildQuery({ pts: '1,2', mode: 'chain', h: '', x: null }) === '?mode=chain&pts=1,2', 'buildQuery: mode first, empty dropped, commas readable');
|
||
}
|
||
|
||
console.log('\n=== params / setParams (history.replaceState) ===');
|
||
{
|
||
const L2 = load({ hash: '#/tools/planner?mode=chain&pts=1,2,3,4&h=10,5' });
|
||
assert(L2.MP.params().pts === '1,2,3,4', 'params() reads the hash query');
|
||
L2.MP.setParams({ h: '7,8', pts: null, extra: 'a b' });
|
||
assert(L2.ctx.location.hash === '#/tools/planner?mode=chain&h=7,8&extra=a%20b', 'setParams merges, removes null, encodes (got ' + L2.ctx.location.hash + ')');
|
||
assert(L2.replaced.length === 1, 'setParams uses replaceState (no hashchange, no new history entry)');
|
||
L2.MP.setParams({ h: '7,8' });
|
||
assert(L2.replaced.length === 1, 'unchanged params → no replaceState');
|
||
const L3 = load({ hash: '#/map' });
|
||
assert(L3.MP.setParams({ mode: 'relay' }) === false && L3.replaced.length === 0, 'setParams is a no-op off the planner route');
|
||
}
|
||
|
||
console.log('\n=== legsFromPoints ===');
|
||
{
|
||
const pts = [
|
||
{ lat: 37.1, lon: -122.1, h: 10, node: null },
|
||
{ lat: 37.2, lon: -122.2, h: 5, node: { name: 'Hilltop', pubkey: 'abcd' } },
|
||
{ lat: 37.3, lon: -122.3, h: 20, node: null }
|
||
];
|
||
const legs = I.legsFromPoints(pts);
|
||
assert(legs.length === 2, '3 points → 2 legs');
|
||
assert(legs[0].ha === 10 && legs[0].hb === 5, 'leg 1 heights: point 1 → point 2');
|
||
assert(legs[1].ha === 5 && legs[1].hb === 20, 'leg 2 heights: the middle mast serves both legs');
|
||
assert(legs[0].a.name === 'Point 1' && legs[0].b.name === 'Hilltop' && legs[1].b.name === 'Point 3', 'names: node name when snapped, else "Point N"');
|
||
assert(legs[1].a.lat === 37.2 && legs[1].b.lon === -122.3 && legs[0].i === 0 && legs[1].i === 1, 'leg i joins points i → i+1');
|
||
assert(legs[0].key === '37.1,-122.1>37.2,-122.2', 'leg key from rounded coordinates, direction-aware');
|
||
const many = Array.from({ length: 16 }, (_, i) => ({ lat: 37 + i / 100, lon: -122, h: i }));
|
||
const ml = I.legsFromPoints(many);
|
||
assert(ml.length === 15 && ml.every((l, k) => l.ha === k && l.hb === k + 1), '16 points → 15 legs, heights in order');
|
||
assert(I.legsFromPoints([pts[0]]).length === 0 && I.legsFromPoints([]).length === 0 && I.legsFromPoints(null).length === 0, 'fewer than 2 points → no legs');
|
||
assert(I.legsFromPoints([{ lat: 1, lon: 1 }, { lat: 2, lon: 2, h: 'x' }])[0].hb === 0, 'missing / bad height → 0');
|
||
}
|
||
|
||
console.log('\n=== chipFor ===');
|
||
{
|
||
// 5 km at 910 MHz: the first Fresnel zone is ~20 m at mid-path.
|
||
const base = { distanceKm: 5, antennaAM: 30, antennaBM: 30, freqMHz: 910.525, kFactor: 4 / 3 };
|
||
const clear = LOS.analyze(Object.assign({ elevM: flat(10) }, base));
|
||
const blocked = LOS.analyze(Object.assign({ elevM: ridge(10, 200) }, base));
|
||
const marginal = LOS.analyze(Object.assign({ elevM: ridge(10, 25) }, base));
|
||
assert(clear.verdict === 'clear' && blocked.verdict === 'blocked' && marginal.verdict === 'marginal', 'fixtures: clear / blocked / marginal profiles');
|
||
const cc = I.chipFor(clear), cb = I.chipFor(blocked), cm = I.chipFor(marginal);
|
||
assert(cc.tone === 'good' && cc.text === 'Clear' && !cc.loading, 'clear → good "Clear"');
|
||
assert(cb.tone === 'bad' && cb.text === 'Blocked' && /terrain \d+ m above/.test(cb.title), 'blocked → bad "Blocked", title says how high the terrain is');
|
||
assert(cm.tone === 'warn' && cm.text === 'Marginal' && /Fresnel/.test(cm.title), 'marginal → warn "Marginal"');
|
||
assert(I.chipFor({ verdict: 'clear60', clearance: 0.7 }).tone === 'good', 'clear60 → good');
|
||
const load0 = I.chipFor(null);
|
||
assert(load0.tone === 'muted' && load0.loading === true && load0.text === 'Checking…', 'null (loading) → muted, loading');
|
||
const err = I.chipFor({ error: 'too_long' });
|
||
assert(err.tone === 'muted' && err.text === 'n/a' && /Too long/.test(err.title) && !err.loading, 'leg error → muted n/a with the reason');
|
||
assert(/failed/.test(I.chipFor({ error: 'whatever' }).title), 'unknown error code → generic reason');
|
||
assert(I.chipFor({ verdict: 'bogus' }).tone === 'muted', 'unknown verdict → muted');
|
||
const link = I.chipFor(I.linkAnalysis({ verdict: 'blocked', clearance_pct: -45, obstruction_m: 30.4, distance_km: 12 }));
|
||
assert(link.tone === 'bad' && /30 m/.test(link.title), 'server Link → chip via linkAnalysis');
|
||
assert(I.chipFor(I.linkAnalysis({ verdict: 'marginal', clearance_pct: 22 })).title.indexOf('22%') >= 0, 'Link clearance_pct shows in the title');
|
||
}
|
||
|
||
console.log('\n=== legResultFrom / resolveQuery / normCfg / parseRetryAfter ===');
|
||
{
|
||
assert(eq(I.legResultFrom({ distance_km: 3, elev_m: [1, 2, 3] }), { distanceKm: 3, elevM: [1, 2, 3] }), 'leg with terrain');
|
||
assert(I.legResultFrom({ distance_km: 300, error: 'too_long' }).error === 'too_long', 'too_long kept');
|
||
assert(I.legResultFrom({ error: 'out_of_area' }).error === 'out_of_area' && I.legResultFrom({ error: 'no_terrain' }).error === 'no_terrain', 'out_of_area / no_terrain kept');
|
||
assert(I.legResultFrom({ error: '<b>x' }).error === 'request', 'unknown error code → request');
|
||
assert(I.legResultFrom({ elev_m: [1, 'x'] }).error === 'bad_data' && I.legResultFrom({ elev_m: [1] }).error === 'bad_data', 'non-numeric / too-short profile → bad_data');
|
||
assert(I.legResultFrom(null).error === 'request', 'missing leg → request');
|
||
|
||
const nodes = I.normNodes([
|
||
{ public_key: 'aa', name: 'Hilltop Repeater', role: 'repeater', lat: 37.5, lon: -122.5 },
|
||
{ public_key: 'bb', name: 'Hilltop', role: 'companion', lat: 37.6, lon: -122.6 },
|
||
{ public_key: 'cc', name: 'Zero', role: 'repeater', lat: 0, lon: 0 },
|
||
{ public_key: 'dd', name: 'NoGps', role: 'room', lat: null, lon: null }
|
||
]);
|
||
assert(nodes.length === 2 && nodes[0].major && !nodes[1].major, 'normNodes drops (0,0) and missing GPS; repeaters/rooms are major');
|
||
assert(I.resolveQuery('hilltop', nodes).node.pubkey === 'bb', 'resolveQuery: exact name beats prefix');
|
||
assert(I.resolveQuery('hill', nodes).node.pubkey === 'aa', 'resolveQuery: prefix tie → repeater wins');
|
||
assert(I.resolveQuery('37.7749, -122.4194', nodes).node === null && I.resolveQuery('37.7749 -122.4194').lat === 37.7749, 'resolveQuery: coordinates → free point');
|
||
assert(I.resolveQuery('nothing', nodes) === null && I.resolveQuery('', nodes) === null && I.resolveQuery('95,0', nodes) === null, 'resolveQuery: no match / empty / out of range → null');
|
||
|
||
const cfg = I.normCfg({ defaultRangeKm: 45, rangesKm: [10, 30], newAntennaM: 'x', maxPoints: 16 });
|
||
assert(cfg.defaultRangeKm === 10 && eq(cfg.rangesKm, [10, 30]) && cfg.newAntennaM === 10, 'normCfg: default range must be offered, bad numbers fall back');
|
||
assert(I.parseRetryAfter('30') === 30 && I.parseRetryAfter(null) === null && I.parseRetryAfter('soon') === null, 'parseRetryAfter: seconds, missing, junk');
|
||
assert(I.roleClass('repeater') === 'repeater' && I.roleClass('x" onload="y') === 'other', 'roleClass: only known roles become class names');
|
||
}
|
||
|
||
console.log('\n=== fetchJob: 202, 202, 200 ===');
|
||
{
|
||
const T = fakeTimers();
|
||
const f = makeFetch([
|
||
res(202, { state: 'loading_terrain', progress: 0.25, retry_after_s: 0.2 }),
|
||
res(202, { state: 'computing', progress: 0.8, retry_after_s: 30 }),
|
||
res(200, { source: 'SRTM', legs: [] })
|
||
]);
|
||
const { MP: P } = load({ timers: T, fetch: f });
|
||
const progress = [];
|
||
let out = null, error = null;
|
||
const url = '/api/planner/relay?a=1,2&b=3,4';
|
||
P.fetchJob(url, { onProgress: (s, p) => progress.push([s, p]) }).then(d => { out = d; }, e => { error = e; });
|
||
await flush();
|
||
assert(f.calls.length === 1, 'first GET sent at once');
|
||
assert(T.pending().length === 1 && T.pending()[0].ms === 1000, 'retry_after_s 0.2 → clamped up to 1 s (got ' + (T.pending()[0] || {}).ms + ')');
|
||
assert(eq(progress[0], ['loading_terrain', 0.25]), 'onProgress(state, progress) on the first 202');
|
||
T.runNext();
|
||
await flush();
|
||
assert(f.calls.length === 2 && T.pending().length === 1 && T.pending()[0].ms === 5000, 'retry_after_s 30 → clamped down to 5 s');
|
||
assert(eq(progress[1], ['computing', 0.8]), 'onProgress on the second 202');
|
||
T.runNext();
|
||
await flush();
|
||
assert(f.calls.length === 3, 'polled exactly three times');
|
||
assert(f.calls.every(c => c.url === url), 'every poll re-requests the SAME url');
|
||
assert(out && out.source === 'SRTM' && !error, 'resolves with the 200 body');
|
||
assert(T.pending().length === 0, 'no timer left behind');
|
||
}
|
||
|
||
console.log('\n=== fetchJob: goes through CSApi (static host on another origin) ===');
|
||
{
|
||
const f = makeFetch([res(200, { source: 'SRTM', legs: [] })]);
|
||
const { MP: P } = load({ fetch: f, deployment: { apiBase: 'https://api.test', auth: false } });
|
||
await P.fetchJob('/api/planner/profile?pts=1,2,3,4');
|
||
assert(f.calls.length === 1 && f.calls[0].url === 'https://api.test/api/planner/profile?pts=1,2,3,4', 'the job is requested from the API origin (got ' + (f.calls[0] || {}).url + ')');
|
||
assert(f.calls[0].opts.credentials === 'omit', 'an anonymous build sends no credentials');
|
||
assert(!/[^.\w]fetch\(/.test(PLANNER_SRC), 'planner.js has no direct fetch()');
|
||
}
|
||
|
||
console.log('\n=== fetchJob: 202 without retry_after_s, odd progress ===');
|
||
{
|
||
const T = fakeTimers();
|
||
const f = makeFetch([res(202, { progress: 7 }), res(200, { ok: 1 })]);
|
||
const { MP: P } = load({ timers: T, fetch: f });
|
||
const progress = [];
|
||
const p = P.fetchJob('/x', { onProgress: (s, v) => progress.push([s, v]) });
|
||
await flush();
|
||
assert(T.pending()[0].ms === 1000, 'missing retry_after_s → 1 s');
|
||
assert(eq(progress[0], ['queued', 1]), 'missing state → "queued", progress clamped to 0..1');
|
||
T.runNext();
|
||
assert((await p).ok === 1, 'then resolves');
|
||
}
|
||
|
||
console.log('\n=== fetchJob: errors ===');
|
||
{
|
||
const T = fakeTimers();
|
||
const f = makeFetch([res(429, { error: 'rate limited' }, { 'Retry-After': '30' })]);
|
||
const { MP: P } = load({ timers: T, fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 429 && e.retryAfter === 30 && e.message === 'rate limited', '429 rejects with {status, retryAfter, message}');
|
||
assert(T.pending().length === 0 && f.calls.length === 1, '429 is not retried by fetchJob');
|
||
}
|
||
{
|
||
const f = makeFetch([res(503, { error: 'busy' }, { 'Retry-After': '5' })]);
|
||
const { MP: P } = load({ fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 503 && e.retryAfter === 5, '503 rejects with retryAfter');
|
||
}
|
||
{
|
||
const f = makeFetch([res(400, { error: 'pts: need 2 points' })]);
|
||
const { MP: P, I: I2 } = load({ fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 400 && e.retryAfter === null && e.message === 'pts: need 2 points', '400 rejects with the server message, no retryAfter');
|
||
assert(/need 2 points/.test(I2.jobErrorText(e)), 'jobErrorText shows a 400 message');
|
||
assert(/Try again in 30 s/.test(I2.jobErrorText({ status: 429, retryAfter: 30 })), 'jobErrorText: 429 says when to retry');
|
||
assert(/connection/.test(I2.jobErrorText({ status: 0 })), 'jobErrorText: network failure');
|
||
assert(/more terrain tiles/.test(I2.jobErrorText({ status: 400, message: 'too_many_tiles' })), 'jobErrorText: too_many_tiles has its own message');
|
||
}
|
||
{
|
||
const f = makeFetch([res(404, undefined)]);
|
||
const { MP: P } = load({ fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 404 && /404/.test(e.message), '404 with no JSON body still rejects with status');
|
||
}
|
||
{
|
||
const f = makeFetch([new TypeError('Failed to fetch')]);
|
||
const { MP: P } = load({ fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 0 && e.name !== 'AbortError', 'network failure rejects with status 0');
|
||
}
|
||
{
|
||
const f = makeFetch([res(200, undefined)]);
|
||
const { MP: P } = load({ fetch: f });
|
||
let e = null;
|
||
await P.fetchJob('/x').catch(x => { e = x; });
|
||
assert(e && e.status === 200, '200 without a JSON body rejects');
|
||
}
|
||
|
||
console.log('\n=== fetchJob: abort ===');
|
||
{
|
||
const T = fakeTimers();
|
||
const f = makeFetch([res(202, { state: 'computing', retry_after_s: 2 }), res(200, { never: true })]);
|
||
const { MP: P } = load({ timers: T, fetch: f });
|
||
const ac = new AbortController();
|
||
let e = null, out = null;
|
||
P.fetchJob('/x', { signal: ac.signal }).then(d => { out = d; }, x => { e = x; });
|
||
await flush();
|
||
assert(f.calls.length === 1 && T.pending().length === 1, 'waiting for the next poll');
|
||
assert(f.calls[0].opts.signal === ac.signal, 'the signal is passed to fetch');
|
||
ac.abort();
|
||
await flush();
|
||
assert(e && e.name === 'AbortError' && !out, 'abort rejects with AbortError');
|
||
assert(T.pending().length === 0, 'abort clears the pending poll');
|
||
T.runNext();
|
||
await flush();
|
||
assert(f.calls.length === 1, 'no poll after abort');
|
||
}
|
||
{
|
||
// Abort while a GET is in flight: the late response is ignored.
|
||
const T = fakeTimers();
|
||
let release;
|
||
const f = makeFetch([() => new Promise(r => { release = r; })]);
|
||
const { MP: P } = load({ timers: T, fetch: f });
|
||
const ac = new AbortController();
|
||
let e = null;
|
||
P.fetchJob('/x', { signal: ac.signal }).catch(x => { e = x; });
|
||
await flush();
|
||
ac.abort();
|
||
release(res(202, { retry_after_s: 1 }));
|
||
await flush();
|
||
assert(e && e.name === 'AbortError', 'abort mid-request rejects with AbortError');
|
||
assert(T.pending().length === 0 && f.calls.length === 1, 'a 202 arriving after abort schedules nothing');
|
||
}
|
||
{
|
||
const f = makeFetch([res(200, {})]);
|
||
const { MP: P } = load({ fetch: f });
|
||
const ac = new AbortController();
|
||
ac.abort();
|
||
let e = null;
|
||
await P.fetchJob('/x', { signal: ac.signal }).catch(x => { e = x; });
|
||
assert(e && e.name === 'AbortError' && f.calls.length === 0, 'an already-aborted signal never fetches');
|
||
}
|
||
|
||
// Node names come off the mesh (anyone can name a node). Every HTML
|
||
// builder in planner.js must render them as inert text: list rows, the
|
||
// relay rows, the direct-link card, tooltips and the profile panel titles.
|
||
console.log('\n=== planner.js HTML builders keep hostile node names inert ===');
|
||
{
|
||
const EVIL_A = '"><img src=x onerror=alert(1)>';
|
||
const EVIL_B = "' onfocus=alert(2) x='<script>alert(3)</script>";
|
||
const nodeA = { name: EVIL_A, role: 'repeater', pubkey: 'aa' };
|
||
const nodeB = { name: EVIL_B, role: EVIL_A, pubkey: 'bb' };
|
||
const pts = [
|
||
{ lat: 37.78406, lon: -122.42401, h: 5, node: nodeA },
|
||
{ lat: 37.76047, lon: -122.41755, h: 10, node: nodeB }
|
||
];
|
||
const leg = I.legsFromPoints(pts)[0];
|
||
const elev = flat(10); elev[50] = 900; // a ridge, so the obstruction sentence renders
|
||
const an = LOS.analyze({ distanceKm: 2.7, elevM: elev, antennaAM: 5, antennaBM: 10, freqMHz: 910.525, kFactor: 4 / 3 });
|
||
const link = { distance_km: 4.2, clearance_pct: -30, verdict: 'blocked', worst_km: 1.1, obstruction_m: 40, elev_m: flat(5) };
|
||
const opt = { kind: 'existing', pubkey: 'cc', name: EVIL_A, lat: 37.77, lon: -122.42, elev_m: 120, antenna_m: 5, leg_a: link, leg_b: link, bottleneck_pct: -30 };
|
||
const profLeg = { a: leg.a, b: leg.b, ha: 5, hb: 10, an: an, distanceKm: 2.7 };
|
||
const outputs = {
|
||
pointRow: I.pointRowHtml(pts[0], 0, I.legRowHtml(leg, an, false)),
|
||
pointRow2: I.pointRowHtml(pts[1], 1, ''),
|
||
legRowLoading: I.legRowHtml(leg, null, true),
|
||
legRowError: I.legRowHtml(leg, { error: 'too_long' }, false),
|
||
endRow: I.endRowHtml('a', { lat: 37.1, lon: -122.1, node: nodeB }, true),
|
||
optionRow: I.optionRowHtml(opt, 'existing', 0, true),
|
||
direct: I.directHtml(link, EVIL_A, EVIL_B),
|
||
directNoTerrain: I.directHtml({ distance_km: 4.2, clearance_pct: 0, verdict: '', error: 'no_terrain' }, EVIL_A, EVIL_B),
|
||
tooltip: I.nodeTooltipHtml(nodeB),
|
||
profile: I.profileSectionHtml(profLeg, 0),
|
||
profileLoading: I.profileSectionHtml({ a: leg.a, b: leg.b, an: null, distanceKm: 2.7 }, 1),
|
||
profileError: I.profileSectionHtml({ a: leg.a, b: leg.b, an: null, error: 'no_terrain', distanceKm: 2.7 }, 2),
|
||
readout: I.readoutHtml(profLeg, an, 50),
|
||
serverError: I.errorHtml(I.jobErrorText({ status: 400, message: EVIL_A }), 'chain')
|
||
};
|
||
// Escaped text still contains " onerror=" as characters, so check structure:
|
||
// every real tag must be well formed (double-quoted attribute values only)
|
||
// and carry no on* attribute. A name that broke out of a text node or an
|
||
// attribute value would leave a malformed tag, an extra tag, or an on* attr.
|
||
const TAG_RE = /<[^>]*>/g;
|
||
const WELL_FORMED = /^<\/?[a-zA-Z][\w:-]*(\s+[\w:-]+="[^"<]*")*\s*\/?>$/;
|
||
Object.keys(outputs).forEach(k => {
|
||
const html = outputs[k];
|
||
const tags = html.match(TAG_RE) || [];
|
||
const bad = tags.filter(t => !WELL_FORMED.test(t) || /\son[a-z]+="/i.test(t) || /^<\/?(img|script)\b/i.test(t));
|
||
assert(bad.length === 0, k + ': hostile name produces no live tag or handler' + (bad.length ? ' (got ' + bad[0] + ')' : ''));
|
||
});
|
||
assert(outputs.pointRow.includes('"><img src=x onerror=alert(1)>'), 'point row shows the name as escaped text');
|
||
assert(outputs.optionRow.includes('<img src=x onerror=alert(1)>'), 'relay option row shows the name as escaped text');
|
||
assert(/<h3 class="mc-pl-prof-title"[^>]*>"><img/.test(outputs.profile), 'profile panel title "A → B" escapes the name');
|
||
assert(outputs.profile.includes('Terrain rises') && outputs.profile.includes('<img'), 'obstruction sentence and SVG labels escape the name');
|
||
assert(outputs.direct.includes('<script>'), 'direct-link card escapes both names');
|
||
assert(outputs.serverError.includes('<img'), 'a server error message is escaped');
|
||
// The panel heading (showProfiles title) is set with textContent, never innerHTML.
|
||
assert(/title\.textContent = P\.title;/.test(PLANNER_SRC) && !/\.mc-los-title'\)\.innerHTML/.test(PLANNER_SRC), 'panel heading is written with textContent');
|
||
}
|
||
|
||
console.log('\n=== loadNodes: fetchAllNodes pages through every node ===');
|
||
{
|
||
// Any property/call on the stub returns another stub, so mount() can run without a DOM or Leaflet.
|
||
const stub = () => new Proxy(function () {}, {
|
||
get: (t, k) => (k === Symbol.toPrimitive ? () => 0 : k === 'then' ? undefined
|
||
: k === 'length' ? 0 : /^(parent|next|previous|first|last)/.test(String(k)) ? null
|
||
: (k in t ? t[k] : stub())),
|
||
set: () => true,
|
||
apply: () => stub(),
|
||
construct: () => stub()
|
||
});
|
||
const repeaters = Array.from({ length: 2500 }, (_, i) => ({
|
||
public_key: 'pk' + String(i).padStart(4, '0'), name: 'R' + i, role: 'repeater',
|
||
lat: 40 + (i % 100) / 1000, lon: -120 + (i % 100) / 1000
|
||
}));
|
||
const mountWith = (globals, fetchImpl) => {
|
||
const pages = {};
|
||
const timers = fakeTimers();
|
||
const { I: II, ctx: cx } = load({
|
||
timers, fetch: fetchImpl,
|
||
globals: Object.assign({
|
||
L: stub(), document: stub(), MC_PLANNER_CFG: {},
|
||
addEventListener() {}, removeEventListener() {},
|
||
registerPage: (name, obj) => { pages[name] = obj; }
|
||
}, globals)
|
||
});
|
||
pages.planner.init(stub());
|
||
return { II, cx, pages };
|
||
};
|
||
|
||
const calls = [];
|
||
const fakeFetchAll = (q, o) => { calls.push({ q, o }); return Promise.resolve({ nodes: repeaters, counts: {}, total: repeaters.length }); };
|
||
const r1 = mountWith({ fetchAllNodes: fakeFetchAll, api: undefined }, makeFetch([]));
|
||
await flush(); await flush();
|
||
const st1 = r1.II.state();
|
||
assert(calls.length === 1, 'loadNodes calls fetchAllNodes once');
|
||
assert(calls[0] && calls[0].q === '' && calls[0].o && calls[0].o.ttl === 60000, 'fetchAllNodes gets empty extra query and the nodeList ttl');
|
||
assert(st1 && st1.nodesState === 'ready', 'planner reaches nodesState ready');
|
||
assert(st1 && st1.rawNodes.length === 2500, 'planner node list holds all 2500 repeaters');
|
||
assert(r1.cx.fetch.calls.filter(c => c.url.indexOf('/api/nodes') === 0).length === 0, 'fetchAllNodes path makes no direct /api/nodes fetch');
|
||
|
||
// Route by URL: mount() also fetches /api/config/map, which must not consume the nodes response.
|
||
const routed = (url) => url.indexOf('/api/nodes') === 0
|
||
? Promise.resolve(res(200, { nodes: repeaters.slice(0, 3) }))
|
||
: Promise.reject(new Error('not routed: ' + url));
|
||
const fb = mountWith({ fetchAllNodes: undefined }, routed);
|
||
await flush(); await flush();
|
||
const st2 = fb.II.state();
|
||
assert(st2 && st2.nodesState === 'ready' && st2.rawNodes.length === 3, 'fallback: without fetchAllNodes, the single /api/nodes fetch still loads');
|
||
}
|
||
|
||
console.log('\n=== modes: the rendered tab order ===');
|
||
{
|
||
const stub = () => new Proxy(function () {}, {
|
||
get: (t, k) => (k === Symbol.toPrimitive ? () => 0 : k === 'then' ? undefined
|
||
: k === 'length' ? 0 : /^(parent|next|previous|first|last)/.test(String(k)) ? null
|
||
: (k in t ? t[k] : stub())),
|
||
set: () => true,
|
||
apply: () => stub(),
|
||
construct: () => stub()
|
||
});
|
||
const pages = {};
|
||
load({
|
||
fetch: makeFetch([]),
|
||
globals: {
|
||
L: stub(), document: stub(), MC_PLANNER_CFG: {}, fetchAllNodes: () => new Promise(() => {}),
|
||
addEventListener() {}, removeEventListener() {},
|
||
registerPage: (name, obj) => { pages[name] = obj; }
|
||
}
|
||
});
|
||
let html = '';
|
||
const container = { set innerHTML(v) { html = v; }, get innerHTML() { return html; }, querySelector: () => stub(), querySelectorAll: () => stub() };
|
||
pages.planner.init(container);
|
||
const tabs = (html.match(/role="tab"[^>]*data-mode="([a-z]+)"/g) || []).map(s => /data-mode="([a-z]+)"/.exec(s)[1]);
|
||
assert(eq(tabs, ['chain', 'relay', 'area', 'coverage']), 'MODES is chain, relay, area, coverage (got ' + tabs.join(',') + ')');
|
||
assert(/id="mc-pl-panel-coverage"/.test(html), 'coverage tab panel rendered');
|
||
assert(load().I.parseParams('?mode=coverage&site=51,5').mode === 'coverage', 'parseParams accepts mode=coverage');
|
||
}
|
||
|
||
console.log('\n=== source checks ===');
|
||
{
|
||
assert(/registerPage\('planner', \{ init: init, destroy: destroy \}\)/.test(PLANNER_SRC), 'registers page "planner" with init/destroy');
|
||
const code = PLANNER_SRC.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\n]*/g, '');
|
||
assert(!/location\.hash\s*=[^=]/.test(code), 'never assigns location.hash (app.js re-inits on hashchange)');
|
||
assert((PLANNER_SRC.match(/\/api\/nodes\?limit=2000/g) || []).length >= 1 && !/\/api\/nodes\/'/.test(PLANNER_SRC), 'one bulk /api/nodes fetch, no per-node requests');
|
||
const css = read('planner.css');
|
||
const hexes = (css.replace(/var\(--[\w-]+,\s*#[0-9a-fA-F]{3,8}\)/g, '').match(/#[0-9a-fA-F]{3,6}\b/g) || []);
|
||
assert(hexes.length === 0, 'planner.css: no raw hex colours outside var(--x, #fallback)');
|
||
}
|
||
|
||
{
|
||
console.log('\nRegion-neutral defaults');
|
||
const { I } = load();
|
||
assert(eq(I.fallbackView.center, [20, 0]) && I.fallbackView.zoom === 2, 'fallback view is the world view');
|
||
assert(I.coordHint([51.0512, 3.7249]) === 'coordinates like 51.05, 3.72', 'hint uses the configured center, 2 decimals');
|
||
const neutral = 'coordinates as latitude, longitude (e.g. 12.34, 56.78)';
|
||
assert(I.coordHint(null) === neutral && I.coordHint([200, 0]) === neutral, 'hint is neutral without a valid center');
|
||
assert(!/-122|37\.6|37\.77/.test(PLANNER_SRC), 'planner.js: no Bay Area coordinates remain');
|
||
}
|
||
|
||
console.log('\n' + passed + ' passed, ' + failed + ' failed');
|
||
process.exit(failed ? 1 : 0);
|
||
}
|
||
|
||
main().catch(e => { console.error(e); process.exit(1); });
|