WMS_MAIN/test/app/model/store.test.js

217 lines
6.1 KiB
JavaScript
Raw 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/
);
});
});