Files
SupplierSystem/server/tests/mainSystemImportUnassigned.test.ts
T
Ubuntu 9005c096b2
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
fix(状态推进): 抵扣完毕必须由代码当次推进 —— 判据收口为一份 + 三条写入路径都补推进
开发者 2026-10-13:「都抵扣完了为什么订单还是卡在待分配……代码层面存在问题 核查一下,
不能光靠脚本,我需要后续不能出现类似的情况……这个待分拣应该是定时器兜底机制推进过去的」。

核查结论(operation_logs 真库时间线):
  09:48:06 点「库存抵扣」→ 抵 46.064 斤、**仍有缺口**(苹果 13.784 / 抄手皮 3.152)
           ⇒ 路由内推进器给「尚有未履约明细」、**状态保持 20** ✅ 这是正确的;
  09:51:22 `order_status_reconcile` 与同一次按钮请求同毫秒(.661 / 响应 .694)
           ⇒ 当时的 45 是**这次请求内**推进的,不是 15 分钟调度器。
所以截图「还写待分配」是有缺口时的正确表现;真正要补的是"有些写入路径只有定时器兜底"。

代码层收口(两件,均由测试钉住):
① 「该行已履约」判据只留**一份** —— `orderItemFulfillmentService.FULFILLED_BRANCHES_SQL`,
   分配侧 `isOrderItemFulfillmentValid` 与推进器 `listUncoveredOrderItems` 共用。
   此前两条查询各写一遍(文件里自己写着「必须同改」)——一次漂移就会出现
   「分配侧认为已履约 ⇒ 推到 30/45,推进器却认为有缺口」的自相矛盾;现在结构上不可能漂移。
   库存履约分支仍要求**足额**(stock_cover_qty ≥ quantity),缺口一律不推进。
② 三条写入路径都在**当次请求内**推进(幂等、失败只告警):
   · 主系统导入(建单/追加)→ `advanceImportedOrders()`(推送与拉取两条通道)
   · 订单详情/小程序改明细保存 → `syncOrderManageItems()` 提交后
   · Web/小程序下单提交 → `customerOrderSubmitService` COMMIT 后
   · 「库存抵扣」按钮 → 路由 reconcileOrderWorkflowStatus(原本就有)
   触发条件统一「本次没建备货也没建自采且状态刚被置 30」,推进器内部按
   uncovered / pendingPreps / pendingPurchases 严格判断。

真库实测(同一个调用内完成,与定时器无关;验证单已删、账外余额已回补):
  A 部分抵扣(米块 10 斤,账外只够 6)⇒ 状态 **20 待分配**、无备货/自采 ✅ 不推进
  B 全额抵扣(米块 6 斤)           ⇒ 状态 **45 待分拣** ✅ 当次完成

回归:server 套件 26 failed files = 基线(无新增;33 failed / 2707 passed);
更新 3 个按"判据两份"写的旧断言(directToDeliveryMode / stockCoverGuard / inventoryFullCoverFulfillment)
为"单份共享常量";root+server tsc exit 0;门禁 8 通过/1 已知红/1 未执行。
2026-10-13 18:03:13 +08:00

