import test from 'node:test'; import assert from 'node:assert/strict'; import * as Cesium from 'cesium'; import { createFlightSnapshotRenderer } from './snapshotRenderer.js'; import { createMotion } from './motion.js'; import { createFlightState } from './state.js'; // A poll that repeats a contact's last position with new speed or course // must not move it past where the stale rules allow, and never adds a // report to the tracked trail. const ICAO = '00a002'; const SEC = 1000; /** Any property is a no-op function unless given. */ function stub(overrides = {}) { return new Proxy(overrides, { get: (target, key) => (key in target ? target[key] : () => undefined), }); } function harness({ ground = () => null }) { const appended = []; const history = []; const iconIds = new Set(); const groundFloor = stub({ cachedGroundFloor: ground, GROUND_FLOOR_LIFT_M: 0.5, }); const services = stub({ camera: stub(), focus: stub(), groundFloor, meshFloor: stub(), groundSnap: stub({ createGroundSnap: () => stub() }), }); const flightState = createFlightState({ services, source: { label: 'Fixture aircraft' }, }); // The shared icon set the layer draws its contacts into. flightState._icons = { has: (id) => iconIds.has(id), ids: () => [...iconIds], get count() { return iconIds.size; }, }; flightState._trackedIcao = ICAO; flightState._positionHistory.set(ICAO, history); const parts = { rendering: stub({ _modelOwnsVisual: () => false }), icons: stub({ _iconIds: () => [...iconIds], _addIcon: (id) => iconIds.add(id), _forgetContact: (id) => iconIds.delete(id), }), }; parts.motion = createMotion({ flightState, services, parts, layer: {} }); let meta = null; let fixEpochMs = 0; const records = stub({ receive: () => { flightState.records.data.set(ICAO, meta); return { prevMeta: meta, meta, groundFlipped: false, fixEpochMs }; }, absence: () => 'retain', }); const apply = createFlightSnapshotRenderer({ flightState, records, militaryRegistry: stub({ isMilitaryIcao: () => false }), groundFloor, meshFloor: stub(), rendering: parts.rendering, tracking: stub({ _militaryLayerSuppresses: () => false, _appendTrailFix: (position, epochMs) => appended.push({ position, epochMs }), }), motion: parts.motion, icons: parts.icons, enrichment: stub(), queries: stub({ _likelyLanded: () => false }), }); return { history, appended, poll(nextMeta, epochMs) { meta = nextMeta; fixEpochMs = epochMs; apply({ records: [{ id: ICAO }], complete: true }, null); }, }; } const lightAircraft = (velocity, track) => ({ rawLat: 38.1436, rawLon: -122.5561, renderAltitudeM: 10, altitude: 40, velocity, true_track: track, onGround: false, wasAirborne: true, klass: 'light', }); test('a low contact whose position repeats while its speed changes is not moved and adds nothing to the trail', (t) => { const t0 = Date.UTC(2026, 10, 9, 20, 0); t.mock.timers.enable({ apis: ['Date'], now: t0 }); const h = harness({ ground: () => -30 }); h.poll(lightAircraft(36.4, 63), t0); const first = h.history[0].position; for (const [i, velocity] of [30, 22, 14, 9].entries()) { t.mock.timers.tick(10 * SEC); h.poll(lightAircraft(velocity, 63 + i * 20), t0); } assert.equal(h.history.length, 1, 'a synthetic fix moved a holding contact'); assert.equal(h.history[0].position, first); assert.equal(h.appended.length, 1, 'only the real report joins the trail'); }); test('a cruising contact whose position repeats coasts no further than the stale cap, and the synthetic fix is not a report', (t) => { const t0 = Date.UTC(2026, 10, 9, 20, 0); t.mock.timers.enable({ apis: ['Date'], now: t0 }); const h = harness({}); const cruise = (velocity, track) => ({ rawLat: 36, rawLon: -115, renderAltitudeM: 9_000, altitude: 9_000, velocity, true_track: track, onGround: false, wasAirborne: true, klass: 'jet', }); h.poll(cruise(220, 90), t0); const report = h.history[0].position; t.mock.timers.tick(300 * SEC); h.poll(cruise(200, 100), t0); assert.equal(h.history.length, 2); const moved = Cesium.Cartesian3.distance(report, h.history[1].position); assert.ok( moved <= 220 * 300 + 1, `the synthetic fix coasted ${moved.toFixed(0)} m`, ); assert.equal(h.appended.length, 1, 'the synthetic fix joined the trail'); }); test('a report drawn at an invented height says so on its history fix', (t) => { const t0 = Date.UTC(2026, 10, 9, 20, 0); t.mock.timers.enable({ apis: ['Date'], now: t0 }); const h = harness({}); h.poll({ ...lightAircraft(60, 90), renderAltitudeInvented: true }, t0); h.poll({ ...lightAircraft(60, 90), rawLon: -122.55 }, t0 + 10 * SEC); assert.equal(h.history[0].inventedHeight, true); assert.equal(h.history[1].inventedHeight, false); }); test('with the ground unknown, a low barometric altitude alone does not hold a contact', (t) => { const t0 = Date.UTC(2026, 10, 9, 20, 0); t.mock.timers.enable({ apis: ['Date'], now: t0 }); const h = harness({}); h.poll(lightAircraft(36.4, 63), t0); t.mock.timers.tick(10 * SEC); h.poll(lightAircraft(30, 80), t0); // It coasts under the stale cap instead. assert.equal(h.history.length, 2); assert.equal(h.history[0].staleHolds, false); assert.equal(h.appended.length, 1); });