import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; import { resolveReturnFollowThrottleMs } from '../src/services/scheduledJobs'; /** * **退货/删单跟随的发现延迟**(2026-10-14 事故根治)。 * * 事故:主系统删掉 `ORD_WEB_20261014_BR002_0002`(本地 10:56:30),我方 * `ORD_API_20261014_BR002_0001` 的 15 行**迟迟不消失**、订单也不取消。 * 核查结论: * · 跟随**逻辑本身正确**(手动跑一次 ⇒ 15 行 `cancelled` + 空镜像单收口进垃圾桶); * · 但跟随原先只写在导入循环里,被 `runMainSystemImportReconcile` 的 **10 分钟**限流罩住 * ⇒ 实测操作日志 10:56:30 → 11:01:37 **零条跟随记录**; * · 主系统**没有**删单/退货推送事件(`supplierPrepWebhookService` 只发 * allocated/picked_up/inbound_completed/price_qty_decided/price_changed/ * actual_qty_request/actual_qty_decided/binding.changed/unit_changed) * ⇒ **轮询是唯一通道**。 * * 本文件钉住:① 节流解析;② 跟随拿到**自己的 60 秒定时器**(不许再被 15 分钟块吞掉)。 */ const SRC = path.join(__dirname, '..', 'src'); const read = (rel: string) => readFileSync(path.join(SRC, rel), 'utf8'); describe('① resolveReturnFollowThrottleMs:节流解析', () => { it('不设 / 空 ⇒ 默认 120 秒', () => { expect(resolveReturnFollowThrottleMs('')).toBe(120_000); expect(resolveReturnFollowThrottleMs(undefined)).toBe(120_000); expect(resolveReturnFollowThrottleMs(null)).toBe(120_000); }); it('显式秒数按值生效', () => { expect(resolveReturnFollowThrottleMs('60')).toBe(60_000); expect(resolveReturnFollowThrottleMs(30)).toBe(30_000); expect(resolveReturnFollowThrottleMs(' 90 ')).toBe(90_000); }); it('非法值回落到默认 120 秒(不静默关停跟随)', () => { expect(resolveReturnFollowThrottleMs('abc')).toBe(120_000); expect(resolveReturnFollowThrottleMs('NaN')).toBe(120_000); }); it('0 / 负数 ⇒ 0(显式退回旧行为:跟随只随导入跑)', () => { expect(resolveReturnFollowThrottleMs('0')).toBe(0); expect(resolveReturnFollowThrottleMs('-5')).toBe(0); }); it('上限 1 小时(避免误填超大值把跟随实质关停)', () => { expect(resolveReturnFollowThrottleMs('99999')).toBe(3600_000); }); }); describe('② 源码守卫:跟随必须有自己的 60 秒定时器', () => { const src = read('services/scheduledJobs.ts'); it('存在独立的 returnFollowTimer 与 60 秒 interval', () => { expect(src).toContain('returnFollowTimer = setInterval('); // 紧跟其后的周期必须是 60 秒 const at = src.indexOf('returnFollowTimer = setInterval('); const tail = src.slice(at, at + 260); expect(tail).toContain('60 * 1000'); }); it('跟随**不得**被塞进 15 分钟的那个块里(塞进去 = 仍是 15 分钟一轮,等于没改)', () => { // 15 分钟块:withDistributedLock(MAIN_SYSTEM_LOCK_KEY, ...) … }, 15 * 60 * 1000); const blockStart = src.indexOf('withDistributedLock(MAIN_SYSTEM_LOCK_KEY'); expect(blockStart).toBeGreaterThan(-1); const blockEnd = src.indexOf('}, 15 * 60 * 1000)', blockStart); expect(blockEnd).toBeGreaterThan(blockStart); const block = src.slice(blockStart, blockEnd); expect(block).not.toContain('runReturnFollowReconcile'); }); it('停机时清理该定时器(否则 stop 后仍在轮询主系统)', () => { expect(src).toContain('clearInterval(returnFollowTimer)'); expect(src).toContain('returnFollowTimer = null'); }); it('跟随受配置开关与节流双重约束(关闭时不打主系统)', () => { expect(src).toContain('returnFollowEnabled'); expect(src).toContain('lastReturnFollowAt'); expect(src).toContain('RETURN_FOLLOW_THROTTLE_MS'); }); it('启动日志如实标注跟随频率', () => { expect(src).toContain('退货/删单跟随(每2分钟)'); }); }); describe('③ 失败不拖垮其它分公司(并发遍历 + 逐家兜错)', () => { const src = read('services/scheduledJobs.ts'); it('用 mapWithConcurrency 逐家跑,单家异常只记日志', () => { const at = src.indexOf('async function runReturnFollowReconcile'); expect(at).toBeGreaterThan(-1); const body = src.slice(at, at + 1600); expect(body).toContain('mapWithConcurrency'); expect(body).toContain('退货跟随失败'); expect(body).toContain('followMainSystemPrepChanges'); }); }); /** * 「收口被跳过」必须留痕(2026-10-14 同批加固)。 * * 扫描的 SQL 预筛已排除「有有效明细 / 在途分拣 / 在途配送」三种常态原因 ⇒ 能走到判定树又被跳过的, * 基本都要人工:`statement_frozen`(有意不自动收口)、`has_active_items`(行级跟随没跟上 = 要防的形态)。 * 原实现只在 `closed > 0` 时打汇总 ⇒ **"什么都没做"和"做不了"操作员看起来一样**,这正是本次事故的教训。 */ describe('④ 镜像空单收口:跳过原因必须可见', () => { const src = read('services/scheduledJobs.ts'); it('收集并输出 skipped 明细(不再只报 closed)', () => { const at = src.indexOf('async function runEmptyMirrorOrderClosure'); expect(at).toBeGreaterThan(-1); const body = src.slice(at, at + 2200); expect(body).toContain('skippedDetail'); expect(body).toContain('镜像空单收口跳过'); // 必须遍历 outcomes 而不是只看 r.closed expect(body).toContain('r.outcomes'); }); it('原因码有人话(操作员看不懂 statement_frozen)', () => { expect(src).toContain('function describeMirrorSkipReason'); expect(src).toContain('客户结算单已冻结,交人工'); expect(src).toContain('仍有未退货明细(行级跟随未跟上)'); }); it('明细有截断上限(500 家规模下不刷爆日志)', () => { const at = src.indexOf('skippedDetail.slice('); expect(at).toBeGreaterThan(-1); }); });