import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; describe('批量代完成供货独立事务连接', () => { const src = readFileSync( new URL('../src/services/supplierPreparationStatusService.ts', import.meta.url), 'utf8' ); it('updateSupplierPreparationStatus 用 raw connect,避免请求级连接被 COMMIT 冲掉 SAVEPOINT', () => { const fnStart = src.indexOf('export async function updateSupplierPreparationStatus'); expect(fnStart).toBeGreaterThan(0); const fn = src.slice(fnStart, src.indexOf('export function assertPreparationAdminAccess')); expect(fn).toContain('connectExclusivePrepClient'); expect(fn).not.toContain('pool.connect()'); expect(src).toContain('getRawPoolConnect()'); expect(src).toContain("set_config('app.branch_code'"); expect(src).toContain('batchSoftSkip'); expect(src).toContain('SAVEPOINT'); }); it('成功路径 RELEASE SAVEPOINT 失败不向外抛', () => { const fnStart = src.indexOf('export async function updateSupplierPreparationStatus'); const fn = src.slice(fnStart, src.indexOf('export function assertPreparationAdminAccess')); expect(fn).toContain('RELEASE SAVEPOINT ${spName}`).catch(() => undefined)'); }); it('独立连接 BEGIN 后注入 RLS,避免 FORCE RLS 下 FOR UPDATE 空结果', () => { const fnStart = src.indexOf('export async function updateSupplierPreparationStatus'); const fn = src.slice(fnStart, src.indexOf('export function assertPreparationAdminAccess')); expect(fn).toContain('applyBranchRlsToClient'); }); }); describe('备货完成补建配送任务不跨连接抢锁', () => { it('ensureDeliveryTaskForOrder 支持传入已有 client 且不对其 COMMIT', () => { const src = readFileSync( new URL('../src/services/orderWorkflowFulfillmentService.ts', import.meta.url), 'utf8' ); const start = src.indexOf('export async function ensureDeliveryTaskForOrder'); const fn = src.slice(start, src.indexOf('export async function syncUnassignedDeliveryTasks')); expect(fn).toContain('existingClient?: PoolClient'); expect(fn).toContain('const manageTransaction = !existingClient'); expect(fn).toContain("if (manageTransaction) await client.query('COMMIT')"); }); it('直送补核货与推进待配送把当前 client 传给补建', () => { const skip = readFileSync( new URL('../src/services/skipInboundLoadingService.ts', import.meta.url), 'utf8' ); expect(skip).toContain('ensureDeliveryTaskForOrder(code, branchCode, client)'); const prep = readFileSync( new URL('../src/services/preparationFulfillmentService.ts', import.meta.url), 'utf8' ); expect(prep).toContain( 'ensureDeliveryTaskForOrder(orderCode, order.branch_code || null, client)' ); }); });