import { readFileSync } from 'node:fs'; import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * B2(2026-10-19 T2 终局复攻):行级台账处置**按优先级合并** —— 硬删不被 `reallocated` 降级。 * * 旧实现 `markPrepDisposition` 是裸 UPDATE:item_removed→removed 后,迟到的 reallocated 事件 * 会把 disposition 改回 reallocated,`needsAppendToOrder` 于是允许追加 ⇒ **已删行被重建**。 * 修复:reallocated 遇到硬删(removed/cancelled/returned/completed/suspended)保持原值; * removed 仍可覆盖 reallocated(升级)。 */ const H = vi.hoisted(() => ({ poolQuery: vi.fn() })); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() }, query: (...args: unknown[]) => H.poolQuery(...args), getRawPoolQuery: () => H.poolQuery, })); import { markPrepDisposition } from '../src/services/mainSystemPrepLedger'; beforeEach(() => { H.poolQuery.mockReset(); }); describe('B2:markPrepDisposition 优先级(源码/SQL 契约)', () => { it('UPDATE 含优先级 CASE:reallocated 不降级硬删;removed 可升级', async () => { H.poolQuery.mockResolvedValue({ rows: [], rowCount: 1 }); await markPrepDisposition({ branchCode: 'BR002', prepId: '11111111-1111-4111-8111-111111111111', disposition: 'reallocated', reason: '改派' }); const sql = String(H.poolQuery.mock.calls[0][0]); expect(sql).toContain("WHEN $3::text = 'reallocated'"); expect(sql).toContain("IN ('removed', 'cancelled', 'returned', 'completed', 'suspended')"); expect(sql).toContain('ELSE $3'); }); it('服务里不再出现裸 UPDATE disposition = $3', () => { const source = readFileSync(new URL('../src/services/mainSystemPrepLedger.ts', import.meta.url), 'utf8'); expect(source).not.toMatch(/SET disposition = \$3, disposition_reason = \$4/); }); }); type PgClient = { connect: () => Promise; end: () => Promise; query: (sql: string, params?: unknown[]) => Promise<{ rows: Array> }>; }; async function loadPgClient(): Promise { const { loadEnv } = await import('../src/env'); loadEnv(); if (!process.env.DATABASE_URL) return null; const pg = (await import('pg')).default as unknown as { Client: new (cfg: { connectionString: string }) => PgClient; }; const client = new pg.Client({ connectionString: process.env.DATABASE_URL }); try { await client.connect(); } catch { return null; } return client; } describe('B2:真实状态(事务内回滚,无库时跳过)', () => { it('removed → 迟到 reallocated 仍是 removed;reallocated → removed 正常升级', async (ctx) => { const client = await loadPgClient(); if (!client) { ctx.skip(); return; } const PREP = 'dddddddd-0001-4000-8000-000000000001'; try { await client.query('BEGIN'); await client.query( `INSERT INTO public.main_system_order_import_items (branch_code, main_order_code, prep_id, disposition, disposition_reason) VALUES ('__T2B2__','M_X',$1::uuid,'removed','主系统删除商品行')`, [PREP] ); const mark = (disposition: 'reallocated' | 'removed', reason: string) => markPrepDisposition({ branchCode: '__T2B2__', prepId: PREP, disposition, reason, client: { query: (sql: string, params?: unknown[]) => client.query(sql, params) }, }); await mark('reallocated', '迟到的改派事件'); let row = ( await client.query( `SELECT disposition, disposition_reason FROM public.main_system_order_import_items WHERE branch_code='__T2B2__' AND prep_id=$1::uuid`, [PREP] ) ).rows[0]; expect(row.disposition, '硬删不得被 reallocated 降级').toBe('removed'); expect(row.disposition_reason).toBe('主系统删除商品行'); // 反向:先 reallocated,再 removed ⇒ 升级 await client.query( `UPDATE public.main_system_order_import_items SET disposition='reallocated' WHERE branch_code='__T2B2__'` ); await mark('removed', '主系统删除商品行'); row = ( await client.query( `SELECT disposition FROM public.main_system_order_import_items WHERE branch_code='__T2B2__'` ) ).rows[0]; expect(row.disposition).toBe('removed'); } finally { await client.query('ROLLBACK').catch(() => undefined); await client.end(); } }); });