import test from 'node:test'; import assert from 'node:assert/strict'; import { MilitaryFlightRecords } from './records.js'; const observation = (fields = {}) => ({ id: 'aaa001', reference: 'aaa001', longitude: -77, latitude: 39, baroAltitudeM: 1000, ellipsoidAltitudeM: null, onGround: false, callsign: 'MIL1', typeCode: 'C17', speedMps: 100, courseDeg: 90, verticalRateMps: 1, positionTimeMs: 1700000000000, ...fields, }); const context = (fields = {}) => ({ observedAtMs: 1700000001000, floorWarmPoints: [], modelOwnsVisual: false, ...fields, }); const store = () => new MilitaryFlightRecords({ geoidHeight: () => 40, cachedGroundFloor: () => 200, floorAltitudeM: (height, floor) => Math.max(height, floor + 1.5), }); test('military records preserve feet for aviation, metres for rendering and source epochs', () => { const records = store(); records.geoidReady = true; const first = records.receive(observation(), context()); assert.equal(first.meta.altitudeFt, 1000 / 0.3048); assert.equal(first.meta.renderAltitudeM, 1040); assert.equal(first.fixEpochMs, 1700000000000); const next = records.receive( observation({ callsign: '', typeCode: '', positionTimeMs: null }), context(), ); assert.equal(next.meta.callsign, 'MIL1'); assert.equal(next.meta.type, 'C17'); assert.equal(next.fixEpochMs, 1700000001000); assert.equal(Object.hasOwn(next.meta, 'cullPosition'), false); }); test('military ground records leave model-owned placement alone and warm the same floor cell', () => { const records = store(); const owned = context({ modelOwnsVisual: true }); const first = records.receive( observation({ onGround: true, baroAltitudeM: null }), owned, ); assert.equal(first.meta.renderAltitudeM, 0); assert.deepEqual(owned.floorWarmPoints, [{ lat: 39, lon: -77 }]); const unowned = context(); const second = records.receive( observation({ onGround: true, baroAltitudeM: null }), unowned, ); assert.equal(second.meta.renderAltitudeM, 201.5); assert.deepEqual(unowned.floorWarmPoints, owned.floorWarmPoints); }); test('a military contact that lands with no altitude sits on the ground, not at its last airborne height', () => { for (const [modelOwnsVisual, expected] of [ [false, 201.5], [true, 0], ]) { const records = store(); records.receive( observation({ baroAltitudeM: 1539 }), context({ modelOwnsVisual }), ); // Every grounded report, not just the first after the landing. for (let poll = 0; poll < 3; poll += 1) { const landed = records.receive( observation({ onGround: true, baroAltitudeM: null, speedMps: 4.6 }), context({ modelOwnsVisual }), ); assert.equal(landed.meta.renderAltitudeM, expected); // The aviation readout keeps the last reported altitude, as before. assert.equal(landed.meta.altitudeFt, 1539 / 0.3048); } } }); test('a grounded military contact holds a ground altitude it reported earlier', () => { const records = store(); records.receive( observation({ onGround: true, baroAltitudeM: 320 }), context({ modelOwnsVisual: true }), ); for (let poll = 0; poll < 2; poll += 1) { const next = records.receive( observation({ onGround: true, baroAltitudeM: null }), context({ modelOwnsVisual: true }), ); assert.equal(next.meta.renderAltitudeM, 320); } // Airborne again: that ground altitude is forgotten, so the next landing // without an altitude starts from the ground, not from it. records.receive(observation({ baroAltitudeM: 900 }), context()); const relanded = records.receive( observation({ onGround: true, baroAltitudeM: null }), context({ modelOwnsVisual: true }), ); assert.equal(relanded.meta.renderAltitudeM, 0); }); test('military records keep bounded partial retention and release metadata only after removal admission', () => { const first = store(); const second = store(); const { meta } = first.receive(observation(), context()); assert.equal(second.data.size, 0); assert.equal( first.absence('aaa001', { complete: false, likelyLanded: true }), 'retain', ); meta.observedReceiptMs = Date.now() - 300001; assert.equal( first.absence('aaa001', { complete: false, likelyLanded: false }), 'stale', ); assert.equal( first.absence('aaa001', { complete: true, likelyLanded: false }), 'stale', ); assert.equal( first.absence('aaa001', { complete: true, likelyLanded: false }), 'remove', ); assert.equal(first.data.get('aaa001'), meta); first.forget('aaa001'); assert.equal(first.data.size, 0); assert.equal(first.geoidNCache.size, 0); assert.equal(first.missingPolls.size, 0); first.receive(observation(), context()); assert.equal( first.absence('aaa001', { complete: true, likelyLanded: true }), 'remove', ); }); test('complete military snapshots age out vanished aircraft and retain position-less ones unless landed', () => { const records = store(); for (const id of ['aaa001', 'aaa002', 'aaa003']) records.receive(observation({ id, reference: id }), context()); // aaa001 and aaa003 are still listed without a position; aaa002 vanished. const absent = (id, likelyLanded = false) => records.absence(id, { complete: true, retainIds: new Set(['aaa001', 'aaa003']), likelyLanded, }); assert.deepEqual( [absent('aaa002'), absent('aaa002'), absent('aaa002')], ['stale', 'stale', 'remove'], ); assert.equal(absent('aaa001'), 'retain'); assert.equal(records.missingPolls.has('aaa001'), false); // A landed contact that lost its position takes the landed fast-cull. assert.equal(absent('aaa003', true), 'remove'); // Position-less retention stays bounded like partial admission. records.data.get('aaa001').observedReceiptMs = Date.now() - 300001; assert.equal(absent('aaa001'), 'stale'); }); test('military emergency status follows the latest poll instead of sticking', () => { const records = store(); const first = records.receive( observation({ squawk: '1234', emergency: 'minfuel' }), context(), ); assert.equal(first.meta.squawk, '1234'); assert.equal(first.meta.emergency.kind, 'minfuel'); assert.equal(first.meta.emergency.severity, 'priority'); const next = records.receive( observation({ squawk: '1234', emergency: 'none' }), context(), ); assert.equal(next.meta.emergency, null); }); test('military records hold the last track and speed when a poll omits them', () => { const records = store(); records.receive(observation({ courseDeg: 270, speedMps: 200 }), context()); const missing = records.receive( observation({ courseDeg: null, speedMps: null }), context(), ); assert.equal(missing.meta.track, 270); assert.equal(missing.meta.speedMps, 200); const north = records.receive( observation({ courseDeg: 0, speedMps: 0 }), context(), ); assert.equal(north.meta.track, 0); assert.equal(north.meta.speedMps, 0); const unseen = store().receive( observation({ courseDeg: undefined, speedMps: undefined }), context(), ); assert.equal(unseen.meta.track, 0); assert.equal(unseen.meta.speedMps, 0); const landing = store(); landing.receive(observation({ courseDeg: 90, speedMps: 70 }), context()); const parked = landing.receive( observation({ onGround: true, baroAltitudeM: null, courseDeg: null, speedMps: null, }), context(), ); assert.equal(parked.meta.speedMps, 0); assert.equal(parked.meta.track, 90); }); test('an airborne military contact reported with no altitude is marked as drawn at an invented height', () => { const records = store(); const none = records.receive( observation({ baroAltitudeM: null, ellipsoidAltitudeM: null }), context(), ); assert.equal(none.meta.renderAltitudeInvented, true); const known = records.receive( observation({ positionTimeMs: 1700000005000 }), context(), ); assert.equal(known.meta.renderAltitudeInvented, false); const parked = store().receive( observation({ baroAltitudeM: null, onGround: true }), context(), ); assert.equal(parked.meta.renderAltitudeInvented, false); }); test('the tracked military contact is stale from its first missed update and kept until ten minutes after its newest fix', () => { const realNow = Date.now; const fixMs = Date.UTC(2026, 10, 9, 20, 0); let clock = fixMs + 5_000; Date.now = () => clock; try { for (const snapshot of [ { complete: true }, { complete: true, retainIds: new Set(['aaa001']) }, { complete: false }, ]) { const records = store(); records.receive( observation({ positionTimeMs: fixMs }), context({ observedAtMs: fixMs + 1_000 }), ); const absent = (tracked) => records.absence('aaa001', { ...snapshot, likelyLanded: false, tracked, }); clock = fixMs + 15_000; assert.equal(absent(true), 'stale'); assert.equal(records.missingPolls.get('aaa001'), 1); clock = fixMs + 9 * 60_000; assert.equal(absent(true), 'stale'); clock = fixMs + 10 * 60_000 - 1; assert.equal(absent(true), 'stale'); clock = fixMs + 10 * 60_000; assert.equal(absent(true), 'remove'); assert.equal(records.missingPolls.has('aaa001'), false); } const records = store(); records.receive( observation({ positionTimeMs: fixMs }), context({ observedAtMs: fixMs + 1_000 }), ); clock = fixMs + 15_000; const untracked = (snapshot) => records.absence('aaa001', { ...snapshot, likelyLanded: false }); assert.equal(untracked({ complete: false }), 'retain'); assert.deepEqual( [1, 2, 3].map(() => untracked({ complete: true })), ['stale', 'stale', 'remove'], ); } finally { Date.now = realNow; } }); test('a repeated military fix time marks the contact as not updated; removal is unchanged', () => { const records = store(); const fixMs = Date.UTC(2026, 10, 9, 20, 0); const poll = (snapshotMs, positionTimeMs) => { records.beginSnapshot(snapshotMs); records.receive( observation({ positionTimeMs }), context({ observedAtMs: snapshotMs }), ); }; poll(fixMs + 2_000, fixMs); assert.equal(records.fixRepeated.has('aaa001'), false); // adsb.lol re-sends the last position for up to a minute. poll(fixMs + 12_000, fixMs); assert.equal(records.fixRepeated.has('aaa001'), true); assert.equal(records.missingPolls.has('aaa001'), false); poll(fixMs + 12_000, fixMs); assert.equal(records.fixRepeated.has('aaa001'), true); poll(fixMs + 22_000, fixMs + 10_000); assert.equal(records.fixRepeated.has('aaa001'), false); poll(fixMs + 32_000, fixMs + 10_000); records.forget('aaa001'); assert.equal(records.fixRepeated.has('aaa001'), false); }); test('a military contact reported with no course faces the way its newer fixes went', () => { const records = store(); const T = 1700000000000; const taxi = (latitude, seconds) => records.receive( observation({ latitude, onGround: true, baroAltitudeM: null, courseDeg: null, speedMps: 8, positionTimeMs: T + seconds * 1000, }), context({ observedAtMs: T + seconds * 1000 + 500 }), ); assert.equal(taxi(39, 0).meta.track, 0); assert.ok(Math.abs(taxi(38.9995, 5).meta.track - 180) < 0.1); assert.ok( Math.abs(taxi(39.002, 3).meta.track - 180) < 0.1, 'an older fix gives no course', ); });