413 lines
12 KiB
JavaScript
413 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
const PendingOrderService = require('../../../../../app/service/systemMethods/order/pendingOrder');
|
|
|
|
/**
|
|
* 创建待处理订单储位分配系统方法测试上下文。
|
|
*
|
|
* @param {Object} options 可选覆盖项。
|
|
* @param {Function} options.allocateStorageImpl 自定义储位分配实现。
|
|
* @return {{ ctx: Object, capture: Object, cacheStore: Map<string, Object>, service: PendingOrderService }} 测试上下文。
|
|
*/
|
|
function createHarness(options = {}) {
|
|
const capture = {
|
|
request: null,
|
|
};
|
|
const cacheStore = new Map();
|
|
const allocateStorageImpl = typeof options.allocateStorageImpl === 'function'
|
|
? options.allocateStorageImpl
|
|
: null;
|
|
|
|
const ctx = {
|
|
app: {
|
|
async getCacheAsync(cacheKey) {
|
|
return cacheStore.get(cacheKey);
|
|
},
|
|
async setCacheAsync(cacheKey, value) {
|
|
cacheStore.set(cacheKey, value);
|
|
},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
service: {
|
|
systems: {
|
|
orderStorageAllocation: {
|
|
async allocateStorage(request) {
|
|
capture.request = request;
|
|
if (allocateStorageImpl) {
|
|
return await allocateStorageImpl(request, capture);
|
|
}
|
|
return {
|
|
code: 200,
|
|
message: '储位分配完成',
|
|
data: {
|
|
ok: true,
|
|
},
|
|
};
|
|
},
|
|
async cancelAllocateStorage(request) {
|
|
capture.cancelRequest = request;
|
|
return {
|
|
code: 200,
|
|
message: '取消分配完成',
|
|
data: {
|
|
ok: true,
|
|
},
|
|
};
|
|
},
|
|
async syncOrderIds(request) {
|
|
capture.syncRequest = request;
|
|
return {
|
|
code: 200,
|
|
message: '同步已受理,当前仅透传 order_ids',
|
|
data: {
|
|
ok: true,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
capture,
|
|
cacheStore,
|
|
service: new PendingOrderService(ctx),
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/order/pendingOrder.allocateStorage.test.js', () => {
|
|
it('should return request_id immediately and delegate normalized input asynchronously', async () => {
|
|
const { capture, service } = createHarness();
|
|
|
|
const result = await service.allocateStorage({
|
|
allowStorageLocation: true,
|
|
ids: '1,2,3',
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.warehouse_id, 100000);
|
|
assert.equal(result.message, '请求已提交,后端处理中');
|
|
assert.match(result.request_id, /^storage_alloc_100000_\d+_[a-z0-9]+$/);
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
assert.equal(capture.request.allow_storage_location, true);
|
|
assert.equal(capture.request.order_ids, '1,2,3');
|
|
assert.equal(typeof capture.request.progress_callback, 'function');
|
|
});
|
|
|
|
it('should query allocate storage progress by request_id after async task success', async () => {
|
|
const { service } = createHarness();
|
|
|
|
const submitResult = await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1, 2, 3 ],
|
|
});
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
const progressResult = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(progressResult.flag, 'success');
|
|
assert.equal(progressResult.message, 'ok');
|
|
assert.equal(progressResult.request_id, submitResult.request_id);
|
|
assert.equal(progressResult.status, 'success');
|
|
assert.equal(progressResult.progress, 100);
|
|
assert.equal(progressResult.total_count, 0);
|
|
assert.equal(progressResult.processed_count, 0);
|
|
assert.deepEqual(progressResult.result, {
|
|
code: 200,
|
|
message: '储位分配完成',
|
|
data: {
|
|
ok: true,
|
|
},
|
|
});
|
|
assert.equal(typeof progressResult.update_time, 'number');
|
|
});
|
|
|
|
it('should return not_found when request_id does not exist in cache', async () => {
|
|
const { service } = createHarness();
|
|
|
|
const result = await service.allocateStorageProgress({
|
|
requestId: 'not_exist_request_id',
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.request_id, 'not_exist_request_id');
|
|
assert.equal(result.status, 'not_found');
|
|
assert.equal(result.progress, 0);
|
|
});
|
|
|
|
it('should backfill progress from status for legacy cache payload without progress field', async () => {
|
|
const { service, cacheStore } = createHarness();
|
|
|
|
const requestId = 'legacy_request_id';
|
|
cacheStore.set(service.getAllocateStorageTaskCacheKey(requestId), {
|
|
request_id: requestId,
|
|
status: 'running',
|
|
update_time: Date.now(),
|
|
});
|
|
|
|
const result = await service.allocateStorageProgress({
|
|
request_id: requestId,
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, 'ok');
|
|
assert.equal(result.request_id, requestId);
|
|
assert.equal(result.status, 'running');
|
|
assert.equal(result.progress, 50);
|
|
assert.equal(result.total_count, 0);
|
|
assert.equal(result.processed_count, 0);
|
|
});
|
|
|
|
it('should report increasing progress and counters while allocation is running', async () => {
|
|
let resolveFirstProgress;
|
|
const firstProgressReady = new Promise(resolve => {
|
|
resolveFirstProgress = resolve;
|
|
});
|
|
let resolveSecondProgress;
|
|
const secondProgressGate = new Promise(resolve => {
|
|
resolveSecondProgress = resolve;
|
|
});
|
|
let resolveAllocation;
|
|
const allocationDone = new Promise(resolve => {
|
|
resolveAllocation = resolve;
|
|
});
|
|
|
|
const { service } = createHarness({
|
|
allocateStorageImpl: async request => {
|
|
await request.progress_callback({
|
|
total_count: 4,
|
|
processed_count: 1,
|
|
});
|
|
resolveFirstProgress();
|
|
await secondProgressGate;
|
|
await request.progress_callback({
|
|
total_count: 4,
|
|
processed_count: 2,
|
|
});
|
|
await allocationDone;
|
|
await request.progress_callback({
|
|
total_count: 4,
|
|
processed_count: 4,
|
|
});
|
|
return {
|
|
code: 200,
|
|
message: '储位分配完成',
|
|
data: {
|
|
ok: true,
|
|
},
|
|
};
|
|
},
|
|
});
|
|
|
|
const submitResult = await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1, 2, 3, 4 ],
|
|
});
|
|
|
|
await firstProgressReady;
|
|
|
|
const firstRunningProgress = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(firstRunningProgress.flag, 'success');
|
|
assert.equal(firstRunningProgress.status, 'running');
|
|
assert.equal(firstRunningProgress.total_count, 4);
|
|
assert.equal(firstRunningProgress.processed_count, 1);
|
|
assert.equal(firstRunningProgress.progress, 25);
|
|
|
|
resolveSecondProgress();
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
const runningProgress = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(runningProgress.flag, 'success');
|
|
assert.equal(runningProgress.status, 'running');
|
|
assert.equal(runningProgress.total_count, 4);
|
|
assert.equal(runningProgress.processed_count, 2);
|
|
assert.equal(runningProgress.progress, 50);
|
|
|
|
resolveAllocation();
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
const successProgress = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(successProgress.flag, 'success');
|
|
assert.equal(successProgress.status, 'success');
|
|
assert.equal(successProgress.total_count, 4);
|
|
assert.equal(successProgress.processed_count, 4);
|
|
assert.equal(successProgress.progress, 100);
|
|
});
|
|
|
|
it('should return failure when request_id is empty', async () => {
|
|
const { service } = createHarness();
|
|
|
|
const result = await service.allocateStorageProgress({
|
|
request_id: ' ',
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.message, 'request_id 参数不能为空');
|
|
assert.equal(result.code, 400);
|
|
});
|
|
|
|
it('should keep explainable progress when allocation fails after partial processing', async () => {
|
|
const { service } = createHarness({
|
|
allocateStorageImpl: async request => {
|
|
await request.progress_callback({
|
|
total_count: 4,
|
|
processed_count: 1,
|
|
});
|
|
throw new Error('模拟分配失败');
|
|
},
|
|
});
|
|
|
|
const submitResult = await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1, 2, 3, 4 ],
|
|
});
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
const failedProgress = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(failedProgress.flag, 'success');
|
|
assert.equal(failedProgress.status, 'failed');
|
|
assert.equal(failedProgress.total_count, 4);
|
|
assert.equal(failedProgress.processed_count, 1);
|
|
assert.equal(failedProgress.progress, 25);
|
|
assert.equal(failedProgress.error, '模拟分配失败');
|
|
});
|
|
|
|
it('should keep explainable progress when allocation is canceled', async () => {
|
|
const { service } = createHarness({
|
|
allocateStorageImpl: async request => {
|
|
await request.progress_callback({
|
|
total_count: 5,
|
|
processed_count: 2,
|
|
});
|
|
throw new Error('任务已取消');
|
|
},
|
|
});
|
|
|
|
const submitResult = await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1, 2, 3, 4, 5 ],
|
|
});
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
const canceledProgress = await service.allocateStorageProgress({
|
|
request_id: submitResult.request_id,
|
|
});
|
|
|
|
assert.equal(canceledProgress.flag, 'success');
|
|
assert.equal(canceledProgress.status, 'failed');
|
|
assert.equal(canceledProgress.total_count, 5);
|
|
assert.equal(canceledProgress.processed_count, 2);
|
|
assert.equal(canceledProgress.progress, 40);
|
|
assert.equal(canceledProgress.error, '任务已取消');
|
|
});
|
|
|
|
it('should forward only order IDs when canceling storage allocation', async () => {
|
|
const { capture, service } = createHarness();
|
|
|
|
const result = await service.cancelAllocateStorage({
|
|
orderId: 7,
|
|
});
|
|
|
|
assert.deepEqual(capture.cancelRequest, {
|
|
order_ids: 7,
|
|
});
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, '取消分配完成');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, {
|
|
ok: true,
|
|
});
|
|
});
|
|
|
|
it('should forward only order ID aliases for cancel allocation', async () => {
|
|
const { capture, service } = createHarness();
|
|
|
|
await service.cancelAllocateStorage({
|
|
orderIds: [ 8, 9 ],
|
|
});
|
|
|
|
assert.deepEqual(capture.cancelRequest, {
|
|
order_ids: [ 8, 9 ],
|
|
});
|
|
});
|
|
|
|
it('should forward order_ids to the sync service without extra processing', async () => {
|
|
const { capture, service } = createHarness();
|
|
|
|
const result = await service.syncOrderIds({
|
|
order_ids: [ 11, 12 ],
|
|
});
|
|
|
|
assert.deepEqual(capture.syncRequest, {
|
|
order_ids: [ 11, 12 ],
|
|
});
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.message, '同步已受理,当前仅透传 order_ids');
|
|
assert.equal(result.code, 200);
|
|
assert.deepEqual(result.data, {
|
|
ok: true,
|
|
});
|
|
});
|
|
|
|
it('should reject warehouse_id and warehouseId on the pending order entry', async () => {
|
|
const { capture, service } = createHarness();
|
|
|
|
await assert.rejects(
|
|
async () => await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1 ],
|
|
warehouse_id: 200000,
|
|
}),
|
|
/不再接受 warehouse_id \/ warehouseId/
|
|
);
|
|
|
|
await assert.rejects(
|
|
async () => await service.allocateStorage({
|
|
allow_storage_location: true,
|
|
order_ids: [ 1 ],
|
|
warehouseId: 200000,
|
|
}),
|
|
/不再接受 warehouse_id \/ warehouseId/
|
|
);
|
|
|
|
assert.equal(capture.request, null);
|
|
});
|
|
|
|
it('should not expose the legacy order.storageAllocation system method file anymore', () => {
|
|
const legacyModule = require('../../../../../app/service/systemMethods/order/storageAllocation');
|
|
|
|
assert.equal(legacyModule, undefined);
|
|
});
|
|
});
|