import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; const svc = readFileSync(new URL('../src/services/inventoryOutboundService.ts', import.meta.url), 'utf8'); describe('同单出库并发串行化', () => { it('BEGIN 后先取 advisory lock,再查 outbound/扣减', () => { const begin = svc.indexOf("if (ownsTxn) await client.query('BEGIN');"); const lock = svc.indexOf("pg_advisory_xact_lock(hashtext('inventory_outbound:' || $1))", begin); const outbound = svc.indexOf('loadOutboundStateOnClient(client, code)', begin); expect(lock).toBeGreaterThan(begin); expect(outbound).toBeGreaterThan(lock); }); it('保留 orders FOR UPDATE 与逐 SKU 幂等,不新增唯一索引', () => { expect(svc).toContain('SELECT order_code, branch_code FROM orders WHERE order_code = $1 FOR UPDATE'); expect(svc).toContain('deductLines = allLines.filter((line) => !outboundSkuSet.has(line.sku))'); expect(svc).not.toContain('inventory_logs_order_batch_unique'); }); });