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 是原生组件,普通 view 盖不住,必须用 cover-view + cover-image expect(wxml).toMatch(/ { 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: '' }); }); });