Files
gods-eye-view/src/data/trailReports.test.mjs
T
Sameh Khamis 5f98f8bdf3 Aircraft drawn as far behind real time as their feed is late, along one curve their trail follows
Civil and military aircraft were drawn a fixed 30 s (15 s) behind real time
while anonymous feed reports arrive about 90 s old every 30 s, so every
contact dead reckoned about a minute past its newest report and snapped
when the next one landed (several km on a turn), and the tracked trail,
recorded from those drawn positions and patched as reports arrived, kinked.

- The display delay (data/displayDelay.js) is the 90th percentile of the
  newest 256 report lags plus 5 s, between the layer's floor (30 s, 15 s)
  and five minutes, slewed at most 0.2 s/s up and 0.05 s/s down. Icons,
  the renderer's live motion clock, labels, HUD, detection, the tracked
  camera and the trail read the one delay; following the world clock
  draws with none and deliveries leave it alone.
- Between two reports an aircraft flies a biarc (data/fixCurve.js) that
  passes through both and leaves each along its track, sent as contract
  arc segments; the trail is the reports joined along the same curve, so
  the icon sits on it. Nothing drawn between reports is kept.
- A report a coasting contact missed is eased onto from where it would
  have been that frame (at least 3 s, at most 30% over its speed, within
  20 s); over 5 km it jumps. Past its newest report a contact coasts
  straight on: a turn rate averaged over earlier reports outlived the turn.
- Forward synthetic fixes no longer block real reports older than them,
  and a history keeps 24 fixes.
- A feed simulation (testSupport/aircraftFeedSim.mjs) checks both layers
  against a 90 s late feed, a fallback switch, repeated reports, outages
  and the world clock.
2026-11-14 00:31:29 -08:00

137 lines
4.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import {
insertTrailFix,
trailBodySegments,
trailHeadCurvePoints,
} from './trailContinuity.js';
// A trail is the aircraft's reports and nothing else, each pair that is one
// path joined along the curve the icon is drawn along between them: no
// stretch it was drawn at between reports is kept, so a late report can
// never leave a kink.
const SEC = 1000;
const LIMIT = 2_000;
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(-100, 40, 0),
);
const at = (e, n, u = 3_000) =>
Cesium.Matrix4.multiplyByPoint(
enu,
new Cesium.Cartesian3(e, n, u),
new Cesium.Cartesian3(),
);
/** The sharpest turn (degrees) at any vertex of `line`, steps under 1 m skipped. */
function sharpestTurnDeg(line) {
const u = new Cesium.Cartesian3();
const v = new Cesium.Cartesian3();
let worst = 0;
for (let i = 1; i < line.length - 1; i++) {
Cesium.Cartesian3.subtract(line[i], line[i - 1], u);
Cesium.Cartesian3.subtract(line[i + 1], line[i], v);
const lu = Cesium.Cartesian3.magnitude(u);
const lv = Cesium.Cartesian3.magnitude(v);
if (!(lu > 1 && lv > 1)) continue;
const cos = Math.min(1, Cesium.Cartesian3.dot(u, v) / (lu * lv));
worst = Math.max(worst, (Math.acos(cos) * 180) / Math.PI);
}
return worst;
}
// A 100 m/s aircraft in a left turn on a 2 km circle centred at (0, R),
// heading east at the origin, reporting every 15 s with its track.
const R = 2_000;
const W = 100 / R;
const circle = (t) => at(R * Math.sin(W * t), R - R * Math.cos(W * t));
const trackAt = (t) => (90 - (W * t * 180) / Math.PI + 360) % 360;
test('reports in a turn are joined along a smooth curve through each report', () => {
const fixes = [];
for (let t = 0; t <= 60; t += 15)
insertTrailFix(fixes, circle(t), t * SEC, LIMIT, { track: trackAt(t) });
assert.deepEqual(
fixes.map((f) => f.timeMs),
[0, 15, 30, 45, 60].map((s) => s * SEC),
);
const [line] = trailBodySegments(fixes, fixes.length);
for (const f of fixes)
assert.ok(
line.some((p) => Cesium.Cartesian3.distance(p, f.position) < 0.01),
'every report is on the line',
);
assert.ok(sharpestTurnDeg(line) < 3, `${sharpestTurnDeg(line)}°`);
// On the circle, not across its chords.
for (const p of line)
assert.ok(
Math.abs(Cesium.Cartesian3.distance(p, at(0, R)) - R) < 20,
'a point off the turn',
);
});
test('reports with no track are joined straight, report to report', () => {
const fixes = [];
for (let t = 0; t <= 60; t += 15)
insertTrailFix(fixes, circle(t), t * SEC, LIMIT);
const [line] = trailBodySegments(fixes, fixes.length);
assert.equal(line.length, fixes.length);
});
test('two reports 60 s and 51 km apart are not joined', () => {
const fixes = [];
insertTrailFix(
fixes,
Cesium.Cartesian3.fromDegrees(-100, 40, 9_000),
0,
LIMIT,
{ track: 90 },
);
insertTrailFix(
fixes,
Cesium.Cartesian3.fromDegrees(-99.4, 40, 9_000),
60 * SEC,
LIMIT,
{ track: 90 },
);
assert.equal(trailBodySegments(fixes, fixes.length).length, 2);
// Nor is the head drawn along a curve across them.
assert.deepEqual(trailHeadCurvePoints(fixes, 1, 30 * SEC), []);
});
test('a report with no altitude keeps no position and breaks the line', () => {
const fixes = [];
insertTrailFix(fixes, circle(0), 0, LIMIT, { track: trackAt(0) });
insertTrailFix(fixes, circle(15), 15 * SEC, LIMIT, { nullAltitude: true });
insertTrailFix(fixes, circle(30), 30 * SEC, LIMIT, { track: trackAt(30) });
assert.equal(fixes[1].position, null);
assert.equal(trailBodySegments(fixes, fixes.length).length, 2);
});
test('a report arriving late goes in time order and the curve stays smooth', () => {
const fixes = [];
for (const t of [0, 30, 15, 45])
insertTrailFix(fixes, circle(t), t * SEC, LIMIT, { track: trackAt(t) });
assert.deepEqual(
fixes.map((f) => f.timeMs / SEC),
[0, 15, 30, 45],
);
const [line] = trailBodySegments(fixes, fixes.length);
assert.ok(sharpestTurnDeg(line) < 3);
});
test('the head runs along the curve to the next report and stops before the display time', () => {
const fixes = [];
insertTrailFix(fixes, circle(0), 0, LIMIT, { track: trackAt(0) });
insertTrailFix(fixes, circle(15), 15 * SEC, LIMIT, { track: trackAt(15) });
const head = trailHeadCurvePoints(fixes, 1, 10 * SEC);
assert.ok(head.length > 5);
const drawnAt = circle(10);
const [body] = trailBodySegments(fixes, 1);
// Body, head points, then the aircraft: one smooth line.
assert.ok(sharpestTurnDeg([...body, ...head, drawnAt]) < 3);
for (const p of head)
assert.ok(Math.abs(Cesium.Cartesian3.distance(p, at(0, R)) - R) < 20);
});