490 lines
19 KiB
JavaScript
490 lines
19 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const PlatformService = require('../../../../app/service/systems/platform');
|
|
const IndexMethodService = require('../../../../app/service/systemMethods/index');
|
|
|
|
/**
|
|
* 创建平台服务测试桩。
|
|
*
|
|
* @param {Object} [options={}] 测试配置。
|
|
* @return {Object} 测试上下文和调用记录。
|
|
*/
|
|
function createPlatformServiceHarness(options = {}) {
|
|
const calls = {
|
|
findAll: 0,
|
|
platformFindAll: 0,
|
|
platformSettingFindAll: 0,
|
|
printPlatformFindAll: 0,
|
|
qimenAppFindAll: 0,
|
|
getCache: 0,
|
|
setCache: 0,
|
|
};
|
|
|
|
const printPlatformModel = options.hasPrintPlatformModel === false ? undefined : {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.printPlatformFindAll += 1;
|
|
calls.printPlatformFindAllQuery = query;
|
|
|
|
const printPlatformRows = options.printPlatformRows || [
|
|
{ id: 201, platform_code: 'LEGACY_OPEN_API', request_platform_code: 'OPEN_API' },
|
|
{ id: 202, platform_code: 'LEGACY_QM_WMS', request_platform_code: 'QM_WMS' },
|
|
];
|
|
const whereIds = query && query.where && Array.isArray(query.where.id) ? query.where.id.map(Number) : [];
|
|
|
|
return printPlatformRows.filter(row => whereIds.includes(Number(row.id)));
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
user: {
|
|
store_id: options.storeId === undefined ? 100000 : options.storeId,
|
|
},
|
|
app: {
|
|
async getCacheAsync(cacheKey) {
|
|
calls.getCache += 1;
|
|
if (typeof options.getCacheAsync === 'function') {
|
|
return await options.getCacheAsync(cacheKey);
|
|
}
|
|
return options.cachedValue === undefined ? null : options.cachedValue;
|
|
},
|
|
async setCacheAsync(cacheKey, value, ttl) {
|
|
calls.setCache += 1;
|
|
calls.cachePayload = { cacheKey, value, ttl };
|
|
if (typeof options.setCacheAsync === 'function') {
|
|
return await options.setCacheAsync(cacheKey, value, ttl);
|
|
}
|
|
},
|
|
},
|
|
model: {
|
|
Platform: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.platformFindAll += 1;
|
|
calls.platformFindAllQuery = query;
|
|
return options.rows || [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'DIFF_ROUTER', print_platform_id: 201 },
|
|
{ code: 'TB', name: '淘宝平台', platform_code: 'TB_ROUTE', print_platform_id: 202 },
|
|
];
|
|
},
|
|
},
|
|
PlatformSetting: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.platformSettingFindAll += 1;
|
|
calls.platformSettingFindAllQuery = query;
|
|
return options.settingRows || [
|
|
{ id: 11, code: 'JD-STORE', platform_code: 'JD', warehouse_id: 100000 },
|
|
{ id: 12, code: 'TB-STORE', platform_code: 'TB', warehouse_id: 100000 },
|
|
];
|
|
},
|
|
},
|
|
QimenApp: {
|
|
async findAll(query) {
|
|
calls.findAll += 1;
|
|
calls.qimenAppFindAll += 1;
|
|
calls.qimenAppFindAllQuery = query;
|
|
return options.qimenAppRows || [
|
|
{ id: 1, name: '奇门应用A', app_key: 'app-key-a' },
|
|
{ id: 2, name: '奇门应用B', app_key: 'app-key-b' },
|
|
];
|
|
},
|
|
},
|
|
PrintPlatform: printPlatformModel,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/platform.test.js', () => {
|
|
it('should load merged platform list from database and cache it when cache is empty', async () => {
|
|
const { ctx, calls } = createPlatformServiceHarness();
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 3);
|
|
assert.equal(calls.platformFindAll, 1);
|
|
assert.equal(calls.platformSettingFindAll, 1);
|
|
assert.equal(calls.printPlatformFindAll, 1);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.equal(calls.cachePayload.cacheKey, 'platform:list:100000');
|
|
assert.equal(calls.cachePayload.ttl, 600);
|
|
assert.deepEqual(result, [
|
|
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API', value: 'JD', label: '京东平台', platform_code: 'JD', platform_name: '京东平台', is_system_setting: false },
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
{ id: 11, code: 'JD-STORE', name: 'JD-STORE (京东平台)', value: 'JD-STORE', label: 'JD-STORE (京东平台)', is_system_setting: true, alias: '', platform_code: 'JD', platform_name: '京东平台', default_express_interface: 'OPEN_API' },
|
|
{ id: 12, code: 'TB-STORE', name: 'TB-STORE (淘宝平台)', value: 'TB-STORE', label: 'TB-STORE (淘宝平台)', is_system_setting: true, alias: '', platform_code: 'TB', platform_name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
]);
|
|
});
|
|
|
|
it('should load full platform list with raw fields and cache it with a dedicated cache key', async () => {
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'OPEN_API', interface_url: 'https://jd.example.com', appkey: 'JD_APP' },
|
|
{ code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS', interface_url: 'https://tb.example.com', appkey: 'TB_APP' },
|
|
],
|
|
settingRows: [
|
|
{ id: 11, code: 'JD-STORE', platform_code: 'JD', warehouse_id: 100000, name: 'JD-STORE' },
|
|
{ id: 12, code: 'TB-STORE', platform_code: 'TB', warehouse_id: 100000, name: 'TB-STORE' },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList(false, true);
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 2);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.equal(calls.cachePayload.cacheKey, 'platform:list:100000:full');
|
|
assert.equal(calls.cachePayload.ttl, 600);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result[0], 'interface_url'), true);
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result[0], 'appkey'), true);
|
|
assert.equal(result[0].value, 'JD');
|
|
assert.equal(result[0].label, '京东平台');
|
|
assert.equal(Object.prototype.hasOwnProperty.call(result[2], 'warehouse_id'), true);
|
|
assert.equal(result[2].warehouse_id, 100000);
|
|
assert.equal(result[2].label, 'JD-STORE (京东平台)');
|
|
});
|
|
|
|
it('should resolve route_platform_code from PrintPlatform.request_platform_code in full mode', async () => {
|
|
const { ctx } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'KS', name: '快手平台', platform_code: 'KS_ROUTE', print_platform_id: 9527 },
|
|
],
|
|
printPlatformRows: [
|
|
{ id: 9527, platform_code: 'LEGACY_POLYAPI', request_platform_code: 'POLYAPI' },
|
|
],
|
|
settingRows: [
|
|
{ id: 31, code: 'KS-STORE', platform_code: 'KS', warehouse_id: 100000, name: 'KS-STORE' },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList(false, true);
|
|
|
|
assert.equal(result[0].route_platform_code, 'POLYAPI');
|
|
assert.equal(result[0].default_express_interface, 'POLYAPI');
|
|
assert.equal(result[1].route_platform_code, 'POLYAPI');
|
|
assert.equal(result[1].default_express_interface, 'POLYAPI');
|
|
});
|
|
|
|
it('should keep flat and full platform list caches isolated', async () => {
|
|
const cacheValues = new Map([
|
|
[ 'platform:list:100000', [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
]],
|
|
[ 'platform:list:100000:full', [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false, interface_url: 'https://tb.example.com' },
|
|
]],
|
|
]);
|
|
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
getCacheAsync: async cacheKey => cacheValues.get(cacheKey) || null,
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const flatResult = await service.getPlatformList();
|
|
const fullResult = await service.getPlatformList(false, true);
|
|
|
|
assert.equal(calls.getCache, 2);
|
|
assert.equal(calls.findAll, 0);
|
|
assert.equal(calls.setCache, 0);
|
|
assert.deepEqual(flatResult, cacheValues.get('platform:list:100000'));
|
|
assert.deepEqual(fullResult, cacheValues.get('platform:list:100000:full'));
|
|
});
|
|
|
|
it('should return cached platform list without querying database', async () => {
|
|
const cachedValue = [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
];
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
cachedValue,
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.findAll, 0);
|
|
assert.equal(calls.setCache, 0);
|
|
assert.deepEqual(result, cachedValue);
|
|
});
|
|
|
|
it('should merge platform setting code to real platform code and name', async () => {
|
|
const { ctx } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'TB', name: '淘宝平台', platform_code: 'TB_ROUTE', print_platform_id: 202 },
|
|
],
|
|
settingRows: [
|
|
{ id: 21, code: 'TB-STORE', platform_code: 'TB', warehouse_id: 100000 },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.deepEqual(result, [
|
|
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
|
{ id: 21, code: 'TB-STORE', name: 'TB-STORE (淘宝平台)', value: 'TB-STORE', label: 'TB-STORE (淘宝平台)', is_system_setting: true, alias: '', platform_code: 'TB', platform_name: '淘宝平台', default_express_interface: 'QM_WMS' },
|
|
]);
|
|
});
|
|
|
|
it('should derive default_express_interface from PrintPlatform.request_platform_code instead of platform.platform_code', async () => {
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'DIFF_ROUTER', print_platform_id: 9527 },
|
|
],
|
|
printPlatformRows: [
|
|
{ id: 9527, platform_code: 'LEGACY_ROUTER', request_platform_code: 'OPEN_API' },
|
|
],
|
|
settingRows: [
|
|
{ id: 11, code: 'JD-STORE', platform_code: 'JD', warehouse_id: 100000 },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList();
|
|
|
|
assert.equal(calls.printPlatformFindAll, 1);
|
|
assert.deepEqual(result, [
|
|
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API', value: 'JD', label: '京东平台', platform_code: 'JD', platform_name: '京东平台', is_system_setting: false },
|
|
{ id: 11, code: 'JD-STORE', name: 'JD-STORE (京东平台)', value: 'JD-STORE', label: 'JD-STORE (京东平台)', is_system_setting: true, alias: '', platform_code: 'JD', platform_name: '京东平台', default_express_interface: 'OPEN_API' },
|
|
]);
|
|
});
|
|
|
|
it('should not read wms_platform.platform_code for route_platform_code when request_platform_code is missing', async () => {
|
|
const { ctx } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'DIFF_ROUTER', print_platform_id: 9527 },
|
|
],
|
|
printPlatformRows: [
|
|
{ id: 9527, platform_code: 'LEGACY_ONLY', request_platform_code: '' },
|
|
],
|
|
settingRows: [
|
|
{ id: 11, code: 'JD-STORE', platform_code: 'JD', warehouse_id: 100000 },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList(false, true);
|
|
|
|
assert.equal(result[0].route_platform_code, 'JD');
|
|
assert.notEqual(result[0].route_platform_code, 'DIFF_ROUTER');
|
|
assert.equal(result[0].default_express_interface, '');
|
|
assert.equal(result[1].route_platform_code, 'JD');
|
|
assert.notEqual(result[1].route_platform_code, 'DIFF_ROUTER');
|
|
assert.equal(result[1].default_express_interface, '');
|
|
});
|
|
|
|
it('should load system platform list and cache it with the system cache key when systemOnly is true', async () => {
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
rows: [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'DIFF_ROUTER', print_platform_id: 201 },
|
|
],
|
|
});
|
|
const service = new PlatformService(ctx);
|
|
|
|
const result = await service.getPlatformList(true);
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.printPlatformFindAll, 1);
|
|
assert.equal(calls.platformFindAll, 1);
|
|
assert.equal(calls.platformSettingFindAll, 0);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.equal(calls.cachePayload.cacheKey, 'platform:list:system');
|
|
assert.equal(calls.cachePayload.ttl, 600);
|
|
assert.deepEqual(result, [
|
|
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API', value: 'JD', label: '京东平台', platform_code: 'JD', platform_name: '京东平台', is_system_setting: false },
|
|
]);
|
|
});
|
|
|
|
it('should delegate index.getPlatformList to the platform service', async () => {
|
|
const { ctx } = createPlatformServiceHarness();
|
|
let serviceCalls = 0;
|
|
let receivedSystemOnly = null;
|
|
let receivedFull = null;
|
|
ctx.service = {
|
|
systems: {
|
|
platform: {
|
|
async getPlatformList(systemOnly, full) {
|
|
serviceCalls += 1;
|
|
receivedSystemOnly = systemOnly;
|
|
receivedFull = full;
|
|
return [
|
|
{ code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS', value: 'TB', label: '淘宝平台' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const methodService = new IndexMethodService(ctx);
|
|
const result = await methodService.getPlatformList({});
|
|
|
|
assert.equal(serviceCalls, 1);
|
|
assert.equal(receivedSystemOnly, false);
|
|
assert.equal(receivedFull, false);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, [
|
|
{ code: 'TB', name: '淘宝平台', platform_code: 'QM_WMS', value: 'TB', label: '淘宝平台' },
|
|
]);
|
|
});
|
|
|
|
it('should delegate index.getPlatformList with systemOnly to the platform service', async () => {
|
|
const { ctx } = createPlatformServiceHarness();
|
|
let serviceCalls = 0;
|
|
let receivedSystemOnly = null;
|
|
let receivedFull = null;
|
|
ctx.service = {
|
|
systems: {
|
|
platform: {
|
|
async getPlatformList(systemOnly, full) {
|
|
serviceCalls += 1;
|
|
receivedSystemOnly = systemOnly;
|
|
receivedFull = full;
|
|
return [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'OPEN_API', value: 'JD', label: '京东平台' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const methodService = new IndexMethodService(ctx);
|
|
const result = await methodService.getPlatformList({ systemOnly: true, full: true });
|
|
|
|
assert.equal(serviceCalls, 1);
|
|
assert.equal(receivedSystemOnly, true);
|
|
assert.equal(receivedFull, true);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'OPEN_API', value: 'JD', label: '京东平台' },
|
|
]);
|
|
});
|
|
|
|
it('should delegate index.getPlatformList with is_system to the platform service', async () => {
|
|
const { ctx } = createPlatformServiceHarness();
|
|
let serviceCalls = 0;
|
|
let receivedSystemOnly = null;
|
|
let receivedFull = null;
|
|
ctx.service = {
|
|
systems: {
|
|
platform: {
|
|
async getPlatformList(systemOnly, full) {
|
|
serviceCalls += 1;
|
|
receivedSystemOnly = systemOnly;
|
|
receivedFull = full;
|
|
return [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'OPEN_API', value: 'JD', label: '京东平台' },
|
|
];
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const methodService = new IndexMethodService(ctx);
|
|
const result = await methodService.getPlatformList({ is_system: 1, full: 0 });
|
|
|
|
assert.equal(serviceCalls, 1);
|
|
assert.equal(receivedSystemOnly, true);
|
|
assert.equal(receivedFull, false);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, [
|
|
{ code: 'JD', name: '京东平台', platform_code: 'OPEN_API', value: 'JD', label: '京东平台' },
|
|
]);
|
|
});
|
|
|
|
it('should return cached qimen app list without querying database', async () => {
|
|
const cachedValue = [
|
|
{ id: 1, name: '奇门应用A', app_key: 'app-key-a' },
|
|
];
|
|
const { ctx, calls } = createPlatformServiceHarness({
|
|
cachedValue,
|
|
});
|
|
const methodService = new IndexMethodService(ctx);
|
|
|
|
const result = await methodService.getQimenAppList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.qimenAppFindAll, 0);
|
|
assert.equal(calls.setCache, 0);
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual({
|
|
list: result.list,
|
|
total: result.total,
|
|
code: result.code,
|
|
}, {
|
|
list: cachedValue,
|
|
total: 1,
|
|
code: 200,
|
|
});
|
|
});
|
|
|
|
it('should query qimen app list and cache it for 300 seconds on cache miss', async () => {
|
|
const qimenAppRows = [
|
|
{
|
|
get() {
|
|
return { id: 2, name: '奇门应用B', app_key: 'app-key-b', created_at: '2024-01-01', updated_at: '2024-01-02' };
|
|
},
|
|
},
|
|
{
|
|
get() {
|
|
return { id: 3, name: '奇门应用C', app_key: 'app-key-c', created_at: '2024-01-03', updated_at: '2024-01-04' };
|
|
},
|
|
},
|
|
];
|
|
const { ctx, calls } = createPlatformServiceHarness({ qimenAppRows });
|
|
const methodService = new IndexMethodService(ctx);
|
|
|
|
const result = await methodService.getQimenAppList();
|
|
|
|
assert.equal(calls.getCache, 1);
|
|
assert.equal(calls.qimenAppFindAll, 1);
|
|
assert.equal(calls.setCache, 1);
|
|
assert.deepEqual(calls.qimenAppFindAllQuery, {
|
|
attributes: [ 'id', 'name', 'app_key' ],
|
|
order: [[ 'id', 'ASC' ]],
|
|
});
|
|
assert.deepEqual(calls.cachePayload, {
|
|
cacheKey: 'index:qimenAppList',
|
|
value: [
|
|
{ id: 2, name: '奇门应用B', app_key: 'app-key-b' },
|
|
{ id: 3, name: '奇门应用C', app_key: 'app-key-c' },
|
|
],
|
|
ttl: 300,
|
|
});
|
|
assert.deepEqual({
|
|
list: result.list,
|
|
total: result.total,
|
|
code: result.code,
|
|
}, {
|
|
list: [
|
|
{ id: 2, name: '奇门应用B', app_key: 'app-key-b' },
|
|
{ id: 3, name: '奇门应用C', app_key: 'app-key-c' },
|
|
],
|
|
total: 2,
|
|
code: 200,
|
|
});
|
|
});
|
|
});
|