WMS_MAIN/test/app/schedule/print_retry_compensation.test.js
liufumingren 8cf2c5befa 1
2026-05-20 16:31:29 +08:00

256 lines
7.1 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const schedule = require('../../../app/schedule/print_retry_compensation');
describe('test/app/schedule/print_retry_compensation.test.js', () => {
it('should scan retryable records and invoke retryFailed grouped by warehouse', async () => {
assert.equal(schedule.schedule.interval, '30s');
assert.equal(schedule.schedule.type, 'worker');
assert.equal(schedule.schedule.immediate, false);
const Op = {
and: Symbol('and'),
or: Symbol('or'),
in: Symbol('in'),
lte: Symbol('lte'),
lt: Symbol('lt'),
};
const retryCalls = [];
const ctx = {
app: {
_printRetryCompensationRunning: false,
_printRetryCompensationLastRunAt: 0,
config: {
printRetryCompensation: {
enable: true,
intervalMs: 0,
batchSize: 50,
force: false,
},
},
Sequelize: {
Op,
col(fieldName) {
return `col:${fieldName}`;
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
model: {
PrintRecord: {
async findAll(options) {
assert.equal(options.limit, 50);
assert.equal(options.raw, true);
return [
{ id: 101, warehouse_id: 1 },
{ id: 102, warehouse_id: 1 },
{ id: 201, warehouse_id: 2 },
];
},
},
},
service: {
systemMethods: {
print: {
async retryFailed(data) {
retryCalls.push(data);
return {
flag: 'success',
success_count: data.print_ids.length,
failed_count: 0,
skipped_count: 0,
};
},
},
},
},
};
await schedule.task(ctx);
assert.equal(retryCalls.length, 2);
const firstCall = retryCalls[0];
const secondCall = retryCalls[1];
assert.deepEqual(firstCall, {
print_ids: [ 101, 102 ],
warehouse_id: 1,
force: false,
});
assert.deepEqual(secondCall, {
print_ids: [ 201 ],
warehouse_id: 2,
force: false,
});
assert.equal(ctx.app._printRetryCompensationRunning, false);
});
it('should skip when disabled or already running', async () => {
let findAllCount = 0;
const ctx = {
app: {
_printRetryCompensationRunning: false,
_printRetryCompensationLastRunAt: 0,
config: {
printRetryCompensation: {
enable: false,
intervalMs: 60000,
batchSize: 10,
},
},
Sequelize: {
Op: {},
col(fieldName) {
return `col:${fieldName}`;
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
model: {
PrintRecord: {
async findAll() {
findAllCount += 1;
return [];
},
},
},
service: {
systemMethods: {
print: {
async retryFailed() {
throw new Error('should not reach');
},
},
},
},
};
await schedule.task(ctx);
assert.equal(findAllCount, 0);
ctx.app.config.printRetryCompensation.enable = true;
ctx.app._printRetryCompensationRunning = true;
await schedule.task(ctx);
assert.equal(findAllCount, 0);
});
it('should include retry_count < max_retry_count in where clause', async () => {
const Op = {
and: Symbol('and'),
or: Symbol('or'),
in: Symbol('in'),
lte: Symbol('lte'),
lt: Symbol('lt'),
};
let whereClause;
const ctx = {
app: {
_printRetryCompensationRunning: false,
_printRetryCompensationLastRunAt: 0,
config: { printRetryCompensation: { enable: true, intervalMs: 0, batchSize: 50 } },
Sequelize: { Op, col: fieldName => `col:${fieldName}` },
},
logger: { info() {}, warn() {}, error() {} },
model: {
PrintRecord: {
async findAll(options) {
whereClause = options.where;
return [];
},
},
},
service: { systemMethods: { print: { async retryFailed() {} } } },
};
await schedule.task(ctx);
const andConditions = whereClause[Op.and];
assert(Array.isArray(andConditions), 'where[Op.and] should be an array');
const retryCountCondition = andConditions.find(condition => condition && condition.retry_count);
assert(retryCountCondition, 'retry_count condition should exist in where[Op.and]');
assert.deepStrictEqual(retryCountCondition.retry_count, { [Op.lt]: 'col:max_retry_count' });
});
it('should include next_retry_at <= now in where clause', async () => {
const Op = {
and: Symbol('and'),
or: Symbol('or'),
in: Symbol('in'),
lte: Symbol('lte'),
lt: Symbol('lt'),
};
let whereClause;
const ctx = {
app: {
_printRetryCompensationRunning: false,
_printRetryCompensationLastRunAt: 0,
config: { printRetryCompensation: { enable: true, intervalMs: 0, batchSize: 50 } },
Sequelize: { Op, col: () => {} },
},
logger: { info() {}, warn() {}, error() {} },
model: {
PrintRecord: {
async findAll(options) {
whereClause = options.where;
return [];
},
},
},
service: { systemMethods: { print: { async retryFailed() {} } } },
};
await schedule.task(ctx);
const andConditions = whereClause[Op.and];
assert(Array.isArray(andConditions), 'where[Op.and] should be an array');
const nextRetryAtCondition = andConditions.find(condition => condition && condition.next_retry_at);
assert(nextRetryAtCondition, 'next_retry_at condition should exist in where[Op.and]');
assert(nextRetryAtCondition.next_retry_at[Op.lte], 'should use lte operator');
assert(nextRetryAtCondition.next_retry_at[Op.lte] instanceof Date, 'should compare with a Date object');
});
it('should skip invalid records and not trigger retryFailed', async () => {
let retryFailedCalled = false;
const ctx = {
app: {
_printRetryCompensationRunning: false,
_printRetryCompensationLastRunAt: 0,
config: { printRetryCompensation: { enable: true, intervalMs: 0, batchSize: 50 } },
Sequelize: { Op: {}, col: () => {} },
},
logger: { info() {}, warn() {}, error() {} },
model: {
PrintRecord: {
async findAll() {
return [
{ id: 0, warehouse_id: 1 },
{ id: 1, warehouse_id: 0 },
{ id: -1, warehouse_id: 1 },
{ id: 1, warehouse_id: -1 },
];
},
},
},
service: {
systemMethods: {
print: {
async retryFailed() {
retryFailedCalled = true;
},
},
},
},
};
await schedule.task(ctx);
assert.equal(retryFailedCalled, false, 'retryFailed should not be called for invalid records');
});
});