import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8'); const loading = read('../src/services/deliveryLoadingService.ts'); const backfill = read('../src/services/deliveryLoadingBackfillService.ts'); const fulfillment = read('../src/services/orderWorkflowFulfillmentService.ts'); const queryService = read('../src/services/deliveryTaskQueryService.ts'); const migration = read('../../migrations/20261084_delivery_loading_backfill_logs.sql'); const registration = read('../../migrations/20261074_standalone_processing_outputs.sql'); const ledger = read('../src/services/mainSystemPrepLedger.ts'); const visibility = read('../src/services/supplierSkipInbound.ts'); const outbound = read('../src/services/inventoryOutboundService.ts'); const pickingSrc = read('../src/services/pickingService.ts'); const atomicDoc = read('../../../.team/early-pickup-delivery/t3-atomicity.md'); const print = read('../src/services/deliveryPrintLineService.ts'); const routing = read('../src/services/deliveryRoutingService.ts'); const mobile = read('../src/routes/mobile/mobileDeliveryRoutes.ts'); const orderAuto = read('../src/services/orderAutoAdvanceService.ts'); const directDelivery = read('../src/services/directToDeliveryService.ts'); const processing = read('../src/services/processingTaskService.ts'); const pickingConfig = read('../src/services/pickingConfigService.ts'); describe('T3 早期任务核货/发车/补录', () => { it('AC-13:核货不再要求 order_status>=50;早期显示全部行', () => { expect(loading).not.toContain('&& orderStatus >= 50'); expect(loading).not.toContain('orderStatus >= 50 &&'); expect(loading).toContain('const earlySkipOnly = false;'); }); it('AC-14/15/19/20:核货前补上游,30 只完成备货不写入库', () => { expect(loading).toContain('backfillLoadingItemUpstream({'); expect(loading).toContain('taskCode: input.taskCode'); const branch30 = backfill.slice(backfill.indexOf('if (orderStatus < 40)'), backfill.indexOf('const inspectionResult')); expect(branch30).toContain('UPDATE supplier_preparations'); expect(branch30).toContain('finalizeAutoCompletedSupplierPreparations'); expect(branch30).toContain("inventoryAction: 'no_inventory'"); expect(branch30).not.toContain('submitInspectionCompliance'); expect(branch30).not.toContain('INSERT INTO inventory'); expect(backfill).toContain('submitInspectionCompliance('); expect(backfill).not.toContain('INSERT INTO inventory'); expect(backfill).not.toContain('allowBypassPrepackagedCheck'); // AC-21:ensureInspectionsForOrder 前必须已有照片/成本预检;失败清理新建 pending const preflightAt = backfill.indexOf('isCategoryPhotoInspectionRequired'); const ensureAt = backfill.indexOf('ensureInspectionsForOrder(orderCode'); expect(preflightAt).toBeGreaterThan(0); expect(preflightAt).toBeLessThan(ensureAt); expect(backfill).toContain('backfill_photo_required'); expect(backfill).toContain('backfill_cost_missing'); expect(backfill).toContain('cleanupNewPendingInspections'); expect(backfill).toContain('backfill_sale_price_missing'); expect(backfill).toContain('backfill_picking_state_invalid'); // N-1:无 picking 行先同步,仍无则阻断,不伪造完成分拣行 expect(backfill).toContain('syncOpenPickingItemsForOrder'); expect(backfill).toContain('backfill_picking_missing'); expect(loading.indexOf('backfillLoadingItemUpstream({')).toBeLessThan(loading.indexOf('ensureDeliveryLoadingPickTask(String(task.order_code))')); expect(backfill).toContain('审计写入失败'); // P1-6:未分配任务只允许同分公司配送员,不能任意人操作 expect(loading).toContain('无权操作其他分公司的配送任务'); expect(fulfillment).toContain('无权操作该配送任务'); }); it('AC-16:已核货的 30/40/45 先补 50 再 60;失败回滚', () => { const adv = fulfillment.slice(fulfillment.indexOf('export async function tryAdvanceOrderToDelivering')); expect(adv).toContain('loading_completed_at IS NOT NULL'); expect(adv).toContain('UPDATE orders SET order_status = 50'); expect(adv).toContain('UPDATE orders SET order_status = 60 WHERE order_code = $1 AND order_status = 50'); expect(adv).toContain('withOrderLock'); expect(adv).toContain('status < 50 ? [status, 50] : 50'); expect(adv).toContain('noInventory: skipDepartureInventory'); expect(adv).toContain('has_stock_cover'); expect(adv).toContain('hasStockCover'); expect(adv).toContain('delivery_early_status_advance'); expect(adv).toContain('syncDeliveryTaskForOrder(orderCode, branchCodeForWriteback)'); expect(adv).toContain("operationType: 'delivery_task_claim'"); expect(adv).toContain('auto_advanced_from_status'); expect(backfill).toContain('auto_advanced_from_status'); expect(backfill).toContain('const skipAutoInbound = isPickingOnly40 || isPickingOnly45'); expect(backfill).toContain('isPickingOnly40 = autoAdvancedFrom === 40'); expect(backfill).toContain('isPickingOnly45 = autoAdvancedFrom === 45'); expect(backfill).toContain('let shouldBackfillInbound ='); expect(backfill).toContain('hasPendingInboundSource'); expect(backfill).toContain('pendingSources.preps'); expect(backfill).toContain('closePendingUpstreamWithoutInbound'); expect(backfill).toContain('DELETE FROM inspections'); expect(outbound).toContain('fallbackToRequiredQty'); expect(outbound).toContain('options?.fallbackToRequiredQty ? Number(row.required_qty)'); expect(adv).toContain('fallbackToRequiredQty:'); expect(adv).toContain('auto_advanced_from_status) === 30'); expect(adv).toContain('UPDATE orders SET order_status = 50 WHERE order_code = $1 AND order_status < 50 RETURNING order_code'); }); it('AC-23:司机/小程序列表也使用 management 可见性', () => { const oldCount = (queryService.match(/orderVisibleToDeliverySql\('o'\)/g) || []).length; const managementCount = (queryService.match(/orderVisibleToDeliveryManagementSql\('o'\)/g) || []).length; expect(managementCount).toBeGreaterThanOrEqual(3); expect(oldCount).toBeLessThanOrEqual(1); expect(print).toContain('voided_at IS NULL'); expect(routing).toContain('dt.voided_at IS NULL'); expect(mobile).toContain('dt.voided_at IS NULL'); expect(ledger).toContain('待取货残留配送任务清理'); expect(visibility).toContain('COALESCE(${orderAlias}.order_status, 0) < 50'); expect(visibility).toContain('skuDirectDeliveryAppliesSql'); expect(print).toContain('deliveryPrintReadySql'); expect(print).toContain('loading_completed_at IS NOT NULL'); expect(routing).toContain('COALESCE(dt.task_status, 10) >= 20 OR dt.loading_completed_at IS NOT NULL'); expect(mobile).toContain('dt.loading_completed_at IS NOT NULL OR dt.task_status >= 20'); }); it('AC-28:审计表字段齐全、登记 tenant_business_tables、全部租户 + RLS', () => { for (const field of ['missing_stage', 'responsible_code', 'operator_id', 'confirmed_qty', 'inbound_qty', 'picking_qty', 'inventory_action']) { expect(migration, field).toContain(field); } expect(migration).toContain('delivery_loading_backfill_logs'); expect(migration).toContain('ENABLE ROW LEVEL SECURITY'); expect(migration).toContain('tenant_business_tables'); expect(migration).toContain("'delivery_loading_backfill_logs',"); expect(registration).toContain("'delivery_tasks',"); expect(migration).toContain("nspname LIKE 'tenant"); expect(backfill).toContain('INSERT INTO delivery_loading_backfill_logs'); expect(backfill).toContain("? 'inbound_then_deduct'"); expect(backfill).toContain("? 'inbound'"); expect(backfill).toContain(": 'no_inventory'"); expect(backfill).toContain("missingStage: 'prep'"); expect(atomicDoc).toContain('prep'); }); it('AC-27:补分拣复用 confirmPickingComplete/update,不直接写库存', () => { expect(backfill).toContain('confirmPickingComplete'); expect(backfill).toContain('updatePickingItemProgress'); expect(backfill).not.toContain('confirmPickingComplete({\n pickCode,\n reviewerId: input.operatorId || null,\n skipAutoInbound'); }); it('不改财务公式/状态链常量', () => { expect(read('../src/modules/kernel/orderStatus.ts')).not.toContain('bumpedFromStatus'); expect(read('../src/services/financeNetProfitFormula.ts')).not.toContain('delivery_loading_backfill_logs'); }); it('无可用待验收时按无入库直送收口,不阻断核货', () => { expect(backfill).toContain('无可用待验收单'); expect(backfill).toContain('inventoryAction: \'no_inventory\''); expect(backfill).toContain('shouldBackfillInbound = false'); expect(backfill).not.toContain('throw error; // backfill_inspection_missing'); }); it('分拣完成强制到 50 时也触发主系统备货完成回写', () => { const idx = pickingSrc.indexOf('async function forceOrderReadyForDeliveryAfterPicking'); const fn = pickingSrc.slice(idx, pickingSrc.indexOf('export async function recoverOrdersReadyForDeliveryAfterPicking', idx)); expect(fn).toContain('enqueueMainSystemWritebackForReadyOrder'); expect(fn).toContain('orderCode: code'); }); it('所有自动到 50 的路径都触发主系统备货完成回写', () => { for (const src of [pickingSrc, orderAuto, directDelivery, processing, pickingConfig]) { expect(src).toContain('enqueueMainSystemWritebackForReadyOrder'); } }); });