import { describe, expect, it } from 'vitest'; import { computeUnloadSeconds, pickGeofenceEnterTask, shouldMarkGeofenceEnter, shouldTakeOpenEnterOnComplete, type GeofenceCandidate, } from '../src/services/deliveryGeofenceLogic'; const A: GeofenceCandidate = { task_code: 'T1', lat: 31.23, lng: 121.47, geofence_entered_at: null, created_at: '2026-09-17T05:00:00+08:00', }; const B: GeofenceCandidate = { task_code: 'T2', lat: 31.2305, lng: 121.4704, geofence_entered_at: null, created_at: '2026-09-17T05:01:00+08:00', }; const nearB = { lat: 31.2305, lng: 121.4704, radiusM: 300 }; describe('delivery geofence / unload', () => { it('unload seconds = end − enter', () => { expect( computeUnloadSeconds('2026-09-17T06:00:00+08:00', '2026-09-17T06:08:30+08:00') ).toBe(510); }); it('缺进圈或结束 → 时长空;负差空', () => { expect(computeUnloadSeconds(null, '2026-09-17T06:08:00+08:00')).toBeNull(); expect(computeUnloadSeconds('2026-09-17T06:08:00+08:00', null)).toBeNull(); expect( computeUnloadSeconds('2026-09-17T06:10:00+08:00', '2026-09-17T06:08:00+08:00') ).toBeNull(); }); it('两家都在圈内取离点更近的一单', () => { const picked = pickGeofenceEnterTask([B, A], nearB); expect(picked?.task_code).toBe('T2'); }); it('第一单仍配送中且已进圈时第二单不能抢记', () => { const enteredA = { ...A, geofence_entered_at: '2026-09-17T06:00:00+08:00' }; expect(pickGeofenceEnterTask([enteredA, B], nearB)).toBeNull(); expect( shouldMarkGeofenceEnter({ lat: 31.23, lng: 121.47, radiusM: 300, next: enteredA, }) ).toBe(false); }); it('第一单结束后只剩第二单则写第二单', () => { expect(pickGeofenceEnterTask([B], nearB)?.task_code).toBe('T2'); }); it('圈外 / 无坐标 / 精度过差不进圈', () => { expect( pickGeofenceEnterTask([A], { lat: 31.24, lng: 121.48, radiusM: 300, }) ).toBeNull(); expect( shouldMarkGeofenceEnter({ lat: 31.23, lng: 121.47, radiusM: 300, next: { ...A, lat: null, lng: null }, }) ).toBe(false); expect( pickGeofenceEnterTask([A], { lat: 31.23, lng: 121.47, accuracy: 400, radiusM: 300, }) ).toBeNull(); }); it('下一站圈内且精度可接受 → 进圈', () => { expect( shouldMarkGeofenceEnter({ lat: 31.2302, lng: 121.4701, accuracy: 20, radiusM: 300, next: A, }) ).toBe(true); expect( pickGeofenceEnterTask([A], { lat: 31.2302, lng: 121.4701, accuracy: 20, radiusM: 300, })?.task_code ).toBe('T1'); }); it('结束配送:本单无进圈钟则拿走另一单的未完成进圈钟', () => { expect(shouldTakeOpenEnterOnComplete(false, true)).toBe(true); expect(shouldTakeOpenEnterOnComplete(true, true)).toBe(false); expect(shouldTakeOpenEnterOnComplete(false, false)).toBe(false); }); });