开发者报障:「11:25 左右发过来一个改成本价的请求,不知道为什么没有展示」。 真库时间线(prep 1b88c432…,大猪肉三线肉): · 11:25:03.726 主系统改成本价 10 → 9 · 11:25:03.841 **同一改动的两条消息几乎同时到达**(`price_changed` 事实 + `actual_qty_request` 可审申请) ⇒ 各自插入一行 pending,各自调用"关掉别人、留下自己" · 11:25:03.848 两条**互相取代** ⇒ 两条全部 superseded(那一刻"待我审核"是空的 = 报障现象) (真库另有一对 838548f8 ↔ dae7b387 同样中招) · 11:25:33.327 主系统再改 9 → 8(只发 price_changed)⇒ 补建待审行,现在有 1 条待审 修法:把"谁后到谁关掉对方"换成**一次确定性收敛** `convergeLatestPending()`: 按 `COALESCE(applied_at, created_at) DESC → created_at DESC → 有真 request_id 优先 → id DESC` 选出唯一幸存者,同组其余 pending 一律 superseded(superseded_by 指向幸存者)。 插入与原地刷新两条路径都调它 ⇒ **与到达顺序无关、幂等、不可能互相取代**。 真库实测:同一改动按 `fact-first` 与 `request-first` 两种顺序各跑一次 ⇒ 都只剩 1 条待审 (且留下的是带真 request_id 的那条)、互相取代 0 对;探针数据已清理, 该商品价格链现为 10→9 ×2 = superseded(历史可回溯)、9→8 = pending。 同时:`GET /api/workflow/main-system/actual-qty-requests?status=pending` 实测返回 `total=1 / counts.pending=1`(大猪肉三线肉 9→8 斤),即接口侧一直是好的,纯粹是这条竞态把待审行吞了。 测试:actualQtyInbox / actualQtyFactSync / actualQtyDecidedAndReconcile 43 绿; 服务端全量失败文件与基线逐文件一致(24,零新增);设计文档补 §11.1.3。
175 lines
8.6 KiB
TypeScript
175 lines
8.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
/**
|
||
* 主系统结果事件 `preparations.actual_qty_decided` + Q6 对账兜底(2026-10-12 补,对接清单 §4.5.4)。
|
||
*
|
||
* 背景:主系统会在申请**还没被我方审**时撤回/作废(如配送员撤销核货)⇒ 我方台账若一直 pending,
|
||
* 审核页就会拿着已失效的值让人审(实测 60 条 pending 里 58 条在对方侧已查不到)。
|
||
*
|
||
* 钉住四件事:
|
||
* ① 事件接收端存在、四种 decision 都落终态、幂等(已终态不动);
|
||
* ② webhook 路由必须在"其它事件 200 忽略"**之前**拦下它;
|
||
* ③ Q6 客户端用 `kind=actual_qty` 只读拉取,取满即标 `truncated`;
|
||
* ④ 对账**保守**:对方仍 pending ⇒ 不动;拉取被截断 ⇒ 不判孤儿;孤儿关闭要过宽限期。
|
||
*/
|
||
const inbox = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemActualQtyInbox.ts'),
|
||
'utf8'
|
||
);
|
||
const route = readFileSync(
|
||
path.join(__dirname, '../src/routes/mainSystemIntegration.ts'),
|
||
'utf8'
|
||
);
|
||
const api = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystem/api/preparations.ts'),
|
||
'utf8'
|
||
);
|
||
const importLoop = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
describe('结果事件接收端(withdrawn / superseded / approved / rejected)', () => {
|
||
it('四种决策都映射到终态,未知决策忽略', () => {
|
||
const idx = inbox.indexOf('const DECIDED_TERMINAL');
|
||
const block = inbox.slice(idx, idx + 500);
|
||
for (const key of ['withdrawn', 'superseded', 'approved', 'rejected']) {
|
||
expect(block, `缺少 ${key} 映射`).toContain(`${key}:`);
|
||
}
|
||
expect(inbox).toMatch(/if \(!nextStatus\)/);
|
||
});
|
||
|
||
it('同一 request_id 未处理 ⇒ 刷新为最新;已终态 ⇒ 不覆盖只留痕(2026-10-12 定稿)', () => {
|
||
// 主系统"未处理就原地更新为最新"(唯一键 (preparation_id, kind, field) + WHERE status='pending')
|
||
expect(inbox).toMatch(/status: 'refreshed'/);
|
||
expect(inbox).toMatch(/main_system_actual_qty_request_refreshed/);
|
||
// 终态行被再次投递时只记迟到痕迹,不覆盖我方结论
|
||
expect(inbox).toMatch(/late_event/);
|
||
expect(inbox).toMatch(/已是终态(\$\{cur\.status\}),本次不刷新/);
|
||
// 比较值/单位/生效时间,完全一致才 duplicate
|
||
expect(inbox).toMatch(/sameNumber\(cur\.proposed_value/);
|
||
expect(inbox).toMatch(/sameInstant\(cur\.applied_at/);
|
||
});
|
||
|
||
it('决策不回声我方旧值:未手填值回传 null,本地落值用回执返回值', () => {
|
||
expect(inbox).toMatch(/const requestedFinalValue =/);
|
||
expect(inbox).toMatch(/const appliedValue =/);
|
||
expect(inbox).toMatch(/sent\.finalValue/);
|
||
// 不得再用本地 proposed_value 兜底当回传值
|
||
expect(inbox).not.toMatch(/params\.finalValue \?\? num\(row\.proposed_value\)/);
|
||
});
|
||
|
||
it('幂等 + 只关 pending(已终态不动)', () => {
|
||
expect(inbox).toMatch(/if \(row\.status !== 'pending'\)/);
|
||
expect(inbox).toMatch(/AND status = 'pending'/);
|
||
});
|
||
|
||
it('webhook 在"其它事件 200 忽略"之前拦下该事件', () => {
|
||
const handlerIdx = route.indexOf('event === ACTUAL_QTY_DECIDED_EVENT');
|
||
const ignoreIdx = route.indexOf("if (event !== 'preparations.allocated')");
|
||
expect(handlerIdx).toBeGreaterThan(-1);
|
||
expect(ignoreIdx).toBeGreaterThan(-1);
|
||
expect(handlerIdx).toBeLessThan(ignoreIdx);
|
||
expect(route).toMatch(/handleActualQtyDecidedEvent\(/);
|
||
});
|
||
});
|
||
|
||
describe('Q6 只读拉取 + 对账', () => {
|
||
it('客户端按 kind 过滤、只读 GET,取满即标 truncated', () => {
|
||
expect(api).toMatch(/endpoint: 'priceQtyRequestsList'/);
|
||
expect(api).toMatch(/method: 'GET'/);
|
||
expect(api).toMatch(/truncated: rows\.length >= limit/);
|
||
});
|
||
|
||
it('对账:对方 pending ⇒ 值不同就刷新为最新;截断不判孤儿;孤儿需过宽限期', () => {
|
||
const idx = inbox.indexOf('export async function reconcilePendingActualQtyRequests');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const body = inbox.slice(idx, idx + 6000);
|
||
expect(body).toMatch(/theirs\.status === 'pending'/);
|
||
expect(body).toMatch(/reconcile_refresh/);
|
||
expect(body).toMatch(/refreshed \+= 1/);
|
||
expect(body).toMatch(/if \(pulled\.truncated\)/);
|
||
expect(body).toMatch(/graceMs/);
|
||
expect(body).toMatch(/kind: 'actual_qty'/);
|
||
});
|
||
|
||
it('对账已挂进分配对账(每轮兜底,失败只日志)', () => {
|
||
expect(importLoop).toMatch(/reconcilePendingActualQtyRequests\(\{ branchCode \}\)/);
|
||
});
|
||
});
|
||
|
||
/**
|
||
* 代码核查(2026-10-12)抓到的 4 处问题的回归钉子:
|
||
* ① 决策事件处理器只认 approved/rejected/applied ⇒ 我方申请对账不得把 withdrawn/superseded 原样传进去
|
||
* (否则处理器忽略、行却已被计成"已关闭"= 假成功);必须映射为 rejected + 写明作废/撤回。
|
||
* ② 我方申请对账对 `kind='both'` 必须**不动**:单条对方行给不出价/量两个值,硬套会把另一个字段写错。
|
||
* ③ 审核页"提交前重取"必须在 `setActingId` **之前**(先置 busy 再 return 会让按钮永远停在忙碌态)。
|
||
* ④ `actual_qty_decided` 没带 `final_value` 时不得用我方(可能过期的)proposed 兜底。
|
||
*/
|
||
const priceQtySrc = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemPriceQtyRequest.ts'),
|
||
'utf8'
|
||
);
|
||
const pageSrc = readFileSync(
|
||
path.join(__dirname, '../../src/pages/orders/PriceQtyRequestsPage.tsx'),
|
||
'utf8'
|
||
);
|
||
|
||
describe('代码核查回归(2026-10-12)', () => {
|
||
it('① withdrawn/superseded 映射为 rejected,不原样丢给决策处理器', () => {
|
||
const idx = priceQtySrc.indexOf('const OUR_REQUEST_TERMINAL');
|
||
const block = priceQtySrc.slice(idx, idx + 600);
|
||
expect(block).toMatch(/superseded: \{ decision: 'rejected'/);
|
||
expect(block).toMatch(/withdrawn: \{ decision: 'rejected'/);
|
||
expect(block).not.toMatch(/decision: 'superseded'/);
|
||
expect(block).not.toMatch(/decision: 'withdrawn'/);
|
||
});
|
||
|
||
it('② kind=both 在我方申请对账里被排除(不猜两值)', () => {
|
||
const idx = priceQtySrc.indexOf('export async function reconcileOurPriceQtyRequestsWithMainSystem');
|
||
const body = priceQtySrc.slice(idx, idx + 6000);
|
||
const guards = body.match(/row\.kind !== 'price' && row\.kind !== 'qty'/g) || [];
|
||
expect(guards.length).toBeGreaterThanOrEqual(2); // 终态落地 + pending 刷新 各一处
|
||
});
|
||
|
||
it('③ 审核页提交前重取在 setActingId 之前(不会卡住按钮)', () => {
|
||
// 2026-10-12 界面重构:决策函数改名为 `decide`(原 `decideInbox`)⇒ 锚点改为**函数体开头的注释锚**
|
||
const idx = pageSrc.indexOf('const decide = async');
|
||
expect(idx, '未找到决策函数').toBeGreaterThan(-1);
|
||
const body = pageSrc.slice(idx, idx + 3000);
|
||
const refetchIdx = body.indexOf("listMainSystemActualQtyRequestsApi({ status: 'pending'");
|
||
const busyIdx = body.indexOf('setActingId(row.id);');
|
||
expect(refetchIdx).toBeGreaterThan(-1);
|
||
expect(busyIdx).toBeGreaterThan(-1);
|
||
expect(refetchIdx).toBeLessThan(busyIdx);
|
||
});
|
||
|
||
it('⑤ 我方价/量申请对账必须查 public.main_system_price_qty_requests(首版写错表名 ⇒ 整段静默失效)', () => {
|
||
const refs = priceQtySrc.match(/supplier_price_qty_requests/g) || [];
|
||
const correct = priceQtySrc.match(/main_system_price_qty_requests/g) || [];
|
||
// 只允许出现在注释里(首版误用的记录),不得再作为表名出现
|
||
const bad = [...priceQtySrc.matchAll(/FROM\s+supplier_price_qty_requests|UPDATE\s+supplier_price_qty_requests|INTO\s+supplier_price_qty_requests/g)];
|
||
expect(bad.length).toBe(0);
|
||
expect(correct.length).toBeGreaterThan(5);
|
||
expect(refs.length).toBeGreaterThan(0);
|
||
});
|
||
|
||
it('⑥ 同一 $N 不得既当 varchar 又当 text(PostgreSQL: inconsistent types deduced)', () => {
|
||
// 2026-10-12 实测:接收端刷新 unit=$8 / 对账刷新 unit=$4 / decided 事件 reviewer=$7 都踩过
|
||
expect(inbox).toMatch(/unit = \$8::text/);
|
||
expect(inbox).toMatch(/unit = \$4::text/);
|
||
expect(inbox).toMatch(/COALESCE\(\$7::text, reviewer\)/);
|
||
expect(inbox).not.toMatch(/unit = \$8,(?!\s*$)/m);
|
||
});
|
||
|
||
it('④ 决策事件不带 final_value 时不写终值(不用我方 proposed 兜底)', () => {
|
||
const idx = inbox.indexOf('const finalValue = nextStatus');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const line = inbox.slice(idx, idx + 200);
|
||
expect(line).toMatch(/num\(payload\?\.final_value\)/);
|
||
expect(line).not.toMatch(/row\.proposed_value/);
|
||
});
|
||
});
|