'use strict'; const assert = require('node:assert/strict'); const StockStoreService = require('../../../../app/service/systems/stock_store'); function createTestContext(options = {}) { const calls = { countArgs: null, findArgs: null, findOneArgs: null, updateArgs: null, createArgs: null, createOptions: null, bulkCreateArgs: null, barcodeCalls: [], fileRefArgs: null, transactionCount: 0, transactionCommits: 0, transactionRollbacks: 0, operationLogCalls: [], }; const Op = { and: '$and', or: '$or', ne: '$ne', like: '$like', notLike: '$notLike', gt: '$gt', lt: '$lt', gte: '$gte', lte: '$lte', in: '$in', notIn: '$notIn', between: '$between', notBetween: '$notBetween', is: '$is', not: '$not', regexp: '$regexp', notRegexp: '$notRegexp', }; const rows = options.rows || [{ id: 1, item_code: 'SKU-1', goods_code: 'G-1', item_name: '商品A', quantity: 10, locked_quantity: 2, create_at: '2026-01-01 10:00:00', update_at: '2026-01-01 10:00:00', }]; const model = { StoreWarehouse: { buildStoreStockInclude(params = {}) { return { as: 'store_stock', required: Boolean(params.required), attributes: params.attributes || [], where: { warehouse_id: params.warehouseId, }, }; }, }, ProhibitedType: {}, StockWarehouse: { async count(args) { calls.countArgs = args; return options.total == null ? rows.length : options.total; }, async findAll(args) { calls.findArgs = args; return rows; }, async findOne(args) { calls.findOneArgs = args; const where = args && args.where ? args.where : {}; if (options.stockInfo && Object.prototype.hasOwnProperty.call(where, 'id') && Number(where.id) === Number(options.stockInfo.id)) { return options.stockInfo; } if (options.uniqueConflict) { return options.uniqueConflict; } return null; }, async update(args) { calls.updateArgs = args; return [ 1 ]; }, async create(args, optionsArg) { calls.createArgs = args; calls.createOptions = optionsArg; return { ...args, id: options.createdId || 123, }; }, }, StockBarcode: { async findAll(args) { calls.barcodeCalls.push(args); const attrs = Array.isArray(args.attributes) ? args.attributes : []; if (attrs.includes('stock_id')) { if (options.matchedBarcodeRows) { return options.matchedBarcodeRows; } return [{ stock_id: 1 }]; } if (options.barcodes) { return options.barcodes; } return [{ stock_id: 1, barcode: 'BC-1' }]; }, async bulkCreate(args) { calls.bulkCreateArgs = args; return args; }, }, FileReference: { async findAll(args) { calls.fileRefArgs = args; return options.images || []; }, }, }; const app = { Sequelize: { Op }, model, logger: { debug() {}, warn() {}, error() {}, }, config: { cos: { cdnurl: '', }, }, }; const ctx = { app, model, user: { store_id: 100000, }, logger: { error() {}, }, service: { systems: { operationLog: { async createOperationLog(args) { calls.operationLogCalls.push(args); }, }, }, }, }; model.transaction = async () => { calls.transactionCount += 1; return { async commit() { calls.transactionCommits += 1; }, async rollback() { calls.transactionRollbacks += 1; }, }; }; return { app, ctx, model, calls, Op, }; } function createServiceHarness(options = {}) { const harness = createTestContext(options); const service = new StockStoreService(harness.ctx); return { ...harness, service, }; } describe('test/app/service/systems/stock_store.stockWarehouse.conditions.test.js', () => { it('should build Sequelize condition for all supported operators', () => { const { service, app, Op } = createServiceHarness(); const cases = [ [ 'eq', { field: 'item_code', value: 'SKU' }, { item_code: 'SKU' }], [ 'ne', { value: 'SKU' }, { item_code: { [Op.ne]: 'SKU' } }], [ 'like', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU%' } }], [ 'contains', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU%' } }], [ 'notlike', { value: 'SKU' }, { item_code: { [Op.notLike]: '%SKU%' } }], [ 'startswith', { value: 'SKU' }, { item_code: { [Op.like]: 'SKU%' } }], [ 'endswith', { value: 'SKU' }, { item_code: { [Op.like]: '%SKU' } }], [ 'gt', { value: 10 }, { item_code: { [Op.gt]: 10 } }], [ 'lt', { value: 10 }, { item_code: { [Op.lt]: 10 } }], [ 'ge', { value: 10 }, { item_code: { [Op.gte]: 10 } }], [ 'le', { value: 10 }, { item_code: { [Op.lte]: 10 } }], [ 'in', { value: [ 'A', 'B' ] }, { item_code: { [Op.in]: [ 'A', 'B' ] } }], [ 'notin', { value: [ 'A', 'B' ] }, { item_code: { [Op.notIn]: [ 'A', 'B' ] } }], [ 'between', { value: [ 1, 2 ] }, { item_code: { [Op.between]: [ 1, 2 ] } }], [ 'notbetween', { value: [ 1, 2 ] }, { item_code: { [Op.notBetween]: [ 1, 2 ] } }], [ 'isnull', { value: null }, { item_code: { [Op.is]: null } }], [ 'isnotnull', { value: null }, { item_code: { [Op.not]: null } }], [ 'regexp', { value: '^SKU' }, { item_code: { [Op.regexp]: '^SKU' } }], [ 'notregexp', { value: '^SKU' }, { item_code: { [Op.notRegexp]: '^SKU' } }], ]; for (const [ operator, input, expected ] of cases) { const actual = service.buildSequelizeCondition('item_code', { operator, ...input, }, app); assert.deepEqual(actual, expected, `operator=${operator}`); } }); it('should normalize boolean, number and text values', () => { const { service } = createServiceHarness(); assert.equal(service.processConditionValue(true, 'boolean'), 'Y'); assert.equal(service.processConditionValue('false', 'boolean'), 'N'); assert.equal(service.processConditionValue('12.5', 'number'), 12.5); assert.deepEqual(service.processConditionValue('A,B,C', 'text'), [ 'A', 'B', 'C' ]); }); it('should support date and daterange condition extraction', () => { const { service, app, Op } = createServiceHarness(); const dateEq = service.buildSequelizeCondition('create_at', { operator: 'eq', type: 'date', value: '2026-03-25', }, app); assert.ok(dateEq.create_at); const between = service.buildSequelizeCondition('create_at', { operator: 'between', type: 'daterange', value: [ '2026-03-01', '2026-03-31' ], }, app); assert.deepEqual(between, { create_at: { [Op.between]: [ '2026-03-01 00:00:00', '2026-03-31 23:59:59' ], }, }); }); it('should parse where and return list with barcode/prohibited branches', async () => { const { service, calls, Op } = createServiceHarness(); const result = await service.getWarehouseStock({ item_code: { operator: 'like', value: 'SKU', }, barcode: { operator: 'like', value: 'BC', }, prohibited_type: { operator: 'eq', value: 2, }, quantity: { operator: 'ge', type: 'number', value: '2', }, }, { page: 1, pageSize: 10, }, { sortField: 'quantity', sortOrder: 'ASC', }); assert.equal(result.code, 200); assert.equal(result.message, '查询成功'); assert.equal(result.data.pagination.page, 1); assert.equal(result.data.pagination.pageSize, 10); assert.equal(result.data.queryInfo.hasBarcodeSearch, true); assert.ok(calls.countArgs); assert.ok(calls.findArgs); assert.ok(Array.isArray(calls.barcodeCalls)); assert.ok(calls.barcodeCalls.length >= 2, '应包含条码匹配查询和条码回填查询'); const stockWhere = calls.findArgs.where; assert.ok(stockWhere[Op.and], '应存在 AND 组合条件'); const includeList = calls.findArgs.include; const prohibitedInclude = includeList.find(item => item.as === 'prohibited'); assert.ok(prohibitedInclude, '应包含 prohibited 关联'); assert.equal(prohibitedInclude.required, true, '含 prohibited_type 时应为必需关联'); }); it('should short-circuit when barcode has no matched stock ids', async () => { const { service, calls } = createServiceHarness({ matchedBarcodeRows: [], rows: [{ id: 99, }], }); const result = await service.getWarehouseStock({ barcode: { operator: 'eq', value: 'NOT-FOUND', }, }, { page: 1, pageSize: 20, }); assert.equal(result.code, 200); assert.deepEqual(result.data.list, []); assert.equal(result.data.pagination.total, 0); assert.equal(calls.countArgs, null); assert.equal(calls.findArgs, null); }); it('should ignore empty where values and fallback invalid sort field', async () => { const { service, calls } = createServiceHarness(); const result = await service.getWarehouseStock({ item_name: { operator: 'like', value: ' ', }, store_id: { operator: 'in', value: [], }, item_code: { operator: 'like', value: 'SKU-1', }, }, { page: 2, pageSize: 5, }, { sortField: 'unknown_field', sortOrder: 'UNKNOWN', }); assert.equal(result.code, 200); assert.deepEqual(calls.findArgs.order, [[ 'create_at', 'DESC' ]]); assert.equal(result.data.pagination.page, 2); assert.equal(result.data.pagination.pageSize, 5); }); it('should return failure reason when where deconstruction is invalid', async () => { const { service } = createServiceHarness(); const result = await service.getWarehouseStock({ item_code: 'SKU-1', }, { page: 1, pageSize: 20, }); assert.equal(result.code, 400); assert.match(result.message, /筛选条件解析失败/); assert.match(result.message, /字段 item_code 条件必须是对象/); }); it('should return shelf life management flag as boolean in search result', async () => { const { service, calls } = createServiceHarness({ rows: [{ toJSON() { return { id: 1, item_code: 'SKU-1', goods_code: 'G-1', item_name: '商品A', sku_property: '默认规格', stock_unit: '件', quantity: 10, locked_quantity: 2, brand_name: '品牌A', item_type: 'normal', is_batch_mgmt: 'Y', is_shelf_life_mgmt: 'Y', barcodes: [{ barcode: 'BC-1' }], store_stock: { store_id: 100000, store_remask: '主仓', }, }; }, }], }); const result = await service.search({ keyword: '', }); assert.equal(result.code, 200); assert.equal(Array.isArray(result.data), true); assert.equal(result.data.length, 1); assert.equal(Object.prototype.hasOwnProperty.call(result.data[0], 'is_batch_mgmt_enabled'), true); assert.equal(typeof result.data[0].is_batch_mgmt_enabled, 'boolean'); assert.equal(result.data[0].is_batch_mgmt_enabled, true); assert.equal(Object.prototype.hasOwnProperty.call(result.data[0], 'is_batch_mgmt'), false); assert.equal(Object.prototype.hasOwnProperty.call(result.data[0], 'is_shelf_life_mgmt'), true); assert.equal(typeof result.data[0].is_shelf_life_mgmt, 'boolean'); assert.equal(result.data[0].is_shelf_life_mgmt, true); assert.deepEqual(result.data[0].barcodes, [ 'BC-1' ]); assert.ok(calls.findArgs); assert.ok(calls.findArgs.attributes.includes('is_shelf_life_mgmt')); }); it('should return shelf life threshold fields in search result', async () => { const { service, calls } = createServiceHarness({ rows: [{ toJSON() { return { id: 2, item_code: 'SKU-2', goods_code: 'G-2', item_name: '商品B', sku_property: '默认规格', stock_unit: '件', quantity: 8, locked_quantity: 1, brand_name: '品牌B', item_type: 'normal', inbound_min_days: 3, outbound_min_days: 5, shelf_life_days: 30, remaining_threshold_days: 7, is_shelf_life_mgmt: 'N', barcodes: [], store_stock: { store_id: 100000, store_remask: '主仓', }, }; }, }], }); const result = await service.search({ keyword: '', }); assert.equal(result.code, 200); assert.equal(result.data.length, 1); assert.equal(result.data[0].inbound_min_days, 3); assert.equal(result.data[0].outbound_min_days, 5); assert.equal(result.data[0].shelf_life_days, 30); assert.equal(result.data[0].remaining_threshold_days, 7); assert.equal(typeof result.data[0].inbound_min_days, 'number'); assert.equal(typeof result.data[0].outbound_min_days, 'number'); assert.equal(typeof result.data[0].shelf_life_days, 'number'); assert.equal(typeof result.data[0].remaining_threshold_days, 'number'); assert.ok(calls.findArgs); for (const field of [ 'inbound_min_days', 'outbound_min_days', 'shelf_life_days', 'remaining_threshold_days' ]) { assert.ok(calls.findArgs.attributes.includes(field)); } }); it('should return warehouse day threshold fields in getInfo response', async () => { const { service, calls } = createServiceHarness({ stockInfo: { id: 1, goods_code: 'G-1', item_code: 'SKU-1', shelf_life_days: 10, inbound_min_days: 3, outbound_min_days: 5, remaining_threshold_days: 7, enable_box_spec: 1, }, }); const result = await service.getInfo({ id: 1, }); assert.equal(result.id, 1); assert.equal(result.goods_code, 'G-1'); assert.equal(result.shelf_life_days, 10); assert.equal(result.inbound_min_days, 3); assert.equal(result.outbound_min_days, 5); assert.equal(result.remaining_threshold_days, 7); assert.equal(result.enable_box_spec, true); assert.ok(calls.findOneArgs); assert.ok(calls.findOneArgs.attributes.includes('shelf_life_days')); assert.ok(calls.findOneArgs.attributes.includes('inbound_min_days')); assert.ok(calls.findOneArgs.attributes.includes('outbound_min_days')); assert.ok(calls.findOneArgs.attributes.includes('remaining_threshold_days')); }); it('should persist shelf life fields when saving warehouse stock', async () => { const { service, calls } = createServiceHarness({ stockInfo: { id: 9, deleted_at: null, }, }); service.getInfo = async ({ id }) => ({ id, shelf_life_days: 30, inbound_min_days: 3, outbound_min_days: 5, remaining_threshold_days: 7, }); const result = await service.saveWarehouseStock({ id: 9, item_code: 'SKU-9', item_name: '商品九号', sku_property: '默认规格', stock_unit: '件', packing_requirements: '', length: 10, width: 11, height: 12, volume: 13, gross_weight: 14, net_weight: 15, tag_price: 16, retail_price: 17, is_sn_mgmt: 'N', is_shelf_life_mgmt: 'Y', is_batch_mgmt: 'N', prohibited_id: null, brand_name: '品牌九', managementFlags: '', enable_box_spec: false, box_unit: '', quantity_per_box: 0, box_gross_weight: 0, box_net_weight: 0, box_length: 0, box_width: 0, box_height: 0, box_volume: 0, box_remark: '', shelf_life_days: 30, inbound_min_days: 3, outbound_min_days: 5, remaining_threshold_days: 7, }); assert.equal(result.id, 9); assert.equal(calls.transactionCount, 1); assert.equal(calls.transactionCommits, 1); assert.equal(calls.transactionRollbacks, 0); assert.ok(calls.updateArgs); assert.equal(calls.updateArgs.shelf_life_days, 30); assert.equal(calls.updateArgs.inbound_min_days, 3); assert.equal(calls.updateArgs.outbound_min_days, 5); assert.equal(calls.updateArgs.remaining_threshold_days, 7); assert.equal(calls.updateArgs.goods_code, undefined); assert.equal(calls.operationLogCalls.length, 1); }); it('should reject saving warehouse stock when shelf life fields are missing or invalid', async () => { const { service, calls } = createServiceHarness({ stockInfo: { id: 10, deleted_at: null, }, }); await assert.rejects( service.saveWarehouseStock({ id: 10, item_code: 'SKU-10', item_name: '商品十号', sku_property: '默认规格', stock_unit: '件', packing_requirements: '', length: 10, width: 11, height: 12, volume: 13, gross_weight: 14, net_weight: 15, tag_price: 16, retail_price: 17, is_sn_mgmt: 'N', is_shelf_life_mgmt: 'Y', is_batch_mgmt: 'N', prohibited_id: null, brand_name: '品牌十', managementFlags: '', enable_box_spec: false, box_unit: '', quantity_per_box: 0, box_gross_weight: 0, box_net_weight: 0, box_length: 0, box_width: 0, box_height: 0, box_volume: 0, box_remark: '', shelf_life_days: 0, inbound_min_days: 3, outbound_min_days: 5, remaining_threshold_days: 7, }), error => { assert.equal(error.message, '启用保质期管理时,shelf_life_days、inbound_min_days、outbound_min_days、remaining_threshold_days 都必须存在且大于 0'); return true; } ); assert.equal(calls.transactionCount, 0); assert.equal(calls.updateArgs, null); assert.equal(calls.operationLogCalls.length, 0); }); it('should write goods_code on update and persist create without barcode', async () => { const { service, calls } = createServiceHarness({ stockInfo: { id: 12, deleted_at: null, store_id: 100000, warehouse_id: 100000, }, createdId: 88, }); service.getInfo = async ({ id }) => ({ id, goods_code: 'G-12', }); const updateResult = await service.saveWarehouseStock({ id: 12, item_code: 'SKU-12', goods_code: 'G-12', item_name: '商品十二号', sku_property: '默认规格', stock_unit: '件', packing_requirements: '', length: 10, width: 11, height: 12, volume: 13, gross_weight: 14, net_weight: 15, tag_price: 16, retail_price: 17, is_sn_mgmt: 'N', is_shelf_life_mgmt: 'N', is_batch_mgmt: 'N', prohibited_id: null, brand_name: '品牌十二', managementFlags: '', enable_box_spec: false, box_unit: '', quantity_per_box: 0, box_gross_weight: 0, box_net_weight: 0, box_length: 0, box_width: 0, box_height: 0, box_volume: 0, box_remark: '', }); assert.equal(updateResult.id, 12); assert.equal(calls.updateArgs.goods_code, 'G-12'); service.checkItemCodeUnique = async () => ({ isUnique: true, }); service.getInfo = async ({ id }) => ({ id, goods_code: 'G-99', }); const createResult = await service.createWarehouseStock({ item_code: 'SKU-99', goods_code: 'G-99', item_name: '商品九十九号', sku_property: '默认规格', stock_unit: '件', packing_requirements: '', length: 10, width: 11, height: 12, volume: 13, gross_weight: 14, net_weight: 15, tag_price: 16, retail_price: 17, is_sn_mgmt: 'N', is_shelf_life_mgmt: 'N', is_batch_mgmt: 'N', prohibited_id: null, brand_name: '品牌九十九', managementFlags: '', enable_box_spec: false, box_unit: '', quantity_per_box: 0, box_gross_weight: 0, box_net_weight: 0, box_length: 0, box_width: 0, box_height: 0, box_volume: 0, box_remark: '', barcode: '', }); assert.equal(createResult.id, 88); assert.equal(calls.createArgs.goods_code, 'G-99'); assert.equal(calls.bulkCreateArgs, null); }); });