251 lines
12 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 { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { resolveWriteAllocationIntent } from '../src/services/orderManageItemsService';
/**
* **主系统导入单默认「未分配」** 的回归(2026-10-13 真库 BR003 土豆单
* `ORD_API_20261013_BR003_0003`,开发者报障):
*
* 「主系统分配过来的应该是先库存抵扣,然后缺少的部分才是进行选择分配给供应商还是自采去采购,
* **不能替他直接分配好给谁**;而且分配给供应商的默认是直接供应完成,导致库存抵扣也无法生效。」
*
* 真库证据(改前):土豆行 `supplier_code=SUP61936431(测试仓上)`、`stock_cover_qty=0`、
* `order_status=40`;同 SKU 批次 `IN20261013X00E9T` 物理自由 30 斤,抵扣日志 8 连发
* `物理自由库存 30,其中账外只够 0`。行一旦有供应商备货 ⇒ `isOrderItemFulfilled` 为真
* ⇒ `tryFulfillFromInventory` 直接返回 ⇒ **抵扣永远轮不到**。
*
* 口径(开发者 2026-10-13 确认):导入行先抵扣、缺口留「未分配」,人工在订单详情选
* 「分配给供应商 / 自采」;应付只对真正采购的量。
* 见 docs/design/20261013-主系统导入单默认未分配(先抵扣后人工选择供应商或自采).md
*/
const itemsSrc = readFileSync(
path.join(__dirname, '../src/services/orderManageItemsService.ts'),
'utf8'
);
const submitSrc = readFileSync(
path.join(__dirname, '../src/services/customerOrderSubmitService.ts'),
'utf8'
);
const importSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
'utf8'
);
const allocationSrc = readFileSync(
path.join(__dirname, '../src/services/orderAllocationService.ts'),
'utf8'
);
describe('分配意图判定:谁能替人分配、谁不能', () => {
it('① 默认(Web 改明细 / 小程序下单):跑规则引擎,新行照常自动分配', () => {
expect(resolveWriteAllocationIntent({})).toEqual({
useItemSuppliers: false,
autoAllocateNewItems: true,
});
});
it('② 手动分配页保存:只落已有供应商,但新增行仍由规则引擎定(原行为不变)', () => {
expect(resolveWriteAllocationIntent({ preferItemSuppliers: true })).toEqual({
useItemSuppliers: true,
autoAllocateNewItems: true,
});
});
it('③ **主系统导入**:只落已有供应商,且新行也不许自动分配 ⇒ 缺口保持未分配', () => {
expect(resolveWriteAllocationIntent({ keepNewItemsUnassigned: true })).toEqual({
useItemSuppliers: true,
autoAllocateNewItems: false,
});
// 两个标志同时给(导入路径可能的组合)也必须是「不许自动分配」
expect(
resolveWriteAllocationIntent({
preferItemSuppliers: true,
keepNewItemsUnassigned: true,
})
).toEqual({ useItemSuppliers: true, autoAllocateNewItems: false });
});
it('④ 显式 false 不等于「不传」以外的行为(只是不覆盖原行为)', () => {
expect(resolveWriteAllocationIntent({ keepNewItemsUnassigned: false })).toEqual({
useItemSuppliers: false,
autoAllocateNewItems: true,
});
});
});
describe('落点接线:标志真的接到了分配调用上', () => {
it('⑤ 新行只在不带导入标志时才进 autoAllocateItemIds', () => {
const pushIdx = itemsSrc.indexOf('autoAllocateItemIds.push(newId)');
expect(pushIdx).toBeGreaterThan(0);
const guard = itemsSrc.slice(Math.max(0, pushIdx - 420), pushIdx);
expect(guard).toContain('allocationIntent.autoAllocateNewItems');
});
it('⑥ 分配调用按 allocationIntent.useItemSuppliers 选择「不跑规则引擎」分支', () => {
const callIdx = itemsSrc.indexOf('allocation = await reprocessOrderAllocation(');
expect(callIdx).toBeGreaterThan(0);
const branch = itemsSrc.slice(callIdx, callIdx + 420);
expect(branch).toContain('allocationIntent.useItemSuppliers');
// 该分支必须把「只认已有供应商」显式传给分配(否则规则引擎仍会替人分配)
expect(branch).toContain('preferItemSuppliers: true');
});
it('⑦ 主系统导入的建单与追加两条路径都传导入标志', () => {
expect(importSrc).toContain('keepItemsUnassigned: true');
expect(importSrc).toContain('keepNewItemsUnassigned: true');
});
it('⑧ 建单路径只在显式置位时才传 preferItemSuppliers(人工录单/小程序行为不变)', () => {
expect(submitSrc).toContain("input.keepItemsUnassigned === true ? { preferItemSuppliers: true } : {}");
});
it('⑨ **先抵扣后分配**:整单抵扣发生在规则引擎分支之前(抵扣不属于「分配」的下游)', () => {
const coverIdx = allocationSrc.indexOf('await applyStockCoverForOrder(');
const ruleIdx = allocationSrc.indexOf('const pendingItemsRaw = !useItemSuppliers');
expect(coverIdx).toBeGreaterThan(0);
expect(ruleIdx).toBeGreaterThan(0);
expect(coverIdx).toBeLessThan(ruleIdx);
});
it('⑩ 已有供应商备货的行会短路抵扣(这正是「替人分配 ⇒ 抵扣失效」的机制,勿删)', () => {
const fulfillIdx = allocationSrc.indexOf('async function tryFulfillFromInventory');
const guardIdx = allocationSrc.indexOf('isOrderItemFulfilled(', fulfillIdx);
expect(guardIdx).toBeGreaterThan(fulfillIdx);
});
});
describe('全额抵扣的收尾:不分配也必须能升状态', () => {
/**
* 2026-10-13 真库实测回归(`ORD_API_20261013_BR003_0004` 海带丝 20 斤):
* 导入行不再走规则引擎后,**裸行不会进明细级抵扣**(`tryFulfillFromInventory` 的 `coveredFully`
* 那一支才会写「库存履约」行)⇒ 整单抵扣虽写了 `stock_cover_qty=20`,却没有任何
* `order_item_fulfillments` 行 ⇒ `isOrderItemFulfillmentValid` 判未履约 ⇒ 订单卡在 20 待分配。
* 修法:覆盖检查之前先按 `stock_cover_qty >= quantity` 补齐库存履约行(幂等,已有备货/自采不碰)。
*/
it('⑪ 覆盖检查之前先补「全额库存抵扣」的履约行', () => {
const callIdx = allocationSrc.indexOf('await linkFullInventoryCoverFulfillments(orderCode, client)');
expect(callIdx).toBeGreaterThan(0);
const checkIdx = allocationSrc.indexOf('if (await isOrderItemFulfilled(orderCode, item, client)) continue;');
expect(checkIdx).toBeGreaterThan(callIdx);
});
it('⑫ 补履约行只认「全额覆盖 + 无真实履约链」,且不碰 >=50 的单', () => {
const fnIdx = allocationSrc.indexOf('async function linkFullInventoryCoverFulfillments');
expect(fnIdx).toBeGreaterThan(0);
const body = allocationSrc.slice(fnIdx, fnIdx + 1800);
expect(body).toContain('COALESCE(oi.stock_cover_qty, 0) >= COALESCE(oi.quantity, 0) - 1e-9');
expect(body).toContain('COALESCE(o.order_status, 0) < 50');
expect(body).toContain('row.supplier_preparation_id != null || row.purchase_code != null');
expect(body).toContain('linkOrderItemToInventoryCover(');
});
});
describe('手动「库存抵扣」按钮:只抵扣、不替人分配', () => {
/**
* 2026-10-13 开发者:「如果是扣完了、不需要去采购,那么应该是直接待分拣才对」。
*
* 该按钮走 `applyInventoryOffsetForMainSystemOrder` → `reprocessOrderAllocation`。
* 此前**不带选项** ⇒ `useItemSuppliers = !smartEnabled`,而智能分配默认开 ⇒ 规则引擎
* 会把未分配的行一并分配给厂商(真库实测:点一次就挂上「测试仓上」、备货直接供应完成、
* 抵扣从此轮不到它)。现在固定传 `preferItemSuppliers: true`:只落已有供应商、未指定行保持待分配;
* 抵扣照旧在入口最前跑,全额抵扣 ⇒ 30 + 路由 `reconcileOrderWorkflowStatus` 续推 40/45。
*/
it('⑬ 抵扣入口带「只落已有供应商」语义', () => {
const fnIdx = allocationSrc.indexOf('export async function applyInventoryOffsetForMainSystemOrder');
expect(fnIdx).toBeGreaterThan(0);
const body = allocationSrc.slice(fnIdx, fnIdx + 3200);
const callIdx = body.indexOf('await reprocessOrderAllocation(');
expect(callIdx).toBeGreaterThan(0);
expect(body.slice(callIdx, callIdx + 400)).toContain('preferItemSuppliers: true');
});
it('⑭ 抵扣按钮后仍会推进状态(路由复用既有对账推进器)', () => {
const routeSrc = readFileSync(
path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'),
'utf8'
);
const idx = routeSrc.indexOf("router.post('/orders/:orderCode/inventory-offset'");
expect(idx).toBeGreaterThan(0);
const body = routeSrc.slice(idx, idx + 4000);
expect(body).toContain('reconcileOrderWorkflowStatus');
});
it('⑮ 「选择抵扣商品」候选同时给出「可抵扣量」(只看物理在库会误导)', () => {
const mappingSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemItemSkuMappingService.ts'),
'utf8'
);
const fnIdx = mappingSrc.indexOf('export async function listCoverableSkuOptions');
expect(fnIdx).toBeGreaterThan(0);
const body = mappingSrc.slice(fnIdx, fnIdx + 2600);
expect(body).toContain('coverable_qty');
expect(body).toContain('sumCoverableOutsourcedQty');
const modalSrc = readFileSync(
path.join(__dirname, '../../src/components/order/OrderItemCoverSkuModal.tsx'),
'utf8'
);
expect(modalSrc).toContain('coverable_qty');
expect(modalSrc).toContain("t('orders.coverSku.coverable'");
});
});
describe('状态推进:全靠代码、不靠定时器(2026-10-13 开发者要求)', () => {
/**
* 开发者原话:「都抵扣完了为什么订单还是卡在待分配……我需要后续不能出现类似的情况,
* 虽然现在是待分拣了,但是这个待分拣应该是定时器兜底机制推进过去的」。
*
* 两条硬要求:
* ① 抵扣完毕 ⇒ **当次请求内**推进(不是等 15 分钟调度器);
* ② 有缺口 ⇒ 任何路径都不许推进(真库 2026-10-13 现场:还没抵完就成了待分拣)。
*/
const manageSrc = readFileSync(
path.join(__dirname, '../src/services/orderManageItemsService.ts'),
'utf8'
);
const submitSrc2 = readFileSync(
path.join(__dirname, '../src/services/customerOrderSubmitService.ts'),
'utf8'
);
const fulfillmentSrc = readFileSync(
path.join(__dirname, '../src/services/orderItemFulfillmentService.ts'),
'utf8'
);
it('① 三条写入路径都在**提交后当次请求内**推进(导入 / 改明细 / 下单)', () => {
// 导入:落单后推进
expect(importSrc).toContain('advanceImportedOrders');
// 改明细保存(订单详情 / 小程序)
expect(manageSrc).toContain('reconcileOrderWorkflowStatus(orderCode)');
expect(manageSrc).toContain('allocation.orderStatusUpdated === 30');
// 下单提交
expect(submitSrc2).toContain('reconcileOrderWorkflowStatus(orderCode)');
expect(submitSrc2).toContain('allocation.orderStatusUpdated === 30');
});
it('② 「已履约」判据只有一份(两份漂移会让分配侧与推进器互相打脸)', () => {
const defs = fulfillmentSrc.match(/const FULFILLED_BRANCHES_SQL =/g) || [];
expect(defs.length).toBe(1);
// 两份查询都必须用它,且不得再各写一份 <= 分支
const users = fulfillmentSrc.match(/\$\{FULFILLED_BRANCHES_SQL\}/g) || [];
expect(users.length).toBe(2);
expect(fulfillmentSrc).not.toContain('-- 注意:本判据在本文件有**两份**');
});
it('③ 库存履约分支仍要求**足额**覆盖(不足额一律判未履约)', () => {
expect(fulfillmentSrc).toContain(
'COALESCE(oi.stock_cover_qty, f.inventory_cover_qty, 0) >= COALESCE(oi.quantity, 0) - 1e-9'
);
// 30→40 的闸门必须查缺口,不能只看「有没有备货/采购」
const prepSrc = readFileSync(
path.join(__dirname, '../src/services/preparationFulfillmentService.ts'),
'utf8'
);
const idx = prepSrc.indexOf('const uncoveredBeforeAdvance = await listUncoveredOrderItems');
expect(idx).toBeGreaterThan(0);
expect(prepSrc.slice(idx, idx + 600)).toContain('尚有未履约明细');
});
});