'use strict'; const assert = require('node:assert/strict'); const StorageReportService = require('../../../../app/service/systems/storage/report'); const StorageReportMethodService = require('../../../../app/service/systemMethods/storage/report'); /** * 构建 storage.report service 测试桩。 * @param {Object} options 自定义参数 * @return {{service: StorageReportService, calls: Object}} service 与调用记录 */ function createReportServiceHarness(options = {}) { const calls = { queries: [], exports: [], }; let queryIndex = 0; const queryResults = Array.isArray(options.queryResults) ? options.queryResults : []; const model = { async query(sql, queryOptions = {}) { calls.queries.push({ sql, options: queryOptions, }); const result = queryResults[queryIndex] || []; queryIndex += 1; return result; }, StockWarehouse: { async findAll() { return options.stockRows || []; }, }, StockBarcode: { async findAll() { return options.barcodeRows || []; }, }, Store: { async findAll() { return options.storeRows || []; }, }, LocationSpace: { async findAll() { return options.spaceRows || []; }, }, }; const ctx = { user: { store_id: options.warehouseId || 100000, }, model, app: { Sequelize: { QueryTypes: { SELECT: 'SELECT', }, }, }, logger: { info() {}, warn() {}, error() {}, }, service: { systems: { excel: { async generateAndUpload(exportOptions) { calls.exports.push(exportOptions); return { filename: exportOptions.filename, fileKey: `exports/mock/${exportOptions.filename}`, url: `https://cdn.example.com/${exportOptions.filename}`, }; }, }, }, }, }; return { service: new StorageReportService(ctx), calls, }; } /** * 构建 storage.report method 测试桩。 * @param {Object} options 自定义参数 * @return {{methodService: StorageReportMethodService, captured: Object, cacheStore: Map}} method 与调用记录 */ function createReportMethodHarness(options = {}) { const captured = { exportCalls: [], }; const cacheStore = new Map(); const ctx = { app: { Sequelize: { Op: {}, }, async setCacheAsync(cacheKey, value) { cacheStore.set(cacheKey, value); }, async getCacheAsync(cacheKey) { return cacheStore.get(cacheKey); }, }, service: { systems: { storage: { report: { async export(startTime, endTime, bySpace, storeIds, sort) { captured.exportCalls.push({ startTime, endTime, bySpace, storeIds, sort, }); return options.exportResult || { filename: '库存报表_20260501_20260502.xlsx', fileKey: 'exports/mock/库存报表_20260501_20260502.xlsx', url: 'https://cdn.example.com/库存报表_20260501_20260502.xlsx', }; }, }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, user: { store_id: options.warehouseId || 100000, }, }; const methodService = new StorageReportMethodService(ctx); methodService.prepareWhereContext = function prepareWhereContext() { return { whereValue: { start_time: '2026-05-01 00:00:00', end_time: '2026-05-02 23:59:59', by_space: false, store_ids: [ 33, 44 ], sort_field: 'inbound_total', sort_order: 'ASC', }, }; }; return { methodService, captured, cacheStore, }; } describe('test/app/service/systems/storage.report.test.js', () => { it('should use last pre-start after and last in-range after by stock+store when by_space is false', async () => { const { service, calls } = createReportServiceHarness({ queryResults: [ [ { stock_id: 101, store_id: 201, quantity: 99 }, { stock_id: 102, store_id: 202, quantity: 88 }, ], [ { stock_id: 101, space_id: 11, store_id: 201, inbound_total: 5, outbound_total: 2 }, { stock_id: 101, space_id: 12, store_id: 201, inbound_total: 3, outbound_total: 1 }, { stock_id: 102, space_id: 13, store_id: 202, inbound_total: 4, outbound_total: 0 }, ], [ { id: 1, stock_id: 101, space_id: 11, storage_id: 9001, store_id: 201, before_quantity: 50, after_quantity: 55, created_at: '2026-05-01 09:00:00' }, { id: 2, stock_id: 101, space_id: 12, storage_id: 9009, store_id: 201, before_quantity: 55, after_quantity: 60, created_at: '2026-05-01 10:00:00' }, { id: 3, stock_id: 101, space_id: 11, storage_id: 9010, store_id: 201, before_quantity: 60, after_quantity: 66, created_at: '2026-05-02 12:00:00' }, { id: 4, stock_id: 102, space_id: 13, storage_id: 9003, store_id: 202, before_quantity: 3, after_quantity: 4, created_at: '2026-05-02 14:00:00' }, ], [ { id: 10, stock_id: 101, space_id: 11, store_id: 201, after_quantity: 47, created_at: '2026-04-30 09:00:00' }, { id: 11, stock_id: 101, space_id: 12, store_id: 201, after_quantity: 48, created_at: '2026-04-30 23:00:00' }, { id: 12, stock_id: 102, space_id: 13, store_id: 202, after_quantity: 2, created_at: '2026-04-30 20:00:00' }, ], ], stockRows: [ { id: 101, item_name: '商品A', item_code: 'SKU-A' }, { id: 102, item_name: '商品B', item_code: 'SKU-B' }, ], barcodeRows: [ { stock_id: 101, barcode: 'BC-A-1' }, { stock_id: 101, barcode: 'BC-A-2' }, { stock_id: 102, barcode: 'BC-B-1' }, ], storeRows: [ { id: 201, company_name: '货主甲' }, { id: 202, company_name: '货主乙' }, ], }); const result = await service.search('2026-05-01 00:00:00', '2026-05-02 23:59:59', false, [ 201, 202 ]); assert.equal(result.code, 200); assert.equal(result.data.list.length, 2); const stockA = result.data.list.find(item => item.stock_id === 101 && item.store_id === 201); assert.ok(stockA); assert.equal(stockA.begin_total, 48); assert.equal(stockA.end_total, 66); assert.equal(stockA.inbound_total, 8); assert.equal(stockA.outbound_total, 3); assert.notEqual(stockA.begin_total, 50); assert.equal(stockA.space_id, undefined); assert.match(stockA.barcode, /BC-A-1/); assert.match(stockA.barcode, /BC-A-2/); const stockB = result.data.list.find(item => item.stock_id === 102 && item.store_id === 202); assert.ok(stockB); assert.equal(stockB.begin_total, 2); assert.equal(stockB.end_total, 4); assert.equal(stockB.inbound_total, 4); assert.equal(stockB.outbound_total, 0); assert.match(calls.queries[2].sql, /before_quantity/); assert.doesNotMatch(calls.queries[2].sql, /storage_id/); assert.match(calls.queries[3].sql, /created_at < :startTime/); assert.match(calls.queries[3].sql, /after_quantity/); }); it('should keep space dimension with pre-start-last-after for begin and in-range-last-after for end', async () => { const { service } = createReportServiceHarness({ queryResults: [ [ { stock_id: 301, space_id: 501, store_id: 401, quantity: 7 }, { stock_id: 301, space_id: 502, store_id: 401, quantity: 11 }, ], [ { stock_id: 301, space_id: 501, store_id: 401, inbound_total: 2, outbound_total: 1 }, { stock_id: 301, space_id: 502, store_id: 401, inbound_total: 3, outbound_total: 0 }, ], [ { id: 1, stock_id: 301, space_id: 501, storage_id: 8801, store_id: 401, before_quantity: 7, after_quantity: 8, created_at: '2026-05-01 09:00:00' }, { id: 2, stock_id: 301, space_id: 501, storage_id: 8802, store_id: 401, before_quantity: 8, after_quantity: 10, created_at: '2026-05-01 12:00:00' }, { id: 3, stock_id: 301, space_id: 502, storage_id: 8803, store_id: 401, before_quantity: 11, after_quantity: 12, created_at: '2026-05-01 10:00:00' }, ], [ { id: 10, stock_id: 301, space_id: 501, store_id: 401, after_quantity: 6, created_at: '2026-04-30 23:00:00' }, { id: 11, stock_id: 301, space_id: 502, store_id: 401, after_quantity: 10, created_at: '2026-04-30 22:00:00' }, ], ], stockRows: [ { id: 301, item_name: '商品C', item_code: 'SKU-C' }, ], barcodeRows: [ { stock_id: 301, barcode: 'BC-C-1' }, ], storeRows: [ { id: 401, company_name: '货主丙' }, ], spaceRows: [ { id: 501, custom_name: '高位货架', space_code: 'A-01' }, { id: 502, custom_name: '', space_code: 'B-02' }, ], }); const result = await service.search('2026-05-01 00:00:00', '2026-05-01 23:59:59', true, [ 401 ]); assert.equal(result.code, 200); assert.equal(result.data.list.length, 2); const spaceA = result.data.list.find(item => item.space_id === 501); const spaceB = result.data.list.find(item => item.space_id === 502); assert.ok(spaceA); assert.ok(spaceB); assert.equal(spaceA.space_name, '高位货架'); assert.equal(spaceB.space_name, 'B-02'); assert.equal(spaceA.begin_total, 6); assert.equal(spaceA.end_total, 10); assert.equal(spaceB.begin_total, 10); assert.equal(spaceB.end_total, 12); }); it('should return base space keys with zero fallback when by_space is true and interval has no transactions', async () => { const { service } = createReportServiceHarness({ queryResults: [ [ { stock_id: 901, space_id: 301, store_id: 701, quantity: 15 }, { stock_id: 902, space_id: 302, store_id: 701, quantity: 9 }, ], [], [], [], ], stockRows: [ { id: 901, item_name: '商品M', item_code: 'SKU-M' }, { id: 902, item_name: '商品N', item_code: 'SKU-N' }, ], storeRows: [ { id: 701, company_name: '货主M' }, ], spaceRows: [ { id: 301, custom_name: '一号位', space_code: 'A-01' }, { id: 302, custom_name: '', space_code: 'A-02' }, ], }); const result = await service.search( '2026-05-10 00:00:00', '2026-05-10 23:59:59', true, [], { sortField: 'begin_total', sortOrder: 'DESC', } ); assert.equal(result.code, 200); assert.equal(result.data.list.length, 2); assert.deepEqual(result.data.list[0], { start_time: '2026-05-10 00:00:00', end_time: '2026-05-10 23:59:59', stock_id: 901, store_id: 701, space_id: 301, begin_total: 0, end_total: 0, inbound_total: 0, outbound_total: 0, store_name: '货主M', item_name: '商品M', barcode: '', item_code: 'SKU-M', space_name: '一号位', }); assert.deepEqual(result.data.list[1], { start_time: '2026-05-10 00:00:00', end_time: '2026-05-10 23:59:59', stock_id: 902, store_id: 701, space_id: 302, begin_total: 0, end_total: 0, inbound_total: 0, outbound_total: 0, store_name: '货主M', item_name: '商品N', barcode: '', item_code: 'SKU-N', space_name: 'A-02', }); }); it('should return base stocks with zero fallback when by_space is false and interval has no transactions', async () => { const { service, calls } = createReportServiceHarness({ queryResults: [ [ { stock_id: 701, store_id: 901, quantity: 37 }, ], [], [], [], ], stockRows: [ { id: 701, item_name: '商品X', item_code: 'SKU-X' }, ], storeRows: [ { id: 901, company_name: '货主X' }, ], }); const result = await service.search('2026-05-10 00:00:00', '2026-05-10 23:59:59', false, [ 901 ]); assert.equal(result.code, 200); assert.equal(result.data.list.length, 1); assert.deepEqual(result.data.list[0], { start_time: '2026-05-10 00:00:00', end_time: '2026-05-10 23:59:59', stock_id: 701, store_id: 901, begin_total: 0, end_total: 0, inbound_total: 0, outbound_total: 0, store_name: '货主X', item_name: '商品X', barcode: '', item_code: 'SKU-X', }); assert.equal(calls.queries.length, 4); assert.match(calls.queries[1].sql, /created_at >= :startTime/); assert.match(calls.queries[1].sql, /created_at <= :endTime/); assert.deepEqual(calls.queries[1].options.replacements.storeIds, [ 901 ]); assert.match(calls.queries[3].sql, /created_at < :startTime/); }); it('should sort by end_total in DESC order when sort params are valid', async () => { const { service } = createReportServiceHarness({ queryResults: [ [ { stock_id: 801, store_id: 901, quantity: 10 }, { stock_id: 802, store_id: 901, quantity: 20 }, ], [ { stock_id: 801, space_id: 0, store_id: 901, inbound_total: 1, outbound_total: 0 }, { stock_id: 802, space_id: 0, store_id: 901, inbound_total: 2, outbound_total: 0 }, ], [ { id: 1, stock_id: 801, space_id: 0, storage_id: 9101, store_id: 901, before_quantity: 10, after_quantity: 11, created_at: '2026-05-01 12:00:00' }, { id: 2, stock_id: 802, space_id: 0, storage_id: 9102, store_id: 901, before_quantity: 20, after_quantity: 22, created_at: '2026-05-01 12:00:00' }, ], [], ], stockRows: [ { id: 801, item_name: '商品Y', item_code: 'SKU-Y' }, { id: 802, item_name: '商品Z', item_code: 'SKU-Z' }, ], storeRows: [ { id: 901, company_name: '货主Y' }, ], }); const result = await service.search( '2026-05-01 00:00:00', '2026-05-01 23:59:59', false, [ 901 ], { sortField: 'end_total', sortOrder: 'DESC', } ); assert.equal(result.code, 200); assert.equal(result.data.list.length, 2); assert.equal(result.data.list[0].stock_id, 802); assert.equal(result.data.list[0].end_total, 22); assert.equal(result.data.list[1].stock_id, 801); assert.equal(result.data.list[1].end_total, 11); }); it('should pass destructured where params from method layer to systems layer', async () => { const captured = { startTime: null, endTime: null, bySpace: null, storeIds: null, sort: null, }; const ctx = { app: { Sequelize: { Op: {}, }, }, service: { systems: { storage: { report: { async search(startTime, endTime, bySpace, storeIds, sort) { captured.startTime = startTime; captured.endTime = endTime; captured.bySpace = bySpace; captured.storeIds = storeIds; captured.sort = sort; return { code: 200, message: '查询成功', data: { list: [] }, }; }, }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, user: { store_id: 100000, }, }; const methodService = new StorageReportMethodService(ctx); methodService.prepareWhereContext = function prepareWhereContext() { return { whereValue: { start_time: '2026-05-01 00:00:00', end_time: '2026-05-02 23:59:59', by_space: true, store_ids: [ 11, 22 ], sort_field: 'begin_total', sort_order: 'asc', }, }; }; const result = await methodService.search({ sort: { sort_field: 'end_total', sort_order: 'desc', }, where: { start_time: 'ignored', }, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(captured.startTime, '2026-05-01 00:00:00'); assert.equal(captured.endTime, '2026-05-02 23:59:59'); assert.equal(captured.bySpace, true); assert.deepEqual(captured.storeIds, [ 11, 22 ]); assert.deepEqual(captured.sort, { sortField: 'end_total', sortOrder: 'desc', }); }); it('should submit export task and return task_id with submitted status', async () => { const { methodService } = createReportMethodHarness(); const result = await methodService.export({ sort: { sort_field: 'end_total', sort_order: 'desc', }, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 1); assert.ok(result.task_id); assert.equal(result.status, 'submitted'); assert.equal(result.progress, 0); }); it('should return success progress and download_url after setImmediate task execution', async () => { const { methodService, captured } = createReportMethodHarness(); const submitResult = await methodService.export({ sort: { sort_field: 'end_total', sort_order: 'desc', }, }); const taskId = submitResult.task_id; assert.ok(taskId); await new Promise(resolve => setImmediate(resolve)); await new Promise(resolve => setImmediate(resolve)); const progressResult = await methodService.exportTaskProgress({ task_id: taskId, }); assert.equal(progressResult.flag, 'success'); assert.equal(progressResult.code, 1); assert.equal(progressResult.task_id, taskId); assert.equal(progressResult.status, 'success'); assert.equal(progressResult.progress, 100); assert.equal(progressResult.download_url, 'https://cdn.example.com/库存报表_20260501_20260502.xlsx'); assert.equal(captured.exportCalls.length, 1); assert.equal(captured.exportCalls[0].startTime, '2026-05-01 00:00:00'); assert.equal(captured.exportCalls[0].endTime, '2026-05-02 23:59:59'); assert.equal(captured.exportCalls[0].bySpace, false); assert.deepEqual(captured.exportCalls[0].storeIds, [ 33, 44 ]); assert.deepEqual(captured.exportCalls[0].sort, { sortField: 'end_total', sortOrder: 'desc', }); }); it('should return not_found when task_id does not exist', async () => { const { methodService } = createReportMethodHarness(); const result = await methodService.exportTaskProgress({ task_id: 'not_exists_task_id', }); assert.equal(result.flag, 'success'); assert.equal(result.code, 1); assert.equal(result.task_id, 'not_exists_task_id'); assert.equal(result.status, 'not_found'); assert.equal(result.progress, 0); assert.equal(result.download_url, ''); }); it('should export summary and transaction sheets with readable detail fields', async () => { const { service, calls } = createReportServiceHarness({ queryResults: [ [ { stock_id: 701, store_id: 901, quantity: 37 }, ], [ { stock_id: 701, space_id: 0, store_id: 901, inbound_total: 2, outbound_total: 1 }, ], [ { id: 1, stock_id: 701, space_id: 0, storage_id: 5001, store_id: 901, before_quantity: 10, after_quantity: 11, created_at: '2026-05-01 12:00:00' }, ], [ { id: 10, stock_id: 701, space_id: 0, store_id: 901, after_quantity: 9, created_at: '2026-04-30 23:00:00' }, ], [ { id: 1001, created_at: '2026-05-01 12:00:00', transaction_type: ' inbound ', store_id: 901, stock_id: 701, space_id: 301, quantity: 2, before_quantity: 10, after_quantity: 12, before_locked_quantity: 1, after_locked_quantity: 1, batch_no: 'BATCH-001', inventory_type: 'ZP', order_type: 'SCRK', order_no: 'RK20260501001', order_id: 6001, receiving_id: 7001, entry_order_id: 8001, operator_name: '张三', remark: '测试备注', }, { id: 1002, created_at: '2026-05-01 12:30:00', transaction_type: ' transfer_out ', store_id: 901, stock_id: 701, space_id: 301, quantity: 1, before_quantity: 12, after_quantity: 12, before_locked_quantity: 0, after_locked_quantity: 0, batch_no: 'BATCH-001', inventory_type: 'ZP', order_type: null, order_no: null, order_id: null, receiving_id: null, entry_order_id: null, operator_name: '张三', remark: '移库出', }, { id: 1003, created_at: '2026-05-01 12:31:00', transaction_type: 'transfer_in', store_id: 901, stock_id: 701, space_id: 301, quantity: 1, before_quantity: 15, after_quantity: 15, before_locked_quantity: 0, after_locked_quantity: 0, batch_no: 'BATCH-001', inventory_type: 'ZP', order_type: null, order_no: null, order_id: null, receiving_id: null, entry_order_id: null, operator_name: '张三', remark: '移库入', }, { id: 1004, created_at: '2026-05-01 12:32:00', transaction_type: ' custom_type ', store_id: 901, stock_id: 701, space_id: 301, quantity: 1, before_quantity: 15, after_quantity: 16, before_locked_quantity: 0, after_locked_quantity: 0, batch_no: 'BATCH-001', inventory_type: 'ZP', order_type: null, order_no: null, order_id: null, receiving_id: null, entry_order_id: null, operator_name: '张三', remark: '未知类型', }, ], ], stockRows: [ { id: 701, item_name: '商品X', item_code: 'SKU-X' }, ], barcodeRows: [ { stock_id: 701, barcode: 'BC-X-1' }, ], storeRows: [ { id: 901, company_name: '货主X' }, ], spaceRows: [ { id: 301, custom_name: 'A01位', space_code: 'A-01' }, ], }); const result = await service.export( '2026-05-01 00:00:00', '2026-05-01 23:59:59', false, [ 901 ], { sortField: 'end_total', sortOrder: 'DESC', } ); assert.ok(result.filename.endsWith('.xlsx')); assert.ok(result.fileKey); assert.ok(result.url); assert.equal(calls.exports.length, 1); const exportPayload = calls.exports[0]; assert.equal(exportPayload.sheets.length, 2); assert.equal(exportPayload.sheets[0].name, '汇总'); assert.equal(exportPayload.sheets[1].name, '变更记录'); assert.equal(exportPayload.sheets[0].rows.length, 1); assert.equal(exportPayload.sheets[0].rows[0].stock_id, 701); assert.equal(exportPayload.sheets[0].rows[0].store_name, '货主X'); assert.equal(exportPayload.sheets[1].rows.length, 4); const detailRow = exportPayload.sheets[1].rows[0]; assert.equal(detailRow.transaction_type, '入库'); assert.equal(detailRow.store_name, '货主X'); assert.equal(detailRow.item_code, 'SKU-X'); assert.equal(detailRow.item_name, '商品X'); assert.equal(detailRow.barcode, 'BC-X-1'); assert.equal(detailRow.space_name, 'A01位'); assert.equal(detailRow.quantity, 2); assert.equal(detailRow.before_locked_quantity, 1); assert.equal(detailRow.after_locked_quantity, 1); assert.equal(detailRow.order_no, 'RK20260501001'); assert.equal(detailRow.operator_name, '张三'); const transferOutRow = exportPayload.sheets[1].rows[1]; const transferInRow = exportPayload.sheets[1].rows[2]; const unknownTypeRow = exportPayload.sheets[1].rows[3]; assert.equal(transferOutRow.transaction_type, '移库出'); assert.equal(transferInRow.transaction_type, '移库入'); assert.equal(unknownTypeRow.transaction_type, 'custom_type'); }); });