diff --git a/frontend/src/__tests__/map/applyDynamicLayerInterp.test.ts b/frontend/src/__tests__/map/applyDynamicLayerInterp.test.ts index 38c00e19..1a3fedb4 100644 --- a/frontend/src/__tests__/map/applyDynamicLayerInterp.test.ts +++ b/frontend/src/__tests__/map/applyDynamicLayerInterp.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; import { applyDynamicLayerInterp } from '@/components/map/applyDynamicLayerInterp'; import type { DynamicMapLayersResult } from '@/components/map/dynamicMapLayers.worker'; +import { MAX_INTERP_SECONDS } from '@/lib/constants'; function pointLayer( props: Record, @@ -56,6 +57,32 @@ describe('applyDynamicLayerInterp', () => { expect(coords && coords.type === 'Point' ? coords.coordinates[1] : null).toBeCloseTo(40, 1); }); + it('caps dead-reckoning when data is stale (tab hidden overnight)', () => { + const layer = (): DynamicMapLayersResult => ({ + commercialFlightsGeoJSON: pointLayer( + { kind: 'flight', baseLat: 40, baseLng: -74, spd: 500, hdg: 90, alt: 35000 }, + [-74, 40], + ), + privateFlightsGeoJSON: null, + privateJetsGeoJSON: null, + militaryFlightsGeoJSON: null, + trackedFlightsGeoJSON: null, + shipsGeoJSON: null, + meshtasticGeoJSON: null, + aprsGeoJSON: null, + }); + const lngAt = (dt: number) => { + const g = applyDynamicLayerInterp(layer(), dt).commercialFlightsGeoJSON?.features[0].geometry; + return g && g.type === 'Point' ? g.coordinates[0] : NaN; + }; + const eightHours = lngAt(8 * 3600); + // Same position as at the cap: no further drift however stale the data is. + expect(eightHours).toBeCloseTo(lngAt(MAX_INTERP_SECONDS), 9); + // 500 kt for 180 s ≈ 25 nm ≈ 0.54° of longitude at 40°N — not thousands of miles. + expect(eightHours - -74).toBeGreaterThan(0.3); + expect(eightHours - -74).toBeLessThan(0.7); + }); + it('does not move grounded flights', () => { const layers: DynamicMapLayersResult = { commercialFlightsGeoJSON: pointLayer( diff --git a/frontend/src/__tests__/map/maplibreDecomposition.test.ts b/frontend/src/__tests__/map/maplibreDecomposition.test.ts index 8289b6ed..4878420b 100644 --- a/frontend/src/__tests__/map/maplibreDecomposition.test.ts +++ b/frontend/src/__tests__/map/maplibreDecomposition.test.ts @@ -157,7 +157,8 @@ describe('MaplibreViewer decomposition — selection and dismissal', () => { it('satellite popup calls onEntityClick(null) on close', () => { const satSection = viewer.slice( viewer.indexOf("selectedEntity?.type === 'satellite'"), - viewer.indexOf("selectedEntity?.type === 'satellite'") + 500, + // 500 chars left no slack: onClose ended exactly at the window edge (LF) and fell outside on CRLF checkouts. + viewer.indexOf("selectedEntity?.type === 'satellite'") + 900, ); expect(satSection).toContain(' onEntityClick?.(null)}'); diff --git a/frontend/src/components/map/applyDynamicLayerInterp.ts b/frontend/src/components/map/applyDynamicLayerInterp.ts index 49c25c82..8e153adc 100644 --- a/frontend/src/components/map/applyDynamicLayerInterp.ts +++ b/frontend/src/components/map/applyDynamicLayerInterp.ts @@ -1,7 +1,6 @@ import { interpolatePosition } from '@/utils/positioning'; import type { DynamicMapLayersResult } from '@/components/map/dynamicMapLayers.worker'; - -const UNBOUNDED_INTERP_SECONDS = Number.POSITIVE_INFINITY; +import { MAX_INTERP_SECONDS } from '@/lib/constants'; type InterpProps = { baseLat?: number; @@ -32,7 +31,7 @@ function interpCoords(props: InterpProps, dtSeconds: number): [number, number] | spd, dtSeconds, 0, - UNBOUNDED_INTERP_SECONDS, + MAX_INTERP_SECONDS, ); return [newLng, newLat]; } diff --git a/frontend/src/components/map/hooks/useInterpolation.ts b/frontend/src/components/map/hooks/useInterpolation.ts index a1117bdc..eaeb57ed 100644 --- a/frontend/src/components/map/hooks/useInterpolation.ts +++ b/frontend/src/components/map/hooks/useInterpolation.ts @@ -2,9 +2,7 @@ import { useCallback, useRef, useEffect, useState } from 'react'; import { interpolatePosition } from '@/utils/positioning'; -import { INTERP_TICK_MS } from '@/lib/constants'; - -const UNBOUNDED_INTERP_SECONDS = Number.POSITIVE_INFINITY; +import { INTERP_TICK_MS, MAX_INTERP_SECONDS } from '@/lib/constants'; /** * Custom hook that provides position interpolation for flights, ships, and satellites. @@ -59,7 +57,7 @@ export function useInterpolation() { f.speed_knots, dt, 0, - UNBOUNDED_INTERP_SECONDS, + MAX_INTERP_SECONDS, ); return [newLng, newLat]; }, @@ -86,7 +84,7 @@ export function useInterpolation() { s.sog, dt, 0, - UNBOUNDED_INTERP_SECONDS, + MAX_INTERP_SECONDS, ); return [newLng, newLat]; }, @@ -105,7 +103,7 @@ export function useInterpolation() { s.speed_knots, dt, 0, - UNBOUNDED_INTERP_SECONDS, + MAX_INTERP_SECONDS, ); return [newLng, newLat]; }, diff --git a/frontend/src/lib/constants.ts b/frontend/src/lib/constants.ts index 53d4e90c..323ecb1e 100644 --- a/frontend/src/lib/constants.ts +++ b/frontend/src/lib/constants.ts @@ -16,6 +16,14 @@ export const NOMINATIM_DEBOUNCE_MS = 350; // ─── Map Interpolation ───────────────────────────────────────────────────── export const INTERP_TICK_MS = 2000; +/** + * Longest stretch, in seconds, that moving markers are dead-reckoned past their last known + * position. Live data refreshes every ~15s, so this never limits normal animation; it stops + * runaway drift when data goes stale (a tab left hidden overnight pauses polling but not the + * interpolation clock, which flung every aircraft thousands of miles along its old heading). + */ +export const MAX_INTERP_SECONDS = 180; + // ─── News/Alert Layout ────────────────────────────────────────────────────── export const ALERT_BOX_WIDTH_PX = 280; export const ALERT_MAX_OFFSET_PX = 350;