'use strict'; const assert = require('node:assert/strict'); const SettingWarehouseService = require('../../../../../app/service/systems/setting/warehouse'); function createHarness(options = {}) { const calls = { findByPkWarehouseIds: [], updatePayload: null, cacheDeletes: [], }; const initialSubjectConfig = options.initialSubjectConfig || { max_wave_quantity: 500, }; const expressRows = Array.isArray(options.expressRows) ? options.expressRows : [ { code: 'STO', name: '申通快递' }, { code: 'YTO', name: '圆通快递' }, ]; const platformRows = Array.isArray(options.platformRows) ? options.platformRows : [ { code: 'TB', name: '淘宝' }, { code: 'JD', name: '京东' }, ]; const storeRecord = { id: Number(options.storeId || 100000), subject_config: { ...initialSubjectConfig, }, async update(payload) { calls.updatePayload = { ...payload, }; this.subject_config = { ...(payload.subject_config || {}), }; return this; }, }; const ctx = { user: { store_id: Number(options.storeId || 100000), }, app: { async delCacheAsync(key) { calls.cacheDeletes.push(key); }, }, model: { Store: { async findByPk(warehouseId) { calls.findByPkWarehouseIds.push(Number(warehouseId)); return storeRecord; }, normalizeSubjectConfig(config) { return { ...config, }; }, }, ...(options.withoutExpressModel ? {} : { Express: { async findAll() { if (options.expressQueryShouldThrow) { throw new Error('query express failed'); } return expressRows; }, }, }), ...(options.withoutPlatformModel ? {} : { Platform: { async findAll() { if (options.platformQueryShouldThrow) { throw new Error('query platform failed'); } return platformRows; }, }, }), }, }; return { ctx, calls, storeRecord, }; } describe('test/app/service/systems/setting/warehouse.test.js', () => { it('should always clear cache for current ctx warehouse when saving settings', async () => { const { ctx, calls } = createHarness({ storeId: 100000, }); const service = new SettingWarehouseService(ctx); const result = await service.saveSettings({ subject_config: { max_wave_quantity: 600, }, cache_warehouse_id: 999999, cacheWarehouseId: 888888, }); assert.deepEqual(calls.findByPkWarehouseIds, [ 100000 ]); assert.equal(result.warehouse_id, 100000); assert.equal(result.cache_cleared_warehouse_id, 100000); assert.equal(calls.updatePayload.subject_config.max_wave_quantity, 600); assert.deepEqual(calls.cacheDeletes, [ 'delivery_order:store:100000:subject_config', 'warehouse:100000store:100000:info', ]); }); it('should keep validation behavior for invalid subject_config', async () => { const { ctx } = createHarness(); const service = new SettingWarehouseService(ctx); await assert.rejects( service.saveSettings({ subject_config: null, }), error => { assert.equal(error.code, 400); assert.match(error.message, /subject_config 参数不能为空且必须是对象/); return true; } ); }); it('外部传入 wave_strategy_version 不应覆盖现有内部版本,且响应不暴露该字段', async () => { const { ctx, calls } = createHarness({ initialSubjectConfig: { max_wave_quantity: 500, wave_strategy_version: 9, }, }); const service = new SettingWarehouseService(ctx); const result = await service.saveSettings({ subject_config: { wave_strategy_version: 2, max_wave_quantity: 700, }, }); assert.equal(calls.updatePayload.subject_config.wave_strategy_version, 9); assert.equal(calls.updatePayload.subject_config.max_wave_quantity, 700); assert.equal(Object.prototype.hasOwnProperty.call(result.subject_config, 'wave_strategy_version'), false); }); it('should omit internal wave_strategy_version from getSettings response', async () => { const { ctx } = createHarness({ initialSubjectConfig: { max_wave_quantity: 500, wave_strategy_version: 3, }, }); const service = new SettingWarehouseService(ctx); const result = await service.getSettings(); assert.equal(result.warehouse_id, 100000); assert.equal(Object.prototype.hasOwnProperty.call(result.subject_config, 'wave_strategy_version'), false); assert.equal(result.subject_config.max_wave_quantity, 500); }); it('should include dynamic options for default_express_code and default_platform_code', async () => { const { ctx } = createHarness(); const service = new SettingWarehouseService(ctx); const list = await service.getFieldMeta(); const defaultExpressField = list.find(item => item.key === 'default_express_code'); const defaultPlatformField = list.find(item => item.key === 'default_platform_code'); assert.ok(defaultExpressField, '字段信息应包含 default_express_code'); assert.ok(defaultPlatformField, '字段信息应包含 default_platform_code'); assert.ok(Array.isArray(defaultExpressField.options), 'default_express_code 应包含 options 数组'); assert.ok(Array.isArray(defaultPlatformField.options), 'default_platform_code 应包含 options 数组'); assert.ok(defaultExpressField.options.every(option => option && typeof option.value === 'string' && typeof option.label === 'string')); assert.ok(defaultPlatformField.options.every(option => option && typeof option.value === 'string' && typeof option.label === 'string')); assert.deepEqual(defaultExpressField.options, [ { value: 'STO', label: '申通快递' }, { value: 'YTO', label: '圆通快递' }, ]); assert.deepEqual(defaultPlatformField.options, [ { value: 'TB', label: '淘宝' }, { value: 'JD', label: '京东' }, ]); }); it('should degrade gracefully when model missing or query fails', async () => { const { ctx } = createHarness({ withoutExpressModel: true, platformQueryShouldThrow: true, }); const service = new SettingWarehouseService(ctx); const list = await service.getFieldMeta(); const defaultExpressField = list.find(item => item.key === 'default_express_code'); const defaultPlatformField = list.find(item => item.key === 'default_platform_code'); assert.ok(defaultExpressField, '字段信息应包含 default_express_code'); assert.ok(defaultPlatformField, '字段信息应包含 default_platform_code'); assert.deepEqual(defaultExpressField.options, []); assert.deepEqual(defaultPlatformField.options, []); }); it('getFieldMeta should include inventory_warning_rules with six-level defaults', async () => { const { ctx } = createHarness(); const service = new SettingWarehouseService(ctx); const list = await service.getFieldMeta(); const inventoryWarningRulesField = list.find(item => item.key === 'inventory_warning_rules'); assert.ok(inventoryWarningRulesField, '字段信息应包含 inventory_warning_rules'); assert.equal(inventoryWarningRulesField.name, '库存预警配置'); assert.equal(inventoryWarningRulesField.type, 'array'); assert.ok(Array.isArray(inventoryWarningRulesField.item_keys), 'inventory_warning_rules 应包含 item_keys 数组'); assert.deepEqual(inventoryWarningRulesField.item_keys, [ { key: 'name', name: '名称', type: 'string', required: true, description: '预警等级名称' }, { key: 'key', name: '颜色标识', type: 'color', required: true, description: '预警颜色值' }, { key: 'expire_days', name: '过期天数', type: 'integer', required: true, description: '距过期天数阈值' }, { key: 'tip_text', name: '提示语', type: 'string', required: false, description: '预警提示文案' }, ]); assert.ok( inventoryWarningRulesField.item_keys.every(item => !Object.prototype.hasOwnProperty.call(item, 'default')), 'inventory_warning_rules.item_keys 子项不应包含 default 字段' ); assert.ok(Array.isArray(inventoryWarningRulesField.default)); assert.equal(inventoryWarningRulesField.default.length, 6); assert.deepEqual(inventoryWarningRulesField.default[0], { name: '01级', key: '#ff4d4f', expire_days: 0, tip_text: '立即报废', }); assert.deepEqual(inventoryWarningRulesField.default[5], { name: '06级', key: '#52c41a', expire_days: 999999, tip_text: '常规监控', }); assert.ok( inventoryWarningRulesField.default.every(rule => typeof rule.tip_text === 'string' && rule.tip_text.trim() !== ''), '默认库存预警规则的 tip_text 应全部为非空字符串' ); assert.equal(inventoryWarningRulesField.required, false); }); });