'use strict'; const assert = require('node:assert/strict'); const ReceivingService = require('../../../../app/service/systems/storage/receiving'); function formatDate(date, formatStr = 'YYYY-MM-DD HH:mm:ss') { const targetDate = date instanceof Date ? date : new Date(date); const year = targetDate.getFullYear(); const month = String(targetDate.getMonth() + 1).padStart(2, '0'); const day = String(targetDate.getDate()).padStart(2, '0'); const hours = String(targetDate.getHours()).padStart(2, '0'); const minutes = String(targetDate.getMinutes()).padStart(2, '0'); const seconds = String(targetDate.getSeconds()).padStart(2, '0'); return formatStr .replace('YYYY', year) .replace('MM', month) .replace('DD', day) .replace('HH', hours) .replace('mm', minutes) .replace('ss', seconds); } function createServiceHarness() { const calls = { detailUpdate: 0, updateReceivingStats: 0, lastUpdateData: null, }; const model = { ReceivingDetail: { async findOne() { return { id: 501, receiving_id: 123, product_name: '测试商品', actual_quantity: 1, barcode: '', system_product_id: null, box_remainder: 0, box_weight: '0.00', updated_at: new Date('2026-04-01T00:00:00Z'), }; }, async update(updateData) { calls.detailUpdate += 1; calls.lastUpdateData = updateData; }, }, Receiving: { async findOne() { return { id: 123, warehouse_id: 100000, store_id: 1000000, store_name: '测试货主', status: 'receiving', }; }, }, StockWarehouse: { async findOne() { throw new Error('should not be called'); }, }, LocationSpace: { async findOne() { throw new Error('should not be called'); }, }, StoreWarehouse: { buildStoreStockInclude() { return {}; }, }, }; const ctx = { user: { store_id: 100000, }, logger: { info() {}, warn() {}, error() {}, }, service: { systems: { operationLog: { async createOperationLog() { calls.operationLog = (calls.operationLog || 0) + 1; }, }, }, }, model, app: { model, Sequelize: { Op: { or: 'or', }, }, formatDate, }, }; const service = new ReceivingService(ctx); return { service, calls, }; } describe('test/app/service/systems/storage.receiving.detailChange.test.js', () => { it('should reject expired expiry_date before updating receiving detail', async () => { const { service, calls } = createServiceHarness(); const result = await service.detailChange({ id: 501, receiving_id: 123, actual_quantity: 1, expiry_date: '2000-01-01', }); assert.equal(result.code, 400); assert.equal(result.message, '到期日期已过期,不能录入收货'); assert.equal(calls.detailUpdate, 0); assert.equal(calls.operationLog || 0, 0); }); it('should allow same-day expiry_date when provided as Date input', async () => { const { service, calls } = createServiceHarness(); service.updateReceivingStats = async () => { calls.updateReceivingStats += 1; }; const result = await service.detailChange({ id: 501, receiving_id: 123, actual_quantity: 1, expiry_date: new Date(), }); assert.equal(result.code, 200); assert.equal(result.message, '修改成功'); assert.equal(calls.detailUpdate, 1); assert.equal(calls.updateReceivingStats, 1); assert.equal(calls.operationLog, 1); }); it('should persist and return box_remainder and box_weight when provided', async () => { const { service, calls } = createServiceHarness(); service.updateReceivingStats = async () => { calls.updateReceivingStats += 1; }; const result = await service.detailChange({ id: 501, receiving_id: 123, actual_quantity: 3, box_remainder: 2, box_weight: 12.5, }); assert.equal(result.code, 200); assert.equal(calls.detailUpdate, 1); assert.equal(calls.lastUpdateData.box_remainder, 2); assert.equal(calls.lastUpdateData.box_weight, 12.5); assert.equal(result.data.box_remainder, 2); assert.equal(result.data.box_weight, 12.5); }); });