Files
SupplierSystem/server/tests/mainSystemImportFailureLedger.test.ts
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

78 lines
3.8 KiB
TypeScript

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\(\{/);
});
});