import { readFileSync } from 'fs'; import { createRequire } from 'module'; import { describe, expect, it } from 'vitest'; const require = createRequire(import.meta.url); const filter = require('../../miniprogram/utils/deliveryLocationFilter.js') as { createLocationReportFilter: () => { last: unknown; recent: unknown[] }; shouldAcceptLocationReport: ( state: unknown, point: { lat: number; lng: number; accuracy?: number | null }, nowMs: number ) => { accept: boolean; nextState: unknown }; }; const TRAIL = ` 1,11:52:29,30.3952919,104.1035121 2,11:52:50,30.3952040,104.1034342 3,11:53:08,30.3953380,104.1035411 4,11:53:58,30.3954839,104.1035411 5,11:54:28,30.3947686,104.1034871 6,11:54:43,30.3949186,104.1040262 7,11:54:58,30.3948486,104.1040001 8,11:55:13,30.3947767,104.1039922 9,11:55:28,30.3946718,104.1039271 10,11:55:43,30.3946696,104.1038650 11,11:56:13,30.3947146,104.1038300 12,11:56:28,30.3945497,104.1034182 13,11:56:43,30.3945747,104.1033900 14,11:56:58,30.3941048,104.1028971 15,11:57:28,30.3917795,104.1026619 16,11:57:43,30.3906863,104.1022700 17,11:58:13,30.3889388,104.1007563 18,11:58:43,30.3882756,104.0979085 19,11:59:13,30.3882430,104.0951804 20,11:59:43,30.3896075,104.0927512 21,11:59:58,30.3899542,104.0916382 22,12:00:28,30.3901478,104.0890015 23,12:00:58,30.3953380,104.1035411 24,12:01:14,30.3953380,104.1035411 25,12:01:30,30.3953380,104.1035411 26,12:01:46,30.3953380,104.1035411 27,12:02:02,30.3953380,104.1035411 28,12:02:20,30.3953380,104.1035411 29,12:02:36,30.3953380,104.1035411 30,12:02:52,30.3953380,104.1035411 31,12:03:08,30.3953380,104.1035411 32,12:03:33,30.3953380,104.1035411 33,12:03:54,30.3953380,104.1035411 34,12:04:09,30.3945204,104.0751392 `.trim().split('\n').map((line) => { const [seq, clock, lat, lng] = line.split(','); const [hh, mm, ss] = clock.split(':').map(Number); return { seq: Number(seq), t: ((hh * 60 + mm) * 60 + ss) * 1000, lat: Number(lat), lng: Number(lng), }; }); describe('deliveryLocationFilter', () => { it('drops freeze, teleport, and snap-back on the 34-point sample', () => { let state = filter.createLocationReportFilter(); const accepted: number[] = []; const rejected: number[] = []; for (const row of TRAIL) { const decision = filter.shouldAcceptLocationReport( state, { lat: row.lat, lng: row.lng }, row.t ); state = decision.nextState; if (decision.accept) accepted.push(row.seq); else rejected.push(row.seq); } expect(accepted[0]).toBe(1); expect(accepted).toContain(22); expect(accepted).toContain(34); expect(accepted).not.toContain(13); expect(rejected).toEqual(expect.arrayContaining([13, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33])); expect(rejected).not.toContain(34); expect(accepted).not.toContain(23); }); it('rejects coarse accuracy', () => { const first = filter.shouldAcceptLocationReport( filter.createLocationReportFilter(), { lat: 30.39, lng: 104.09, accuracy: 250 }, 1000 ); expect(first.accept).toBe(false); }); }); describe('deliveryLocation wiring', () => { it('filters in emit path', () => { const locUtil = readFileSync( new URL('../../miniprogram/utils/deliveryLocation.js', import.meta.url), 'utf8' ); expect(locUtil).toMatch(/shouldAcceptLocationReport/); expect(locUtil).toMatch(/resetLocationReportState/); }); });