import { strict as assert } from 'assert'; import fs from 'fs'; import vm from 'vm'; import { savePickedLocation } from '../yjyf-app/utils/masterHallLocation.mjs'; let payload; const result = await savePickedLocation( { latitude: 26.57, longitude: 106.71, name: '测试地点', address: '测试地址' }, async (data) => { payload = data; return { location_city: '贵阳市', serve_radius: 20 }; } ); assert.deepEqual(payload, { lat: 26.57, lng: 106.71, label: '测试地点' }); assert.equal(result.city, '贵阳市'); assert.equal(result.serveRadius, 20); await assert.rejects(() => savePickedLocation({ latitude: 0, longitude: 0 }, async () => ({}))); await assert.rejects(() => savePickedLocation({ latitude: 26, longitude: 106 }, async () => { throw new Error('保存失败'); }), /保存失败/); // Exercise the real cache code with a simulated uni storage boundary. const storage = {}; const uni = { getStorageSync: (key) => storage[key], setStorageSync: (key, value) => { storage[key] = value; } }; const source = fs.readFileSync(new URL('../yjyf-app/utils/location.js', import.meta.url), 'utf8') .replace(/import \{ tiandituWebKey \} from [^;]+;/, "const tiandituWebKey = ''; ") .replace(/export function /g, 'function '); const context = vm.createContext({ uni, Date, Number, String, Math, Array, Boolean }); vm.runInContext(source, context); context.saveManualLocation({ lat: 26.57, lng: 106.71, label: '测试地点', serveRadius: 20, manual: true }); storage.yjyf_master_manual_loc_v1.updatedAt = Date.now() - 2 * 86400000; assert.equal(context.getSavedManualLocation().label, '测试地点'); assert.equal(context.getSavedManualLocation().manual, true); context.saveManualLocation({ lat: 26.57, lng: 106.71 }); storage.yjyf_master_manual_loc_v1.updatedAt = Date.now() - 2 * 86400000; assert.equal(context.getSavedManualLocation(), null); console.log('master hall location tests passed');