原改动人:雨天 <oncwnuHbbvAwYZMdPF78PsWNPadI@git.weixin.qq.com> 代为提交:Lead 会话(本 commit 内容与该会话工作区逐字节一致,未做任何修改), 目的是不让在研成果丢失、并让工作树回到干净可部署状态。 提交前由 Lead 实跑验证: - 前端 npx tsc --noEmit 0 错误;后端 server npx tsc --noEmit 0 错误 - 本批新增的 7 个测试 54/54 通过;Lead 侧契约测试(散客录单/文案/成本列)42/42 通过 - bash tools/design-consistency/run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost,既有红)/ 未执行 1(verify-facts,本地库不可连)—— 与基线一致 新增能力(各含 docs/design 设计文): - 独立进货不得有配送任务:生成侧兜底 + 补建候选 + 查询侧三道防线(实测 5 张独立进货单里 4 张有僵尸 delivery_tasks) - 主系统追加行零价闸门尊重其已声明的 allowUnpriced(否则整批分配行静默丢失) - 库存抵扣为 0 的原因可见(不再静默,原因判定与抵扣量同源并落到日志与「库存抵扣」入口) - 退货/删单跟随改独立 2 分钟周期(MAIN_SYSTEM_RETURN_FOLLOW_SEC,置 0 退回旧行为) - 加工领料可用本单自己的预留(放行必须与本单预留消费成对,避免短送与同一批算两次) - 出库拍照预览、主系统实收 / SKU 映射 / 单位变更 inbox 等配套 同一批包含对已废弃旧方案的回滚(design doc 与 README 索引行一并移除): - 独立进货建客户档案(方案甲)→ 回到哨兵方案;随之删除 migrations/20261044_independent_inbound_customer.sql - 分类新建去重(同名复用已有) - 分拣标签纸张尺寸预设 / 分拣标签可选客户名与区域 未包含:src/pages/orders/OrderManagementPage.tsx(0 字节空文件,疑似误建,既未提交也未删除)
107 lines
4.7 KiB
TypeScript
107 lines
4.7 KiB
TypeScript
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('⑤ 本地 pending=0 仍要执行补落值兜底(不能被"无未决不拉"的早退吞掉)', async () => {
|
||
/**
|
||
* 补落值目标是 `status='approved'` 的存量行(**不是 pending**)。
|
||
* 修复前早退直接 return,`reapplyUnlandedActualQtySyncs` 这条唯一兜底入口在无待审时永不执行 ⇒
|
||
* "审核同意了、数量/单位却没改"的行永久僵死。
|
||
*/
|
||
const seen: string[] = [];
|
||
query.mockImplementation(async (sql: string) => {
|
||
const text = String(sql);
|
||
seen.push(text);
|
||
if (text.includes('count(*)::int AS n')) return { rowCount: 1, rows: [{ n: 0 }] };
|
||
return { rowCount: 0, rows: [] };
|
||
});
|
||
const out = await reconcilePendingActualQtyRequests({ branchCode: 'BR002' });
|
||
expect(
|
||
seen.some((s) => s.includes("payload->'apply'->>'status'")),
|
||
'补落值候选查询必须执行(它是 approved 未落值行的唯一补救通道)'
|
||
).toBe(true);
|
||
expect(out.reapplied).toBe(0);
|
||
expect(listRequestsForBranch, '无待审仍不得发 HTTP').not.toHaveBeenCalled();
|
||
});
|
||
|
||
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('缺少分公司');
|
||
});
|
||
});
|