Files
SupplierSystem/server/tests/returnFollowCadence.test.ts
雨天andUbuntu 29fbf49481
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(多模块): 代提交另一会话遗留在工作区的整批改动(内容零修改)
原改动人:雨天 <oncwnuHbbvAwYZMdPF78PsWNPadI@git.weixin.qq.com>
代为提交:Lead 会话(本 commit 内容与该会话工作区逐字节一致,未做任何修改),
目的是不让在研成果丢失、并让工作树回到干净可部署状态。

提交前由 Lead 实跑验证:
- 前端 npx tsc --noEmit 0 错误;后端 server npx tsc --noEmit 0 错误
- 本批新增的 7 个测试 54/54 通过;Lead 侧契约测试(散客录单/文案/成本列)42/42 通过
- bash tools/design-consistency/run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost,既有红)/ 未执行 1(verify-facts,本地库不可连)—— 与基线一致

新增能力(各含 docs/design 设计文):
- 独立进货不得有配送任务:生成侧兜底 + 补建候选 + 查询侧三道防线(实测 5 张独立进货单里 4 张有僵尸 delivery_tasks)
- 主系统追加行零价闸门尊重其已声明的 allowUnpriced(否则整批分配行静默丢失)
- 库存抵扣为 0 的原因可见(不再静默,原因判定与抵扣量同源并落到日志与「库存抵扣」入口)
- 退货/删单跟随改独立 2 分钟周期(MAIN_SYSTEM_RETURN_FOLLOW_SEC,置 0 退回旧行为)
- 加工领料可用本单自己的预留(放行必须与本单预留消费成对,避免短送与同一批算两次)
- 出库拍照预览、主系统实收 / SKU 映射 / 单位变更 inbox 等配套

同一批包含对已废弃旧方案的回滚(design doc 与 README 索引行一并移除):
- 独立进货建客户档案(方案甲)→ 回到哨兵方案;随之删除 migrations/20261044_independent_inbound_customer.sql
- 分类新建去重(同名复用已有)
- 分拣标签纸张尺寸预设 / 分拣标签可选客户名与区域

未包含:src/pages/orders/OrderManagementPage.tsx(0 字节空文件,疑似误建,既未提交也未删除)
2026-10-15 11:29:15 +08:00

135 lines
6.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
});
});