102 lines
3.7 KiB
TypeScript
102 lines
3.7 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import {
|
||
HeuristicGeoProvider,
|
||
TencentMapGeoProvider,
|
||
isTencentDirectionEnabled,
|
||
} from '../src/services/geoProvider';
|
||
|
||
/**
|
||
* 2026-10-26 用户要求:「只需要关闭路线规划 而不是地址解析」+「直接是默认关闭的状态」。
|
||
*
|
||
* 目标:
|
||
* · **默认(不设变量)就不请求** /ws/direction/v1/driving;里程/ETA 退回直线估算
|
||
* · 仅显式 `TENCENT_MAP_DIRECTION_ENABLED=1` 才开启腾讯驾车
|
||
* · **地理编码 /ws/geocoder/v1 必须始终工作**(关的是路线规划,不是地址解析)
|
||
*/
|
||
const KEY = 'AAAAAAAA-BBBBBB-CCCCCC-DDDDDD-EEEEEE-FFFFF';
|
||
const SK = 'test-sk';
|
||
|
||
function coords() {
|
||
return [
|
||
{ lat: 29.34, lng: 104.77 }, // 自贡出发
|
||
{ lat: 29.35, lng: 104.78 },
|
||
];
|
||
}
|
||
|
||
describe('腾讯地图:路线规划默认关闭、地址解析保留', () => {
|
||
const envBackup = { ...process.env };
|
||
|
||
beforeEach(() => {
|
||
delete process.env.TENCENT_MAP_DIRECTION_ENABLED;
|
||
});
|
||
|
||
afterEach(() => {
|
||
process.env = { ...envBackup };
|
||
vi.restoreAllMocks();
|
||
vi.unstubAllGlobals();
|
||
});
|
||
|
||
it('isTencentDirectionEnabled:不设=关闭;仅 1/true/yes/on 开启', () => {
|
||
expect(isTencentDirectionEnabled()).toBe(false); // 默认关闭
|
||
for (const v of ['0', 'false', 'no', 'off', '', 'disabled']) {
|
||
process.env.TENCENT_MAP_DIRECTION_ENABLED = v;
|
||
expect(isTencentDirectionEnabled(), `值 ${JSON.stringify(v)} 应视为关闭`).toBe(false);
|
||
}
|
||
for (const v of ['1', 'true', 'yes', 'on', ' TRUE ']) {
|
||
process.env.TENCENT_MAP_DIRECTION_ENABLED = v;
|
||
expect(isTencentDirectionEnabled(), `值 ${v} 应视为开启`).toBe(true);
|
||
}
|
||
});
|
||
|
||
it('默认关闭:不发 direction 请求,返回直线估算(estimated=true)', async () => {
|
||
const fetchSpy = vi.fn();
|
||
vi.stubGlobal('fetch', fetchSpy);
|
||
|
||
const provider = new TencentMapGeoProvider(KEY, SK);
|
||
const legs = await provider.estimateLegsFromCoords(coords());
|
||
|
||
expect(fetchSpy).not.toHaveBeenCalled();
|
||
expect(legs).toHaveLength(1);
|
||
expect(legs[0].estimated).toBe(true);
|
||
expect(legs[0].distance_km).toBeGreaterThan(0);
|
||
|
||
const expected = await new HeuristicGeoProvider().estimateLegsFromCoords(coords());
|
||
expect(legs).toEqual(expected);
|
||
});
|
||
|
||
it('默认关闭:地址解析(geocoder)仍然发起请求并成功返回坐标', async () => {
|
||
const fetchSpy = vi.fn(async (url: string) => {
|
||
expect(String(url)).toContain('/ws/geocoder/v1');
|
||
return {
|
||
headers: { get: () => null },
|
||
json: async () => ({ status: 0, result: { location: { lat: 29.3401, lng: 104.7702 } } }),
|
||
} as any;
|
||
});
|
||
vi.stubGlobal('fetch', fetchSpy);
|
||
|
||
const provider = new TencentMapGeoProvider(KEY, SK);
|
||
const loc = await provider.geocodeAddress('四川省自贡市大安区某路1号');
|
||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||
expect(loc).toEqual({ lat: 29.3401, lng: 104.7702 });
|
||
});
|
||
|
||
it('显式开启(=1)时才调用腾讯驾车接口', async () => {
|
||
process.env.TENCENT_MAP_DIRECTION_ENABLED = '1';
|
||
const fetchSpy = vi.fn(async (url: string) => {
|
||
expect(String(url)).toContain('/ws/direction/v1/driving');
|
||
return {
|
||
headers: { get: () => null },
|
||
json: async () => ({ status: 0, result: { routes: [{ distance: 1200, duration: 4 }] } }),
|
||
} as any;
|
||
});
|
||
vi.stubGlobal('fetch', fetchSpy);
|
||
|
||
const provider = new TencentMapGeoProvider(KEY, SK);
|
||
const legs = await provider.estimateLegsFromCoords(coords());
|
||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
||
expect(legs[0]).toEqual({ distance_km: 1.2, duration_minutes: 4, estimated: false });
|
||
});
|
||
});
|