'use strict'; const assert = require('node:assert/strict'); const Sequelize = require('sequelize'); const OrderOutboundService = require('../../../../app/service/systems/order_outbound'); const { buildRows, createServiceContext, 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 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 })); }, }; } /** * 创建出库服务测试实例。 * @param {Array} rows 初始订单数据。 * @return {{ service: OrderOutboundService, capture: Object }} 服务与调用记录。 */ function createOutboundService(rows) { const ctx = createServiceContext(100000); ctx.logger = { info() {}, warn() {}, error() {}, debug() {}, }; ctx.service = { systems: {}, }; const capture = {}; const sharedModel = createMutableOrderModel(rows, capture); const service = new OrderOutboundService(ctx); service.getOrderModel = async () => sharedModel; return { service, capture, }; } describe('test/app/service/systems/order_outbound.test.js', () => { it('should mark single order as outbound with legacy id/order_id fields', async () => { const scenarios = [ { payload: { id: 1, }, expectedId: 1, }, { payload: { order_id: 2, }, expectedId: 2, }, ]; for (const scenario of scenarios) { const { service, capture } = createOutboundService(buildRows(2, index => ({ id: index + 1, warehouse_id: 100000, step: 1, outbound_status: 'none', delivery_time: null, updated_at: '2026-04-12 00:00:00', }))); const result = await service.markOutboundOrder(scenario.payload); assert.equal(capture.updateWhere.id, scenario.expectedId); assert.equal(capture.updateWhere.warehouse_id, 100000); assert.equal(capture.updateValues.outbound_status, 'fin'); assert.equal(capture.updateValues.step, 4); assert.ok(capture.updateValues.delivery_time instanceof Date); assert.ok(capture.updateValues.updated_at instanceof Date); assert.equal(result.id, scenario.expectedId); assert.equal(result.outbound_status, 'fin'); assert.equal(result.step, 4); assert.equal(result.updated_count, 1); assert.deepEqual(result.ids, [ scenario.expectedId ]); assert.equal(capture.rows.find(row => row.id === scenario.expectedId).outbound_status, 'fin'); assert.equal(capture.rows.find(row => row.id === scenario.expectedId).step, 4); } }); it('should support batch ids and update delivery_time for every matched order', async () => { const { service, capture } = createOutboundService(buildRows(3, index => ({ id: index + 1, warehouse_id: 100000, step: 1, outbound_status: 'none', delivery_time: null, updated_at: '2026-04-12 00:00:00', }))); const result = await service.markOutboundOrder({ 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.outbound_status, 'fin'); assert.equal(capture.updateValues.step, 4); assert.ok(capture.updateValues.delivery_time instanceof Date); assert.equal(result.id, 1); assert.equal(result.outbound_status, 'fin'); assert.equal(result.step, 4); assert.equal(result.updated_count, 2); assert.deepEqual(result.ids, [ 1, 3 ]); assert.equal(capture.rows.find(row => row.id === 1).outbound_status, 'fin'); assert.equal(capture.rows.find(row => row.id === 3).outbound_status, 'fin'); }); it('should reject when order ids are missing or invalid', async () => { const { service } = createOutboundService(buildRows(1, () => ({ id: 1, warehouse_id: 100000, step: 1, outbound_status: 'none', delivery_time: null, updated_at: '2026-04-12 00:00:00', }))); await assert.rejects( async () => await service.markOutboundOrder({}), /订单ID不能为空/ ); await assert.rejects( async () => await service.markOutboundOrder({ ids: [ 0, 'abc' ], }), /订单ID不能为空/ ); }); });