WMS_MAIN/test/app/service/systems/delivery_order.addOrder.test.js

252 lines
6.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const assert = require('node:assert/strict');
const DeliveryOrderService = require('../../../../app/service/systems/delivery_order');
/**
* 创建 addOrder 测试桩。
*
* @return {Object} 测试上下文、服务实例与调用记录。
*/
function createHarness() {
const sequence = [];
const operationLogCalls = [];
const cacheStore = new Map();
const captures = {};
const transaction = {
commitCalls: 0,
rollbackCalls: 0,
async commit() {
this.commitCalls += 1;
sequence.push('transaction.commit');
},
async rollback() {
this.rollbackCalls += 1;
sequence.push('transaction.rollback');
},
};
const deliveryModel = {
async findOne() {
sequence.push('delivery.findOne');
return null;
},
async create(orderData) {
sequence.push('delivery.create');
captures.deliveryCreateData = { ...orderData };
return {
id: 7788,
...orderData,
};
},
async update(values) {
sequence.push('delivery.update');
return [ 1, [ values ]];
},
};
const appLogger = {
debug() {},
info() {},
warn() {},
error() {},
};
const app = {
logger: appLogger,
getCacheAsync: async cacheKey => cacheStore.get(cacheKey),
setCacheAsync: async (cacheKey, value) => {
cacheStore.set(cacheKey, value);
},
delCacheAsync: async cacheKey => {
cacheStore.delete(cacheKey);
},
};
const ctx = {
app,
logger: appLogger,
user: {
employees_id: 10001,
employees_name: '测试员',
},
model: {
async transaction() {
return transaction;
},
OrderDeliveryFactory: {
async getCurrentModel() {
return deliveryModel;
},
},
Store: {
async findByPk() {
return {
id: 1000000,
subject_config: {
default_platform_code: 'TB',
default_express_code: 'STO',
},
};
},
},
StoreWarehouse: {
async findOne() {
return {
id: 77,
};
},
},
},
service: {
systems: {
platform: {
async getPlatformList() {
return [
{ code: 'TB', name: '淘宝' },
];
},
},
express: {
async getExpressList() {
return [
{ code: 'STO', name: '申通' },
];
},
},
storeWarehouse: {
async getEffectivePolicyConfigByContractId() {
return {
need_order_approval: 0,
};
},
},
operationLog: {
async createOperationLogs(paramsList = []) {
for (const params of paramsList) {
operationLogCalls.push(params);
sequence.push(`operationLog:${params.operationCode}`);
}
return paramsList.map((params, index) => ({
id: index + 1,
...params,
}));
},
async createOperationLog(params) {
operationLogCalls.push(params);
sequence.push(`operationLog:${params.operationCode}`);
return {
id: operationLogCalls.length,
};
},
},
},
},
};
app.model = ctx.model;
const service = new DeliveryOrderService(ctx);
service.addOrderLines = async () => ({
items_str: 'sku-1',
stock_count: 1,
item_total_count: 1,
total_weight: 2,
});
service.addOrderToQueue = async () => {
sequence.push('order.queue');
};
return {
app,
ctx,
service,
transaction,
sequence,
operationLogCalls,
captures,
};
}
describe('test/app/service/systems/delivery_order.addOrder.test.js', () => {
it('should create order with default platform and express fallback logs after commit', async () => {
const { service, sequence, operationLogCalls, transaction, captures } = createHarness();
const result = await service.addOrder({
request: {
deliveryOrder: {
deliveryOrderCode: 'ORDER-001',
sourcePlatformCode: 'INVALID_PLATFORM',
logisticsCode: 'INVALID_EXPRESS',
receiverInfo: {
name: '测试收件人',
},
},
orderLines: {
orderLine: [
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
],
},
},
}, {
storeId: 1000000,
warehouseId: 100000,
appKey: 'APP-KEY',
});
assert.equal(result.success, true);
assert.equal(result.data.orderId, 7788);
assert.equal(transaction.commitCalls, 1);
assert.equal(transaction.rollbackCalls, 0);
assert.ok(sequence.indexOf('delivery.create') < sequence.indexOf('transaction.commit'));
assert.ok(sequence.indexOf('transaction.commit') < sequence.indexOf('operationLog:ORDER_CREATE'));
assert.ok(sequence.indexOf('operationLog:ORDER_CREATE') < sequence.indexOf('order.queue'));
assert.equal(operationLogCalls.length, 3);
const createLog = operationLogCalls.find(item => item.operationCode === 'ORDER_CREATE');
const platformFallbackLog = operationLogCalls.find(item => item.operationCode === 'ORDER_PLATFORM_FALLBACK');
const expressFallbackLog = operationLogCalls.find(item => item.operationCode === 'ORDER_EXPRESS_FALLBACK');
assert.ok(createLog);
assert.ok(platformFallbackLog);
assert.ok(expressFallbackLog);
assert.equal(createLog.businessType, 'order');
assert.equal(createLog.businessId, 7788);
assert.equal(createLog.extras.message, '订单创建成功,orderId:7788');
assert.equal(captures.deliveryCreateData.create_channel, 'MANUAL');
assert.equal(platformFallbackLog.businessId, 7788);
assert.equal(platformFallbackLog.extras.message, '平台编码INVALID_PLATFORM匹配不到平台,已转为默认平台:淘宝');
assert.equal(expressFallbackLog.businessId, 7788);
assert.equal(expressFallbackLog.extras.message, '快递编码INVALID_EXPRESS匹配不到快递,已转为默认快递:申通');
});
it('should persist explicit create channel from qimen caller', async () => {
const { service, captures } = createHarness();
const result = await service.addOrder({
request: {
deliveryOrder: {
deliveryOrderCode: 'ORDER-002',
sourcePlatformCode: 'TAOBAO',
logisticsCode: 'STO',
receiverInfo: {
name: '测试收件人',
},
},
orderLines: {
orderLine: [
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
],
},
},
}, {
storeId: 1000000,
warehouseId: 100000,
appKey: 'APP-KEY',
}, 'QM');
assert.equal(result.success, true);
assert.equal(captures.deliveryCreateData.create_channel, 'QM');
});
});