import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; import { orderVisibleToDeliveryManagementSql, orderVisibleToDeliverySql } from '../src/services/supplierSkipInbound'; const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8'); const fulfillment = read('../src/services/orderWorkflowFulfillmentService.ts'); const submit = read('../src/services/customerOrderSubmitService.ts'); const picking = read('../src/services/pickingService.ts'); const queryService = read('../src/services/deliveryTaskQueryService.ts'); const navBadge = read('../src/services/navBadgeService.ts'); const cost = read('../src/services/costAllocationService.ts'); const migration = read('../../migrations/20261083_delivery_task_early_void.sql'); function body(source: string, name: string): string { const start = source.indexOf(`export async function ${name}`); expect(start, name).toBeGreaterThanOrEqual(0); const rest = source.slice(start + 1); const next = rest.search(/export (async )?function /); return next >= 0 ? source.slice(start, start + 1 + next) : source.slice(start); } describe('T1 早期配送/分拣任务地基', () => { it('AC-01/AC-06:下单 COMMIT 后建分拣+配送;early 分支不派员、不通知', () => { expect(submit).toContain('ensurePickingTaskOnOrderSubmit(orderCode, input.branchCode)'); expect(submit).toContain('ensureEarlyDeliveryTaskForOrder(orderCode, input.branchCode)'); const commitAt = submit.indexOf("await client.query('COMMIT');\n\n /**"); expect(submit.indexOf('ensurePickingTaskOnOrderSubmit(orderCode')).toBeGreaterThan(commitAt); expect(submit.indexOf('ensureEarlyDeliveryTaskForOrder(orderCode')).toBeGreaterThan(commitAt); const ensure = body(fulfillment, 'ensureDeliveryTaskForOrder'); expect(ensure).toContain('options?: { early?: boolean }'); expect(ensure).toContain('options?.early'); expect(ensure).toContain('options?.early ? null : await resolveDeliveryPersonForOrder'); expect(ensure).toContain('if (!options?.early)'); expect(ensure).toContain('notifyDeliveryTaskCreated'); expect(ensure).toContain('person_code, delivery_id'); }); it('AC-02~04/AC-25:early 复用五道守卫;可见性只放宽管理列表/角标', () => { const early = body(fulfillment, 'ensureEarlyDeliveryTaskForOrder'); expect(early).toContain('ensureDeliveryTaskForOrder(code, branch, undefined, { early: true })'); expect(early).toContain('isOrderOnlyDirectDelivery(code)'); expect(early).toContain('isDirectToDeliveryEnabled(branch)'); const ensure = body(fulfillment, 'ensureDeliveryTaskForOrder'); for (const guard of ['isRetailOrder', 'isOrderOnlyDirectDelivery', 'isOrderIndependentInbound', 'isSelfPickupOrder', 'isOrderFullySkipInbound']) { expect(ensure, guard).toContain(guard); } expect(ensure).toContain("DELETE FROM delivery_tasks"); const management = orderVisibleToDeliveryManagementSql('o'); expect(management).toContain('dt_mgmt.voided_at IS NULL'); // AC-12 可见性:旧可见/active 任务 OR;仅 <50 的 voided 才隐藏 expect(management).toContain('voided_at IS NOT NULL'); expect(management).toContain('COALESCE(o.order_status, 0) < 50'); expect(management).not.toContain('dt_active'); expect(management).toContain(orderVisibleToDeliverySql('o')); expect(queryService).toContain("orderVisibleToDeliveryManagementSql('o')"); expect(navBadge).toContain("orderVisibleToDeliveryManagementSql('o')"); // 司机端三个查询仍用旧口径 // T3 后司机端也放开早期任务;仍保留 stats/旧口径查询 expect((queryService.match(/orderVisibleToDeliverySql\('o'\)/g) || []).length).toBeGreaterThanOrEqual(1); expect((queryService.match(/orderVisibleToDeliveryManagementSql\('o'\)/g) || []).length).toBeGreaterThanOrEqual(3); }); it('AC-05/AC-11:防重沿用 advisory lock/唯一索引回读;查询带 voided_at IS NULL', () => { const ensure = body(fulfillment, 'ensureDeliveryTaskForOrder'); expect(ensure).toContain('pg_advisory_xact_lock'); expect(ensure).toContain('23505'); expect(ensure).toContain('WHERE order_code = $1 AND voided_at IS NULL LIMIT 1'); }); it('AC-12:early 任务管理可见;迁移加 void 列/部分索引且不动全唯一索引', () => { expect(migration).toContain('voided_at TIMESTAMPTZ'); expect(migration).toContain('void_reason VARCHAR(100)'); expect(migration).toContain('idx_delivery_tasks_active_order'); expect(migration).toContain('WHERE voided_at IS NULL'); expect(migration).not.toContain('DROP INDEX'); expect(migration).not.toContain('delivery_loading_backfill_logs'); }); it('AC-25/成本:角标与列表同口径;里程只算 voided_at IS NULL 且已发车任务', () => { expect(navBadge).toContain('COALESCE(dt.task_status, 10) < 30'); expect(cost).toContain('dt.voided_at IS NULL'); expect(cost).toContain('COALESCE(dt.task_status, 10) >= 20'); expect(cost).not.toContain('COUNT(*)::int AS task_count FROM delivery_tasks dt WHERE dt.order_code = o.order_code\n )'); }); it('AC-26:权限/隔离仍由原有路由与租户上下文保证', () => { const routes = read('../src/modules/workflow/routes/deliveryRoutes.ts'); expect(routes).toContain('delivery_manage'); }); it('分拣:下单后建单仅启用 workflow;不引入“未到45禁止完成”闸', () => { const ensure = body(picking, 'ensurePickingTaskOnOrderSubmit'); expect(ensure).toContain('isPickingWorkflowEnabled(branch)'); expect(ensure).toContain('createPickingTask({ orderCode: code, branchCode: branch }, null)'); expect(ensure).not.toContain("order_status < 45"); expect(picking).not.toContain('assertPickingTaskReadyForCompletion'); }); });