'use strict'; const assert = require('node:assert/strict'); const Sequelize = require('sequelize'); const ReviewOrderService = require('../../../app/service/systemMethods/order/reviewOrder'); const PendingOrderService = require('../../../app/service/systemMethods/order/pendingOrder'); const { buildRows, createOrderHarness, matchesWhere } = require('../../support/order_service_test_helper'); /** * 创建支持更新结果回写的订单模型。 * @param {Array} rows 初始订单数据。 * @param {Object} capture 调用信息。 * @return {Object} 模拟模型。 */ function createMutableOrderModel(rows, capture = {}) { const storedRows = Array.isArray(rows) ? rows.map(row => ({ ...row })) : []; capture.rows = storedRows; return { async count({ where } = {}) { capture.countWhere = where; return storedRows.filter(row => matchesWhere(row, where)).length; }, async update(values = {}, options = {}) { capture.updateValues = { ...values }; capture.updateWhere = options.where; const matchedRows = storedRows.filter(row => matchesWhere(row, options.where)); for (const row of matchedRows) { Object.assign(row, values); } return [ matchedRows.length ]; }, async findAll(options = {}) { capture.findAllWhere = options.where; return storedRows .filter(row => matchesWhere(row, options.where)) .map(row => ({ ...row })); }, async findOne(options = {}) { capture.findOneWhere = options.where; const matchedRow = storedRows.find(row => matchesWhere(row, options.where)); return matchedRow ? { ...matchedRow } : null; }, }; } /** * 创建共享同一份行数组的订单模型。 * @param {Array} rows 共享订单数据。 * @param {Object} capture 调用信息。 * @return {Object} 模拟模型。 */ function createSharedMutableOrderModel(rows, capture = {}) { const storedRows = Array.isArray(rows) ? rows : []; capture.rows = storedRows; return { async count({ where } = {}) { capture.countWhere = where; return storedRows.filter(row => matchesWhere(row, where)).length; }, async update(values = {}, options = {}) { capture.updateValues = { ...values }; capture.updateWhere = options.where; const matchedRows = storedRows.filter(row => matchesWhere(row, options.where)); for (const row of matchedRows) { Object.assign(row, values); } return [ matchedRows.length ]; }, async findAll(options = {}) { capture.findAllWhere = options.where; return storedRows .filter(row => matchesWhere(row, options.where)) .map(row => ({ ...row })); }, async findOne(options = {}) { capture.findOneWhere = options.where; const matchedRow = storedRows.find(row => matchesWhere(row, options.where)); return matchedRow ? { ...matchedRow } : null; }, }; } /** * 创建共享数据的分页模型。 * @param {Array} rows 共享订单数据。 * @param {Object} capture 调用信息。 * @return {Object} 模拟模型。 */ function createSharedPagedOrderModel(rows, capture = {}) { const storedRows = Array.isArray(rows) ? rows : []; capture.rows = storedRows; return { async count({ where } = {}) { capture.countWhere = where; return storedRows.filter(row => matchesWhere(row, where)).length; }, async findAll(options = {}) { capture.findAllWhere = options.where; return storedRows .filter(row => matchesWhere(row, options.where)) .map(row => ({ ...row })); }, }; } /** * 创建审核服务测试实例。 * @param {Array} rows 初始订单数据。 * @return {{ service: ReviewOrderService, capture: Object }} 服务与调用记录。 */ function createReviewOrderService(rows) { const { service } = createOrderHarness(ReviewOrderService, { defaultRows: rows, }); const capture = {}; service.getOrderModel = async () => createMutableOrderModel(rows, capture); return { service, capture, }; } /** * 创建共享数据的待处理订单服务实例。 * @param {Array} rows 共享订单数据。 * @return {{ service: PendingOrderService, capture: Object }} 服务与调用记录。 */ function createPendingOrderService(rows) { const { service } = createOrderHarness(PendingOrderService, { defaultRows: rows, }); const capture = {}; service.getOrderModel = async () => createSharedPagedOrderModel(rows, capture); return { service, capture, }; } /** * 创建共享数据的审核服务实例。 * @param {Array} rows 共享订单数据。 * @return {{ service: ReviewOrderService, capture: Object }} 服务与调用记录。 */ function createSharedReviewOrderService(rows) { const { service } = createOrderHarness(ReviewOrderService, { defaultRows: rows, }); const capture = {}; service.getOrderModel = async () => createSharedMutableOrderModel(rows, capture); return { service, capture, }; } describe('test/app/controller/system.order.reviewOrder.test.js', () => { it('should return list/pagination for reviewOrder.list', async () => { const { service, captures } = createOrderHarness(ReviewOrderService, { rowsByModule: { reviewOrder: buildRows(1, () => ({ step: 0, dispatch_lane: 'm4', })), }, }); const result = await service.list({ pagination: { page: 1, pageSize: 1, }, where: {}, }); assert.equal(captures[0].moduleName, 'reviewOrder'); assert.ok(Array.isArray(result.list)); assert.ok(result.pagination); assert.equal(result.pagination.page, 1); assert.equal(result.pagination.pageSize, 1); assert.equal(result.list.length, 1); }); it('should update batch orders by ids and force review approval status', async () => { const { service, capture } = createReviewOrderService(buildRows(3, index => ({ id: index + 1, warehouse_id: 100000, step: 0, inspection_status: 'none', order_flag: '待清理异常标记', updated_at: '2026-04-12 00:00:00', }))); const result = await service.reviewOrder({ ids: [ 1, 3 ], }); assert.equal(capture.updateWhere.warehouse_id, 100000); assert.equal(capture.updateWhere.id[Sequelize.Op.in].length, 2); assert.ok(matchesWhere({ id: 1, warehouse_id: 100000 }, capture.updateWhere)); assert.ok(!matchesWhere({ id: 2, warehouse_id: 100000 }, capture.updateWhere)); assert.ok(matchesWhere({ id: 3, warehouse_id: 100000 }, capture.updateWhere)); assert.equal(capture.updateValues.inspection_status, 'fin'); assert.equal(capture.updateValues.step, 1); assert.equal(capture.updateValues.order_flag, undefined); assert.equal(result.id, 1); assert.equal(result.inspection_status, 'fin'); assert.equal(result.step, 1); assert.equal(result.updated_count, 2); assert.deepEqual(result.ids, [ 1, 3 ]); assert.equal(capture.rows.find(row => row.id === 1).inspection_status, 'fin'); assert.equal(capture.rows.find(row => row.id === 3).inspection_status, 'fin'); }); it('should reject empty ids', async () => { const { service } = createReviewOrderService(buildRows(1, () => ({ id: 1, warehouse_id: 100000, step: 0, inspection_status: 'none', updated_at: '2026-04-12 00:00:00', }))); await assert.rejects( async () => await service.reviewOrder({ ids: [], }), /订单ID不能为空/ ); }); it('should reject legacy review fields and warehouse bypass fields', async () => { const { service } = createReviewOrderService(buildRows(1, () => ({ id: 1, warehouse_id: 100000, step: 0, inspection_status: 'none', updated_at: '2026-04-12 00:00:00', }))); const invalidPayloads = [ { id: 1 }, { order_id: 1 }, { ids: [ 1 ], review_status: 'fin' }, { ids: [ 1 ], inspection_status: 'fin' }, { ids: [ 1 ], warehouse_id: 100000 }, { ids: [ 1 ], warehouseId: 100000 }, ]; for (const payload of invalidPayloads) { await assert.rejects( async () => await service.reviewOrder(payload), /仅支持 ids 入参/ ); } }); it('should reject orders outside current warehouse', async () => { const { service, capture } = createReviewOrderService([ { id: 1, warehouse_id: 100000, step: 0, inspection_status: 'none', updated_at: '2026-04-12 00:00:00', }, { id: 2, warehouse_id: 200000, step: 0, inspection_status: 'none', updated_at: '2026-04-12 00:00:00', }, ]); await assert.rejects( async () => await service.reviewOrder({ ids: [ 1, 2 ], }), /不属于当前仓库/ ); assert.equal(capture.updateValues, undefined); }); it('should move approved orders from review list to pending list', async () => { const sharedRows = buildRows(2, index => ({ id: index + 1, warehouse_id: 100000, step: 0, inspection_status: 'none', updated_at: '2026-04-12 00:00:00', })); const { service: reviewService } = createSharedReviewOrderService(sharedRows); const { service: pendingService } = createPendingOrderService(sharedRows); const reviewBefore = await reviewService.list({ pagination: { page: 1, pageSize: 10, }, where: {}, }); const pendingBefore = await pendingService.list({ pagination: { page: 1, pageSize: 10, }, where: {}, }); assert.equal(reviewBefore.list.length, 2); assert.equal(pendingBefore.list.length, 0); const result = await reviewService.reviewOrder({ ids: [ 1, 2 ], }); const reviewAfter = await reviewService.list({ pagination: { page: 1, pageSize: 10, }, where: {}, }); const pendingAfter = await pendingService.list({ pagination: { page: 1, pageSize: 10, }, where: {}, }); assert.equal(result.step, 1); assert.deepEqual(result.ids, [ 1, 2 ]); assert.equal(reviewAfter.list.length, 0); assert.deepEqual(pendingAfter.list.map(item => item.id), [ 1, 2 ]); assert.ok(pendingAfter.list.every(item => [ 1, 2 ].includes(Number(item.step)))); }); });