import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { classifyImportFailure } from '../src/services/mainSystemImportFailureService'; /** * 「后续不能出现这种无法接收到的情况」——**失败留痕 + 首次告警 + 恢复销账**(2026-10-17)。 * * 真机事故:`ORD_WEB_20261017_BR002_0001` 因"台账指向的本系统订单已不存在"连续失败, * 主系统重试 3 次后放弃,而**界面/通知里没有任何痕迹**,只能去查主系统的投递记录才发现。 * * 现在的三层保证: * ① 自愈:陈旧占坑/台账指针自动清理重建(mainSystemOrderImport); * ② 可见:失败落 `main_system_import_failures`(同键累加次数),首次失败站内通知管理员; * ③ 兜底:定时对账每 10 分钟重试一次,成功即自动销账(resolved_at)。 */ const importer = readFileSync( path.join(__dirname, '../src/services/mainSystemOrderImport.ts'), 'utf8' ); const service = readFileSync( path.join(__dirname, '../src/services/mainSystemImportFailureService.ts'), 'utf8' ); const migration = readFileSync( path.join(__dirname, '../../migrations/20261057_main_system_import_failures.sql'), 'utf8' ); const routes = readFileSync(path.join(__dirname, '../src/routes/mainSystemIntegration.ts'), 'utf8'); describe('② 可见性:导入失败一定落台账', () => { it('fail 分支记失败;created/appended 分支销账', () => { const idx = importer.indexOf('async function trackDayOutcome'); expect(idx).toBeGreaterThan(-1); const block = importer.slice(idx, idx + 2200); expect(block).toMatch(/outcome\.status === 'failed'[\s\S]*recordMainSystemImportFailure/); expect(block).toMatch(/'created' \|\| outcome\.status === 'appended'[\s\S]*clearMainSystemImportFailures/); }); it('首次失败才发站内通知(去重 6 小时),且通知链路异常不许影响导入', () => { const idx = importer.indexOf('async function trackDayOutcome'); const block = importer.slice(idx, idx + 2200); expect(block).toMatch(/recorded\?\.isFirst/); expect(block).toMatch(/notifyRoleUsers\(/); expect(block).toMatch(/dedupWithinHours: 6/); // 通知链路异常被吞掉:notifyRoleUsers(...).catch(() => undefined) expect(block).toMatch(/\.catch\(\(\) => undefined\)/); }); it('两条通道(建单 / 追加)都调用同一处留痕', () => { const calls = importer.match(/await trackDayOutcome\(branchCode, bucket, outcome\);/g) || []; expect(calls.length).toBeGreaterThanOrEqual(2); }); it('台账:同键累加 attempts、恢复置 resolved_at、唯一键含 COALESCE(main_order_code)', () => { expect(service).toMatch(/ON CONFLICT \(branch_code, delivery_date, main_branch_code, COALESCE\(main_order_code, ''\)\)/); expect(service).toMatch(/attempts = f\.attempts \+ 1/); expect(service).toMatch(/resolved_at = NULL/); expect(service).toMatch(/SET resolved_at = NOW\(\)/); expect(migration).toMatch(/CREATE TABLE IF NOT EXISTS public\.main_system_import_failures/); expect(migration).toMatch(/uq_main_system_import_failures_key/); }); it('分类只按已知特征,不瞎猜根因', () => { expect(classifyImportFailure('台账指向的本系统订单已不存在')).toBe('stale_order_pointer'); expect(classifyImportFailure('商品价格不能为空')).toBe('zero_price'); expect(classifyImportFailure('超时')).toBe('timeout'); expect(classifyImportFailure('随便什么')).toBe('unknown'); }); }); describe('排障入口:未解决失败可查询', () => { it('新增只读接口 GET /main-system/import-failures(绑定分公司,不再让人只翻日志)', () => { expect(routes).toMatch(/router\.get\('\/main-system\/import-failures'/); expect(routes).toMatch(/listOpenImportFailures\(\{/); }); });