'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}` ); }); }); });