251 lines
8.8 KiB
JavaScript
251 lines
8.8 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const { app, mm } = require('egg-mock/bootstrap');
|
||
|
||
describe('test/app/service/systems/delivery_order.auditStatus.test.js', () => {
|
||
let ctx;
|
||
let deliveryOrderService;
|
||
let cacheStore;
|
||
|
||
/**
|
||
* Mock StoreWarehouse.findOne,兼容固定返回值或函数实现。
|
||
*
|
||
* @param {Function|any} implOrValue mock 实现或返回值。
|
||
*/
|
||
function mockStoreWarehouseFindOne(implOrValue) {
|
||
const findOneImpl = typeof implOrValue === 'function'
|
||
? implOrValue
|
||
: async () => implOrValue;
|
||
mm(ctx, 'model', {
|
||
StoreWarehouse: {
|
||
findOne: findOneImpl,
|
||
},
|
||
});
|
||
}
|
||
|
||
beforeEach(() => {
|
||
ctx = app.mockContext();
|
||
deliveryOrderService = ctx.service.systems.deliveryOrder;
|
||
cacheStore = new Map();
|
||
|
||
mm(app, 'getCacheAsync', async cacheKey => cacheStore.get(cacheKey));
|
||
mm(app, 'setCacheAsync', async (cacheKey, value) => {
|
||
cacheStore.set(cacheKey, value);
|
||
});
|
||
});
|
||
|
||
afterEach(() => {
|
||
mm.restore();
|
||
});
|
||
|
||
it('should resolveOrderStep return 0 when need_order_approval is 1', async () => {
|
||
const info = { storeId: 1000000, warehouseId: 100000 };
|
||
mockStoreWarehouseFindOne(async () => ({ id: 1 }));
|
||
mm(ctx.service.systems.storeWarehouse, 'getEffectivePolicyConfigByContractId', async () => ({ need_order_approval: 1 }));
|
||
|
||
const step = await deliveryOrderService.resolveOrderStep(info, null);
|
||
assert.equal(step, 0);
|
||
});
|
||
|
||
it('should resolveOrderStep return 1 when need_order_approval is 0', async () => {
|
||
const info = { storeId: 1000000, warehouseId: 100000 };
|
||
mockStoreWarehouseFindOne(async () => ({ id: 1 }));
|
||
mm(ctx.service.systems.storeWarehouse, 'getEffectivePolicyConfigByContractId', async () => ({ need_order_approval: 0 }));
|
||
|
||
const step = await deliveryOrderService.resolveOrderStep(info, null);
|
||
assert.equal(step, 1);
|
||
});
|
||
|
||
it('should resolveOrderStep return 0 when relation not found', async () => {
|
||
const info = { storeId: 1, warehouseId: 2 };
|
||
mockStoreWarehouseFindOne(async () => null);
|
||
|
||
const step = await deliveryOrderService.resolveOrderStep(info, null);
|
||
assert.equal(step, 0);
|
||
});
|
||
|
||
it('should resolveOrderStep return 1 when policy_config is missing', async () => {
|
||
const info = { storeId: 1000000, warehouseId: 100000 };
|
||
mockStoreWarehouseFindOne(async () => ({ id: 1 }));
|
||
mm(ctx.service.systems.storeWarehouse, 'getEffectivePolicyConfigByContractId', async () => null);
|
||
|
||
const step = await deliveryOrderService.resolveOrderStep(info, null);
|
||
assert.equal(step, 1);
|
||
});
|
||
|
||
it('should prepareOrderData include step', async () => {
|
||
const deliveryOrder = {
|
||
deliveryOrderCode: 'TEST_ORDER_001',
|
||
sourcePlatformCode: 'TAOBAO',
|
||
receiverInfo: { name: 'test' },
|
||
};
|
||
const info = {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
};
|
||
|
||
mm(ctx.service.systems.platform, 'getPlatformList', async () => ([
|
||
{ code: 'TAOBAO', name: '淘宝' },
|
||
{ code: 'TB', name: '淘宝默认' },
|
||
]));
|
||
mm(ctx.service.systems.express, 'getExpressList', async () => ([
|
||
{ code: 'STO', name: '申通' },
|
||
]));
|
||
mm(ctx, 'model', {
|
||
...(ctx.model || {}),
|
||
Store: {
|
||
async findByPk() {
|
||
return {
|
||
id: 1000000,
|
||
subject_config: {
|
||
default_platform_code: 'TB',
|
||
default_express_code: 'STO',
|
||
},
|
||
};
|
||
},
|
||
},
|
||
});
|
||
|
||
// 1. Test with step = 0
|
||
const preparedDataPending = await deliveryOrderService.prepareOrderData(deliveryOrder, info, 0);
|
||
assert.ok(preparedDataPending, 'preparedDataPending should not be null');
|
||
assert.equal(preparedDataPending.step, 0, 'step should be 0');
|
||
|
||
// 2. Test with step = 1
|
||
const preparedDataApproved = await deliveryOrderService.prepareOrderData(deliveryOrder, info, 1);
|
||
assert.ok(preparedDataApproved, 'preparedDataApproved should not be null');
|
||
assert.equal(preparedDataApproved.step, 1, 'step should be 1');
|
||
assert.equal(preparedDataApproved.source_platform_code, 'TAOBAO');
|
||
assert.equal(preparedDataApproved.sys_platform_code, 'TAOBAO');
|
||
});
|
||
|
||
it('should fallback invalid platform and logistics codes to cached store defaults without overwriting source fields', async () => {
|
||
const deliveryOrder = {
|
||
deliveryOrderCode: 'TEST_ORDER_002',
|
||
sourcePlatformCode: 'INVALID_PLATFORM',
|
||
sourcePlatformName: '外部平台',
|
||
logisticsCode: 'INVALID_EXPRESS',
|
||
logisticsName: '外部快递',
|
||
expressCode: 'YT',
|
||
receiverInfo: { name: 'test' },
|
||
};
|
||
const info = {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
};
|
||
|
||
let findByPkCallCount = 0;
|
||
mm(ctx, 'model', {
|
||
...(ctx.model || {}),
|
||
Store: {
|
||
async findByPk() {
|
||
findByPkCallCount += 1;
|
||
return {
|
||
id: 1000000,
|
||
subject_config: {
|
||
default_platform_code: 'TB',
|
||
default_express_code: 'STO',
|
||
},
|
||
};
|
||
},
|
||
},
|
||
});
|
||
mm(ctx.service.systems.platform, 'getPlatformList', async () => ([
|
||
{ code: 'TB', name: '淘宝' },
|
||
{ code: 'JD', name: '京东' },
|
||
]));
|
||
mm(ctx.service.systems.express, 'getExpressList', async () => ([
|
||
{ code: 'STO', name: '申通' },
|
||
{ code: 'YTO', name: '圆通' },
|
||
]));
|
||
|
||
const firstPrepared = await deliveryOrderService.prepareOrderData(deliveryOrder, info, 1);
|
||
const secondPrepared = await deliveryOrderService.prepareOrderData(deliveryOrder, info, 1);
|
||
|
||
assert.equal(firstPrepared.source_platform_code, 'INVALID_PLATFORM');
|
||
assert.equal(firstPrepared.source_platform_name, '外部平台');
|
||
assert.equal(firstPrepared.sys_platform_code, 'TB');
|
||
assert.equal(firstPrepared.sys_platform_name, '淘宝');
|
||
assert.equal(firstPrepared.logistics_code, 'INVALID_EXPRESS');
|
||
assert.equal(firstPrepared.logistics_name, '外部快递');
|
||
assert.equal(firstPrepared.sys_logistics_code, 'STO');
|
||
assert.equal(firstPrepared.sys_logistics_name, '申通');
|
||
assert.equal(firstPrepared.express_code, 'YT');
|
||
|
||
assert.equal(secondPrepared.sys_platform_code, 'TB');
|
||
assert.equal(secondPrepared.sys_logistics_code, 'STO');
|
||
assert.equal(findByPkCallCount, 1, 'store subject_config should be loaded from cache on second call');
|
||
});
|
||
|
||
it('should resolve platform setting code to the real system platform code and name', async () => {
|
||
const deliveryOrder = {
|
||
deliveryOrderCode: 'TEST_ORDER_003',
|
||
sourcePlatformCode: 'TB-SETTING',
|
||
sourcePlatformName: '外部平台设置码',
|
||
receiverInfo: { name: 'test' },
|
||
};
|
||
const info = {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
};
|
||
|
||
mm(ctx, 'model', {
|
||
...(ctx.model || {}),
|
||
Store: {
|
||
async findByPk() {
|
||
return {
|
||
id: 1000000,
|
||
subject_config: {
|
||
default_platform_code: 'JD',
|
||
default_express_code: 'STO',
|
||
},
|
||
};
|
||
},
|
||
},
|
||
});
|
||
mm(ctx.service.systems.platform, 'getPlatformList', async () => ([
|
||
{ code: 'TB', name: '淘宝平台', value: 'TB', label: '淘宝平台', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: false },
|
||
{ code: 'TB-SETTING', name: 'TB-SETTING (淘宝平台)', value: 'TB-SETTING', label: 'TB-SETTING (淘宝平台)', platform_code: 'TB', platform_name: '淘宝平台', is_system_setting: true },
|
||
{ code: 'JD', name: '京东平台', value: 'JD', label: '京东平台', platform_code: 'JD', platform_name: '京东平台', is_system_setting: false },
|
||
]));
|
||
mm(ctx.service.systems.express, 'getExpressList', async () => ([
|
||
{ code: 'STO', name: '申通' },
|
||
]));
|
||
|
||
const snapshot = await deliveryOrderService.resolvePlatformSnapshot('TB-SETTING', {
|
||
default_platform_code: 'JD',
|
||
default_express_code: 'STO',
|
||
}, {
|
||
warehouseId: info.warehouseId,
|
||
deliveryOrderCode: deliveryOrder.deliveryOrderCode,
|
||
storeId: info.storeId,
|
||
});
|
||
|
||
assert.deepEqual(snapshot, {
|
||
sysPlatformCode: 'TB',
|
||
sysPlatformName: '淘宝平台',
|
||
});
|
||
});
|
||
|
||
it('should resolve merged express settings to the real express code', async () => {
|
||
mm(ctx.service.systems.express, 'getExpressList', async () => ([
|
||
{ code: 'STO', name: '顺丰', value: 'STO', label: '顺丰' },
|
||
{ code: 'SF-SETTING', name: 'SF-SETTING (顺丰)', value: 'SF-SETTING', label: 'SF-SETTING (顺丰)', express_code: 'STO', warehouse_id: 100000 },
|
||
]));
|
||
|
||
const snapshot = await deliveryOrderService.resolveExpressSnapshot('SF-SETTING', {
|
||
default_express_code: 'TB',
|
||
}, {
|
||
warehouseId: 100000,
|
||
deliveryOrderCode: 'TEST_ORDER_003',
|
||
storeId: 1000000,
|
||
});
|
||
|
||
assert.deepEqual(snapshot, {
|
||
sysLogisticsCode: 'STO',
|
||
sysLogisticsName: 'SF-SETTING (顺丰)',
|
||
});
|
||
});
|
||
});
|