免入库(主系统骑手上门取货): - 50 展示「待取货」、副文案「待主系统骑手上门取货」、不建我方配送任务(守卫与开关解耦) - 我方 50 即回写备货完成;两开关(SKIP_INBOUND_AWAIT_PICKUP / FULFILLMENT_EVENTS)改默认开 核货事实镜像(「核货完毕」标记): - 按主系统 lookup 的 actual_qty 落台账,新增 GET/POST /orders/:orderCode/main-system-prep-facts[/refresh] - 详情行内显示「核货完毕」(含核货量),分配对账兜底刷新 实收数量/单位口径变更(对方先改 + 我方事后同步): - 台账加 applied_at;inbound 与 picking_check 同为事实登记(驳回不回滚) - 同意才落我方值:免入库改装车核货量、送仓改验收实收(已入库走既有库存调整)、已结算/已结案只留痕 - 计划量与金额一起改,并同步结算投影(syncStatementsForOrderSafe) - 审核页语义改「主系统实际值(已生效)/ 是否同步」+ 已处理历史(双方时间与落值结论) 台账对照与回写修复: - 行级对照改整单一次性(同名商品不再重复认领)+ 存量纠正 healOrderPrepMapping - 堵住客户绑定回填旁路(免入库单不再被派我方骑手) - 回写触发函数改对象参数(修 branch/order 传反导致的静默不回写) 文档:设计文档 4 篇 +《给主系统 AI 的对接改造清单》§2.9(第六批契约) 回归:服务端全量与 HEAD 基线一致(24 个失败文件同集合);前端 140 文件 / 699 测试全绿;tsc 双端干净
208 lines
10 KiB
TypeScript
208 lines
10 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
|
||
/**
|
||
* S4:免入库回写时机 + 分支覆盖(2026-10-10,设计文档
|
||
* `docs/design/20261010-第三方订单履约事件接收端与免入库回写时机.md` §3.4)。
|
||
*
|
||
* 要钉住的四件事(任何一条被改回去都会让链路重新死锁):
|
||
* ① 免入库订单在**未启用分拣**时也有分支(原实现只有分拣那条路,见 F5);
|
||
* ② 新口径下分拣完毕**停在 50 等主系统确认发车**,不是直接结案 90;
|
||
* ③ 回写 `status=30` 必须在**事务提交后**触发(HTTP 永不在事务内);
|
||
* ④ 配送任务的公共入口对免入库订单**不建任务**(一个卡点覆盖 8+ 调用方)。
|
||
*/
|
||
|
||
const picking = readFileSync(path.join(__dirname, '../src/services/pickingService.ts'), 'utf8');
|
||
const workflow = readFileSync(
|
||
path.join(__dirname, '../src/services/orderWorkflowFulfillmentService.ts'),
|
||
'utf8'
|
||
);
|
||
const writeback = readFileSync(path.join(__dirname, '../src/services/mainSystemWriteback.ts'), 'utf8');
|
||
|
||
describe('① 分拣分支:新旧两种口径都在,且由开关切换', () => {
|
||
it('新口径:停在 50 等主系统确认发车(不结案)', () => {
|
||
expect(picking).toMatch(/skipInboundAwaitPickup/);
|
||
expect(picking).toMatch(/skipInboundAwaitingPickup:\s*true/);
|
||
// 必须明确写出"停在 50"的语义,而不是拿别的东西冒充
|
||
expect(picking).toMatch(/等主系统确认发车|等主系统取货/);
|
||
});
|
||
|
||
it('旧口径保留:开关关闭时仍"分拣完毕即结案 90"', () => {
|
||
expect(picking).toMatch(/completeOrderWithoutDriverDelivery\(orderCode, client\)/);
|
||
});
|
||
|
||
it('两条口径都先走既有边 45→50(结案入口只接受 30/50)', () => {
|
||
expect(picking).toMatch(
|
||
/UPDATE orders SET order_status = 50 WHERE order_code = \$1 AND order_status = 45/
|
||
);
|
||
});
|
||
});
|
||
|
||
describe('② 回写在事务提交后触发(HTTP 永不在事务内)', () => {
|
||
it('enqueueMainSystemWritebackForReadyOrder 的**调用**出现在 COMMIT 之后', () => {
|
||
// 先剥掉 import 行:否则 indexOf 命中的是 import 语句,而不是真正的调用点
|
||
const body = picking
|
||
.split('\n')
|
||
.filter((line) => !/^\s*import\b/.test(line))
|
||
.join('\n');
|
||
const commitIdx = body.indexOf("await client.query('COMMIT')");
|
||
const enqueueIdx = body.indexOf('enqueueMainSystemWritebackForReadyOrder({');
|
||
expect(commitIdx).toBeGreaterThan(-1);
|
||
expect(enqueueIdx).toBeGreaterThan(-1);
|
||
expect(enqueueIdx).toBeGreaterThan(commitIdx);
|
||
});
|
||
|
||
it('三个回写触发函数只接受对象参数(防 branch/order 位置传反)', () => {
|
||
/**
|
||
* 2026-10-11 复核抓到的**真实缺陷**:
|
||
* `orderWorkflowFulfillmentService.tryAdvanceOrderToDelivery` 里写成了
|
||
* `enqueueMainSystemWritebackForReadyOrder(orderCode, branchCode)` —— 与签名 `(branchCode, orderCode)`
|
||
* 正好相反 ⇒ 台账按 (单号当分公司, 分公司当单号) 查 ⇒ **0 行、静默不回写**(不抛错、不写日志)。
|
||
* 两个参数同为 `string`,TS 结构上拦不住 ⇒ 统一改**对象参数**,并用本用例钉死。
|
||
*/
|
||
expect(writeback).toMatch(
|
||
/export function enqueueMainSystemWritebackForReadyOrder\(params: \{\s*branchCode: string \| null;\s*orderCode: string;\s*\}\)/
|
||
);
|
||
expect(writeback).toMatch(
|
||
/export function enqueueMainSystemWritebackForDeliveredOrder\(params: \{\s*branchCode: string \| null;\s*orderCode: string;\s*\}\)/
|
||
);
|
||
expect(writeback).toMatch(/export async function enqueueSkipInboundWritebackIfReady\(\s*params: \{/);
|
||
|
||
const others = [
|
||
['orderWorkflowFulfillmentService', workflow],
|
||
['pickingService', picking],
|
||
['mainSystemWriteback', writeback],
|
||
[
|
||
'processingTaskService',
|
||
readFileSync(path.join(__dirname, '../src/services/processingTaskService.ts'), 'utf8'),
|
||
],
|
||
[
|
||
'preparationFulfillmentService',
|
||
readFileSync(path.join(__dirname, '../src/services/preparationFulfillmentService.ts'), 'utf8'),
|
||
],
|
||
] as const;
|
||
for (const [name, src] of others) {
|
||
const args = [
|
||
...src.matchAll(/enqueueMainSystemWritebackFor(?:Ready|Delivered)Order\(([^)]*)/g),
|
||
...src.matchAll(/enqueueSkipInboundWritebackIfReady\(([^)]*)/g),
|
||
].map((m) => m[1].trim());
|
||
for (const arg of args) {
|
||
// 定义处写作 `params: {`,调用处必须是 `{ ... }` 对象实参
|
||
if (arg.startsWith('params:')) continue;
|
||
expect(arg.startsWith('{'), `${name}: 回写触发必须用对象实参,禁止位置参数`).toBe(true);
|
||
}
|
||
}
|
||
});
|
||
|
||
it('回写触发点是"货备齐",不是 50→60(免入库不走发车)', () => {
|
||
expect(writeback).toMatch(/export function enqueueMainSystemWritebackForReadyOrder/);
|
||
// 两处触发共存:发车(送仓)与备齐(免入库)
|
||
expect(writeback).toMatch(/export function enqueueMainSystemWritebackForDeliveredOrder/);
|
||
});
|
||
|
||
it('兜底重试对"已到待配送(50)"放行(2026-10-11 起对**所有单**,不再限免入库)', () => {
|
||
const retryIdx = writeback.indexOf('export async function retryPendingWritebacksForBranch');
|
||
expect(retryIdx).toBeGreaterThan(-1);
|
||
const fn = writeback.slice(retryIdx, retryIdx + 1800);
|
||
// 开发者 2026-10-11 定稿:不论送仓还是免入库,到 50 就该回写 status=30
|
||
expect(fn).toContain('if (orderStatus < 50) continue;');
|
||
// 老的"仅免入库放行 50"判据已废弃
|
||
expect(fn).not.toMatch(/skipInbound\s*&&\s*orderStatus\s*>=\s*50/);
|
||
});
|
||
});
|
||
|
||
describe('②b 覆盖**所有**把免入库单推到 50 的路径(不能只修一半)', () => {
|
||
const processing = readFileSync(
|
||
path.join(__dirname, '../src/services/processingTaskService.ts'),
|
||
'utf8'
|
||
);
|
||
const prepFulfillment = readFileSync(
|
||
path.join(__dirname, '../src/services/preparationFulfillmentService.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
it('未启用分拣的 40→50(processingTaskService)挂了回写 —— 这是供应商分公司的**实际默认路径**', () => {
|
||
// 实测:SUP37831443 / SUP92185841 分拣=关、库存抵扣=开 ⇒ 免入库单走的就是这一条
|
||
const idx = processing.indexOf('if (!pickingEnabled)');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const branch = processing.slice(idx, idx + 1200);
|
||
expect(branch).toMatch(/order_status = 50/);
|
||
expect(branch, '未启用分拣分支缺回写触发').toMatch(/enqueueSkipInboundWritebackIfReady/);
|
||
});
|
||
|
||
it('免入库+有缺口的 30→50(preparationFulfillmentService)挂了回写', () => {
|
||
const idx = prepFulfillment.indexOf('直送免入库已齐套');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const around = prepFulfillment.slice(Math.max(0, idx - 900), idx);
|
||
expect(around).toMatch(/enqueueSkipInboundWritebackIfReady/);
|
||
// 且必须只在无外层事务时触发(HTTP 不能早于提交)
|
||
expect(around).toMatch(/if \(!client\)/);
|
||
});
|
||
|
||
it('分拣路径(pickingService)仍挂回写,且在 COMMIT 之后', () => {
|
||
const body = picking
|
||
.split('\n')
|
||
.filter((line) => !/^\s*import\b/.test(line))
|
||
.join('\n');
|
||
expect(body.indexOf('enqueueMainSystemWritebackForReadyOrder({')).toBeGreaterThan(
|
||
body.indexOf("await client.query('COMMIT')")
|
||
);
|
||
});
|
||
|
||
it('漏掉的路径仍有对账兜底:任何单到 50 都允许回写', () => {
|
||
const retryIdx = writeback.indexOf('export async function retryPendingWritebacksForBranch');
|
||
expect(writeback.slice(retryIdx, retryIdx + 1800)).toContain('if (orderStatus < 50) continue;');
|
||
});
|
||
});
|
||
|
||
describe('③ 配送任务:整单免入库不建任务(公共入口一处兜住)', () => {
|
||
it('守卫在 ensureDeliveryTaskForOrder 内,且**不依赖**任何配置开关', () => {
|
||
const fnIdx = workflow.indexOf('export async function ensureDeliveryTaskForOrder');
|
||
expect(fnIdx).toBeGreaterThan(-1);
|
||
const body = workflow.slice(fnIdx, fnIdx + 3000);
|
||
expect(body).toMatch(/isOrderFullySkipInbound/);
|
||
// 与"全单直送"同一卡点(同一位置兜住所有调用方)
|
||
expect(body).toMatch(/isOrderOnlyDirectDelivery/);
|
||
// ⚠️ 2026-10-11:守卫与 `MAIN_SYSTEM_SKIP_INBOUND_AWAIT_PICKUP` **解耦** ——
|
||
// 那个开关只管"停 50 等主系统发车才收口",按回滚指引写 0 不得把配送任务放回来。
|
||
expect(body).not.toMatch(/skipInboundAwaitPickup/);
|
||
});
|
||
|
||
it('判定是 bool_and(整单全免入库),混合单必须按送仓处理', () => {
|
||
const ledger = readFileSync(path.join(__dirname, '../src/services/mainSystemPrepLedger.ts'), 'utf8');
|
||
const idx = ledger.indexOf('export async function isOrderFullySkipInbound');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const body = ledger.slice(idx, idx + 1600);
|
||
expect(body).toMatch(/bool_and\(COALESCE\(payload->>'skipInbound', 'false'\) = 'true'\)/);
|
||
// 退回 bool_or 会把混合单误判成免入库 ⇒ 送仓部分没有配送任务
|
||
expect(body).not.toMatch(/bool_or/);
|
||
});
|
||
|
||
it('唯一绕过公共卡点的创建点(客户绑定配送员回填)也必须排除整单免入库', () => {
|
||
const binding = readFileSync(
|
||
path.join(__dirname, '../src/services/customerDeliveryBindingService.ts'),
|
||
'utf8'
|
||
);
|
||
const idx = binding.indexOf('INSERT INTO delivery_tasks');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const stmt = binding.slice(idx, idx + 2000);
|
||
// 复用台账里那份同语义 SQL,禁止在业务代码里另写一套免入库判据
|
||
expect(stmt).toMatch(/fullySkipInboundOrderExistsSql/);
|
||
expect(stmt).toMatch(/NOT EXISTS/);
|
||
});
|
||
});
|
||
|
||
describe('④ 不新增状态边、不碰财务口径', () => {
|
||
it('pickingService 未出现裸写 order_status 的新目标值', () => {
|
||
// 只允许 50(既有边 45→50);不得出现 60/90 的裸写
|
||
const bare = [...picking.matchAll(/UPDATE orders SET order_status = (\d+)/g)].map((m) => m[1]);
|
||
expect(bare.every((v) => v === '50')).toBe(true);
|
||
});
|
||
|
||
it('接收端与分拣改动都不触碰库存/成本表', () => {
|
||
expect(picking).not.toMatch(/INSERT INTO inventory_logs|UPDATE inventory_logs/);
|
||
expect(writeback).not.toMatch(/INSERT INTO inventory_logs|UPDATE inventory_logs/);
|
||
});
|
||
});
|