From 8e514102514e87d134f6b5a434c952046e6fb828 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:57:21 +0100 Subject: [PATCH] fix(flight-live): do not read an earlier sector's aircraft as this flight having departed Before the scheduled departure, an aircraft in the air that is no nearer the destination than the origin is now stays unpicked, so booking only the later sector of a through flight no longer sends the departure notice hours early. --- .../nest/flight-live/flight-live.derive.ts | 15 +++++++- .../flight-live/flight-live.derive.test.ts | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/server/src/nest/flight-live/flight-live.derive.ts b/server/src/nest/flight-live/flight-live.derive.ts index a663f7626..86e341363 100644 --- a/server/src/nest/flight-live/flight-live.derive.ts +++ b/server/src/nest/flight-live/flight-live.derive.ts @@ -165,12 +165,24 @@ function claimedBySibling( }); } +/** + * Whether an aircraft in the air before the leg's scheduled departure cannot be + * this leg: it is no nearer the destination than the origin is, so it has not + * left the origin for it. That is the aircraft still flying an earlier sector + * of the same flight number, which nobody on the instance may have booked. + */ +function airborneBeforeDeparture(ac: FeedAircraft, lat: number, lng: number, route: Route, now: number): boolean { + if (route.scheduled_departure === null || now >= route.scheduled_departure || ac.alt_baro === 'ground') return false; + return kmTo(lat, lng, route.destination) >= kmTo(route.origin.lat, route.origin.lng, route.destination); +} + /** * The aircraft that flies this leg among those the feed answered for the * callsign: the one with a position nearest the great circle between the two * airports (least detour over the direct distance). A callsign is reused by * the next leg of a rotation, so two can be in the air at once. An aircraft a - * sibling sector of the same flight number claims at `now` is left to it. + * sibling sector of the same flight number claims at `now` is left to it, and + * so is one still on its way to the origin before the scheduled departure. */ export function pickAircraft( aircraft: readonly FeedAircraft[], @@ -183,6 +195,7 @@ export function pickAircraft( const lat = finite(ac.lat); const lng = finite(ac.lon); if (lat !== null && lng !== null && claimedBySibling(lat, lng, route, sectors.now, sectors.siblings)) continue; + if (lat !== null && lng !== null && airborneBeforeDeparture(ac, lat, lng, route, sectors.now)) continue; const score = lat === null || lng === null ? Number.MAX_VALUE : detourKm(lat, lng, route); if (best === null || score < bestScore) { best = ac; diff --git a/server/tests/unit/nest/flight-live/flight-live.derive.test.ts b/server/tests/unit/nest/flight-live/flight-live.derive.test.ts index 4313d3860..aaf35856a 100644 --- a/server/tests/unit/nest/flight-live/flight-live.derive.test.ts +++ b/server/tests/unit/nest/flight-live/flight-live.derive.test.ts @@ -138,6 +138,40 @@ describe('pickAircraft and observe', () => { expect(pick(westbound, second, at('2026-07-01T14:52:00Z'), flying)).toBe('swa'); }); + it('FLDER-021: with only the second sector of a through flight booked, the first sector in the air is not taken for it', () => { + const at = (iso: string) => Date.parse(iso); + const mdw = { code: 'MDW', name: 'Chicago Midway', lat: 41.7868, lng: -87.7522, timezone: 'America/Chicago' }; + const den = { code: 'DEN', name: 'Denver', lat: 39.8561, lng: -104.6737, timezone: 'America/Denver' }; + const second: ScheduledLeg = { + ...LEG, + callsign: 'SWA1234', + flight_number: 'WN 1234', + origin: mdw, + destination: den, + scheduled_departure: at('2026-07-01T14:45:00Z'), + scheduled_arrival: at('2026-07-01T17:15:00Z'), + }; + const swa = (overrides: Partial) => ac({ hex: 'swa', flight: 'SWA1234', ...overrides }); + const pick = (aircraft: FeedAircraft, now: number) => pickAircraft([aircraft], second, { now, siblings: [] }); + + const overOhio = swa({ alt_baro: 36000, lat: 40.5, lon: -82 }); + const onFinal = swa({ alt_baro: 2500, lat: 41.78, lon: -87.6 }); + expect(pick(overOhio, at('2026-07-01T13:00:00Z'))).toBeNull(); + expect(pick(onFinal, at('2026-07-01T14:00:00Z'))).toBeNull(); + const quiet = advance( + emptyState('SWA1234', '2026-07-01', 'MDW', 'DEN'), + pick(onFinal, at('2026-07-01T14:00:00Z')), + second, + at('2026-07-01T14:00:00Z'), + 'adsb.lol', + ); + expect(quiet).toMatchObject({ phase: 'scheduled', departed_at: null }); + + expect(pick(swa({ lat: 41.787, lon: -87.75 }), at('2026-07-01T14:20:00Z'))?.hex).toBe('swa'); + expect(pick(swa({ alt_baro: 6000, lat: 41.77, lon: -88.1 }), at('2026-07-01T14:40:00Z'))?.hex).toBe('swa'); + expect(pick(overOhio, at('2026-07-01T14:50:00Z'))?.hex).toBe('swa'); + }); + it('FLDER-003: reads a report at its own time, on the ground at zero feet, the geometric height as a fallback', () => { const now = DEP; expect(observe(ac(), now)).toEqual({