Files
SupplierSystem/server/tests/mpDeliveryMap.test.ts
T
雨天 a1321571d2
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(小程序): 配送地图支持定位到当前位置
进页默认静默定位(已授权才执行),地图右下角加准星图标按钮

复用配送上报的授权与 gcj02 取点,只平移地图中心,不改路线拟合
2026-10-25 14:22:48 +08:00

127 lines
5.6 KiB
TypeScript

import { readFileSync } from 'fs';
import { afterEach, describe, expect, it } from 'vitest';
import { getMapClientConfig } from '../src/services/geoProvider';
import { applyRoutePlanToDriverTasks, publicRoutePlanForDriver } from '../src/services/deliveryRoutingService';
const appJson = JSON.parse(
readFileSync(new URL('../../miniprogram/app.json', import.meta.url), 'utf8')
);
const wxml = readFileSync(
new URL('../../miniprogram/components/delivery-panel/index.wxml', import.meta.url),
'utf8'
);
const panelJs = readFileSync(
new URL('../../miniprogram/components/delivery-panel/index.js', import.meta.url),
'utf8'
);
const locUtil = readFileSync(
new URL('../../miniprogram/utils/deliveryLocation.js', import.meta.url),
'utf8'
);
const mapUtil = readFileSync(
new URL('../../miniprogram/utils/deliveryMap.js', import.meta.url),
'utf8'
);
const exampleConfig = readFileSync(
new URL('../../miniprogram/config.example.js', import.meta.url),
'utf8'
);
const mobileRoute = readFileSync(
new URL('../src/routes/mobile/mobileDeliveryRoutes.ts', import.meta.url),
'utf8'
);
describe('小程序配送腾讯地图脚手架', () => {
const prevKey = process.env.TENCENT_MAP_KEY;
const prevSk = process.env.TENCENT_MAP_SK;
afterEach(() => {
if (prevKey === undefined) delete process.env.TENCENT_MAP_KEY;
else process.env.TENCENT_MAP_KEY = prevKey;
if (prevSk === undefined) delete process.env.TENCENT_MAP_SK;
else process.env.TENCENT_MAP_SK = prevSk;
});
it('declares getLocation, background location, and Tencent route-plan plugin', () => {
expect(appJson.requiredPrivateInfos).toContain('getLocation');
expect(appJson.requiredPrivateInfos).toContain('onLocationChange');
expect(appJson.requiredPrivateInfos).toContain('startLocationUpdateBackground');
expect(appJson.requiredPrivateInfos).not.toContain('startLocationUpdate');
expect(appJson.requiredBackgroundModes).toContain('location');
expect(appJson.plugins['route-plan']).toMatchObject({
version: '2.2.0',
provider: 'wx50b5593e81dd937a',
});
expect(appJson.permission['scope.userLocation'].desc.length).toBeLessThanOrEqual(30);
expect(appJson.permission['scope.userLocationBackground'].desc.length).toBeLessThanOrEqual(30);
expect(appJson.permission['scope.userLocation'].desc).toMatch(/导航/);
});
it('shows planned route polyline, next-stop nav, and location watch while delivering', () => {
expect(wxml).toMatch(/<map/);
expect(wxml).toMatch(/polyline/);
expect(wxml).toMatch(/onNavigateNext/);
expect(panelJs).toMatch(/startDeliveryLocationWatch/);
expect(panelJs).toMatch(/reportDeliveryLocation/);
expect(panelJs).not.toMatch(/hide\(\)\s*\{[\s\S]{0,80}stopDeliveryLocationWatch/);
expect(locUtil).toMatch(/startLocationUpdateBackground/);
expect(locUtil).not.toMatch(/wx\.startLocationUpdate\s*\(/);
expect(mobileRoute).toMatch(/markDeliveryGeofenceEntered/);
const locCall = panelJs.slice(panelJs.indexOf('reportDeliveryLocation({'), panelJs.indexOf('is_delivering: true'));
expect(locCall).not.toMatch(/order_code/);
expect(mapUtil).toMatch(/wx\.openLocation/);
expect(mapUtil).not.toMatch(/plugin:\/\/route-plan/);
expect(exampleConfig).not.toMatch(/tencentMapKey/);
expect(mobileRoute).toMatch(/route_plan: routePlan/);
expect(mobileRoute).toMatch(/map_client: getMapClientConfig/);
});
it('map 上有「定位」小按钮,且进页默认定位当前位置', () => {
// map 是原生组件,普通 view 盖不住,必须用 cover-view + cover-image
expect(wxml).toMatch(/<map[\s\S]*?id="deliveryMap"/);
expect(wxml).toMatch(/<cover-view[\s\S]*?class="map-locate/);
expect(wxml).toMatch(/<cover-image[^>]*class="map-locate-icon"/);
expect(wxml).toMatch(/src="\/assets\/icons\/locate\.png"/);
expect(wxml).toMatch(/bindtap="onLocateSelf"/);
// 取点与授权复用配送上报同一套,坐标系 gcj02
expect(panelJs).toMatch(/onLocateSelf\(\)/);
expect(panelJs).toMatch(/wx\.createMapContext\('deliveryMap'/);
expect(panelJs).toMatch(/moveToLocation\(\{ longitude, latitude/);
expect(panelJs).toMatch(/await ensureLocationAuth\(\)/);
expect(panelJs).toMatch(/await getHighAccuracyLocation\(\)/);
// 一进界面就默认定位(静默:未授权时不弹授权框)
expect(panelJs).toMatch(/this\.autoLocateSelf\(\)/);
expect(panelJs).toMatch(/locateSelf\(\{ silent: true \}\)/);
expect(panelJs).toMatch(/hasForegroundLocationAuth/);
});
it('orders driver tasks by last_route_plan sequence', () => {
const plan = publicRoutePlanForDriver({
departure_time: '05:10',
finish_time: '07:00',
total_distance_km: 12,
stops: [
{ sequence: 2, task_code: 'T2', lat: 31, lng: 121 },
{ sequence: 1, task_code: 'T1', lat: 31.1, lng: 121.1 },
],
});
expect(plan?.stops.map((s) => s.task_code)).toEqual(['T1', 'T2']);
const rows = applyRoutePlanToDriverTasks(
[{ task_code: 'T2' }, { task_code: 'T9' }, { task_code: 'T1' }],
plan
);
expect(rows.map((r) => r.task_code)).toEqual(['T1', 'T2', 'T9']);
expect(rows[0].route_sequence).toBe(1);
expect(rows[2].route_sequence).toBeNull();
});
it('exposes tencent key from env and never returns SK', () => {
process.env.TENCENT_MAP_KEY = 'test-client-key';
process.env.TENCENT_MAP_SK = 'must-not-leave-server';
expect(getMapClientConfig()).toEqual({ provider: 'tencent', key: 'test-client-key' });
expect(JSON.stringify(getMapClientConfig())).not.toMatch(/must-not-leave-server/);
delete process.env.TENCENT_MAP_KEY;
expect(getMapClientConfig()).toEqual({ provider: 'none', key: '' });
});
});