Files
SupplierSystem/server/tests/mainSystemImportFailureLedger.test.ts
T
Ubuntu d68cdaf7ff
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
feat(主系统对接): 导入失败留痕 + 首次告警 + 恢复销账(后续不再"无声收不到")
开发者 2026-10-17:「后续不能出现这种无法接收到的情况」。

事故链(本地真机):主系统 22:48 派单并推 preparations.allocated(SUP37831443),
我方因"台账指向的本系统订单已不存在"返回 500,主系统重试 3 次后放弃;
旧实现只打日志、界面/通知零痕迹,直到人工查主系统投递记录才发现。

三层保证:
① 自愈(上一提交 ef81c7c):陈旧占坑/台账指针自动清理并重建;
② 可见+告警(本次):失败落 public.main_system_import_failures
   (键 = 分公司×配送日×主系统分公司×主系统订单,attempts 累加不刷屏),
   首次失败站内通知 admin/super_admin(6 小时内去重),新增只读接口
   GET /api/integration/main-system/import-failures 供界面/排障查看;
   下一次导入成功自动销账(resolved_at)。
③ 兜底重试:定时对账每 10 分钟一次(已存在);生产需补 client secret 才可用(已在文档标注)。

新增迁移 20261057;trackDayOutcome() 挂在建单/追加两条返回路径上,异常一律吞掉(告警不许反过来把导入搞挂)。

验证:
· 服务侧真库冒烟:首次失败 attempts=1/isFirst=true → 再次 attempts=2;open 可查;clear 后 open=0
· 迁移 upsert 语义(同键累加、单行)真库事务内验证通过
· 单测 mainSystemImportFailureLedger 6 条;mainSystem 子集 30 文件 / 473 用例全过
· 设计门禁:8 通过 / 1 已知红 check-ai-cost / verify-facts 因本地缺 tenant_br003 红(均基线)
2026-10-17 23:06:47 +08:00

77 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/);
expect(block).toMatch(/catch \(error\)/);
});
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\(\{/);
});
});