从 dev 日志里抓到自己上一批引入的 bug(真库日志原文): [main-system-branch-api] SUP41472132:3 行的供应商归属未知(台账缺失且绑定不唯一)⇒ 本轮跳过 [main-system-import] 缺货跟随本轮跳过(lookup 不可用) 根因:`settleRowsNoLongerOurs` 是在**逐供应商**循环里被调用的(`params.supplierCode`), 而我把 `lookupMainSystemPreparationStates(supplierCode, …)` 换成了 `lookupPreparationStatesForBranch(…)` —— 后者第一参数要**分公司编码** ⇒ 语义搞反:拿供应商编码去查绑定必然查不到 ⇒ 行归属未知 ⇒ 缺货跟随整轮跳过。 (这类"包装器参数语义"的错,函数名一样、类型都是 string,编译器抓不到。) 修法:该处本就知道是哪一个供应商、items 也全属于它 ⇒ 回到**单供应商**版本 `lookupMainSystemPreparationStates(params.supplierCode, …)`(附原因注释)。 守卫(防同类):`mainSystemSupplierCodeCallSites.test.ts` 新增"反向扫描" —— 按分公司的三个包装器**不得**接收 `supplierCode`/`supplier`/`params.supplierCode` 作为第一参数; 并钉住"逐供应商循环里的缺货跟随用单供应商版本"。 验证:服务端 2354 用例 / 失败文件 25 = 基线 24 + 另一条工作线遗留的 zeroCostClearsPlanPrice(非本次引入); tsc 通过;3022/3021 已重启并 200。
104 lines
5.1 KiB
TypeScript
104 lines
5.1 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
/**
|
||
* 回归守卫(2026-10-10 实测事故):**不许再把「我方分公司编码」传进 `supplierCode` 参数**。
|
||
*
|
||
* 背景:接入编码落地后,`branch_code` 与主系统的 `supplier_code` **不再相等**。
|
||
* 但主系统那些只读接口的第一参数仍是 `supplier_code`(按供货关系鉴权),
|
||
* 旧约定下到处都是 `xxx(branchCode)` 的写法 —— 解耦后**必然 403**,而它们全是
|
||
* 「兜底/跟随」路径(失败只记日志继续跑)⇒ **静默失效**:
|
||
* · 退货/改派跟随的 lookup 与归档反查(`mainSystemReturnFollow`)
|
||
* · 缺货跟随剔除(`mainSystemOrderImport`)、报缺货前的行状态(`mainSystemStockout`)
|
||
* · 履约事件对账兜底(`mainSystemFulfillmentEvents`)、核货事实刷新(`mainSystemActualFacts`)
|
||
* · Q6 漏推自愈(`mainSystemPriceQtyRequest` / `mainSystemActualQtyInbox`)
|
||
* 修法:统一走 `mainSystemBranchApi`(按分公司→多个绑定的包装器)或
|
||
* `branchAccessCode.resolveSupplierCodeForPrep`(行级台账取 supplier_code)。
|
||
*
|
||
* ⚠️ 诊断线索(真库日志原文):
|
||
* `换取对接 Token 失败(forbidden: 未授权访问该供应商:该(分公司 × 供应商)尚未绑定子系统接入编码…)`
|
||
*/
|
||
|
||
const SRC = path.join(__dirname, '../src');
|
||
function walk(dir: string, out: string[] = []): string[] {
|
||
for (const entry of readdirSync(dir)) {
|
||
const full = path.join(dir, entry);
|
||
if (statSync(full).isDirectory()) walk(full, out);
|
||
else if (entry.endsWith('.ts') && !entry.endsWith('.test.ts')) out.push(full);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/** 第一参数是 `supplierCode` 的对接函数(含写路径,一起钉住) */
|
||
const SUPPLIER_CODE_FNS = [
|
||
'lookupMainSystemPreparationStates',
|
||
'fetchMainSystemArchivedPreparations',
|
||
'fetchMainSystemPreparations',
|
||
'listMainSystemRequests',
|
||
'writebackMainSystemPreparationStatus',
|
||
'submitMainSystemPriceQtyRequest',
|
||
'postMainSystemActualQtyDecision',
|
||
'getMainSystemToken',
|
||
'fetchMainSystemPriceAmendRows',
|
||
];
|
||
|
||
describe('对接调用:第一参数不得是分公司编码', () => {
|
||
const files = walk(SRC).filter(
|
||
(f) => !f.includes(`${path.sep}mainSystem${path.sep}api${path.sep}`) // HTTP 层就是收 supplierCode 的
|
||
);
|
||
|
||
it('没有调用点把 branch / branchCode / params.branchCode 传进 supplierCode 参数', () => {
|
||
const violations: string[] = [];
|
||
for (const file of files) {
|
||
const text = readFileSync(file, 'utf8');
|
||
for (const fn of SUPPLIER_CODE_FNS) {
|
||
const re = new RegExp(`${fn}\\(\\s*(branch\\b|branchCode\\b|params\\.branchCode\\b)`, 'g');
|
||
const hits = text.match(re);
|
||
if (hits) violations.push(`${path.relative(SRC, file)} → ${fn}(${hits.length} 处)`);
|
||
}
|
||
}
|
||
expect(violations, `这些调用会把分公司编码当供应商编码 ⇒ 必然 403:\n${violations.join('\n')}`).toEqual([]);
|
||
});
|
||
|
||
it('反向:按分公司的包装器**不得**吃供应商编码(2026-10-10 实测语义搞反过一次)', () => {
|
||
const wrappers = [
|
||
'lookupPreparationStatesForBranch',
|
||
'fetchArchivedPreparationsForBranch',
|
||
'listRequestsForBranch',
|
||
];
|
||
const violations: string[] = [];
|
||
for (const file of walk(SRC)) {
|
||
if (file.includes('mainSystemBranchApi.ts')) continue; // 定义处
|
||
const text = readFileSync(file, 'utf8');
|
||
for (const fn of wrappers) {
|
||
const re = new RegExp(`${fn}\\(\\s*(supplierCode\\b|supplier\\b|params\\.supplierCode\\b)`, 'g');
|
||
const hits = text.match(re);
|
||
if (hits) violations.push(`${path.relative(SRC, file)} → ${fn}(${hits.length} 处)`);
|
||
}
|
||
}
|
||
expect(violations, `按分公司的包装器第一参数是**分公司编码**;传供应商编码会让整轮静默跳过:\n${violations.join('\n')}`).toEqual([]);
|
||
});
|
||
|
||
it('逐供应商循环里的缺货跟随用单供应商版本(不该走分公司包装器)', () => {
|
||
const text = readFileSync(path.join(SRC, 'services/mainSystemOrderImport.ts'), 'utf8');
|
||
expect(text).toContain('const looked = await lookupMainSystemPreparationStates(');
|
||
});
|
||
|
||
it('按分公司的包装器存在且被这些兜底路径使用', () => {
|
||
const expectations: Array<[string, string]> = [
|
||
['services/mainSystemActualFacts.ts', 'lookupPreparationStatesForBranch'],
|
||
['services/mainSystemStockout.ts', 'lookupPreparationStatesForBranch'],
|
||
['services/mainSystemFulfillmentEvents.ts', 'lookupPreparationStatesForBranch'],
|
||
['services/mainSystemReturnFollow.ts', 'lookupPreparationStatesForBranch'],
|
||
['services/mainSystemReturnFollow.ts', 'fetchArchivedPreparationsForBranch'],
|
||
['services/mainSystemPriceQtyRequest.ts', 'listRequestsForBranch'],
|
||
['services/mainSystemActualQtyInbox.ts', 'listRequestsForBranch'],
|
||
];
|
||
for (const [file, fn] of expectations) {
|
||
const text = readFileSync(path.join(SRC, file), 'utf8');
|
||
expect(text, `${file} 应使用 ${fn}`).toContain(fn);
|
||
}
|
||
});
|
||
});
|