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); } }); });