|
| 1 | +import { Point } from 'src/pages/components/map-related/map-types' |
| 2 | +import { |
| 3 | + classifyGap, |
| 4 | + distanceMeters, |
| 5 | + gapSeverity, |
| 6 | + medianPingInterval, |
| 7 | + pingGaps, |
| 8 | +} from './gpsCoverage' |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for the per-ride GPS coverage measure used by the SingleLineMap strip. |
| 12 | + * The strip is built from the elapsed time *between consecutive pings* (not fixed |
| 13 | + * clock buckets), so a bus that stops reporting surfaces as one long gap. The gap |
| 14 | + * extraction and the median-relative classification are locked here. |
| 15 | + */ |
| 16 | + |
| 17 | +// Minimal Point factory — only recordedAtTime matters for coverage. |
| 18 | +const ping = (recordedAtTime: number): Point => ({ |
| 19 | + loc: [0, 0], |
| 20 | + color: 0, |
| 21 | + recordedAtTime, |
| 22 | +}) |
| 23 | + |
| 24 | +const MIN = 60_000 |
| 25 | +const base = 1_000_000 |
| 26 | + |
| 27 | +describe('pingGaps', () => { |
| 28 | + it('returns no gaps for fewer than two pings', () => { |
| 29 | + expect(pingGaps([])).toEqual([]) |
| 30 | + expect(pingGaps([ping(base)])).toEqual([]) |
| 31 | + }) |
| 32 | + |
| 33 | + it('ignores pings without a valid timestamp', () => { |
| 34 | + expect(pingGaps([ping(base), { loc: [0, 0], color: 0 }])).toEqual([]) |
| 35 | + }) |
| 36 | + |
| 37 | + it('produces one gap per consecutive pair, spanning the whole ride contiguously', () => { |
| 38 | + const gaps = pingGaps([ping(base), ping(base + 10_000), ping(base + 30_000)]) |
| 39 | + expect(gaps).toEqual([ |
| 40 | + { startMs: base, endMs: base + 10_000, gapMs: 10_000, startLoc: [0, 0], endLoc: [0, 0] }, |
| 41 | + { |
| 42 | + startMs: base + 10_000, |
| 43 | + endMs: base + 30_000, |
| 44 | + gapMs: 20_000, |
| 45 | + startLoc: [0, 0], |
| 46 | + endLoc: [0, 0], |
| 47 | + }, |
| 48 | + ]) |
| 49 | + // contiguous: each gap starts where the previous ended |
| 50 | + for (let i = 1; i < gaps.length; i++) { |
| 51 | + expect(gaps[i].startMs).toBe(gaps[i - 1].endMs) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + it('sorts out-of-order pings before computing gaps', () => { |
| 56 | + const gaps = pingGaps([ping(base + 30_000), ping(base), ping(base + 10_000)]) |
| 57 | + expect(gaps.map((g) => g.gapMs)).toEqual([10_000, 20_000]) |
| 58 | + }) |
| 59 | + |
| 60 | + it('surfaces a long dropout as a single wide gap', () => { |
| 61 | + const gaps = pingGaps([ping(base), ping(base + 15_000), ping(base + 15_000 + 5 * MIN)]) |
| 62 | + expect(gaps.map((g) => g.gapMs)).toEqual([15_000, 5 * MIN]) |
| 63 | + }) |
| 64 | + |
| 65 | + it('collapses pings that share a recordedAtTime (SIRI re-ingestion duplicates)', () => { |
| 66 | + // base appears twice (same instant, same place) — the duplicate must not create a |
| 67 | + // zero-length gap or a second gap starting at the same startMs. |
| 68 | + const gaps = pingGaps([ping(base), ping(base), ping(base + 10_000)]) |
| 69 | + expect(gaps).toEqual([ |
| 70 | + { startMs: base, endMs: base + 10_000, gapMs: 10_000, startLoc: [0, 0], endLoc: [0, 0] }, |
| 71 | + ]) |
| 72 | + }) |
| 73 | + |
| 74 | + it('keeps every gap startMs unique even with duplicate timestamps', () => { |
| 75 | + const gaps = pingGaps([ping(base), ping(base), ping(base + 10_000), ping(base + 10_000)]) |
| 76 | + const starts = gaps.map((g) => g.startMs) |
| 77 | + expect(new Set(starts).size).toBe(starts.length) |
| 78 | + }) |
| 79 | + |
| 80 | + it('carries each bounding ping location onto the gap', () => { |
| 81 | + const a: Point = { loc: [32.1, 34.8], color: 0, recordedAtTime: base } |
| 82 | + const b: Point = { loc: [32.2, 34.9], color: 0, recordedAtTime: base + 10_000 } |
| 83 | + const [gap] = pingGaps([a, b]) |
| 84 | + expect(gap.startLoc).toEqual([32.1, 34.8]) |
| 85 | + expect(gap.endLoc).toEqual([32.2, 34.9]) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('distanceMeters', () => { |
| 90 | + // Thin [lat, lon]-tuple adapter over geolib.getDistance; these pin the tuple order, not geolib. |
| 91 | + it('is 0 for identical points', () => { |
| 92 | + expect(distanceMeters([32, 34], [32, 34])).toBe(0) |
| 93 | + }) |
| 94 | + |
| 95 | + it('measures ~111 km per degree of latitude', () => { |
| 96 | + const d = distanceMeters([0, 0], [1, 0]) |
| 97 | + expect(d).toBeGreaterThan(111_000) |
| 98 | + expect(d).toBeLessThan(111_400) |
| 99 | + }) |
| 100 | + |
| 101 | + it('reads the tuple as [lat, lon] (not [lon, lat])', () => { |
| 102 | + // At latitude 32°, a degree of longitude is much shorter than a degree of latitude; |
| 103 | + // a swapped adapter would make these equal. |
| 104 | + const oneLat = distanceMeters([32, 34], [33, 34]) |
| 105 | + const oneLon = distanceMeters([32, 34], [32, 35]) |
| 106 | + expect(oneLon).toBeLessThan(oneLat) |
| 107 | + }) |
| 108 | +}) |
| 109 | + |
| 110 | +describe('medianPingInterval', () => { |
| 111 | + it('is 0 with fewer than two pings', () => { |
| 112 | + expect(medianPingInterval([])).toBe(0) |
| 113 | + expect(medianPingInterval([ping(base)])).toBe(0) |
| 114 | + }) |
| 115 | + |
| 116 | + it('computes the median gap between consecutive pings', () => { |
| 117 | + // gaps: 10s, 10s, 30s -> median 10s |
| 118 | + expect( |
| 119 | + medianPingInterval([ |
| 120 | + ping(base), |
| 121 | + ping(base + 10_000), |
| 122 | + ping(base + 20_000), |
| 123 | + ping(base + 50_000), |
| 124 | + ]), |
| 125 | + ).toBe(10_000) |
| 126 | + }) |
| 127 | + |
| 128 | + it('averages the two middle gaps for an even count', () => { |
| 129 | + // gaps: 10s, 20s -> median (10+20)/2 = 15s |
| 130 | + expect(medianPingInterval([ping(base), ping(base + 10_000), ping(base + 30_000)])).toBe(15_000) |
| 131 | + }) |
| 132 | +}) |
| 133 | + |
| 134 | +describe('classifyGap', () => { |
| 135 | + // 15s cadence; bands break at the sparse threshold (2× = 30s) and dropout (4× = 60s). |
| 136 | + const median = 15_000 |
| 137 | + |
| 138 | + it('treats everything as ok when there is no baseline', () => { |
| 139 | + expect(classifyGap(10 * MIN, 0)).toBe('ok') |
| 140 | + }) |
| 141 | + |
| 142 | + it('flags a near-cadence gap as ok up to the sparse threshold', () => { |
| 143 | + expect(classifyGap(median, median)).toBe('ok') |
| 144 | + expect(classifyGap(30_000, median)).toBe('ok') // exactly 2× |
| 145 | + }) |
| 146 | + |
| 147 | + it('flags a moderately stretched gap as sparse', () => { |
| 148 | + expect(classifyGap(30_001, median)).toBe('sparse') // just past 2× |
| 149 | + expect(classifyGap(60_000, median)).toBe('sparse') // exactly 4× |
| 150 | + }) |
| 151 | + |
| 152 | + it('flags a long gap as a dropout', () => { |
| 153 | + expect(classifyGap(60_001, median)).toBe('gap') // just past 4× |
| 154 | + expect(classifyGap(5 * MIN, median)).toBe('gap') |
| 155 | + }) |
| 156 | +}) |
| 157 | + |
| 158 | +describe('gapSeverity', () => { |
| 159 | + // 15s cadence; severity ramps 0 at 1× to 1 at the 4× dropout threshold (60s). |
| 160 | + const median = 15_000 |
| 161 | + |
| 162 | + it('is 0 when there is no baseline', () => { |
| 163 | + expect(gapSeverity(10 * MIN, 0)).toBe(0) |
| 164 | + }) |
| 165 | + |
| 166 | + it('is 0 at or below the median cadence', () => { |
| 167 | + expect(gapSeverity(median, median)).toBe(0) |
| 168 | + expect(gapSeverity(median / 2, median)).toBe(0) |
| 169 | + }) |
| 170 | + |
| 171 | + it('reaches 1 at (and clamps above) the dropout threshold', () => { |
| 172 | + expect(gapSeverity(60_000, median)).toBe(1) // exactly 4× |
| 173 | + expect(gapSeverity(10 * MIN, median)).toBe(1) // far beyond |
| 174 | + }) |
| 175 | + |
| 176 | + it('ramps linearly between the median and the dropout threshold', () => { |
| 177 | + expect(gapSeverity(37_500, median)).toBeCloseTo(0.5) // ratio 2.5, halfway from 1× to 4× |
| 178 | + }) |
| 179 | +}) |
0 commit comments