import { readFileSync } from 'node:fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; /** * 独立入库(新增自采商品/独立库存入库)走验收流程: * 创建只建待验单(inspections 待验行 + independent_inbound 标记),验收称重后才写库存。 */ describe('独立入库待验流程', () => { it('创建链路只建待验单,不直接写库存', () => { const spi = readFileSync( path.join(__dirname, '../src/services/selfPurchaseStockInboundService.ts'), 'utf8' ); const createStart = spi.indexOf('export async function createSelfPurchaseStockInbound'); const healStart = spi.indexOf('export async function healOrphanIndependentInboundsAfterPass'); const createSrc = spi.slice(createStart, healStart > 0 ? healStart : undefined); expect(createSrc).toContain('createIndependentInboundInspection'); expect(createSrc).toContain('inspection_id'); expect(createSrc).toContain('getIndependentInboundFlowConfig'); expect(createSrc).toContain('inboundFromWeigh'); const helper = readFileSync( path.join(__dirname, '../src/services/independentInboundService.ts'), 'utf8' ); expect(helper).toContain('independent_inbound'); expect(helper).toContain('INSERT INTO inspections'); expect(helper).toContain("'purchase'"); expect(helper).toContain('inspectionVisibleInListSql'); // 库存入库路由恒直收;开关只作用在新增自采 / 加单独立入库 const stockInbound = readFileSync( path.join(__dirname, '../src/services/stockInboundService.ts'), 'utf8' ); expect(stockInbound).toContain('export async function inboundFromWeigh'); const webRoute = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/inventoryRoutes.ts'), 'utf8' ); expect(webRoute).toContain('inboundFromWeigh'); expect(webRoute).not.toContain('getIndependentInboundFlowConfig'); expect(webRoute).not.toContain('createPendingStockInbound'); const mobileRoute = readFileSync( path.join(__dirname, '../src/routes/mobile/mobileInventoryRoutes.ts'), 'utf8' ); expect(mobileRoute).toContain('inboundFromWeigh'); expect(mobileRoute).not.toContain('getIndependentInboundFlowConfig'); expect(mobileRoute).not.toContain('createPendingStockInbound'); // 迁移:spi 表关联待验单 const migration = readFileSync( path.join(__dirname, '../../migrations/20260857_independent_inbound_pending.sql'), 'utf8' ); expect(migration).toContain('inspection_id'); }); it('验收完成回填待入库记录并补持有成本,拒收拦截不结案', () => { const svc = readFileSync( path.join(__dirname, '../src/services/inspectionComplianceService.ts'), 'utf8' ); expect(svc).toContain('isIndependentInboundDetails'); // 质检报告硬闸豁免(独立入库无采购单可挂报告) expect(svc).toContain('if (!independentInbound && !options?.skipQualityReportGate)'); // 完工:实称为准回填 spi + 持有成本 expect(svc).toContain('UPDATE self_purchase_stock_inbounds'); expect(svc).toContain('WHERE inspection_id = $1 AND inventory_id IS NULL'); expect(svc).toContain('(!passThrough || independentInbound)'); expect(svc).toContain('inbound_batch_no = $9'); expect(svc).toContain('验收入库 ${input.id}'); expect(svc).toContain('upsertInventoryHoldingCost'); // 硬门槛(2026-09-15):不合格不得入库;仅 reject_fulfillment 走退供。 // 独立入库待验退回自采。见 tests/inspectionInboundRequiresPass.test.ts expect(svc).toContain('reject_fulfillment'); expect(svc).toContain('inspection_fail_cannot_inbound'); expect(svc).toContain('independent_inbound_returned'); expect(svc).toContain('bounceIndependentInboundPendingInspection'); expect(svc).not.toContain('independent_inbound_no_reject'); expect(svc).toContain("const isRejected = input.reject_fulfillment === true"); expect(svc).toContain('self_purchase_stock_inbounds spi'); expect(svc).toContain('independentInbound && !(purchasePrice > 0)'); expect(svc).toContain("code: 'invalid_cost'"); const gateStart = svc.indexOf('const isRejected = input.reject_fulfillment === true'); const completeUpdate = svc.indexOf('SET actual_qty = $2, quality_rating = $3'); expect(gateStart).toBeGreaterThan(0); expect(completeUpdate).toBeGreaterThan(gateStart); }); it('待验列表可见性放宽到独立入库行', () => { const query = readFileSync( path.join(__dirname, '../src/services/inspectionQueryService.ts'), 'utf8' ); expect(query).toContain('inspectionVisibleInListSql'); const pendingStart = query.indexOf('MOBILE_INSPECTION_PENDING_SQL = `'); const pendingSrc = query.slice(pendingStart, pendingStart + 400); expect(pendingSrc).toContain('inspectionVisibleInListSql'); expect(query).toContain('NULLIF(i.purchase_price, 0)'); expect(query).toContain('spi.inspection_id = i.id'); expect(query).toContain('inbound_unit_price'); const weigh = readFileSync( path.join(__dirname, '../src/services/weighStationService.ts'), 'utf8' ); expect(weigh).toContain('inspectionVisibleInListSql'); expect(weigh).toContain('independent_inbound'); expect(weigh).toContain("processing_details->>'independent_inbound'"); expect(weigh).toContain('THEN 0 ELSE 1 END'); expect(weigh).toContain('INSPECTION_PENDING_ORDER'); const mobile = readFileSync( path.join(__dirname, '../src/routes/mobile/mobileWarehouseRoutes.ts'), 'utf8' ); expect(mobile).toContain('independent_inbound'); }); it('孤儿补写幂等:回写批次号并按 SKU+分公司+数量兜底判重', () => { const spi = readFileSync( path.join(__dirname, '../src/services/selfPurchaseStockInboundService.ts'), 'utf8' ); expect(spi).toContain('inspection_id'); expect(spi).toContain('独立入库补写%'); expect(spi).toContain('SET inbound_batch_no = COALESCE'); expect(spi).toContain('FOR UPDATE'); }); it('待入库 SPI 行归采购待办组,历史按验收状态区分', () => { const query = readFileSync( path.join(__dirname, '../src/services/purchaseOrderQueryService.ts'), 'utf8' ); expect(query).toContain('spi.inventory_id IS NULL'); expect(query).toContain('inbound_pending'); expect(query).toContain('healOrphanIndependentInboundsAfterPass'); const insp = readFileSync( path.join(__dirname, '../src/services/inspectionQueryService.ts'), 'utf8' ); expect(insp).toContain('healOrphanIndependentInboundsAfterPass'); const inv = readFileSync( path.join(__dirname, '../src/services/inventoryQueryService.ts'), 'utf8' ); expect(inv).toContain('healOrphanIndependentInboundsAfterPass'); const pick = readFileSync( path.join(__dirname, '../src/services/processingDemandSheetService.ts'), 'utf8' ); expect(pick).toContain('healOrphanIndependentInboundsAfterPass'); const spi = readFileSync( path.join(__dirname, '../src/services/selfPurchaseStockInboundService.ts'), 'utf8' ); expect(spi).toContain('inboundDone ? 30 : 10'); // 待入库行可修改/撤销:操作同步待验单而非库存批次 expect(spi).toContain('UPDATE inspections'); expect(spi).toContain('DELETE FROM inspections'); expect(spi).toContain('healOrphanIndependentInboundsAfterPass'); expect(spi).toContain('独立入库补写'); expect(spi).toContain('inspection_id'); expect(spi).toContain("l.remark LIKE '独立入库补写%'"); expect(spi).toContain('SET inbound_batch_no = COALESCE'); expect(spi).toContain("AND ABS(COALESCE(inv.quantity, 0) - COALESCE(i.actual_qty, 0)) <= 1e-6"); expect(spi).toContain('pg_advisory_xact_lock'); expect(spi).toContain('healNeedsNewInventoryRow'); expect(spi).toContain("from './independentInboundHeal'"); expect(spi).toContain('insertedNewInventory'); }); it('补写判定:已有库存或流水绑验收单则不再插批次', async () => { const { healNeedsNewInventoryRow } = await import('../src/services/independentInboundHeal'); expect(healNeedsNewInventoryRow({ existingInventoryId: null, inboundLogForInspection: false })).toBe( true ); expect( healNeedsNewInventoryRow({ existingInventoryId: 'inv-1', inboundLogForInspection: false }) ).toBe(false); expect( healNeedsNewInventoryRow({ existingInventoryId: null, inboundLogForInspection: true }) ).toBe(false); }); });