WMS_MAIN/test/app/model/store.test.js
liufumingren 61c530c0cc 1
2026-05-14 04:00:07 +08:00

174 lines
4.6 KiB
JavaScript

'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 不能为空/
);
});
});