import test from 'node:test'; import assert from 'node:assert/strict'; import * as Cesium from 'cesium'; import { historyAfterTrail, historyBracketIndex, historyUnseenAt, liftGroundedHistory, mergeAircraftTimelineFixes, } from './timelineHistory.js'; const META = { renderAltitudeM: 3_000 }; const fix = (atMs, lat, altitudeM = null, lon = 0) => ({ atMs, lat, lon, altitudeM, }); const julian = (ms) => Cesium.JulianDate.fromDate(new Date(ms)); test('an aircraft with no fixes merges nothing', () => { assert.equal( mergeAircraftTimelineFixes([], { fixes: null }, META, 100, 0), null, ); }); test("an aircraft's fixes join its history in order, each at the render height moved by its own climb", () => { const history = mergeAircraftTimelineFixes( [], { baroAltitudeM: 1_000, fixes: [fix(120_000, 1, 1_200), fix(60_000, 0, 1_000), fix(180_000, 2)], }, META, 100, 120_000, ); assert.deepEqual( history.map((entry) => entry.epochMs), [60_000, 120_000, 180_000], ); const height = (entry) => Cesium.Cartographic.fromCartesian(entry.position).height; assert.ok(Math.abs(height(history[0]) - 3_000) < 1e-3); assert.ok(Math.abs(height(history[1]) - 3_200) < 1e-3); assert.ok( Math.abs(height(history[2]) - 3_000) < 1e-3, 'no altitude: the render height', ); // A later snapshot's fixes add to the earlier ones. const more = mergeAircraftTimelineFixes( history, { baroAltitudeM: 1_000, fixes: [fix(180_000, 2), fix(240_000, 3)] }, META, 100, 180_000, ); assert.deepEqual( more.map((entry) => entry.epochMs), [60_000, 120_000, 180_000, 240_000], ); }); test('the bracketing entry is the first at or after the time, else the last', () => { const history = [1_000, 2_000, 3_000].map((ms) => ({ time: Cesium.JulianDate.fromDate(new Date(ms)), })); const at = (ms) => historyBracketIndex(history, Cesium.JulianDate.fromDate(new Date(ms))); assert.equal(at(500), 0); assert.equal(at(1_000), 0); assert.equal(at(1_500), 1); assert.equal(at(3_000), 2); assert.equal(at(9_000), 2); }); test('each fix heads where the track goes next, the last where it came from', () => { const history = mergeAircraftTimelineFixes( [], { fixes: [ fix(0, 0, null, 0), fix(60_000, 0, null, 0.01), fix(120_000, 0.01, null, 0.01), fix(180_000, 0.01, null, 0.01), ], }, META, 100, 0, ); const tracks = history.map((entry) => entry.track); assert.ok(Math.abs(tracks[0] - 90) < 0.5, 'east'); assert.ok(Math.abs(tracks[1]) < 0.5, 'north'); assert.equal(tracks[2], undefined, 'no move to read'); assert.equal(tracks[3], undefined); assert.ok(history.every((entry) => entry.timeline === true)); }); test('a contact is unseen only well before the first of its own fixes', () => { const history = mergeAircraftTimelineFixes( [], { fixes: [fix(600_000, 0), fix(660_000, 1)] }, META, 100, 600_000, ); assert.equal(historyUnseenAt(history, julian(0)), true); assert.equal(historyUnseenAt(history, julian(560_000)), false); const live = [{ time: julian(600_000) }]; assert.equal(historyUnseenAt(live, julian(0)), false, 'not on its fixes'); assert.equal(historyUnseenAt(undefined, julian(0)), false); }); test('a grounded contact on its own fixes is lifted whole; else its newest fix', () => { const height = (entry) => Cesium.Cartographic.fromCartesian(entry.position).height; const history = mergeAircraftTimelineFixes( [], { fixes: [fix(0, 0), fix(60_000, 0.001)] }, { renderAltitudeM: 5 }, 0, 0, ); const lifted = Cesium.Cartesian3.fromDegrees(0, 0.001, 30); liftGroundedHistory(history, lifted, 5, 30); assert.ok(history.every((entry) => Math.abs(height(entry) - 30) < 1e-3)); const live = [ { position: Cesium.Cartesian3.fromDegrees(0, 0, 5) }, { position: Cesium.Cartesian3.fromDegrees(0, 0.001, 5) }, ]; liftGroundedHistory(live, lifted, 5, 30); assert.ok(Math.abs(height(live[0]) - 5) < 1e-3); assert.ok(Math.abs(height(live[1]) - 30) < 1e-3); }); test("a tracked trail takes the fixes after its newest point, up to the snapshot's time", () => { const history = [0, 60_000, 120_000, 180_000].map((epochMs) => ({ epochMs })); assert.deepEqual( historyAfterTrail(history, [{ timeMs: 60_000 }], 120_000).map( (entry) => entry.epochMs, ), [120_000], ); assert.equal(historyAfterTrail(history, [], 120_000).length, 3); }); test('playing on, the fixes a contact holds stay as they are and only the seam and newer ones are made', () => { const held = mergeAircraftTimelineFixes( [], { fixes: [fix(0, 0), fix(60_000, 1), fix(120_000, 2)] }, META, 100, 60_000, ); const kept = held.map((entry) => entry); // The next snapshot's run reaches back into the held fixes, a little // differently picked: those held stay, the newer are added after them. const next = mergeAircraftTimelineFixes( held, { fixes: [ fix(30_000, 0.6), fix(90_000, 1.6), fix(120_000, 2.1), fix(180_000, 3), fix(240_000, 4), ], }, META, 100, 120_000, ); assert.deepEqual( next.map((entry) => entry.epochMs), [0, 60_000, 120_000, 180_000, 240_000], ); for (const [k, entry] of kept.slice(0, -1).entries()) assert.equal(next[k], entry, 'a held entry is kept, not made again'); // The seam is made again from the new run: its course heads where the // new run goes, so the course and height do not step there. assert.notEqual(next[2], kept[2]); const lat = (entry) => Cesium.Math.toDegrees( Cesium.Cartographic.fromCartesian(entry.position).latitude, ); assert.ok(Math.abs(lat(next[2]) - 2.1) < 1e-9); assert.ok(Math.abs(lat(next[3]) - 3) < 1e-9); // Each new fix heads where the track goes next, read from the new run. assert.ok( Math.abs(next[3].track) < 1e-6 || Math.abs(next[3].track - 360) < 1e-6, ); }); test('a run that starts after the held fixes, or a history not of its own fixes, is merged whole', () => { const held = mergeAircraftTimelineFixes( [], { fixes: [fix(0, 0), fix(60_000, 1)] }, META, 100, 0, ); const later = mergeAircraftTimelineFixes( held, { fixes: [fix(120_000, 2), fix(180_000, 3)] }, META, 100, 120_000, ); assert.deepEqual( later.map((entry) => entry.epochMs), [0, 60_000, 120_000, 180_000], ); // A run reaching back over the held fixes replaces them, as before, when // the history holds a live report. const report = { ...held[1], timeline: undefined }; const mixed = mergeAircraftTimelineFixes( [held[0], report], { fixes: [fix(30_000, 0.5), fix(90_000, 1.5)] }, META, 100, 60_000, ); assert.deepEqual( mixed.map((entry) => entry.epochMs), [0, 30_000, 90_000], ); });