WMS_MAIN/test/app/service/systems/platform.test.js
2026-04-16 20:00:01 +08:00

118 lines
3.7 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const PlatformService = require('../../../../app/service/systems/platform');
const IndexMethodService = require('../../../../app/service/systemMethods/index');
function createPlatformServiceHarness(options = {}) {
const calls = {
findAll: 0,
getCache: 0,
setCache: 0,
};
const ctx = {
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.findAllQuery = query;
return options.rows || [
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API' },
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS' },
];
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
};
return {
ctx,
calls,
};
}
describe('test/app/service/systems/platform.test.js', () => {
it('should load 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, 1);
assert.equal(calls.setCache, 1);
assert.equal(calls.cachePayload.cacheKey, 'platform:list');
assert.equal(calls.cachePayload.ttl, 600);
assert.deepEqual(result, [
{ code: 'JD', name: '京东平台', default_express_interface: 'OPEN_API', value: 'JD', label: '京东平台' },
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
]);
});
it('should return cached platform list without querying database', async () => {
const cachedValue = [
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
];
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 delegate index.getPlatformList to the platform service', async () => {
const { ctx } = createPlatformServiceHarness();
let serviceCalls = 0;
ctx.service = {
systems: {
platform: {
async getPlatformList() {
serviceCalls += 1;
return [
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
];
},
},
},
};
const methodService = new IndexMethodService(ctx);
const result = await methodService.getPlatformList();
assert.equal(serviceCalls, 1);
assert.equal(result.flag, 'success');
assert.equal(result.message, 'ok');
assert.equal(result.code, 200);
assert.deepEqual(result.data, [
{ code: 'TB', name: '淘宝平台', default_express_interface: 'QM_WMS', value: 'TB', label: '淘宝平台' },
]);
});
});