Files
SupplierSystem/server/tests/mainSystemReconcileShortCircuit.test.ts
T
Ubuntu 44a660e18a
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
perf(跨系统): 出站闸门唯一化 + 无未决不拉(未绑编码 = 零沟通)
开发者要求:未绑接入编码的分公司全程不该与主系统沟通;后面可能几千家,
有的连主系统有的不连。核查发现 8-13 的收紧只闸住了"拉货"一条路:

- 申请列表 Q5/Q6(实收对账 / 我方价量对账 / 归档反查 / 按行分组)仍读"任何 active 绑定"
  ⇒ 迁移回填行、空接入编码行也解析出 supplier_code ⇒ 抱着注定 403 的编码换 Token + 发 GET
  (对方已收紧为「必须有接入编码才放行」,回执 §4.5.22)
- 定时对账 fan-out 名单用"有任何 active 绑定行"⇒ 这些分公司每轮白跑一遍
- 两条对账**先拉后判**:本地一条待审都没有也每 10 分钟发一次 GET

P1(判定点唯一):
- branchAccessCode:新增 listConnectableBindingsForBranch(四条规则压进一条 JOIN)+
  listConnectableBranchCodes(同谓词的分公司名单);listAutoPullableBindingsForBranch 保留旧名转发
- mainSystemBranchApi.listSupplierCodesForBranch → 同一判定点(出站正门)
- mainSystemOrderImport.listMainSupplierBranchCodes → listConnectableBranchCodes(fan-out 名单)

P2(无未决 ⇒ 不拉):
- reconcilePendingActualQtyRequests / reconcileOurPriceQtyRequestsWithMainSystem
  先查本地待审计数(轻查询),为 0 直接返回 —— 两者本来就只处理本地 pending 行,语义无变化

验证(真库冒烟,本地栈):
- 已绑编码的 BR002/BR003:仍进名单、仍解析出 1/2 条编码、拉取 ok(真 HTTP);
  真跑 fan-out branches=2 / failed=0
- 反证:临时清空 BR003 编码 ⇒ 名单 ["BR002"]、编码 []、拉取 not_bound(零 HTTP),BR002 不受影响;
  夹具已还原(残留 0)
- P2:0 条待审 ⇒ 跳过拉取;BR002 现库 1 条真实待审 ⇒ 不短路照旧拉;探针待审 ⇒ 立刻恢复真拉取
- 用例:mainSystemReconcileShortCircuit.test.ts(4 例,新)+ bindingAutoPullGuard P1/P2 段(14 例)
  + mainSystemBranchApi(11 例);服务端 tsc 0 错;全量 26 failed/377 passed 与基线持平
- 设计文 docs/design/20261013-主系统订单被本地拉取机制与绑定缺陷.md §9(收益估算:1000 家 43万→14万 GET/天)
2026-10-13 14:28:30 +08:00

85 lines
3.6 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 { beforeEach, describe, expect, it, vi } from 'vitest';
/**
* **P2:本地无待审 ⇒ 不发任何请求**(2026-10-13 优化)。
*
* 两条跨系统对账原来都是**先拉后判**:只要该分公司能解析出 `supplier_code`(= 已连接),
* 即使本地一条待审行都没有,也每 10 分钟向主系统发一次 GET。几千家分公司规模下,
* 这是最大的一笔纯浪费(对方是单体系统)。
*
* 本文件是**行为**用例(源码钉在 `bindingAutoPullGuard.test.ts` 的 P2 段):
* 本地待审计数为 0 时,`listRequestsForBranch`(唯一 HTTP 出口)**一次都不许被调用**。
*/
const query = vi.hoisted(() => vi.fn());
const listRequestsForBranch = vi.hoisted(() => vi.fn());
vi.mock('../src/db', () => ({
pool: { query: (...args: unknown[]) => query(...args), connect: vi.fn() },
getRawPoolQuery: () => query,
}));
vi.mock('../src/services/mainSystemBranchApi', () => ({
listRequestsForBranch: (...args: unknown[]) => listRequestsForBranch(...args),
}));
const { reconcilePendingActualQtyRequests } = await import('../src/services/mainSystemActualQtyInbox');
const { reconcileOurPriceQtyRequestsWithMainSystem } = await import(
'../src/services/mainSystemPriceQtyRequest'
);
function stubLocalPendingCount(n: number) {
query.mockImplementation(async (sql: string) => {
const text = String(sql);
if (text.includes('count(*)::int AS n')) return { rowCount: 1, rows: [{ n }] };
// n>0 时会继续走本地待审明细查询;这里返回空表示"明细为空、无事可做"
return { rowCount: 0, rows: [] };
});
}
beforeEach(() => {
query.mockReset();
listRequestsForBranch.mockReset();
listRequestsForBranch.mockResolvedValue({ kind: 'ok', rows: [], truncated: false });
});
describe('P2:本地无待审 ⇒ 零请求', () => {
it('① 实收/单位申请对账:本地 pending=0 ⇒ 不拉取、不发 HTTP', async () => {
stubLocalPendingCount(0);
const out = await reconcilePendingActualQtyRequests({ branchCode: 'BR002' });
expect(listRequestsForBranch).not.toHaveBeenCalled();
expect(out.pulled).toBe(0);
expect(out.checked).toBe(0);
expect(String(out.reason)).toContain('跳过拉取');
});
it('② 我方价/量申请对账:本地 pending=0 ⇒ 不拉取、不发 HTTP', async () => {
stubLocalPendingCount(0);
const out = await reconcileOurPriceQtyRequestsWithMainSystem({ branchCode: 'BR002' });
expect(listRequestsForBranch).not.toHaveBeenCalled();
expect(out.pulled).toBe(0);
expect(out.checked).toBe(0);
expect(String(out.reason)).toContain('跳过拉取');
});
it('③ 本地有待审时才继续走拉取(不是把功能关掉)', async () => {
stubLocalPendingCount(2);
await reconcilePendingActualQtyRequests({ branchCode: 'BR002' });
expect(listRequestsForBranch, '有待审 ⇒ 照常拉取').toHaveBeenCalledTimes(1);
});
it('④ 开关关闭时连本地计数都不查(与改造前逐字一致)', async () => {
// 这两个函数的开关在各自模块内:实测价/量那条在 disabled 时直接返回
const { __setPriceQtyLocalFirstForTests } = await import(
'../src/services/mainSystemPriceQtyRequest'
);
__setPriceQtyLocalFirstForTests(true);
query.mockImplementation(async () => {
throw new Error('不该查任何 SQL');
});
// 无 branchCode ⇒ 参数校验即返回
const out = await reconcileOurPriceQtyRequestsWithMainSystem({ branchCode: '' });
expect(listRequestsForBranch).not.toHaveBeenCalled();
expect(String(out.reason)).toContain('缺少分公司');
});
});