Files
meshcore-analyzer/tests/unit/test-planner-coverage.js
T
11eb72f578 feat(planner): Coverage tab, reach of a candidate repeater site (#2406)
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>
2026-11-17 23:19:49 +01:00

515 lines
24 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Unit tests for public/planner-coverage.js (Repeater Planner, Coverage mode)
* in a vm sandbox with the real deployment.js + api-base.js and los.js. */
'use strict';
const path = require('path');
const fs = require('fs');
const vm = require('vm');
const assert = require('assert');
const { loadApiBase } = require('./api-base-sandbox');
const read = (f) => fs.readFileSync(path.resolve(__dirname, '..', '..', 'public', f), 'utf8');
const SRC = read('planner-coverage.js');
const LOS_SRC = read('los.js');
let passed = 0, failed = 0;
const pending = [];
function test(name, fn) {
let out;
try { out = fn(); } catch (e) { failed++; console.log(' FAIL ' + name + ': ' + e.message); return; }
if (out && out.then) {
pending.push(out.then(() => { passed++; console.log(' ok ' + name); },
(e) => { failed++; console.log(' FAIL ' + name + ': ' + e.message); }));
} else { passed++; console.log(' ok ' + name); }
}
const flush = () => new Promise((r) => setImmediate(r));
/* ---- stubs ---- */
function fakeEl() {
const el = {
_l: {}, style: {}, hidden: false, disabled: false, value: '', checked: false, textContent: '', innerHTML: '',
classList: { add() {}, remove() {}, toggle() {}, contains() { return false; } },
addEventListener(t, f) { (el._l[t] = el._l[t] || []).push(f); },
removeEventListener(t, f) { el._l[t] = (el._l[t] || []).filter((x) => x !== f); },
setAttribute() {}, getAttribute() { return null; }, closest() { return null; }
};
return el;
}
function fakePanel() {
const els = {};
const p = fakeEl();
p.querySelector = (sel) => (els[sel] = els[sel] || fakeEl());
p.querySelectorAll = () => [];
p._els = els;
return p;
}
function makeL(log) {
const lg = () => {
const o = { layers: [], addTo(g) { if (g.layers) g.layers.push(o); return o; }, addLayer(l) { o.layers.push(l); },
removeLayer(l) { o.layers = o.layers.filter((x) => x !== l); }, clearLayers() { o.layers = []; } };
return o;
};
const shape = (kind) => (a, opts) => {
const o = { kind, a, opts, addTo(g) { g.layers.push(o); return o; }, bindTooltip() { return o; }, on() { return o; } };
log.shapes.push(o);
return o;
};
return {
layerGroup: lg, polyline: shape('line'), circleMarker: shape('circle'),
imageOverlay: (url, bounds, opts) => {
const o = { kind: 'image', url, bounds, opts, addTo(g) { g.layers.push(o); return o; } };
log.overlays.push(o);
return o;
}
};
}
// A 2d context that knows just enough: fillStyle → the pixel getImageData reads back.
function fakeCanvas(log) {
const c = {
width: 0, height: 0,
getContext() {
const g = {
fillStyle: '',
clearRect() {}, fillRect() { g._px = g.fillStyle; },
getImageData() { return { data: log.rgb[g._px] || [0, 0, 0, 0] }; },
createImageData(w, h) { return { width: w, height: h, data: new Uint8ClampedArray(w * h * 4) }; },
putImageData(img) { log.images.push(img); }
};
return g;
},
toDataURL() { return 'data:image/png;base64,AAAA'; }
};
return c;
}
const VARS = {
'--pl-cov-user-2': 'orange', '--pl-cov-user-3': 'yellow', '--pl-cov-user-4': 'lime', '--pl-cov-user-5': 'green',
'--pl-cov-rep-1': 'red', '--pl-cov-rep-2': 'orange', '--pl-cov-rep-3': 'lime',
'--pl-cov-opacity': ' 0.5', '--pl-cov-opacity-outdated': ' 0.2'
};
const RGB = { red: [255, 0, 0, 255], orange: [255, 128, 0, 255], yellow: [255, 255, 0, 255], lime: [128, 255, 0, 255], green: [0, 128, 0, 255] };
function load() {
const log = { shapes: [], overlays: [], images: [], rgb: RGB, styleReads: 0 };
const sandbox = {
L: makeL(log), console, AbortController, Promise, Uint8Array, Uint8ClampedArray,
atob: (s) => Buffer.from(s, 'base64').toString('binary'),
addEventListener() {}, removeEventListener() {},
getComputedStyle: () => ({ getPropertyValue: (k) => { log.styleReads++; return VARS[k] || ''; } })
};
sandbox.document = { documentElement: fakeEl(), createElement: () => fakeCanvas(log) };
sandbox.window = sandbox;
vm.createContext(sandbox);
loadApiBase(sandbox);
vm.runInContext(LOS_SRC, sandbox, { filename: 'los.js' });
vm.runInContext(SRC, sandbox, { filename: 'planner-coverage.js' });
return { api: sandbox.MeshPlannerCoverage, log };
}
const CFG = { defaultRangeKm: 30, rangesKm: [10, 20, 30, 50], newAntennaM: 10, existingAntennaM: 5, userReach: { d50Km: 2.7, slope: 0.3 } };
const esc = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
const base = load();
const I = base.api._internals;
console.log('planner-coverage.js');
test('decodeCells round-trips a known base64', () => {
const b64 = Buffer.from([0x00, 0x15, 0x4f]).toString('base64');
assert.deepStrictEqual(Array.from(I.decodeCells(b64)), [0x00, 0x15, 0x4f]);
});
test('cellCode splits the nibbles per layer', () => {
assert.strictEqual(I.cellCode(0x15, 'users'), 5);
assert.strictEqual(I.cellCode(0x15, 'repeaters'), 1);
assert.strictEqual(I.cellCode(0x00, 'users'), 0);
assert.strictEqual(I.cellCode(0x00, 'repeaters'), 0);
assert.strictEqual(I.cellCode(0x4f, 'repeaters'), 4);
});
test('colorVarFor names the CSS variable; 0 and out-of-range are transparent', () => {
assert.strictEqual(I.colorVarFor('users', 3), '--pl-cov-user-3');
assert.strictEqual(I.colorVarFor('users', 1), null, 'the <10% class is not painted');
assert.strictEqual(I.colorVarFor('repeaters', 3), '--pl-cov-rep-3');
assert.strictEqual(I.colorVarFor('repeaters', 4), null, 'three repeater classes since the 2026-11-16 calibration');
assert.strictEqual(I.colorVarFor('repeaters', 1), null, 'the blocked class is not painted');
assert.strictEqual(I.colorVarFor('users', 0), null);
assert.strictEqual(I.colorVarFor('repeaters', 0), null);
assert.strictEqual(I.colorVarFor('users', 6), null);
assert.strictEqual(I.colorVarFor('repeaters', 5), null);
});
test('parseCoverageParams: bad values fall back to cfg defaults and users', () => {
const p = I.parseCoverageParams({ site: 'x,y', h: 'abc', rx: '-3', range: '7', layer: 'x' }, CFG);
assert.strictEqual(p.site, null);
assert.strictEqual(p.h, 10);
assert.strictEqual(p.rx, 5);
assert.strictEqual(p.range, 30);
assert.strictEqual(p.layer, 'users');
assert.strictEqual(I.parseCoverageParams({ site: '95,5' }, CFG).site, null);
assert.strictEqual(I.parseCoverageParams(null, CFG).site, null);
});
test('stateToHash(parseCoverageParams(p)) round-trips a valid set', () => {
const p = { site: '51.0023,5.6708', h: '12', rx: '6', range: '20', layer: 'repeaters' };
assert.deepStrictEqual(JSON.parse(JSON.stringify(I.stateToHash(I.parseCoverageParams(p, CFG)))), p);
const q = { site: null, h: '10', rx: '5', range: '30', layer: 'users' };
assert.deepStrictEqual(JSON.parse(JSON.stringify(I.stateToHash(I.parseCoverageParams({}, CFG)))), q);
});
test('buildViewshedUrl: fixed key order, 4 decimals', () => {
const st = { site: { lat: 51.00231, lon: 5.67079 }, h: 10, rx: 5, range: 30, layer: 'users' };
assert.strictEqual(I.buildViewshedUrl(st), '/api/planner/viewshed?site=51.0023,5.6708&h=10&rx=5&range=30');
assert.strictEqual(I.buildViewshedUrl({ site: null, h: 10, rx: 5, range: 30 }), null);
});
/* ---- mount ---- */
function result(over) {
return Object.assign({
source: 'srtm', params: {}, bbox: [51, 5, 51.2, 5.4], rows: 2, cols: 2, cell_km: 0.2,
cells: Buffer.from([0x15, 0x24, 0x31, 0x00]).toString('base64'),
links: [{ pubkey: 'ab', name: 'Rep <b>', lat: 51.1, lon: 5.2, link: { distance_km: 4.2, clearance_pct: 80, verdict: 'clear60' } }],
warnings: [], truncated: false
}, over || {});
}
function deferred() {
let resolve, reject;
const promise = new Promise((a, b) => { resolve = a; reject = b; });
return { promise, resolve, reject };
}
function mountWith(params, loaded) {
const m = loaded || load();
const calls = [];
const hash = [];
const panel = fakePanel();
const map = { getPane: () => null, createPane: () => ({ style: {} }), fitBounds() { m.log.fits = (m.log.fits || 0) + 1; }, getContainer: () => fakeEl() };
const ctx = {
map, layer: { layers: [], clearLayers() { this.layers = []; } }, panel, cfg: CFG,
params: params || {}, setParams: (o) => hash.push(o),
fetchJob: (url, opts) => { const d = deferred(); calls.push({ url, opts, d }); return d.promise; },
esc, nodes: [], fmtClearance: (x) => x + '%',
jobErrorText: () => 'failed', progressHtml: () => '<div>progress</div>', errorHtml: (m) => '<p>' + m + '</p>', warningsHtml: () => ''
};
const inst = m.api.mount(ctx);
return { m, ctx, calls, hash, panel, inst };
}
function fireLayer(panel, value) {
(panel._l.change || []).forEach((f) => f({ target: { getAttribute: (k) => (k === 'data-f' ? 'layer' : null), value, checked: true } }));
}
test('mount without a site fetches nothing', () => {
const t = mountWith({});
assert.strictEqual(t.calls.length, 0);
t.inst.unmount();
});
test('mount with a site in the hash fetches once with the state URL', () => {
const t = mountWith({ site: '51.0023,5.6708', h: '12', rx: '6', range: '20' });
assert.strictEqual(t.calls.length, 1);
assert.strictEqual(t.calls[0].url, '/api/planner/viewshed?site=51.0023,5.6708&h=12&rx=6&range=20');
t.inst.unmount();
});
test('stale result: a Compute click supersedes the in-flight run and its answer is ignored', async () => {
const t = mountWith({ site: '51.0023,5.6708' });
const first = t.calls[0];
t.inst.setSite({ lat: 51.1, lon: 5.3, node: null });
assert.strictEqual(t.calls.length, 1, 'setSite does not fetch');
assert.strictEqual(first.opts.signal.aborted, false, 'setSite keeps the run');
clickRun(t.panel);
assert.strictEqual(first.opts.signal.aborted, true, 'Compute aborts the first request');
assert.strictEqual(t.calls.length, 2);
assert.strictEqual(t.calls[1].opts.signal.aborted, false);
assert(/site=51\.1000,5\.3000/.test(t.calls[1].url), t.calls[1].url);
first.d.resolve(result({ bbox: [1, 1, 2, 2] }));
await flush();
assert.strictEqual(t.m.log.overlays.length, 0, 'stale result painted nothing');
t.calls[1].d.resolve(result());
await flush();
assert.strictEqual(t.m.log.overlays.length, 1);
assert.deepStrictEqual(JSON.parse(JSON.stringify(t.m.log.overlays[0].bounds)), [[51, 5], [51.2, 5.4]]);
assert.strictEqual(t.m.log.overlays[0].opts.opacity, 0.5);
const last = t.hash[t.hash.length - 1];
assert.strictEqual(last.site, '51.1000,5.3000');
t.inst.unmount();
});
test('paint: users layer colours each cell from its nibble; code 0 stays transparent', async () => {
const t = mountWith({ site: '51,5' });
t.calls[0].d.resolve(result());
await flush();
const img = t.m.log.images[0];
assert.strictEqual(img.width, 2);
const px = (i) => Array.from(img.data.slice(i * 4, i * 4 + 4));
assert.deepStrictEqual(px(0), RGB.green); // 0x15 users 5
assert.deepStrictEqual(px(1), RGB.lime); // 0x24 users 4
assert.deepStrictEqual(px(2), [0, 0, 0, 0]); // 0x31 users 1 (<10%) is not painted
assert.deepStrictEqual(px(3), [0, 0, 0, 0]); // 0x00 transparent
t.inst.unmount();
});
test('mercatorRowMap: each canvas row takes the grid row under its Mercator centre', () => {
const merc = (lat) => Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360));
const S = 50.46, N = 51.54, rows = 300; // about 60 km either side of 51° N
const map = Array.from(I.mercatorRowMap(S, N, rows));
assert.strictEqual(map.length, rows);
assert.strictEqual(map[0], 0);
assert.strictEqual(map[rows - 1], rows - 1);
for (let j = 0; j < rows; j++) {
const y = merc(N) + (j + 0.5) / rows * (merc(S) - merc(N));
const lat = Math.atan(Math.sinh(y)) * 180 / Math.PI;
assert.strictEqual(map[j], Math.floor((N - lat) / (N - S) * rows), 'row ' + j);
if (j) assert(map[j] >= map[j - 1], 'rows stay in order');
}
assert(map.some((r, j) => r !== j), 'at 60 km the mapping is not the identity');
});
test('paint: canvas rows follow Mercator, not equal latitude bands', async () => {
// 4 bands of 20° from 80° N to the equator. Spaced evenly in Mercator, the
// canvas row centres fall at about 76°, 65°, 46° and 17° N.
const t = mountWith({ site: '51,5' });
t.calls[0].d.resolve(result({ bbox: [0, 0, 80, 1], rows: 4, cols: 1, cells: Buffer.from([0x05, 0x04, 0x03, 0x02]).toString('base64') }));
await flush();
const img = t.m.log.images[0];
assert.strictEqual(img.height, 4);
const px = (i) => Array.from(img.data.slice(i * 4, i * 4 + 4));
assert.deepStrictEqual([px(0), px(1), px(2), px(3)], [RGB.green, RGB.green, RGB.lime, RGB.orange]);
t.inst.unmount();
});
test('users legend has no <10% swatch', () => {
const html = I.legendHtml('users', { rx: 5 }, CFG);
assert(html.indexOf('< 10%') < 0, html);
assert(html.indexOf('sw-user-1') < 0, html);
assert(html.indexOf('10–25%') >= 0);
});
test('repeaters legend has no blocked swatch', () => {
const html = I.legendHtml('repeaters', { rx: 5 }, CFG);
assert(html.indexOf('sw-rep-1') < 0, html);
assert(html.indexOf('sw-rep-4') < 0, html);
assert(html.indexOf('sw-rep-3') >= 0 && /Line of sight/.test(html), html);
assert(html.indexOf('sw-rep-2') >= 0 && /Slightly blocked/.test(html), html);
assert(/20 km/.test(html), 'says line of sight alone stops predicting links beyond about 20 km');
});
test('linkClass follows the grid classes from clearance_pct', () => {
const c = (k) => I.linkClass(k);
assert.strictEqual(c({ clearance_pct: 54, verdict: 'marginal' }), 3);
assert.strictEqual(c({ clearance_pct: 0, verdict: 'marginal' }), 3);
assert.strictEqual(c({ clearance_pct: -0.1, verdict: 'blocked' }), 2);
assert.strictEqual(c({ clearance_pct: -20, verdict: 'blocked' }), 2);
assert.strictEqual(c({ clearance_pct: -20.1, verdict: 'blocked' }), 1);
assert.strictEqual(c({ error: 'no terrain' }), 0);
assert.strictEqual(c({}), 0);
});
test('links list names the class, not the textbook verdict', () => {
const d = { links: [
{ pubkey: 'a', name: 'A', link: { distance_km: 4, clearance_pct: 20, verdict: 'marginal' } },
{ pubkey: 'b', name: 'B', link: { distance_km: 9, clearance_pct: -10, verdict: 'blocked' } },
{ pubkey: 'c', name: 'C', link: { distance_km: 20, clearance_pct: -90, verdict: 'blocked' } }
] };
const html = I.linksHtml(d, esc);
assert(/sw-rep-3[^]*A · 4\.0 km · Line of sight/.test(html), html);
assert(/sw-rep-2[^]*B · 9\.0 km · Slightly blocked/.test(html), html);
assert(/sw-rep-1[^]*C · 20\.0 km · Blocked by terrain/.test(html), html);
});
test('layer switch repaints without another fetch', async () => {
const t = mountWith({ site: '51,5' });
t.calls[0].d.resolve(result());
await flush();
assert.strictEqual(t.m.log.overlays.length, 1);
const dots = () => t.m.log.shapes.filter((s) => s.kind === 'circle' && /mc-pl-cov-node/.test(s.opts.className));
assert.strictEqual(dots().length, 0, 'users layer recolours no repeater dots');
fireLayer(t.panel, 'repeaters');
assert.strictEqual(t.calls.length, 1, 'no new fetch');
assert.strictEqual(t.m.log.overlays.length, 2, 'repainted');
const img = t.m.log.images[1];
const px = (i) => Array.from(img.data.slice(i * 4, i * 4 + 4));
assert.deepStrictEqual(px(0), [0, 0, 0, 0]); // 0x15 repeaters 1 (blocked) is not painted
assert.deepStrictEqual(px(3), [0, 0, 0, 0]);
const lines = t.m.log.shapes.filter((s) => s.kind === 'line');
assert.strictEqual(lines.length, 1, 'repeaters layer draws the link line');
assert(/mc-pl-cov-link-3/.test(lines[0].opts.className), lines[0].opts.className);
assert.strictEqual(dots().length, 1, 'the linked repeater gets a dot in its class colour');
assert(/mc-pl-cov-node-3/.test(dots()[0].opts.className), dots()[0].opts.className);
assert.strictEqual(JSON.stringify(dots()[0].a), "[51.1,5.2]");
assert.strictEqual(dots()[0].opts.interactive, false, 'planner node tooltip and click stay usable');
assert.strictEqual(t.hash[t.hash.length - 1].layer, 'repeaters');
t.inst.unmount();
});
test('blocked links get a red dot but no line', async () => {
const t = mountWith({ site: '51,5', layer: 'repeaters' });
t.calls[0].d.resolve(result({ links: [
{ pubkey: 'a', name: 'A', lat: 51.1, lon: 5.2, link: { distance_km: 4, clearance_pct: 20, verdict: 'marginal' } },
{ pubkey: 'b', name: 'B', lat: 51.0, lon: 5.3, link: { distance_km: 9, clearance_pct: -10, verdict: 'blocked' } },
{ pubkey: 'c', name: 'C', lat: 50.9, lon: 5.1, link: { distance_km: 12, clearance_pct: -90, verdict: 'blocked' } }
] }));
await flush();
const lines = t.m.log.shapes.filter((s) => s.kind === 'line');
const dots = t.m.log.shapes.filter((s) => s.kind === 'circle' && /mc-pl-cov-node/.test(s.opts.className));
assert.strictEqual(lines.length, 2, 'lines for line of sight and slightly blocked only');
assert(!lines.some((l) => /mc-pl-cov-link-1/.test(l.opts.className)), 'no blocked line');
assert.strictEqual(dots.length, 3, 'every linked repeater keeps a coloured dot');
assert.strictEqual(dots.filter((d) => /mc-pl-cov-node-1/.test(d.opts.className)).length, 1);
t.inst.unmount();
});
test('links list escapes repeater names', async () => {
const t = mountWith({ site: '51,5', layer: 'repeaters' });
t.calls[0].d.resolve(result());
await flush();
const html = Object.values(t.panel._els).map((e) => e.innerHTML).join('');
assert(/Rep &lt;b&gt;/.test(html), 'name escaped');
assert(!/Rep <b>/.test(html), 'no raw markup');
assert(/4\.2 km/.test(html), 'distance shown');
t.inst.unmount();
});
function clickRun(panel) {
const btn = { getAttribute: (k) => (k === 'data-act' ? 'run' : null) };
(panel._l.click || []).forEach((f) => f({ target: { closest: () => btn } }));
}
function fireInput(panel, name, value) {
panel._els['[data-f="' + name + '"]'].value = value;
(panel._l.change || []).forEach((f) => f({ target: { getAttribute: (k) => (k === 'data-f' ? name : null), value, checked: true } }));
}
const runBtn = (t) => t.panel._els['[data-act="run"]'];
const outdatedHtml = (t) => t.panel._els['[data-f="outdated"]'].innerHTML;
test('mount with a site in the hash computes once, without one never', () => {
const a = mountWith({ site: '51,5' });
assert.strictEqual(a.calls.length, 1);
a.inst.unmount();
const b = mountWith({});
assert.strictEqual(b.calls.length, 0);
assert.strictEqual(runBtn(b).disabled, true, 'no site: button disabled');
b.inst.setSite({ lat: 51.1, lon: 5.1, node: null });
assert.strictEqual(b.calls.length, 0, 'setSite does not fetch');
assert.strictEqual(runBtn(b).disabled, false, 'site: button enabled');
assert.strictEqual(outdatedHtml(b), '', 'no overlay yet: no outdated note');
b.inst.unmount();
});
test('param changes never fetch, hash updates, overlay is dimmed and flagged outdated', async () => {
const t = mountWith({ site: '51,5' });
t.calls[0].d.resolve(result());
await flush();
assert.strictEqual(t.m.log.overlays[0].opts.opacity, 0.5);
assert.strictEqual(outdatedHtml(t), '');
const hashLen = t.hash.length;
t.inst.setSite({ lat: 51.1, lon: 5.1, node: null });
fireInput(t.panel, 'h', '12');
fireInput(t.panel, 'rx', '7');
fireInput(t.panel, 'range', '20');
assert.strictEqual(t.calls.length, 1, 'no refetch');
assert.strictEqual(t.hash.length, hashLen + 4, 'hash follows each change');
assert(/Outdated/.test(outdatedHtml(t)), outdatedHtml(t));
const last = t.m.log.overlays[t.m.log.overlays.length - 1];
assert.strictEqual(last.opts.opacity, 0.2, 'overlay dimmed');
t.inst.unmount();
});
test('a parameter change during the first compute does not abort it; the result arrives outdated', async () => {
const t = mountWith({ site: '51,5' });
fireInput(t.panel, 'h', '12');
assert.strictEqual(t.calls.length, 1, 'no second fetch');
assert.strictEqual(t.calls[0].opts.signal.aborted, false, 'in-flight run kept');
t.calls[0].d.resolve(result());
await flush();
assert.strictEqual(t.calls.length, 1);
assert.strictEqual(t.m.log.overlays.length, 1, 'overlay rendered');
assert.strictEqual(t.m.log.overlays[0].opts.opacity, 0.2, 'dimmed');
assert(/Outdated/.test(outdatedHtml(t)), 'note shown');
t.inst.unmount();
});
test('button click starts exactly one job with the latest values; a newer click supersedes', async () => {
const t = mountWith({ site: '51,5' });
t.calls[0].d.resolve(result());
await flush();
t.inst.setSite({ lat: 51.1, lon: 5.1, node: null });
fireInput(t.panel, 'h', '12');
fireInput(t.panel, 'range', '20');
clickRun(t.panel);
assert.strictEqual(t.calls.length, 2);
assert.strictEqual(t.calls[1].url, '/api/planner/viewshed?site=51.1000,5.1000&h=12&rx=5&range=20');
clickRun(t.panel);
assert.strictEqual(t.calls.length, 3);
assert.strictEqual(t.calls[1].opts.signal.aborted, true, 'older click aborted');
t.calls[1].d.resolve(result({ bbox: [1, 1, 2, 2] }));
await flush();
const n = t.m.log.overlays.length;
t.calls[2].d.resolve(result());
await flush();
assert.strictEqual(t.m.log.overlays.length, n + 1, 'only the newest answer painted');
assert.strictEqual(outdatedHtml(t), '');
assert.strictEqual(t.m.log.overlays[n].opts.opacity, 0.5, 'fresh overlay at full opacity');
t.inst.unmount();
});
const plain = (v) => JSON.parse(JSON.stringify(v)); // sandbox arrays → this realm
test('site marker and link lines start at the site the server computed for', async () => {
const t = mountWith({ site: '51.00037,5.00041', layer: 'repeaters' });
t.calls[0].d.resolve(result({ params: { site: { lat: 51, lon: 5 } } }));
await flush();
const site = t.m.log.shapes.filter((s) => s.kind === 'circle' && /mc-pl-cov-site/.test(s.opts.className)).pop();
assert.deepStrictEqual(plain(site.a), [51, 5], 'marker on the rounded site');
const line = t.m.log.shapes.filter((s) => s.kind === 'line').pop();
assert.deepStrictEqual(plain(line.a[0]), [51, 5], 'line from the rounded site');
t.inst.setSite({ lat: 51.3, lon: 5.3, node: null });
const moved = t.m.log.shapes.filter((s) => s.kind === 'circle' && /mc-pl-cov-site/.test(s.opts.className)).pop();
assert.deepStrictEqual(plain(moved.a), [51.3, 5.3], 'a new site shows where it was placed');
t.inst.unmount();
});
test('an outdated result dims the links list as well', async () => {
const t = mountWith({ site: '51,5', layer: 'repeaters' });
t.calls[0].d.resolve(result());
await flush();
const list = t.panel._els['[data-f="results"]'];
assert(!/is-outdated/.test(list.className || ''), 'fresh result: list not dimmed');
fireInput(t.panel, 'h', '12');
assert(/is-outdated/.test(list.className), 'outdated: list dimmed');
clickRun(t.panel);
t.calls[1].d.resolve(result());
await flush();
assert(!/is-outdated/.test(list.className), 'recomputed: list back to normal');
t.inst.unmount();
});
test('the map fits a result once; coming back to the tab keeps the view', async () => {
const m = load();
const a = mountWith({ site: '51,5' }, m);
a.calls[0].d.resolve(result());
await flush();
assert.strictEqual(m.log.fits, 1, 'first open fits the result');
a.inst.unmount();
const b = mountWith({ site: '51,5' }, m);
b.calls[0].d.resolve(result());
await flush();
assert.strictEqual(m.log.fits, 1, 'same site again: pan and zoom kept');
b.inst.unmount();
const c = mountWith({ site: '50,4' }, m);
c.calls[0].d.resolve(result());
await flush();
assert.strictEqual(m.log.fits, 2, 'a deep link to another site fits again');
c.inst.unmount();
});
test('unmount aborts the running request and clears the layer', () => {
const t = mountWith({ site: '51,5' });
t.inst.unmount();
assert.strictEqual(t.calls[0].opts.signal.aborted, true);
assert.strictEqual(t.panel.innerHTML, '');
});
test('panel carries the height note verbatim', () => {
const t = mountWith({});
assert(t.panel.innerHTML.indexOf('Height mainly changes reach to other repeaters; user reach is measured and barely depends on height.') >= 0);
t.inst.unmount();
});
Promise.all(pending).then(() => {
console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed ? 1 : 0);
});