import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8'); const manage = read('../src/services/orderManageItemsService.ts'); const fulfillment = read('../src/services/orderWorkflowFulfillmentService.ts'); const picking = read('../src/services/pickingService.ts'); const submit = read('../src/services/customerOrderSubmitService.ts'); const visibility = read('../src/services/supplierSkipInbound.ts'); const queryService = read('../src/services/deliveryTaskQueryService.ts'); const nav = read('../src/services/navBadgeService.ts'); 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('T2 改单同步早期配送/分拣', () => { it('AC-07/08/09:syncOrderManageItems COMMIT 后同步分拣行与配送单', () => { expect(manage).toContain('syncOpenPickingItemsForOrder(orderCode, order.branch_code || null)'); expect(manage).toContain('syncDeliveryTaskForOrder(orderCode, order.branch_code || null)'); const commitAt = manage.indexOf("await client.query('COMMIT');"); expect(manage.indexOf('syncOpenPickingItemsForOrder(orderCode')).toBeGreaterThan(commitAt); expect(manage.indexOf('syncDeliveryTaskForOrder(orderCode')).toBeGreaterThan(commitAt); const sync = body(picking, 'syncOpenPickingItemsForOrder'); expect(sync).toContain('pick_status < 40'); expect(sync).toContain('pick_status < 40'); expect(sync).toContain('insertPickingItems(pool, pickCode, missing, branch)'); expect(sync).toContain('includeExtraDish: true'); expect(sync).toContain('picked_qty'); expect(sync).toContain('loading_photos'); expect(sync).toContain('outbound_photos'); expect(sync).not.toContain('INSERT INTO picking_tasks'); }); it('AC-10/11:全直送/待取货等 void 未开始任务;恢复时复用原行清 void', () => { const sync = body(fulfillment, 'syncDeliveryTaskForOrder'); for (const guard of ['isRetailOrder', 'isOrderOnlyDirectDelivery', 'isOrderIndependentInbound', 'isSelfPickupOrder', 'isDirectToDeliveryEnabled', 'isOrderFullySkipInbound']) { expect(sync, guard).toContain(guard); } expect(sync).toContain('status >= 20 || task.loading_completed_at || task.start_time'); expect(sync).toContain("SET voided_at = NOW(), void_reason = 'not_requires_delivery'"); expect(sync).toContain('SET voided_at = NULL, void_reason = NULL'); expect(sync).toContain('if (task?.voided_at)'); expect(sync).not.toContain('INSERT INTO delivery_tasks'); expect(visibility).toContain('dt_void.voided_at IS NOT NULL'); expect(queryService).toContain('dt.voided_at IS NULL'); expect(nav).toContain('dt.voided_at IS NULL'); const advance = body(fulfillment, 'tryAdvanceOrderToDelivering'); expect(advance).toContain('voided_delivery_task'); expect(advance).toContain('person_code, branch_code, voided_at'); const ensure = body(fulfillment, 'ensureDeliveryTaskForOrder'); expect(ensure).toContain('WHERE order_code = $1 AND voided_at IS NULL LIMIT 1'); expect(ensure).toContain('SET voided_at = NULL, void_reason = NULL'); expect(ensure).toContain('if (row.voided_at && !started)'); }); it('AC-29:主系统导入走共用 submit,自动获得两个 ensure', () => { const importSrc = read('../src/services/mainSystemOrderImport.ts'); expect(importSrc).toContain('submitMobileCustomerOrder'); expect(submit).toContain('ensurePickingTaskOnOrderSubmit'); expect(submit).toContain('ensureEarlyDeliveryTaskForOrder'); }); it('只同步开放分拣任务;不引入 T3 发车/核货改动', () => { const sync = body(picking, 'syncOpenPickingItemsForOrder'); expect(sync).toContain('Number(order.order_status) >= 50'); expect(sync).not.toContain('confirmDeliveryLoadingItem'); const loading = read('../src/services/deliveryLoadingService.ts'); expect(loading).not.toContain('syncDeliveryTaskForOrder'); }); });