WMS_MAIN/test/app/model/store.test.js
liufumingren a667f1401e feat(inbox): 推送失败自动重发改为仓库级开关与重发时间范围(默认关闭)
仓库设置新增嵌套配置 `subject_config.inbox_push_retry = { enabled, window_minutes }`,
结构与「收货未入库提醒」(receiving_pending_inbound_notice)保持一致:

- `enabled`:默认 false —— 未配置、配置缺失或无法读取时一律**关闭**,需在仓库设置中显式开启
- `window_minutes`:重发时间范围,单位**分钟**,1-1440,默认 30(仅开关开启时生效)

改动:

- app/model/store.js:新增 DEFAULT_INBOX_PUSH_RETRY 与 normalizeInboxPushRetry(非法结构/非法 JSON
  回退默认;enabled 非布尔值、window_minutes 越界抛「subject_config.inbox_push_retry.window_minutes
  必须是 1-1440 的整数(分钟)」),由 normalizeSubjectConfig 统一调用
- app/service/systems/setting/warehouse.js:WAREHOUSE_SETTING_FIELDS 新增 key=inbox_push_retry
  (type=object + item_keys=[enabled, window_minutes]),供仓库设置页经 getFieldMeta 动态渲染
- app/service/inbox/message.js:retryFailedByWarehouse 逐仓读仓库设置(缺省关闭、按仓库配置的窗口
  调用 retryFailed);retryFailed 新增 warehouseId 过滤;readPushRetryConfig 读嵌套结构并兜底
  enabled=false / 30 分钟
- app/schedule/retry_inbox_push.js:注释同步为「仅处理显式开启的仓库、缺省 30 分钟」
- config/config.default.js:兜底失败时间窗 2 小时 → 30 分钟;pushRetryEnabled 保留为全局应急总闸
  (默认 true,关闭后所有仓库都不重发)

单测:test/inbox/retry_inbox_push.test.js + test/app/model/store.test.js 共 40 passing,覆盖默认关闭、
非法结构回退、flag 真值归一化、窗口 1-1440 越界、仓库开关与自定义窗口、复用原记录等场景。
2026-09-16 15:04:25 +08:00

