Files
SupplierSystem/server/tests/stockCoverSupplierFlagRepair.test.ts
T
Ubuntu 83825fa2df
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(主系统同步单): 修「标着库存抵扣但抵扣为 0」的卡行 + 放开「同步行可删」(删单仍禁)
开发者 2026-10-17 报障:「订单管理,从配送系统同步过来的订单商品行可以删除,同步过来的商品无法处理」。

生产只读排查(BR005 / ORD_API_20261017_BR005_0001):香蕉 500 斤这一行
supplier_code='STOCK_COVER'、stock_cover_qty=0、履约行 inventory_cover_qty=0,
既没有采购单也没有备货单 ⇒ 既不走库存抵扣也不进采购/备货 ⇒ 验收/分拣拿不到货。
全生产扫一遍只有这 1 行(br005 1 行,其余租户 0 行)。

A)卡行修复
· 迁移 20261056:三条边界**同时**成立才把 supplier_code 改回 'UNASSIGNED'
  (① stock_cover_qty≤0 ② 履约行也没有手动抵扣量 ③ 未退货)——只纠正标记、不删行、
  不误伤"真抵扣"和"手动选择抵扣商品"的行;遍历所有 tenant_* + tenant_template,幂等。
· 增量自愈:inventoryStockCoverService.normalizeStockCoverSupplierFlags(),在整单抵扣收尾
  (补完 FIFO 批次计划之后)调用;失败只记日志、不影响本次抵扣结果。

B)同步行可删(口径变更,开发者 2026-10-17 授权,覆盖 2026-10-10 的"禁删")
· 服务层不再对主系统单抛 409 main_system_item_locked;
· 删除时**同一事务**把该行行级台账(main_system_order_import_items,按 our_order_item_id 反查)
  置 disposition='cancelled' ⇒ needsAppendToOrder 不再追加、台账 upsert 的处置是粘性的
  ⇒ 下一轮对账/主系统 webhook 追加不会把它塞回来;
· markPrepDisposition 增加可选 client,保证处置与删除同事务提交(失败只记日志,巡检兜底);
· 界面:主系统行也出现删除按钮(复用既有 title 文案,无新增文案);
· **删单仍然禁止**(orderDeleteService.assertNotMainSystemOrder 未动),缺货按钮保留。

验证:
· 本地真库(事务内造 3 行 + ROLLBACK):假标记行 STOCK_COVER/0 → UNASSIGNED;
  STOCK_COVER/500 与"履约行 manual cover 500"两行保持 STOCK_COVER(不误伤)
· cd server && npx tsc --noEmit → 0 错;npm run typecheck(web)→ 0 错
· 新增用例:stockCoverSupplierFlagRepair 5 条、mainSystemItemDeletable 9 条,全过;
  相关后端子集 35 文件 / 536 用例通过(independentInbound 有 1 条既有失败,与基线同)
· 前端 order 相关 42 文件 / 245 用例全过
· 设计门禁:8 通过 / 1 已知红 check-ai-cost / verify-facts 因本地缺 tenant_br003 红(均基线)
2026-10-17 16:51:09 +08:00

72 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 { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* 「标着库存抵扣、实际抵扣为 0」的假标记行(2026-10-17 生产真机事故)。
*
* 生产 `BR005 / ORD_API_20261017_BR005_0001`:香蕉 500 斤这一行
* `supplier_code='STOCK_COVER'` 但 `stock_cover_qty=0`、履约行 `inventory_cover_qty=0`,
* 既没有采购单也没有备货单 ⇒ 后续验收/分拣拿不到货,界面表现为"同步过来的商品无法处理"。
*
* 这个文件钉住修复的两条腿:
* ① **存量**:迁移 `20261056` 按"三条边界同时成立"纠正(不误伤真抵扣行);
* ② **增量**:整单抵扣收尾自愈(`normalizeStockCoverSupplierFlags`),否则下次还会出现。
*/
const service = readFileSync(
path.join(__dirname, '../src/services/inventoryStockCoverService.ts'),
'utf8'
);
const migration = readFileSync(
path.join(__dirname, '../../migrations/20261056_fix_stock_cover_supplier_flags.sql'),
'utf8'
);
describe('存量修复:迁移 20261056 的判据', () => {
it('三条安全边界齐全:抵扣为 0 / 履约行也无手动抵扣量 / 未退货', () => {
expect(migration).toMatch(/UPPER\(TRIM\(COALESCE\(oi\.supplier_code, ''\)\)\) = 'STOCK_COVER'/);
expect(migration).toMatch(/COALESCE\(oi\.stock_cover_qty, 0\) <= 1e-9/);
expect(migration).toMatch(/NOT EXISTS \(/);
expect(migration).toMatch(/COALESCE\(f\.inventory_cover_qty, 0\) > 1e-9/);
expect(migration).toMatch(/COALESCE\(oi\.is_returned, false\) = false/);
});
it('只改成本地可处理的 UNASSIGNED(交回采购/备货),不是删行、不是改成自采', () => {
expect(migration).toMatch(/SET supplier_code = 'UNASSIGNED'/);
expect(migration).not.toMatch(/DELETE FROM %1\$I\.order_items/);
expect(migration).not.toMatch(/INTERNAL/);
});
it('幂等 + 遍历所有租户 schema(含 tenant_template),且不因模板被 RLS 挡住而中断迁移', () => {
expect(migration).toMatch(/tenant_template' OR nspname LIKE 'tenant\\_b%'/);
expect(migration).toMatch(/EXCEPTION WHEN others THEN/);
expect(migration).toMatch(/set_config\('app\.branch_code'/);
});
});
describe('增量自愈:整单抵扣收尾纠正假标记', () => {
it('导出 normalizeStockCoverSupplierFlags,且判据与迁移同口径', () => {
const idx = service.indexOf('export async function normalizeStockCoverSupplierFlags');
expect(idx).toBeGreaterThan(-1);
const block = service.slice(idx, idx + 1600);
expect(block).toMatch(/SET supplier_code = 'UNASSIGNED'/);
expect(block).toMatch(/COALESCE\(oi\.stock_cover_qty, 0\) <= 1e-9/);
expect(block).toMatch(/COALESCE\(f\.inventory_cover_qty, 0\) > 1e-9/);
expect(block).toMatch(/COALESCE\(oi\.is_returned, false\) = false/);
});
it('applyStockCoverForOrder 收尾一定调用它(且失败不影响本次抵扣结果)', () => {
const applyIdx = service.indexOf('export async function applyStockCoverForOrder(');
expect(applyIdx).toBeGreaterThan(-1);
const tail = service.slice(applyIdx, applyIdx + 6000);
const callIdx = tail.indexOf('normalizeStockCoverSupplierFlags(db, code)');
expect(callIdx).toBeGreaterThan(-1);
// 必须先补完批次计划、再自愈(顺序写反会把刚抵上的行误清)
expect(tail.indexOf('planFifoBatchesForCoveredItems')).toBeLessThan(callIdx);
// 自愈失败只记日志
const around = tail.slice(Math.max(0, callIdx - 300), callIdx + 200);
expect(around).toMatch(/catch/);
expect(around).toMatch(/不影响抵扣/);
});
});