448 lines
12 KiB
JavaScript
448 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { Op } = require('sequelize');
|
|
|
|
const WaveService = require('../../../../../app/service/systemMethods/order/wave');
|
|
|
|
/**
|
|
* 构建波次抓取测试上下文。
|
|
*
|
|
* @param {boolean} [mockRunFetchTask=true] 是否默认桩化 runFetchTask。
|
|
* @return {{ service: WaveService, cache: Map<string, Object>, runTaskCalls: Array<Object> }} 测试上下文。
|
|
*/
|
|
function createHarness(mockRunFetchTask = true) {
|
|
const cache = new Map();
|
|
const runTaskCalls = [];
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
async setCacheAsync(key, payload) {
|
|
cache.set(key, payload);
|
|
},
|
|
async getCacheAsync(key) {
|
|
return cache.get(key) || null;
|
|
},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.getDefaultWaveQuantity = async () => 50;
|
|
if (mockRunFetchTask) {
|
|
service.runFetchTask = async params => {
|
|
runTaskCalls.push(params);
|
|
};
|
|
}
|
|
|
|
return {
|
|
service,
|
|
cache,
|
|
runTaskCalls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/order/wave.fetch.test.js', () => {
|
|
it('groupFetch 应正常提交异步任务并返回 task_id', async () => {
|
|
const { service } = createHarness();
|
|
const token = service.buildMachineStructureToken({
|
|
item_part_type: 'items_hash',
|
|
item_part: 'hash-group-a',
|
|
distinguish_express: false,
|
|
logistics_name: '',
|
|
item_snapshot_key: '',
|
|
});
|
|
|
|
const result = await service.groupFetch({
|
|
structure_list: [
|
|
{
|
|
item_detail_machine: token,
|
|
quantity: 5,
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, '任务已提交,后端异步处理中');
|
|
assert.ok(typeof result.task_id === 'string' && result.task_id.length > 0);
|
|
assert.equal(result.fetch_mode, 'group');
|
|
});
|
|
|
|
it('fetch 兼容别名应转调 groupFetch', async () => {
|
|
const { service } = createHarness();
|
|
const expected = {
|
|
flag: 'success',
|
|
message: 'ok',
|
|
task_id: 'alias-task-id',
|
|
};
|
|
|
|
let called = false;
|
|
service.groupFetch = async data => {
|
|
called = true;
|
|
assert.deepEqual(data, { a: 1 });
|
|
return expected;
|
|
};
|
|
|
|
const result = await service.fetch({ a: 1 });
|
|
|
|
assert.equal(called, true);
|
|
assert.deepEqual(result, expected);
|
|
});
|
|
|
|
it('singleFetch 缺失 max_fetch_quantity 时应报错', async () => {
|
|
const { service } = createHarness();
|
|
const token = service.buildMachineStructureToken({
|
|
item_part_type: 'items_hash',
|
|
item_part: 'hash-single-a',
|
|
distinguish_express: false,
|
|
logistics_name: '',
|
|
item_snapshot_key: '',
|
|
});
|
|
|
|
await assert.rejects(
|
|
() => service.singleFetch({
|
|
structure_list: [
|
|
{
|
|
item_detail_machine: token,
|
|
quantity: 3,
|
|
},
|
|
],
|
|
}),
|
|
/max_fetch_quantity 参数不能为空/
|
|
);
|
|
});
|
|
|
|
it('single 模式 runFetchTask 达到 max_fetch_quantity 后应停止', async () => {
|
|
const { service } = createHarness(false);
|
|
service.getGroupWavePrefix = async () => 'B';
|
|
service.getMinGroupOrderFetchQuantity = async () => 13;
|
|
service.getOrderModel = async () => ({
|
|
async findAll() {
|
|
return [];
|
|
},
|
|
async update() {
|
|
return [ 0 ];
|
|
},
|
|
});
|
|
|
|
const progressList = [];
|
|
service.setFetchTaskProgress = async (_taskId, payload) => {
|
|
progressList.push(payload);
|
|
};
|
|
|
|
const minGroupList = [];
|
|
const maxQuantityList = [];
|
|
service.fetchSingleStructure = async (
|
|
_model,
|
|
structure,
|
|
_warehouseId,
|
|
_wavePrefix,
|
|
minGroupOrderFetchQuantity,
|
|
options = {}
|
|
) => {
|
|
minGroupList.push(minGroupOrderFetchQuantity);
|
|
maxQuantityList.push(Number(options.maxFetchQuantity || 0));
|
|
|
|
const requestQuantity = Number(structure.quantity || 0);
|
|
const fetchLimit = Number(options.maxFetchQuantity || 0);
|
|
const fetchedQuantity = Math.min(requestQuantity, fetchLimit);
|
|
|
|
return {
|
|
item_detail_machine: structure.item_detail_machine,
|
|
request_quantity: requestQuantity,
|
|
fetched_quantity: fetchedQuantity,
|
|
wave_ids: [],
|
|
order_ids: [],
|
|
};
|
|
};
|
|
|
|
await service.runFetchTask({
|
|
taskId: 'wave_fetch_100000_123456',
|
|
warehouseId: 100000,
|
|
defaultQuantity: 50,
|
|
fetchMode: 'single',
|
|
maxFetchQuantity: 5,
|
|
structures: [
|
|
{ item_detail_machine: 's-1', quantity: 4 },
|
|
{ item_detail_machine: 's-2', quantity: 4 },
|
|
{ item_detail_machine: 's-3', quantity: 4 },
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(minGroupList, [ 1, 1 ]);
|
|
assert.deepEqual(maxQuantityList, [ 5, 1 ]);
|
|
|
|
const finalProgress = progressList[progressList.length - 1] || {};
|
|
assert.equal(finalProgress.status, 'success');
|
|
assert.equal(finalProgress.fetch_mode, 'single');
|
|
assert.equal(finalProgress.max_fetch_quantity, 5);
|
|
assert.equal(finalProgress.total_fetched_quantity, 5);
|
|
assert.equal(finalProgress.finished_structure_count, 2);
|
|
assert.equal(finalProgress.limit_reached, true);
|
|
assert.equal(finalProgress.stop_reason, '达到最大抓取上限');
|
|
});
|
|
|
|
it('fetchSingleStructure 应使用 wave_batch 自增 wave_id 更新订单', async () => {
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {},
|
|
async rollback() {},
|
|
};
|
|
|
|
const sqlCalls = [];
|
|
const orders = [
|
|
{ id: 101, wave_id: 0 },
|
|
{ id: 102, wave_id: 0 },
|
|
];
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
model: {
|
|
QueryTypes: {
|
|
SELECT: 'SELECT',
|
|
},
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
async query(sql, options = {}) {
|
|
sqlCalls.push({ sql, options });
|
|
if (sql.includes('INSERT INTO wms_wave_batch')) {
|
|
return [ { insertId: 900001 }, { insertId: 900001 } ];
|
|
}
|
|
|
|
if (sql.includes('SELECT wave_no')) {
|
|
return [];
|
|
}
|
|
|
|
if (sql.includes('UPDATE wms_wave_batch')) {
|
|
return [ { affectedRows: 1 } ];
|
|
}
|
|
|
|
if (sql.includes('DELETE FROM wms_wave_batch')) {
|
|
return [ { affectedRows: 1 } ];
|
|
}
|
|
|
|
return [ [] ];
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.app.Sequelize = {
|
|
literal(value) {
|
|
return value;
|
|
},
|
|
};
|
|
service.batchCreateOrderOperationLogs = async () => 0;
|
|
service.getCurrentDatePart = () => '20260506';
|
|
service.getNextWaveNoSequence = async () => 1;
|
|
|
|
const model = {
|
|
async findAll(options = {}) {
|
|
const where = options.where || {};
|
|
if (where.wave_id === 0) {
|
|
return orders
|
|
.filter(item => item.wave_id === 0)
|
|
.map(item => ({ id: item.id }));
|
|
}
|
|
|
|
if (where.wave_id === 900001) {
|
|
return orders
|
|
.filter(item => item.wave_id === 900001)
|
|
.map(item => ({ id: item.id }));
|
|
}
|
|
|
|
return [];
|
|
},
|
|
async update(values = {}, options = {}) {
|
|
const where = options.where || {};
|
|
const targetIds = (where.id && where.id[Op.in]) || [];
|
|
for (const item of orders) {
|
|
if (targetIds.includes(item.id) && item.wave_id === 0) {
|
|
item.wave_id = Number(values.wave_id || 0);
|
|
item.wave_no = String(values.wave_no || '');
|
|
}
|
|
}
|
|
return [ targetIds.length ];
|
|
},
|
|
};
|
|
|
|
const result = await service.fetchSingleStructure(
|
|
model,
|
|
{
|
|
item_detail_machine: 'token-a',
|
|
quantity: 2,
|
|
item_part_type: 'items_hash',
|
|
item_part: 'hash-a',
|
|
distinguish_express: false,
|
|
logistics_name: '',
|
|
item_snapshot_key: '',
|
|
},
|
|
100000,
|
|
'B',
|
|
1,
|
|
{
|
|
maxFetchQuantity: 2,
|
|
}
|
|
);
|
|
|
|
assert.equal(result.fetched_quantity, 2);
|
|
assert.deepEqual(result.wave_ids, [ 900001 ]);
|
|
assert.equal(orders[0].wave_id, 900001);
|
|
assert.equal(orders[1].wave_id, 900001);
|
|
|
|
const insertCall = sqlCalls.find(call => call.sql.includes('INSERT INTO wms_wave_batch'));
|
|
assert.ok(insertCall);
|
|
assert.equal(insertCall.options.replacements.waveId, undefined);
|
|
});
|
|
|
|
it('fetchSingleStructure 在 INSERT 未返回 insertId 时应回查 LAST_INSERT_ID 并成功更新订单', async () => {
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
async commit() {},
|
|
async rollback() {},
|
|
};
|
|
|
|
const sqlCalls = [];
|
|
const orders = [
|
|
{ id: 201, wave_id: 0 },
|
|
{ id: 202, wave_id: 0 },
|
|
];
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
model: {
|
|
QueryTypes: {
|
|
SELECT: 'SELECT',
|
|
},
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
async query(sql, options = {}) {
|
|
sqlCalls.push({ sql, options });
|
|
if (sql.includes('INSERT INTO wms_wave_batch')) {
|
|
return [ {}, {} ];
|
|
}
|
|
|
|
if (sql.includes('SELECT LAST_INSERT_ID() AS insertId')) {
|
|
return [ [ { insertId: 900002 } ], {} ];
|
|
}
|
|
|
|
if (sql.includes('SELECT wave_no')) {
|
|
return [];
|
|
}
|
|
|
|
if (sql.includes('UPDATE wms_wave_batch')) {
|
|
return [ { affectedRows: 1 } ];
|
|
}
|
|
|
|
if (sql.includes('DELETE FROM wms_wave_batch')) {
|
|
return [ { affectedRows: 1 } ];
|
|
}
|
|
|
|
return [ [] ];
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.app.Sequelize = {
|
|
literal(value) {
|
|
return value;
|
|
},
|
|
};
|
|
service.batchCreateOrderOperationLogs = async () => 0;
|
|
service.getCurrentDatePart = () => '20260506';
|
|
service.getNextWaveNoSequence = async () => 2;
|
|
|
|
const model = {
|
|
async findAll(options = {}) {
|
|
const where = options.where || {};
|
|
if (where.wave_id === 0) {
|
|
return orders
|
|
.filter(item => item.wave_id === 0)
|
|
.map(item => ({ id: item.id }));
|
|
}
|
|
|
|
if (where.wave_id === 900002) {
|
|
return orders
|
|
.filter(item => item.wave_id === 900002)
|
|
.map(item => ({ id: item.id }));
|
|
}
|
|
|
|
return [];
|
|
},
|
|
async update(values = {}, options = {}) {
|
|
const where = options.where || {};
|
|
const targetIds = (where.id && where.id[Op.in]) || [];
|
|
for (const item of orders) {
|
|
if (targetIds.includes(item.id) && item.wave_id === 0) {
|
|
item.wave_id = Number(values.wave_id || 0);
|
|
item.wave_no = String(values.wave_no || '');
|
|
}
|
|
}
|
|
return [ targetIds.length ];
|
|
},
|
|
};
|
|
|
|
const result = await service.fetchSingleStructure(
|
|
model,
|
|
{
|
|
item_detail_machine: 'token-b',
|
|
quantity: 2,
|
|
item_part_type: 'items_hash',
|
|
item_part: 'hash-b',
|
|
distinguish_express: false,
|
|
logistics_name: '',
|
|
item_snapshot_key: '',
|
|
},
|
|
100000,
|
|
'B',
|
|
1,
|
|
{
|
|
maxFetchQuantity: 2,
|
|
}
|
|
);
|
|
|
|
assert.equal(result.fetched_quantity, 2);
|
|
assert.deepEqual(result.wave_ids, [ 900002 ]);
|
|
assert.equal(orders[0].wave_id, 900002);
|
|
assert.equal(orders[1].wave_id, 900002);
|
|
|
|
const fallbackCall = sqlCalls.find(call => call.sql.includes('SELECT LAST_INSERT_ID() AS insertId'));
|
|
assert.ok(fallbackCall);
|
|
assert.equal(fallbackCall.options.transaction, transaction);
|
|
});
|
|
});
|