import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 跨系统对账的**查询护栏 + 每轮预算 + 增量化**(2026-10-24 优化,开发者: * 「这个你看看怎么优化 需要主系统配合给我说」)。 * * 要解决的问题:几百家分公司 × 每家上万批次的规模下,对账类"全量/大批量"查询会 * · 把 15 分钟 tick 拖过 10 分钟锁 TTL ⇒ 下一轮并发叠加; * · 长期占用连接池(max 30)⇒ 前台业务超时(表现"系统卡死")。 */ const guard = readFileSync(new URL('../src/services/dbQueryGuard.ts', import.meta.url), 'utf8'); const follow = readFileSync(new URL('../src/services/mainSystemReturnFollow.ts', import.meta.url), 'utf8'); const push = readFileSync(new URL('../src/services/mainSystemReportPushService.ts', import.meta.url), 'utf8'); const scheduler = readFileSync(new URL('../src/services/scheduledJobs.ts', import.meta.url), 'utf8'); const migration = readFileSync(new URL('../../migrations/20261077_reconcile_guard_indexes.sql', import.meta.url), 'utf8'); describe('对账护栏与增量化', () => { it('查询超时护栏:SET 后必须 RESET(否则连接回池会毒化后续业务查询)', () => { expect(guard).toContain('SET statement_timeout'); expect(guard).toContain('RESET statement_timeout'); expect(guard).toContain('createRoundBudget'); }); it('报告推送的全量兜底查询走超时护栏(30s)', () => { expect(push).toContain('queryWithTimeout'); expect(push).toMatch(/LIMIT \$2`,\s*\[branchCode, limit\],\s*30_000/); }); it('退货跟随按预算循环(超出跳过剩余分公司,下一轮继续)', () => { expect(scheduler).toContain('createRoundBudget(90_000)'); expect(scheduler).toContain('skippedByBudget'); expect(scheduler).toContain('本轮预算用尽'); }); it('自愈补录增量化:只扫最近窗口 + 上限 50 单(不再每轮全量)', () => { expect(follow).toContain('MAIN_SYSTEM_REPAIR_WINDOW_DAYS'); expect(follow).toContain("i.imported_at >= NOW() - ($2::int || ' days')::interval"); expect(follow).toContain('LIMIT 50'); }); it('配套索引(退回轮询 / 曾活跃 / 最近导入)', () => { expect(migration).toContain('idx_import_items_pending_returns'); expect(migration).toContain('idx_import_items_last_seen_active'); expect(migration).toContain('idx_main_system_order_imports_recent'); }); });