'use strict'; const assert = require('node:assert/strict'); const DeliveryOrderService = require('../../../../app/service/systems/delivery_order'); const StockStoreService = require('../../../../app/service/systems/stock_store'); function createHarness({ allowOversell }) { const calls = { bulkCreateArgs: null, }; const cache = new Map(); const stockWarehouseInstance = { id: 9001, store_id: 1000000, warehouse_id: 100000, quantity: 0, locked_quantity: 0, gross_weight: 2, update_at: new Date('2026-04-10T00:00:00Z'), async increment(field, options) { this[field] += Number(options && options.by) || 0; }, async decrement(field, options) { this[field] -= Number(options && options.by) || 0; }, async reload() { return this; }, }; const app = { Sequelize: { Op: { gte: 'gte', }, literal(value) { return value; }, }, logger: { debug() {}, info() {}, warn() {}, error() {}, }, async getCacheAsync(key) { return cache.get(key); }, async setCacheAsync(key, value) { cache.set(key, value); }, async delCacheAsync(key) { cache.delete(key); }, }; const ctx = { app, logger: app.logger, user: { store_id: 1000000, employees_id: 10001, employees_name: '测试员', }, model: { StockStore: { async findByPk(id) { if (Number(id) !== 1) { return null; } return { id: 1, stock_id: stockWarehouseInstance.id, warehouse_code: 'WH-001', item_code: 'SKU-ZERO', item_name: '零库存商品', goods_code: 'GOODS-ZERO', sku_property: '颜色:黑', stock_unit: '件', gross_weight: 2, net_weight: 1.5, length: 10, width: 20, height: 30, volume: 6000, retail_price: 88.5, barcode: 'BAR-ZERO', brand_name: '测试品牌', category_id: 9, category_name: '测试分类', item_type: 'normal', }; }, }, StockWarehouse: { async findOne({ raw } = {}) { if (raw) { return { id: stockWarehouseInstance.id, store_id: stockWarehouseInstance.store_id, warehouse_id: stockWarehouseInstance.warehouse_id, quantity: stockWarehouseInstance.quantity, locked_quantity: stockWarehouseInstance.locked_quantity, gross_weight: stockWarehouseInstance.gross_weight, }; } return stockWarehouseInstance; }, }, StoreWarehouse: { async findOne() { return { id: 77, policy_config: null, auth_status: 2, use_status: 1, }; }, }, OrderItemsFactory: { async getCurrentModel() { return { async bulkCreate(rows, options) { calls.bulkCreateArgs = { rows, options, }; return rows; }, }; }, }, }, service: { systems: {}, }, }; app.model = ctx.model; ctx.service.systems.storeWarehouse = { async getEffectivePolicyConfigByContractId() { return { allow_oversell: allowOversell ? 1 : 0, }; }, }; const stockStoreService = new StockStoreService(ctx); ctx.service.systems.stockStore = stockStoreService; const deliveryOrderService = new DeliveryOrderService(ctx); const transaction = { LOCK: { UPDATE: 'UPDATE', }, }; return { app, ctx, calls, deliveryOrderService, stockStoreService, stockWarehouseInstance, transaction, }; } function createOrderLine() { return { itemId: 1, itemCode: 'SKU-ZERO', itemName: '零库存商品', planQty: 1, inventoryType: 'ZP', orderLineNo: '1', }; } describe('test/app/service/systems/delivery_order.oversell.test.js', () => { it('should allow zero-available stock to proceed when oversell is enabled', async () => { const { calls, deliveryOrderService, stockStoreService, stockWarehouseInstance, transaction } = createHarness({ allowOversell: true, }); const stockResult = await stockStoreService.getItemStorages(1, transaction); assert.equal(stockResult.success, true); assert.equal(stockResult.data.storage.canUse, 0); assert.equal(stockResult.data.allow_oversell, true); const result = await deliveryOrderService.addOrderLines(createOrderLine(), 12345, { warehouseId: 100000, appKey: 'TEST', storeId: 1000000, }, transaction); assert.equal(result.length, 1); assert.equal(result.stock_count, 1); assert.equal(result.item_total_count, 1); assert.equal(calls.bulkCreateArgs.rows.length, 1); assert.equal(calls.bulkCreateArgs.rows[0].item_id, 1); assert.equal(calls.bulkCreateArgs.rows[0].stock_id, 9001); assert.deepEqual(calls.bulkCreateArgs.rows[0].item_snapshot, { id: 1, stock_id: 9001, item_code: 'SKU-ZERO', item_name: '零库存商品', goods_code: 'GOODS-ZERO', sku_property: '颜色:黑', barcode: 'BAR-ZERO', brand_name: '测试品牌', category_id: 9, category_name: '测试分类', item_type: 'normal', stock_unit: '件', inventory_type: 'ZP', retail_price: 88.5, gross_weight: 2, net_weight: 1.5, length: 10, width: 20, height: 30, volume: 6000, }); assert.equal(calls.bulkCreateArgs.rows[0].storage_id, undefined); assert.equal(calls.bulkCreateArgs.rows[0].space_id, undefined); assert.equal(calls.bulkCreateArgs.rows[0].stock_snapshot, undefined); assert.equal(stockWarehouseInstance.locked_quantity, 1); }); it('should reject zero-available stock when oversell is disabled', async () => { const { calls, deliveryOrderService, stockStoreService, stockWarehouseInstance, transaction } = createHarness({ allowOversell: false, }); const stockResult = await stockStoreService.getItemStorages(1, transaction); assert.equal(stockResult.success, true); assert.equal(stockResult.data.storage.canUse, 0); assert.equal(stockResult.data.allow_oversell, false); await assert.rejects( deliveryOrderService.addOrderLines(createOrderLine(), 12345, { warehouseId: 100000, appKey: 'TEST', storeId: 1000000, }, transaction), error => String(error.message || error).includes('库存不足') ); assert.equal(calls.bulkCreateArgs, null); assert.equal(stockWarehouseInstance.locked_quantity, 0); }); });