Files
SupplierSystem/server/tests/pickupSettleInventoryOnClose.test.ts
Ubuntu 42c5cb8332 fix(库存审计面): P0 三条收口 —— 网关禁写库存台账 / 审计表补 RLS / 出库幂等改逐 SKU
8 维度多 agent 审计(含真库核查 + 对抗复核)结论:进销存主链路可用,库存管理体系 4 条高危缺口。
本次落地 P0 三条(开发者批准「可以」):

P0-1 通用数据网关禁写 `inventory` / `inventory_logs`(accessPolicy.DENY_WRITE_TABLES):
  裸写会绕过「备注必填 + 双日志 + FIFO/预留保护」;读取不受影响(两表仍在 ALLOWED_TABLES,
  前端对它们只有 SELECT,已 grep 核对)。台账写入必须走验收/独立入库/自采入库/调整/出库受控路由。

P0-2 审计面补 RLS(迁移 20261014_tenant_inventory_audit_rls.sql):
  apply_tenant_core_rls 的 core_tables 11 → 14 张,新增 inventory_reservations / picking_items /
  picking_tasks(三张都有 branch_code、真库 0 行 NULL),并幂等重放到全部 tenant_*;
  tenantRlsGuard.CORE_TABLES 同步扩到 14 张(名单漏一张 ⇒ 缺 RLS 也会报 ok,这正是本次缺口成因)。
  实测:tenant_br003 有 RLS 的表 11 → 14;正确分支可读 12 行分拣行,错误分支读到 0 行。
  ⚠️ inventory_logs 无 branch_code 列(40 行 + 17 处服务端写入点 + prod-* 脚本),
  需「加列+回填+逐写入点补列+收紧策略」整体做,列下一轮 P0-2b。

P0-3 出库幂等:整单 → **逐 SKU**(inventoryOutboundService + 分拣漏斗):
  · 删掉整单守卫 orderHasOutboundOnClient,改 loadOutboundStateOnClient(DISTINCT sku_code)
    + orderHasPendingOutboundOnClient(逐 SKU 是否仍有"有实物动作却没流水"的行);
  · 计划按已扣 SKU 过滤;全扣过才 already_outbound;流水缺 sku_code 的老数据退回整单语义;
  · 结案兜底只有真扣成功才置 deducted(原先无条件 true ⇒ 全行软扣失败也报"补扣成功");
  · softSkipped 被消费:结案口径上抛 + 分拣漏斗逐行告警(原来算了没人读);
  · listOrdersMissingOutboundSettlement 判据改逐 SKU ⇒ 部分扣过的单也会被 10 分钟任务重扫。
  真库验证:listOrdersMissingOutboundSettlement()=[](无待补扣);已全扣的单再跑返回
  already_outbound 且出库流水 4 条/63.00 不变(不重复扣)。

测试:新增 inventoryAuditHardening.test.ts(9 例:P0-1/P0-2 名单与迁移一致、P0-3 逐 SKU + softSkipped);
更新 pickingCompleteDeductsInventory / pickupSettleInventoryOnClose 两处断言到新代码形状。
回归:server 25 failed / 396 passed(失败文件集与基线逐一比对一致,无新增失败);root tsc 0;
门禁 8 通过 / 1 已知红 / 1 未执行。

待批准(P1):90 状态历史缺口单 1 张(4 行/29 斤)的收口或兜底上延;成本口径三处对齐(财务冻结);
整单抵扣入口补直送/免入库闸门;效期闸门 + 验收写 expiry_date;盘点单据化。
2026-10-14 10:43:26 +08:00

79 lines
3.6 KiB
TypeScript
Raw Permalink 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 { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* 主系统骑手上门取货(免入库)单:收口时软扣库存(开发者 2026-10-13,方案 B)。
*
* 改前:扣库点只有「装车核货/发车」,而这类单不发车、收口又 `skipInventorySettle: true`
* ⇒ 库存永不扣。改后:`picked_up` + status=50 收口时先按既有出库口径**软扣**一次再置 90。
*
* 设计:docs/design/20261013-骑手上门取货单出库时点.md
*/
const readServer = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
describe('收口入口:completeOrderWithoutDriverDelivery 支持软扣', () => {
const svc = readServer('src/services/orderWorkflowFulfillmentService.ts');
it('新增可选 settleInventory;true 走「不跳过 + 软扣」,否则保持「跳过」', () => {
expect(svc).toContain('opts?: { settleInventory?: boolean }');
expect(svc).toContain('opts?.settleInventory === true');
// true:不 skipInventorySettle + 软扣
expect(svc).toContain(
'{ skipInventorySettle: false, softInventorySettle: true, allowFromPrep: true }'
);
// 否:保持 skipInventorySettle: true(商品直送/独立进货逐字不变,旧契约测试照常通过)
expect(svc).toContain('{ skipInventorySettle: true, allowFromPrep: true }');
});
it('finalizeOrderFulfillmentCore 把 softInventorySettle 透传给结案补扣', () => {
expect(svc).toContain('softInventorySettle?: boolean');
expect(svc).toContain('softInventory: options?.softInventorySettle === true');
});
});
describe('骑手取货分支带 settleInventory', () => {
it('picked_up + status=50 收口显式传 { settleInventory: true }', () => {
const events = readServer('src/services/mainSystemFulfillmentEvents.ts');
const start = events.indexOf('if (event === PREP_PICKED_UP_EVENT)');
const end = events.indexOf('// inbound_completed');
expect(start).toBeGreaterThan(-1);
expect(end).toBeGreaterThan(start);
const pickedUp = events.slice(start, end);
expect(pickedUp).toContain('{ settleInventory: true }');
});
it('直送 / 独立进货 / 旧口径结案入口都不传该选项(保持不扣库)', () => {
for (const rel of [
'src/services/preparationFulfillmentService.ts',
'src/services/processingTaskService.ts',
'src/services/orderStatusReconcileService.ts',
'src/services/pickingService.ts',
]) {
expect(readServer(rel)).not.toContain('settleInventory: true');
}
});
});
describe('结案补扣:软扣语义(不足不抛、逐行容错)', () => {
const outbound = readServer('src/services/inventoryOutboundService.ts');
it('ensureOrderInventorySettledOnComplete 支持 softInventory', () => {
expect(outbound).toContain('softInventory?: boolean');
expect(outbound).toContain('softInventory: options?.softInventory === true');
});
it('软扣时「计划补扣」分支不因缺口抛错,且逐行容错', () => {
expect(outbound).toContain('const soft = options?.softInventory === true;');
expect(outbound).toContain('if (short.length && !soft)');
// 2026-10-14(P0-3):软扣跳过的行不再"算了不报",改为记入 softSkipped 上抛 + 日志;
// 语义不变:软扣下库存缺口不抛错、逐行容错、且只有真扣成功才置 deducted。
expect(outbound).toContain(
"if (soft && (error?.code === 'inventory_outbound' || error?.status === 400)) {"
);
expect(outbound).toContain('softSkipped.push({');
expect(outbound).toContain('if (settledAny) deducted = true;');
});
});