import { readFileSync } from 'node:fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; import { assertAmendDemandMaterialQty, planLifoPickReturns, } from '../src/services/processingDemandPickAmendService'; describe('assertAmendDemandMaterialQty', () => { it('rejects zero and qty below net weight', () => { expect(() => assertAmendDemandMaterialQty({ newQty: 0, currentQty: 9, netWeight: 2 })).toThrow(/大于 0/); expect(() => assertAmendDemandMaterialQty({ newQty: 1, currentQty: 9, netWeight: 2 })).toThrow(/不能小于已入库净重/); expect(assertAmendDemandMaterialQty({ newQty: 5, currentQty: 9, netWeight: 2 }).delta).toBe(-4); expect(assertAmendDemandMaterialQty({ newQty: 11, currentQty: 9, netWeight: 2 }).delta).toBe(2); }); }); describe('planLifoPickReturns', () => { it('shrinks newest pick first and splits cost', () => { const slices = planLifoPickReturns( [ { id: 'new', sku_code: 'RAW2', qty: 4, material_cost: 8 }, { id: 'old', sku_code: 'RAW1', qty: 5, material_cost: 10 }, ], 6 ); expect(slices).toEqual([ { id: 'new', sku_code: 'RAW2', qty: 4, cost: 8, remainingQty: 0, remainingCost: 0 }, { id: 'old', sku_code: 'RAW1', qty: 2, cost: 4, remainingQty: 3, remainingCost: 6 }, ]); }); it('rejects return above picked qty', () => { expect(() => planLifoPickReturns([{ id: 'a', sku_code: 'S', qty: 2, material_cost: 4 }], 3)).toThrow( /超过已领料/ ); }); }); describe('amend vs existing pick/return', () => { it('does not call pickDemandSheetMaterial or void returnDemandSheetMaterials', () => { const amend = readFileSync( path.join(__dirname, '../src/services/processingDemandPickAmendService.ts'), 'utf8' ); expect(amend).not.toContain('pickDemandSheetMaterial('); expect(amend).not.toContain('returnDemandSheetMaterials('); expect(amend).toContain('process_status = $5'); expect(amend).toContain('overwriteInboundCostPrice: true'); expect(amend).not.toContain('PROCESS_STATUS.inProgress'); }); it('keeps completed pick path closed and settle can overwrite inbound batch cost', () => { const demand = readFileSync( path.join(__dirname, '../src/services/processingDemandSheetService.ts'), 'utf8' ); expect(demand).toContain('需求单已关闭'); expect(demand).toContain('已完工且已有料仍拒追加'); const settleStart = demand.indexOf('export async function settleDemandSheetCostOnCompletion'); const settleSrc = demand.slice(settleStart, demand.indexOf('export async function recalcDemandSheetsForOrder')); expect(settleSrc).toContain('overwriteInboundCostPrice'); expect(settleSrc).toContain('$4::boolean'); }); it('PATCH material-qty requires admin and processing_manage', () => { const routes = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/inspectionRoutes.ts'), 'utf8' ); expect(routes).toContain("/processing/demand/:taskId/material-qty"); expect(routes).toContain('amendDemandSheetMaterialQty'); expect(routes).toContain('isSuperAdmin'); expect(routes).toContain('isBranchAdmin'); const page = readFileSync( path.join(__dirname, '../../src/pages/inspection/ProcessingManagement.tsx'), 'utf8' ); expect(page).toContain('改领料'); expect(page).toContain('canAmendCompletedPick'); expect(page).toContain('amendDemandSheetMaterialQtyApi'); }); });