'use strict'; const assert = require('node:assert/strict'); const Sequelize = require('sequelize'); const PickService = require('../../../../app/service/systemMethods/pick'); const PickSystemService = require('../../../../app/service/systems/pick'); const { matchesWhere } = require('../../../support/order_service_test_helper'); /** * 创建 pick.query 测试上下文。 * * @param {Object} fixtures 测试数据。 * @return {{ service: PickService }} Method 服务实例。 */ function createHarness(fixtures = {}) { const orderExpressRows = Array.isArray(fixtures.orderExpressRows) ? fixtures.orderExpressRows.map(item => ({ ...item })) : []; const orderRows = Array.isArray(fixtures.orderRows) ? fixtures.orderRows.map(item => ({ ...item })) : []; const pickingRows = Array.isArray(fixtures.pickingRows) ? fixtures.pickingRows.map(item => ({ ...item })) : []; const locationRows = Array.isArray(fixtures.locationRows) ? fixtures.locationRows.map(item => ({ ...item })) : []; const storeWarehouseRows = Array.isArray(fixtures.storeWarehouseRows) ? fixtures.storeWarehouseRows.map(item => ({ ...item })) : []; const findByWhere = (rows, where) => rows.filter(row => matchesWhere(row, where)); const staticOrderModelCallCount = { findOne: 0, findAll: 0, }; const factoryCallWarehouseIds = []; const ctx = { app: { Sequelize, config: {}, }, user: fixtures.withoutUserContext ? {} : { store_id: Number( Object.prototype.hasOwnProperty.call(fixtures, 'storeId') ? fixtures.storeId : 100000 ), }, state: fixtures.withStateUserContext ? { user: { store_id: Number(fixtures.stateStoreId || 0), }, } : {}, logger: { error() {}, }, model: { OrderExpress: { async findOne(options = {}) { const rows = findByWhere(orderExpressRows, options.where || {}); return rows.length > 0 ? { ...rows[0] } : null; }, }, OrderDelivery: { async findOne(options = {}) { staticOrderModelCallCount.findOne += 1; if (fixtures.throwOnStaticOrderModelUse) { throw new Error('不应回退到静态 OrderDelivery.findOne'); } const rows = findByWhere(orderRows, options.where || {}); return rows.length > 0 ? { ...rows[0] } : null; }, async findAll(options = {}) { staticOrderModelCallCount.findAll += 1; if (fixtures.throwOnStaticOrderModelUse) { throw new Error('不应回退到静态 OrderDelivery.findAll'); } return findByWhere(orderRows, options.where || {}).map(item => ({ ...item })); }, }, Picking: { async findAll(options = {}) { return findByWhere(pickingRows, options.where || {}).map(item => ({ ...item })); }, }, LocationSpace: { async findAll(options = {}) { return findByWhere(locationRows, options.where || {}).map(item => ({ ...item })); }, }, StoreWarehouse: { async findOne(options = {}) { const rows = findByWhere(storeWarehouseRows, options.where || {}); return rows.length > 0 ? { ...rows[0] } : null; }, }, }, service: { systems: {}, }, }; if (!fixtures.withoutFactory) { const defaultFactory = { async getCurrentModel(warehouseId) { factoryCallWarehouseIds.push(Number(warehouseId || 0)); if (fixtures.factoryReturnsInvalidModel) { return fixtures.invalidFactoryModel || {}; } return { async findOne(options = {}) { const rows = findByWhere(orderRows, options.where || {}); return rows.length > 0 ? { ...rows[0] } : null; }, async findAll(options = {}) { return findByWhere(orderRows, options.where || {}).map(item => ({ ...item })); }, }; }, }; ctx.model.OrderDeliveryFactory = fixtures.orderFactory || defaultFactory; } ctx.service.systems.pick = new PickSystemService(ctx); return { service: new PickService(ctx), factoryCallWarehouseIds, staticOrderModelCallCount, }; } describe('test/app/service/systemMethods/pick.query.test.js', () => { it('不传 warehouse_id 且存在登录用户仓库上下文时应查询成功', async () => { const { service, factoryCallWarehouseIds } = createHarness({ storeId: 100000, orderExpressRows: [ { order_id: 9001, warehouse_id: 100000, waybill_code: 'WB-9001' }, ], orderRows: [ { id: 9001, warehouse_id: 100000, store_id: 2001, wave_id: 77, wave_no: 'WAVE-77', inspection_status: 'none' }, { id: 9002, warehouse_id: 100000, store_id: 2001, wave_id: 77, wave_no: 'WAVE-77', inspection_status: 'fin' }, ], pickingRows: [ { id: 1, order_id: 9001, order_item_id: 11, storage_id: 501, quantity: 2, space_id: 3001, space_name: 'A-01', status: 'pending' }, { id: 2, order_id: 9001, order_item_id: 12, storage_id: 502, quantity: 1, space_id: 3002, space_name: 'A-02', status: 'pending' }, { id: 3, order_id: 9002, order_item_id: 13, storage_id: 503, quantity: 5, space_id: 3001, space_name: 'A-01', status: 'pending' }, ], locationRows: [ { id: 3001, warehouse_id: 100000, space_code: 'A-01', custom_name: 'A区-01', custom_barcode: 'BC-A01', zone_id: 10, space_status: 'AVAILABLE', is_enabled: true, pick_sequence: 1, pick_priority: 10 }, { id: 3002, warehouse_id: 100000, space_code: 'A-02', custom_name: 'A区-02', custom_barcode: 'BC-A02', zone_id: 10, space_status: 'AVAILABLE', is_enabled: true, pick_sequence: 2, pick_priority: 20 }, ], storeWarehouseRows: [ { id: 88, store_id: 2001, warehouse_id: 100000, store_remask: '货主A', warehouse_remask: '仓库A', auth_status: 2, use_status: 1 }, ], }); const result = await service.query({ waybill_code: 'WB-9001', full: true, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(result.data.wave_no, 'WAVE-77'); assert.equal(result.data.list.length, 3); assert.equal(result.data.summary.total_count, 3); assert.equal(result.data.summary.pending_review_count, 2); assert.equal(result.data.summary.reviewed_count, 1); assert.equal(result.data.summary.quantity_total, 8); assert.ok(result.data.list.every(item => item.location_space)); assert.ok(result.data.list.every(item => item.store_warehouse)); assert.ok(factoryCallWarehouseIds.length >= 2); assert.ok(factoryCallWarehouseIds.every(warehouseId => warehouseId === 100000)); }); it('full_display/is_full 为 false 时仅返回未复核订单拣货信息', async () => { const { service } = createHarness({ storeId: 100000, orderExpressRows: [ { order_id: 9101, warehouse_id: 100000, waybill_code: 'WB-9101' }, ], orderRows: [ { id: 9101, warehouse_id: 100000, store_id: 2002, wave_id: 78, wave_no: 'WAVE-78', inspection_status: 'none' }, { id: 9102, warehouse_id: 100000, store_id: 2002, wave_id: 78, wave_no: 'WAVE-78', inspection_status: 'fin' }, ], pickingRows: [ { id: 11, order_id: 9101, order_item_id: 21, storage_id: 601, quantity: 3, space_id: 4001, space_name: 'B-01', status: 'pending' }, { id: 12, order_id: 9102, order_item_id: 22, storage_id: 602, quantity: 4, space_id: 4002, space_name: 'B-02', status: 'pending' }, ], locationRows: [ { id: 4001, warehouse_id: 100000, space_code: 'B-01', custom_name: 'B区-01', custom_barcode: 'BC-B01', zone_id: 20, space_status: 'AVAILABLE', is_enabled: true, pick_sequence: 1, pick_priority: 10 }, { id: 4002, warehouse_id: 100000, space_code: 'B-02', custom_name: 'B区-02', custom_barcode: 'BC-B02', zone_id: 20, space_status: 'AVAILABLE', is_enabled: true, pick_sequence: 2, pick_priority: 20 }, ], storeWarehouseRows: [ { id: 89, store_id: 2002, warehouse_id: 100000, store_remask: '货主B', warehouse_remask: '仓库B', auth_status: 2, use_status: 1 }, ], }); const result = await service.query({ waybill_code: 'WB-9101', is_full: false, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(result.data.full_display, false); assert.equal(result.data.list.length, 1); assert.equal(result.data.list[0].order_id, 9101); assert.equal(result.data.list[0].inspection_status, 'none'); assert.equal(result.data.summary.total_count, 2); assert.equal(result.data.summary.pending_review_count, 1); assert.equal(result.data.summary.reviewed_count, 1); }); it('ctx.user.store_id 缺失时应返回仓库上下文错误', async () => { const { service } = createHarness({ withoutUserContext: true, }); const result = await service.query({ waybill_code: 'WB-NO-CONTEXT', }); assert.equal(result.flag, 'failure'); assert.equal(result.code, 400); assert.equal(result.message, '缺少当前用户仓库上下文 store_id'); }); it('快递号不属于当前仓库时应返回越界/不存在错误', async () => { const { service } = createHarness({ storeId: 100000, orderExpressRows: [ { order_id: 9301, warehouse_id: 200000, waybill_code: 'WB-OUT-9301' }, ], orderRows: [ { id: 9301, warehouse_id: 200000, store_id: 2003, wave_id: 79, wave_no: 'WAVE-79', inspection_status: 'none' }, ], }); const result = await service.query({ waybill_code: 'WB-OUT-9301', }); assert.equal(result.flag, 'failure'); assert.equal(result.code, 404); assert.equal(result.message, '快递号不存在或不属于当前仓库'); }); it('工厂缺失时应返回明确错误而非回退静态订单表', async () => { const { service } = createHarness({ withoutFactory: true, storeId: 100000, orderExpressRows: [ { order_id: 9401, warehouse_id: 100000, waybill_code: 'WB-9401' }, ], orderRows: [ { id: 9401, warehouse_id: 100000, store_id: 2004, wave_id: 80, wave_no: 'WAVE-80', inspection_status: 'none' }, ], }); const result = await service.query({ waybill_code: 'WB-9401', }); assert.equal(result.flag, 'failure'); assert.equal(result.code, 500); assert.equal(result.message, '订单分表模型工厂不可用'); }); it('工厂返回不可用模型时应返回明确错误', async () => { const { service } = createHarness({ factoryReturnsInvalidModel: true, storeId: 100000, orderExpressRows: [ { order_id: 9501, warehouse_id: 100000, waybill_code: 'WB-9501' }, ], orderRows: [ { id: 9501, warehouse_id: 100000, store_id: 2005, wave_id: 81, wave_no: 'WAVE-81', inspection_status: 'none' }, ], }); const result = await service.query({ waybill_code: 'WB-9501', }); assert.equal(result.flag, 'failure'); assert.equal(result.code, 500); assert.equal(result.message, '订单分表模型不可用'); }); it('waybill + 用户仓库上下文命中时不应回退静态 OrderDelivery 模型', async () => { const { service, staticOrderModelCallCount } = createHarness({ throwOnStaticOrderModelUse: true, storeId: 100000, orderExpressRows: [ { order_id: 9601, warehouse_id: 100000, waybill_code: 'WB-9601' }, ], orderRows: [ { id: 9601, warehouse_id: 100000, store_id: 2006, wave_id: 82, wave_no: 'WAVE-82', inspection_status: 'none' }, ], pickingRows: [ { id: 21, order_id: 9601, order_item_id: 31, storage_id: 701, quantity: 6, space_id: 5001, space_name: 'C-01', status: 'pending' }, ], locationRows: [ { id: 5001, warehouse_id: 100000, space_code: 'C-01', custom_name: 'C区-01', custom_barcode: 'BC-C01', zone_id: 30, space_status: 'AVAILABLE', is_enabled: true, pick_sequence: 1, pick_priority: 10 }, ], storeWarehouseRows: [ { id: 90, store_id: 2006, warehouse_id: 100000, store_remask: '货主C', warehouse_remask: '仓库C', auth_status: 2, use_status: 1 }, ], }); const result = await service.query({ waybill_code: 'WB-9601', full: true, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(staticOrderModelCallCount.findOne, 0); assert.equal(staticOrderModelCallCount.findAll, 0); }); });