来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
165 lines
7.7 KiB
TypeScript
165 lines
7.7 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 配送进圈上报断档修复契约(2026-09-18)
|
||
* 设计:docs/design/20260918-delivery-geofence-report-gap.md
|
||
*
|
||
* 背景:ORD_WEB_20260918_BR004_0001 卸货时长为空。
|
||
* 轨迹表全天仅 3 点、全在 2.5 公里外、12:00:34 后中断,任务 12:07:12 才结束
|
||
* → geofence_entered_at 为空 → computeUnloadSeconds 返回 null。
|
||
*
|
||
* 防回退点:
|
||
* ① 小程序定时轮询必须【恒开】,不能再「后台更新可用就不启轮询」
|
||
* ② 后台定位授权不能再走无效的 wx.authorize(scope.userLocationBackground)
|
||
* ③ 结束配送必须带上最后定位,服务端在置 task_status=30 【之前】补一次进圈判定
|
||
*/
|
||
describe('配送进圈上报断档修复', () => {
|
||
const mpLocation = readFileSync(
|
||
path.join(__dirname, '../../miniprogram/utils/deliveryLocation.js'),
|
||
'utf8'
|
||
);
|
||
const mpRequest = readFileSync(
|
||
path.join(__dirname, '../../miniprogram/utils/request.js'),
|
||
'utf8'
|
||
);
|
||
const mpPanel = readFileSync(
|
||
path.join(__dirname, '../../miniprogram/components/delivery-panel/index.js'),
|
||
'utf8'
|
||
);
|
||
const stopRoute = readFileSync(
|
||
path.join(__dirname, '../src/routes/mobile/mobileDeliveryRoutes.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
it('① 定时轮询恒开:startDeliveryLocationWatch 内无条件调用 startGetLocationFallback', () => {
|
||
const watch = mpLocation.slice(mpLocation.indexOf('function startDeliveryLocationWatch'));
|
||
const body = watch.slice(0, watch.indexOf('\n}'));
|
||
const callIdx = body.indexOf('startGetLocationFallback(onPoint);');
|
||
expect(callIdx).toBeGreaterThan(-1);
|
||
// 无条件:从「绑定位置回调」到「启动轮询」之间不得有任何条件分支
|
||
// (旧实现把轮询放在 if (!wx.startLocationUpdateBackground) 与 fail 回调里)
|
||
const bindIdx = body.indexOf('bindLocationChange(onPoint);');
|
||
expect(bindIdx).toBeGreaterThan(-1);
|
||
expect(bindIdx).toBeLessThan(callIdx);
|
||
const between = body.slice(bindIdx, callIdx);
|
||
expect(between).not.toContain('if (');
|
||
expect(between).not.toContain('fail:');
|
||
});
|
||
|
||
it('② 后台定位授权不再用 wx.authorize(该 scope 无法这样授予)', () => {
|
||
const fn = mpLocation.slice(
|
||
mpLocation.indexOf('function ensureBackgroundLocationAuth'),
|
||
mpLocation.indexOf('function getHighAccuracyLocation')
|
||
);
|
||
expect(fn).toContain('wx.getSetting');
|
||
expect(fn).toContain('openSetting');
|
||
expect(fn).not.toContain("scope: 'scope.userLocationBackground'");
|
||
});
|
||
|
||
it('③ 上报接受后缓存最后已知点并导出', () => {
|
||
expect(mpLocation).toContain('lastAcceptedPoint =');
|
||
expect(mpLocation).toContain('function getLastAcceptedPoint()');
|
||
expect(mpLocation).toContain('getLastAcceptedPoint,');
|
||
});
|
||
|
||
it('④ stopDelivery 透传 lat/lng/accuracy', () => {
|
||
const fn = mpRequest.slice(
|
||
mpRequest.indexOf('function stopDelivery'),
|
||
mpRequest.indexOf('function reportDeliveryLocation')
|
||
);
|
||
expect(fn).toContain('position');
|
||
expect(fn).toContain('data.lat = Number(position.lat)');
|
||
expect(fn).toContain('data.lng = Number(position.lng)');
|
||
expect(fn).toContain('data.accuracy');
|
||
});
|
||
|
||
it('⑤ 结束配送时带上实时定位(带超时),失败回落最后已知点', () => {
|
||
expect(mpPanel).toContain('getLastAcceptedPoint');
|
||
expect(mpPanel).toContain('getHighAccuracyLocationWithin(3000)');
|
||
expect(mpPanel).toContain('stopDelivery(taskCode, stopPosition)');
|
||
// 关键:结束配送这段不得裸 await getHighAccuracyLocation(),否则 wx.getLocation
|
||
// 两回调都不触发时会把「结束配送」永久卡死。必须走带超时的包装。
|
||
// (文件别处 readDepartPoint 用裸调用是可以的:它是发车取点,非关键动作。)
|
||
const stopIdx = mpPanel.indexOf('stopDelivery(taskCode, stopPosition)');
|
||
expect(stopIdx).toBeGreaterThan(-1);
|
||
const stopBlock = mpPanel.slice(Math.max(0, stopIdx - 700), stopIdx);
|
||
expect(stopBlock).not.toContain('await getHighAccuracyLocation()');
|
||
expect(stopBlock).toContain('getHighAccuracyLocationWithin');
|
||
});
|
||
|
||
it('⑤b 带超时定位包装存在且超时后 resolve(null),不会挂住调用方', () => {
|
||
const fn = mpLocation.slice(
|
||
mpLocation.indexOf('function getHighAccuracyLocationWithin'),
|
||
mpLocation.indexOf('function pointFromLocation')
|
||
);
|
||
expect(fn).toContain('setTimeout');
|
||
expect(fn).toContain('finish(null)');
|
||
expect(fn).toContain('.catch(');
|
||
expect(mpLocation).toContain('getHighAccuracyLocationWithin,');
|
||
});
|
||
|
||
it('⑥ 服务端在置 task_status=30 之前补进圈判定,且 SQL 在 service 层', () => {
|
||
const stopFnStart = stopRoute.indexOf("router.post('/delivery/stop'");
|
||
expect(stopFnStart).toBeGreaterThan(-1);
|
||
const stopFn = stopRoute.slice(stopFnStart);
|
||
|
||
const fallbackIdx = stopFn.indexOf('claimGeofenceEnterOnStop({');
|
||
const status30Idx = stopFn.indexOf('SET task_status = 30, end_time = NOW()');
|
||
|
||
expect(fallbackIdx).toBeGreaterThan(-1);
|
||
// 关键顺序:进圈判定必须早于 task_status=30
|
||
// (service 内只认 task_status = 20 的任务)
|
||
if (status30Idx > -1) expect(fallbackIdx).toBeLessThan(status30Idx);
|
||
expect(stopFn).toContain('req.body?.lat');
|
||
expect(stopFn).toContain('req.body?.lng');
|
||
|
||
// SQL 下沉到 service 层(路由层裸 SQL 由 routeLayerBoundary 棘轮测试全局把关)
|
||
const geofenceSvc = readFileSync(
|
||
path.join(__dirname, '../src/services/deliveryGeofenceService.ts'),
|
||
'utf8'
|
||
);
|
||
expect(geofenceSvc).toContain('export async function claimGeofenceEnterOnStop');
|
||
const svcFn = geofenceSvc.slice(
|
||
geofenceSvc.indexOf('export async function markGeofenceEnteredForTask')
|
||
);
|
||
expect(svcFn).toContain('geofence_entered_at IS NULL');
|
||
expect(svcFn).toContain('Number(row.task_status) !== 20');
|
||
expect(svcFn).toContain('shouldMarkGeofenceEnter');
|
||
});
|
||
|
||
it('⑦ 定位上报把任务写成 20 时必须带 task_status < 20,已结束不会被打回', () => {
|
||
const locBlock = stopRoute
|
||
.split("router.post('/delivery/location'")[1]
|
||
?.split("router.get('/delivery/track/:taskCode'")[0];
|
||
expect(locBlock).toBeTruthy();
|
||
expect(locBlock).toContain('tryAdvanceOrderToDelivering');
|
||
expect(locBlock).toContain('WHERE task_code = $1 AND task_status < 20');
|
||
expect(locBlock).not.toMatch(
|
||
/UPDATE delivery_tasks SET task_status = 20[^\n]*\n\s*WHERE task_code = \$1`/
|
||
);
|
||
});
|
||
|
||
it('⑧ 结束配送进圈失败不 return 400,仍走 finalize', () => {
|
||
const stopFnStart = stopRoute.indexOf("router.post('/delivery/stop'");
|
||
const stopFn = stopRoute.slice(stopFnStart, stopRoute.indexOf("router.post('/delivery/sign-photo'"));
|
||
const fence = stopFn.indexOf('claimGeofenceEnterOnStop({');
|
||
const finalize = stopFn.indexOf('finalizeOrderFulfillment');
|
||
expect(fence).toBeGreaterThan(-1);
|
||
expect(finalize).toBeGreaterThan(fence);
|
||
expect(stopFn.slice(fence, finalize)).toContain('.catch(() => undefined)');
|
||
expect(stopFn.slice(fence, finalize)).not.toContain('return res.status(400)');
|
||
expect(stopFn.slice(finalize)).toContain('markDeliveryGeofenceEntered');
|
||
});
|
||
|
||
it('⑨ 配送中回前台会补一次超时取点上报,且不 await', () => {
|
||
const syncStart = mpPanel.indexOf('syncLocationWatch(tasks)');
|
||
const sync = mpPanel.slice(syncStart, mpPanel.indexOf('onNavigateNext()'));
|
||
expect(sync).toContain('getHighAccuracyLocationWithin(3000)');
|
||
expect(sync).not.toMatch(/await\s+getHighAccuracyLocationWithin/);
|
||
expect(sync).toContain('reportDeliveryLocation({');
|
||
expect(sync).toContain('.catch(() => {})');
|
||
});
|
||
});
|