272 lines
8.4 KiB
JavaScript
Raw Permalink 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');
/**
* 构造 store 模型测试所需的最小 Sequelize 类型集合。
* @return {Object} Sequelize 类型集合。
*/
function createSequelizeTypes() {
const makeType = sql => ({
toSql() {
return sql;
},
});
return {
BIGINT: {
UNSIGNED: makeType('BIGINT UNSIGNED'),
},
STRING: length => makeType(`VARCHAR(${length})`),
DATE: makeType('DATETIME'),
TINYINT: length => makeType(`TINYINT(${length})`),
JSON: makeType('JSON'),
literal(value) {
return value;
},
};
}
/**
* 构造加载 store 模型所需的最小应用对象。
* @return {Object} 应用对象。
*/
function createApp() {
return {
Sequelize: createSequelizeTypes(),
model: {
define(name, attributes, options) {
return {
name,
rawAttributes: attributes,
options,
prototype: {},
belongsTo() {},
hasMany() {},
};
},
},
};
}
describe('test/app/model/store.test.js', () => {
it('should keep auto_grouping_enabled disabled by default in normalized subject config', () => {
const Store = require('../../../app/model/store')(createApp());
const fromNull = Store.normalizeSubjectConfig(null);
const fromEmptyObject = Store.normalizeSubjectConfig({});
assert.equal(Store.DEFAULT_SUBJECT_CONFIG.auto_grouping_enabled, false);
assert.equal(fromNull.auto_grouping_enabled, false);
assert.equal(fromEmptyObject.auto_grouping_enabled, false);
});
it('should normalize auto_grouping_enabled to true when explicitly enabled', () => {
const Store = require('../../../app/model/store')(createApp());
const truthyInputs = [ true, 1, '1', 'true' ];
truthyInputs.forEach(input => {
const normalized = Store.normalizeSubjectConfig({
auto_grouping_enabled: input,
});
assert.equal(normalized.auto_grouping_enabled, true);
});
});
it('should include six-level inventory_warning_rules by default', () => {
const Store = require('../../../app/model/store')(createApp());
const normalized = Store.normalizeSubjectConfig({});
assert.ok(Array.isArray(normalized.inventory_warning_rules));
assert.equal(normalized.inventory_warning_rules.length, 6);
assert.deepEqual(normalized.inventory_warning_rules[0], {
name: '01级',
key: '#ff4d4f',
expire_days: 0,
tip_text: '立即报废',
});
assert.deepEqual(normalized.inventory_warning_rules[5], {
name: '06级',
key: '#52c41a',
expire_days: 999999,
tip_text: '常规监控',
});
assert.ok(
normalized.inventory_warning_rules.every(rule => typeof rule.tip_text === 'string' && rule.tip_text.trim() !== ''),
'默认库存预警规则六级 tip_text 应全部为非空字符串'
);
});
it('should normalize valid inventory_warning_rules entries with key and tip_text', () => {
const Store = require('../../../app/model/store')(createApp());
const normalized = Store.normalizeSubjectConfig({
inventory_warning_rules: [
{
name: ' 临期预警 ',
level: '1',
key: '#000000',
expire_days: '10',
tip_text: 123,
},
],
});
assert.deepEqual(normalized.inventory_warning_rules, [
{
name: '临期预警',
key: '#000000',
expire_days: 10,
tip_text: '123',
},
]);
});
it('should throw for invalid inventory_warning_rules entries', () => {
const Store = require('../../../app/model/store')(createApp());
assert.throws(
() => {
Store.normalizeSubjectConfig({
inventory_warning_rules: [
{
name: '临期预警',
key: '#ff4d4f',
expire_days: -1,
},
],
});
},
/subject_config\.inventory_warning_rules\[0\]\.expire_days 必须是非负整数/
);
assert.throws(
() => {
Store.normalizeSubjectConfig({
inventory_warning_rules: [
{
level: '高',
key: '#ff4d4f',
expire_days: 7,
},
],
});
},
/subject_config\.inventory_warning_rules\[0\]\.name 不能为空/
);
assert.throws(
() => {
Store.normalizeSubjectConfig({
inventory_warning_rules: [
{
name: '临期预警',
expire_days: 7,
},
],
});
},
/subject_config\.inventory_warning_rules\[0\]\.key 不能为空/
);
});
it('should default allocate_start_time to "08:00" and backfill legacy config', () => {
const Store = require('../../../app/model/store')(createApp());
const fromNull = Store.normalizeSubjectConfig(null);
const fromLegacy = Store.normalizeSubjectConfig({ cutoff_order_time: '16:00' });
assert.equal(fromNull.allocate_start_time, '08:00');
assert.equal(fromLegacy.allocate_start_time, '08:00');
});
it('should normalize valid allocate_start_time and allow cross-day window', () => {
const Store = require('../../../app/model/store')(createApp());
const normalized = Store.normalizeSubjectConfig({
allocate_start_time: '20:00',
cutoff_order_time: '06:00',
});
assert.equal(normalized.allocate_start_time, '20:00');
assert.equal(normalized.cutoff_order_time, '06:00');
});
it('should throw for invalid allocate_start_time format', () => {
const Store = require('../../../app/model/store')(createApp());
assert.throws(
() => Store.normalizeSubjectConfig({ allocate_start_time: '8:00' }),
/subject_config\.allocate_start_time 格式必须为 HH:mm(如 08:00)/
);
});
it('should reject allocate_start_time equal to cutoff_order_time', () => {
const Store = require('../../../app/model/store')(createApp());
assert.throws(
() => Store.normalizeSubjectConfig({
allocate_start_time: '09:00',
cutoff_order_time: '09:00',
}),
/subject_config\.allocate_start_time 不能等于 subject_config\.cutoff_order_time/
);
});
it('should default inbox_push_retry to disabled with 30-minute window (nested object)', () => {
const Store = require('../../../app/model/store')(createApp());
const fromNull = Store.normalizeSubjectConfig(null);
const fromEmptyObject = Store.normalizeSubjectConfig({});
assert.deepEqual(fromNull.inbox_push_retry, { enabled: false, window_minutes: 30 });
assert.deepEqual(fromEmptyObject.inbox_push_retry, { enabled: false, window_minutes: 30 });
// 非法结构(非对象 / 非法 JSON / 数组)一律回退默认
[ 'not-json', '[]', 123, true ].forEach(input => {
assert.deepEqual(Store.normalizeSubjectConfig({ inbox_push_retry: input }).inbox_push_retry, {
enabled: false,
window_minutes: 30,
}, `input=${input}`);
});
});
it('should normalize inbox_push_retry.enabled from flag-like values', () => {
const Store = require('../../../app/model/store')(createApp());
[ false, 0, '0', 'false' ].forEach(input => {
const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: input } });
assert.equal(normalized.inbox_push_retry.enabled, false, `input=${input}`);
});
[ true, 1, '1', 'true' ].forEach(input => {
const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: input } });
assert.equal(normalized.inbox_push_retry.enabled, true, `input=${input}`);
});
// 显式传非法类型时抛错(与其它 flag 字段口径一致)
assert.throws(
() => Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: 'yes' } }),
/subject_config\.inbox_push_retry\.enabled 必须是布尔值/
);
});
it('should accept 1-1440 minute inbox_push_retry.window_minutes and reject out-of-range values', () => {
const Store = require('../../../app/model/store')(createApp());
[ 1, 30, 120, 1440 ].forEach(input => {
const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { window_minutes: input } });
assert.equal(normalized.inbox_push_retry.window_minutes, input);
});
[ 0, -1, 1441, 1.5, 'abc' ].forEach(input => {
assert.throws(
() => Store.normalizeSubjectConfig({ inbox_push_retry: { window_minutes: input } }),
/subject_config\.inbox_push_retry\.window_minutes 必须是 1-1440 的整数(分钟)/,
`input=${input}`
);
});
});
});