73 lines
2.4 KiB
JavaScript
73 lines
2.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { buildApp } from '../dist/app.js';
|
|
import {
|
|
haversineMeters,
|
|
StoreDiscoveryRepository
|
|
} from '../dist/stores/store-discovery-repository.js';
|
|
|
|
assert.ok(haversineMeters(31.2304, 121.4737, 31.2304, 121.4737) < 1);
|
|
assert.ok(haversineMeters(31.2304, 121.4737, 31.2200, 121.4800) > 1000);
|
|
|
|
const rows = [
|
|
{
|
|
id: 11, name: '近店', address: 'A', city: '上海市', district: '黄浦区',
|
|
longitude: '121.4737', latitude: '31.2304', contactPhone: '',
|
|
timezone: 'Asia/Shanghai', businessStatus: 'OPEN',
|
|
weekday: 4, openMinute: 0, closeMinute: 0, isClosed: 0, sortOrder: 1
|
|
},
|
|
{
|
|
id: 12, name: '远店', address: 'B', city: '上海市', district: '浦东新区',
|
|
longitude: '121.6000', latitude: '31.3000', contactPhone: '',
|
|
timezone: 'Asia/Shanghai', businessStatus: 'OPEN',
|
|
weekday: 4, openMinute: 0, closeMinute: 0, isClosed: 0, sortOrder: 2
|
|
}
|
|
];
|
|
const repository = new StoreDiscoveryRepository({
|
|
async execute(sql, params) {
|
|
assert.match(sql, /s\.tenant_id = \?/);
|
|
assert.deepEqual(params, ['7', '上海市']);
|
|
return [rows, []];
|
|
}
|
|
});
|
|
const stores = await repository.findStores({
|
|
tenantId: '7', city: '上海市', latitude: 31.2304, longitude: 121.4737,
|
|
now: new Date('2026-06-18T04:00:00.000Z')
|
|
});
|
|
assert.deepEqual(stores.map((store) => store.id), ['11', '12']);
|
|
assert.equal(stores[0].distanceMeters, 0);
|
|
assert.equal(stores[0].openNow, true);
|
|
|
|
const app = await buildApp({
|
|
storeDiscovery: {
|
|
tenancy: {
|
|
async resolveLoginContext(appId, tenantId) {
|
|
assert.equal(appId, 'wx-test-app');
|
|
assert.equal(tenantId, '7');
|
|
return { tenantId: '7', platformAppId: '9', appId };
|
|
}
|
|
},
|
|
repository: {
|
|
async findStores(query) {
|
|
assert.equal(query.city, '上海市');
|
|
return stores;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/app-api/stores?city=%E4%B8%8A%E6%B5%B7%E5%B8%82',
|
|
headers: { 'x-wechat-appid': 'wx-test-app', 'tenant-id': '7' }
|
|
});
|
|
assert.equal(response.statusCode, 200);
|
|
assert.equal(response.json().data[0].id, '11');
|
|
const invalid = await app.inject({
|
|
method: 'GET',
|
|
url: '/app-api/stores?latitude=31.2',
|
|
headers: { 'x-wechat-appid': 'wx-test-app', 'tenant-id': '7' }
|
|
});
|
|
assert.equal(invalid.statusCode, 400);
|
|
await app.close();
|
|
|
|
console.log('PASS: M03-C trusted distance sorting, city fallback and public store discovery are present.');
|