Files
SupplierSystem/server/tests/actualQtyDecidedAndReconcile.test.ts
T
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

178 lines
8.9 KiB
TypeScript
Raw 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';
/**
* 主系统结果事件 `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);
// FE-2 的 findPendingRowById helper 展开后锚点更远,切片扩到 6000(只影响搜索窗口,不改语义)
const body = pageSrc.slice(idx, idx + 6000);
// 2026-10-19 FE-2:重取改为 findPendingRowById 的**多行调用**(翻页);
// 锚点仍精确到 statusFilter 参数(不得退化成只认调用名)
const refetchIdx = body.search(/listMainSystemActualQtyRequestsApi\(\{\s*status: 'pending',\s*page,/);
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/);
});
});