mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
Supersedes #218 by @spcpza, ported onto the current component layout with fixes from review. Verified before merge: format, package boundaries, unit suite, build, and the browser tracking suite; a browser harness for the change where one applies; adversarial review applied. Contributor: @spcpza. His original commit is the base of this branch, and his own re-port onto main (2026-09-15, `5b69011`) was consulted while finishing the height work. That port reads every vehicle's surface from the app's shared ground-floor system (`cachedGroundFloor` / `warmGroundFloor` / `GROUND_FLOOR_LIFT_M`) rather than sampling the scene per layer, keeps a vehicle hidden until its cell answers, warms a bounded batch per poll on a timer instead of per frame, and renders at the ellipsoid above the altitude gate. That is the sounder primitive — one floor system for the app, with the rooftop-versus-street question already settled against a datum-correct DEM prior — and this branch's height work was rebuilt on it, replacing a bespoke cell cache, ground prior and rooftop rule written here. Credit for that direction is his. Future work (not in this PR), both written up in docs/CURRENT-STATE.md: - Readable route names. The realtime feeds carry only `route_id`; `route_short_name` / `route_long_name` live in each operator's static GTFS `routes.txt`, and headsigns in `trips.txt`. Doing it properly wants a server-side metadata loader beside the transit proxy with fixed registered static sources, bounded indexes built once and persisted, a slow conditional refresh, and a versioned route dictionary the browser caches. Shipped here: ids are never rewritten, and a cut one carries an ellipsis instead of reading as a typo. - Below-ground rendering. Subway vehicles are drawn on the surface directly above themselves, because the feeds carry no depth and a train nobody can see is not worth drawing; the card says so plainly. Real treatment wants OSM tunnel geometry per city and a ghosted sprite tethered to the surface. The owner recorded it and left it. Round four and after (2026-09-15). The owner tested round three under FLIR and NVG, and then asked for the design to be taken further: elegant in performance, beautiful on camera, trails with a cache for public transport, Cesium-native, and a core other moving layers can adopt. Codex Astra reviewed the plan (it confirmed the speed fault: a glide started from the drawn position at receipt time but spanned the gap between report times, so with MBTA reporting every ~10 s against a 15 s poll every second glide ran at 1.5x; it rejected a queued-segment patch), then wrote the design, then implemented it in commit-sized passes with adversarial review between them and real-browser harness runs by the integrator. What this PR now contains beyond the contributor's original port: - Delayed playback in a contact-agnostic core (`src/data/contactPlayback.js`): each vehicle keeps a typed, bounded fix history and a display clock that runs at one second per second through it, a feed-derived lag behind real time, holding at its newest fix rather than extrapolating and never sprinting to catch up. Marker, course, motion word, card and trail all read the same displayed sample. The clock has an explicit time-offset and seek API so a rewind is a later feature, not a rewrite; flights and vessels are untouched. - A selected-vehicle trail (`src/data/contactTrailRenderer.js`, `src/layers/transit/trails.js`): the retained history drawn with batched Cesium primitives, clipped at display time so the head is exactly the sprite, faded by age, in the mode colour (white under sensor presets), on the same surface path as the marker. - Bounded proxy-side history (`server/providers/transitHistory.js`): fifteen minutes and at most 128 fixes per vehicle with hard byte ceilings, MBTA only for now (opt-in per feed), served by a small endpoint that never fetches upstream, so a selected trail survives a browser reload while the server keeps running. - Scene work bounded to what is visible and moving, an occluder, cached DETECT candidates, holds on the render loop only while something visible moves. - Sensor presets: white-hot haloed sprites under NVG/FLIR/noir (black-hot inverts), saturated mode colours under CRT, per-mode bracket tiers in every theme, the selected card keeping its mode colour. - A discriminating browser harness (`scripts/qa-transit.mjs`) that samples postRender frames, reads the real card anchor, and measures fleet budgets at 800 and 3,000 vehicles. Not built, recorded as follow-ups: curated route display names (lowest priority per the owner), fleet-wide tails, route snapping, a rewind UI, adoption of the playback core by flights and vessels. Verified at merge (2026-09-15 morning): unit suite 3,896 passing, build clean, format and boundary gates clean, tracking regression 109/109, browser harness keyless 200 pass / 0 fail (13 sensor checks unexercised because the live view offered fewer than six isolated sprites; every judged sprite passed), keyed on Google 3D tiles 198 pass with the per-frame allocation gate as the only failure. Follow-ups recorded: the keyed build's playback sampler still allocates per frame (CPU and frame time stay within budget; the 3,000-vehicle metro view sits at a 27.6 ms frame interval against a 25 ms budget), the shared detection overlay's per-paint allocation, friendlier route names, fleet-wide tails, a rewind UI, and adopting the playback core for vessels and flights. Co-authored-by: spcpza <[email protected]>
195 lines
8.5 KiB
JavaScript
195 lines
8.5 KiB
JavaScript
// Unit tests for the pure drawing/format helpers behind the detection overlay.
|
|
// These are renderer-agnostic (no Cesium, no DOM) so they pin the Phase-1 label
|
|
// + batching behavior and carry straight into the Phase-2 GPU renderer.
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
formatFlightLevel,
|
|
formatKnots,
|
|
monoTextWidth,
|
|
composeLabel,
|
|
acquireAlpha,
|
|
appendCornerBracket,
|
|
resolveTier,
|
|
measureLabelCard,
|
|
measureTrackLabel,
|
|
nearFarScale,
|
|
rectIntersectsAny,
|
|
} from './detectionDraw.js';
|
|
|
|
test('formatFlightLevel converts metres to a 3-digit flight level', () => {
|
|
assert.equal(formatFlightLevel(10363), 'FL340'); // 34,000 ft
|
|
assert.equal(formatFlightLevel(1524), 'FL050'); // 5,000 ft, zero-padded
|
|
});
|
|
|
|
test('formatFlightLevel returns empty string for missing/zero/negative altitude', () => {
|
|
assert.equal(formatFlightLevel(0), '');
|
|
assert.equal(formatFlightLevel(-50), '');
|
|
assert.equal(formatFlightLevel(null), '');
|
|
assert.equal(formatFlightLevel(undefined), '');
|
|
assert.equal(formatFlightLevel(NaN), '');
|
|
});
|
|
|
|
test('formatKnots rounds and suffixes; omits non-positive/non-finite', () => {
|
|
assert.equal(formatKnots(14), '14 kn');
|
|
assert.equal(formatKnots(14.6), '15 kn');
|
|
assert.equal(formatKnots(0), '');
|
|
assert.equal(formatKnots(null), '');
|
|
assert.equal(formatKnots(NaN), '');
|
|
});
|
|
|
|
test('monoTextWidth multiplies length by advance, 0 for empty', () => {
|
|
assert.equal(monoTextWidth('ABCD', 6), 24);
|
|
assert.equal(monoTextWidth('', 6), 0);
|
|
assert.equal(monoTextWidth(null, 6), 0);
|
|
});
|
|
|
|
test('composeLabel: id only -> empty secondary (degrades to today)', () => {
|
|
assert.deepEqual(composeLabel({ id: 'VEH-0001' }), { primary: 'VEH-0001', secondary: '' });
|
|
});
|
|
|
|
test('composeLabel: id + metric -> secondary is the metric', () => {
|
|
assert.deepEqual(composeLabel({ id: 'UAL2476', metric: 'FL340' }), {
|
|
primary: 'UAL2476',
|
|
secondary: 'FL340',
|
|
});
|
|
});
|
|
|
|
test('composeLabel: id + class + metric -> class · metric secondary', () => {
|
|
assert.deepEqual(composeLabel({ id: 'VIPER11', klass: 'MIL', metric: 'FL280' }), {
|
|
primary: 'VIPER11',
|
|
secondary: 'MIL · FL280',
|
|
});
|
|
});
|
|
|
|
test('composeLabel truncates an over-long primary and is defensive about empties', () => {
|
|
assert.equal(composeLabel({ id: 'SUPERLONGVESSELNAME12345' }).primary, 'SUPERLONGVESSELNAM'); // 18
|
|
assert.deepEqual(composeLabel({}), { primary: '', secondary: '' });
|
|
});
|
|
|
|
test('acquireAlpha ramps 0->1 across the fade window', () => {
|
|
assert.equal(acquireAlpha(1000, 1000, 200), 0);
|
|
assert.equal(acquireAlpha(1000, 1100, 200), 0.5);
|
|
assert.equal(acquireAlpha(1000, 1300, 200), 1);
|
|
});
|
|
|
|
test('acquireAlpha clamps and defaults safely', () => {
|
|
assert.equal(acquireAlpha(1000, 900, 200), 0); // before first-seen
|
|
assert.equal(acquireAlpha(1000, 5000, 200), 1); // long after
|
|
assert.equal(acquireAlpha(NaN, 5000, 200), 1); // no timestamp -> visible
|
|
assert.equal(acquireAlpha(1000, 1100, 0), 1); // no fade -> visible
|
|
});
|
|
|
|
test('appendCornerBracket emits 4 L-shaped corners (4 moveTo + 8 lineTo)', () => {
|
|
const calls = [];
|
|
const sink = {
|
|
moveTo: (x, y) => calls.push(['m', x, y]),
|
|
lineTo: (x, y) => calls.push(['l', x, y]),
|
|
};
|
|
appendCornerBracket(sink, 100, 100, 20, 10);
|
|
|
|
assert.equal(calls.length, 12);
|
|
assert.equal(calls.filter((c) => c[0] === 'm').length, 4);
|
|
assert.equal(calls.filter((c) => c[0] === 'l').length, 8);
|
|
// top-left corner of box (x0=80, y0=90), seg = max(4, floor(10*0.55)) = 5
|
|
assert.deepEqual(calls.slice(0, 3), [
|
|
['m', 80, 95],
|
|
['l', 80, 90],
|
|
['l', 85, 90],
|
|
]);
|
|
});
|
|
|
|
test('resolveTier maps type to a threat tier, with explicit override winning', () => {
|
|
assert.equal(resolveTier({ type: 'AIR' }), 'civil');
|
|
assert.equal(resolveTier({ type: 'AIR', tier: 'military' }), 'military'); // layer-supplied override
|
|
assert.equal(resolveTier({ type: 'SEA' }), 'sea');
|
|
assert.equal(resolveTier({ type: 'SAT' }), 'space');
|
|
assert.equal(resolveTier({ type: 'VEH' }), 'vehicle');
|
|
assert.equal(resolveTier({}), 'civil');
|
|
assert.equal(resolveTier(null), 'civil');
|
|
// Live-traffic congestion tiers ride the same override: keyless VEH
|
|
// contacts carry no tier and keep the stock 'vehicle' color.
|
|
assert.equal(resolveTier({ type: 'VEH', tier: 'veh_jam' }), 'veh_jam');
|
|
assert.equal(resolveTier({ type: 'VEH', tier: 'veh_nodata' }), 'veh_nodata');
|
|
});
|
|
|
|
test('measureLabelCard sizes a two-line card so the second line never clips', () => {
|
|
const card = measureLabelCard('UAL2476', 'B738 · FL340', 6);
|
|
// bottom of the last baseline + descender must fit inside the card height
|
|
assert.ok(card.subBase + 3 <= card.h, `subBase+desc ${card.subBase + 3} must fit in h ${card.h}`);
|
|
assert.ok(card.idBase < card.subBase, 'id line sits above sub line');
|
|
assert.ok(card.w >= 12 * 6, 'width covers the wider (sub) text');
|
|
assert.equal(card.hasSec, true);
|
|
});
|
|
|
|
test('measureLabelCard collapses to a single line when there is no secondary', () => {
|
|
const card = measureLabelCard('VEH-0001', '', 6);
|
|
assert.equal(card.hasSec, false);
|
|
assert.equal(card.subBase, 0);
|
|
assert.ok(card.idBase + 3 <= card.h, 'single line + descender fits');
|
|
const two = measureLabelCard('VEH-0001', 'VEH · 38mph', 6);
|
|
assert.ok(two.h > card.h, 'two-line card is taller than one-line');
|
|
});
|
|
|
|
test('measureTrackLabel lays out callsign + altitude on one line', () => {
|
|
const c = measureTrackLabel('WOLF21', '270', 6);
|
|
assert.equal(c.hasMicro, true);
|
|
assert.ok(c.microX > c.primaryX, 'altitude sits to the right of the callsign');
|
|
assert.ok(c.baseline + 3 <= c.h, 'single line + descender fits inside height');
|
|
assert.ok(c.w >= 6 * 6, 'width covers the callsign');
|
|
});
|
|
|
|
test('measureTrackLabel handles a missing altitude (callsign only)', () => {
|
|
const c = measureTrackLabel('SAT-12345', '', 6);
|
|
assert.equal(c.hasMicro, false);
|
|
assert.ok(c.w >= 9 * 6, 'width covers the longer callsign');
|
|
});
|
|
|
|
test('callout cards avoid live HUD rectangles without rejecting edge-adjacent space', () => {
|
|
const hud = [{ x: 100, y: 100, w: 80, h: 60 }];
|
|
assert.equal(rectIntersectsAny({ x: 120, y: 80, w: 40, h: 40 }, hud), true);
|
|
assert.equal(rectIntersectsAny({ x: 60, y: 100, w: 40, h: 20 }, hud), false);
|
|
assert.equal(rectIntersectsAny({ x: 60, y: 100, w: 40, h: 20 }, hud, 1), true);
|
|
});
|
|
|
|
test('nearFarScale interpolates by distance and clamps to the near/far values', () => {
|
|
// mirrors Cesium NearFarScalar(1000, 3.0, 8000000, 0.5) used by the flight billboards
|
|
assert.equal(nearFarScale(1000, 1000, 3.0, 8000000, 0.5), 3.0);
|
|
assert.equal(nearFarScale(8000000, 1000, 3.0, 8000000, 0.5), 0.5);
|
|
assert.equal(nearFarScale(500, 1000, 3.0, 8000000, 0.5), 3.0); // below near -> clamp
|
|
assert.equal(nearFarScale(9e6, 1000, 3.0, 8000000, 0.5), 0.5); // beyond far -> clamp
|
|
const mid = nearFarScale((1000 + 8000000) / 2, 1000, 3.0, 8000000, 0.5);
|
|
assert.ok(Math.abs(mid - 1.75) < 1e-6, `midpoint ~1.75, got ${mid}`);
|
|
});
|
|
|
|
|
|
test('transit brackets paint a 1.25 px mode stroke over 3.25 px dark backing', async () => {
|
|
const { paintTransitBracket } = await import('./detectionDraw.js');
|
|
const strokes = [];
|
|
const ctx = { stroke(path) { strokes.push([path, this.lineWidth, this.strokeStyle, this.globalAlpha]); } };
|
|
const path = {};
|
|
paintTransitBracket(ctx, path, '#FF4538', 0.75);
|
|
assert.deepEqual(strokes, [[path, 3.25, '#05080C', 0.75], [path, 1.25, '#FF4538', 0.75]]);
|
|
});
|
|
|
|
test('transit bracket backing and colour use source-over under every inherited sensor theme', async () => {
|
|
const {paintTransitBracket}=await import('./detectionDraw.js');
|
|
for(const theme of ['normal','thermal','surveillance','noir','retro']) {
|
|
const strokes=[];
|
|
const ctx={globalCompositeOperation:'screen',globalAlpha:0.3,stroke(p){strokes.push([this.globalCompositeOperation,this.lineWidth,this.strokeStyle]);},save(){this.saved=[this.globalCompositeOperation,this.globalAlpha];},restore(){[this.globalCompositeOperation,this.globalAlpha]=this.saved;}};
|
|
paintTransitBracket(ctx,{},'#FF4538',1);
|
|
assert.deepEqual(strokes,[['source-over',3.25,'#05080C'],['source-over',1.25,'#FF4538']],theme);
|
|
assert.equal(ctx.globalCompositeOperation,'screen');
|
|
}
|
|
});
|
|
|
|
test('transit bracket stroke centres have full pixel coverage at the failing fractional positions', async () => {
|
|
const {appendTransitBracket}=await import('./detectionDraw.js');
|
|
for(const sy of [305.076,490.54,279.56]) {
|
|
const points=[],sink={moveTo:(x,y)=>points.push([x,y]),lineTo:(x,y)=>points.push([x,y])};
|
|
appendTransitBracket(sink,759.016,sy,11,7);
|
|
assert.equal(points.length,12);
|
|
for(const [x,y] of points){assert.equal(x%1,0.5);assert.equal(y%1,0.5);}
|
|
}
|
|
});
|