原改动人:雨天 <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 字节空文件,疑似误建,既未提交也未删除)
215 lines
8.9 KiB
TypeScript
215 lines
8.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import {
|
||
claimOwnOrderReservationForProcessingPick,
|
||
sumActiveReservationsForOrders,
|
||
} from '../src/services/processingPickReservationService';
|
||
|
||
/**
|
||
* **加工领料可用「本单自己的」预留**(2026-10-14 开发者批准)。
|
||
*
|
||
* 事故:需求单成品「二刀肉 5 斤 · 去皮」的原料,就是**本单自己**已被预留的那批货
|
||
* (`ORD_API_20261014_BR002_0002`:预留持有者 = 该单自身加工行),
|
||
* 而旧口径领料一律扣「毛可用 − **全部**在用预留」⇒ 5−5=0 ⇒ **需求单永远领不到料**。
|
||
*
|
||
* 口径(批准):可领 = 毛可用 − **他单**预留;领料后按量**消费本单预留**。
|
||
*
|
||
* ⚠️ 必须成对:只放开校验不消费预留 ⇒ 库存已扣为 0、预留仍 status=10
|
||
* ⇒ ① 发车扣不到(短送)② 加工成品又产出一份 ⇒ **同一批货算两次**。
|
||
* 本文件第二节专门钉这条。
|
||
*/
|
||
|
||
const SRC = path.join(__dirname, '..', 'src');
|
||
const read = (rel: string) => readFileSync(path.join(SRC, rel), 'utf8');
|
||
|
||
type Call = { sql: string; params?: unknown[] };
|
||
|
||
function makeDb(reservations: Array<{ id: string; qty: number }>, calls: Call[]) {
|
||
return {
|
||
query: async (sql: string, params?: unknown[]) => {
|
||
const text = String(sql);
|
||
calls.push({ sql: text, params });
|
||
if (text.includes('FROM inventory_reservations') && text.includes('FOR UPDATE')) {
|
||
return { rowCount: reservations.length, rows: reservations };
|
||
}
|
||
return { rowCount: 0, rows: [] };
|
||
},
|
||
};
|
||
}
|
||
|
||
describe('① claimOwnOrderReservationForProcessingPick:按领料量消费本单预留', () => {
|
||
it('全额领料 ⇒ 整条预留置 status=20(已消费)', async () => {
|
||
const calls: Call[] = [];
|
||
const claimed = await claimOwnOrderReservationForProcessingPick(
|
||
makeDb([{ id: '11111111-1111-1111-1111-111111111111', qty: 5 }], calls) as never,
|
||
{ orderCodes: ['ORD_A'], skuCode: 'SKU1', branchCode: 'BR002', qty: 5, planned: 5 }
|
||
);
|
||
expect(claimed).toBe(5);
|
||
const consume = calls.find((c) => c.sql.includes('SET status = 20'));
|
||
expect(consume).toBeTruthy();
|
||
// 全额消费不准降 qty(CHECK qty > 0 且语义上是"消费")
|
||
expect(calls.some((c) => c.sql.includes('SET qty = $2'))).toBe(false);
|
||
});
|
||
|
||
it('部分领料 ⇒ 只把预留降额到剩余,不整条消费', async () => {
|
||
const calls: Call[] = [];
|
||
const claimed = await claimOwnOrderReservationForProcessingPick(
|
||
makeDb([{ id: '22222222-2222-2222-2222-222222222222', qty: 5 }], calls) as never,
|
||
{ orderCodes: ['ORD_A'], skuCode: 'SKU1', branchCode: 'BR002', qty: 2, planned: 5 }
|
||
);
|
||
expect(claimed).toBe(2);
|
||
const reduce = calls.find((c) => c.sql.includes('SET qty = $2'));
|
||
expect(reduce).toBeTruthy();
|
||
expect(reduce!.params).toEqual(['22222222-2222-2222-2222-222222222222', 3]);
|
||
expect(calls.some((c) => c.sql.includes('SET status = 20'))).toBe(false);
|
||
});
|
||
|
||
it('多行预留:逐行取用,直到领料量覆盖完(一次领料算满一次)', async () => {
|
||
const calls: Call[] = [];
|
||
const claimed = await claimOwnOrderReservationForProcessingPick(
|
||
makeDb(
|
||
[
|
||
{ id: '33333333-3333-3333-3333-333333333333', qty: 3 },
|
||
{ id: '44444444-4444-4444-4444-444444444444', qty: 4 },
|
||
],
|
||
calls
|
||
) as never,
|
||
{ orderCodes: ['ORD_A'], skuCode: 'SKU1', branchCode: 'BR002', qty: 5, planned: 7 }
|
||
);
|
||
expect(claimed).toBe(5);
|
||
const status20 = calls.filter((c) => c.sql.includes('SET status = 20'));
|
||
const reduced = calls.filter((c) => c.sql.includes('SET qty = $2'));
|
||
expect(status20.length).toBe(1); // 3 斤那条整条消费
|
||
expect(reduced.length).toBe(1); // 4 斤那条降到 2
|
||
expect(reduced[0].params?.[1]).toBe(2);
|
||
});
|
||
|
||
it('幂等:已消费过的不再重复消费(alreadyClaimed 计入预算)', async () => {
|
||
const calls: Call[] = [];
|
||
const claimed = await claimOwnOrderReservationForProcessingPick(
|
||
makeDb([{ id: '55555555-5555-5555-5555-555555555555', qty: 3 }], calls) as never,
|
||
{ orderCodes: ['ORD_A'], skuCode: 'SKU1', branchCode: 'BR002', qty: 2, planned: 5, alreadyClaimed: 2 }
|
||
);
|
||
// 目标 = 5 − 2(已消费) − 2(本次) = 1 ⇒ 只降 2
|
||
expect(claimed).toBe(2);
|
||
expect(calls.filter((c) => c.sql.includes('SET qty = $2')).length).toBe(1);
|
||
});
|
||
|
||
it('无订单 / 无量 ⇒ 早退,一条 SQL 都不产生(不误伤他单)', async () => {
|
||
const calls: Call[] = [];
|
||
const db = makeDb([], calls) as never;
|
||
expect(
|
||
await claimOwnOrderReservationForProcessingPick(db, {
|
||
orderCodes: [],
|
||
skuCode: 'SKU1',
|
||
qty: 5,
|
||
planned: 5,
|
||
})
|
||
).toBe(0);
|
||
expect(
|
||
await claimOwnOrderReservationForProcessingPick(db, {
|
||
orderCodes: ['ORD_A'],
|
||
skuCode: 'SKU1',
|
||
qty: 0,
|
||
planned: 5,
|
||
})
|
||
).toBe(0);
|
||
expect(calls.length).toBe(0);
|
||
});
|
||
|
||
it('本单无活跃预留 ⇒ 返回 0(只读查询,不写任何东西)', async () => {
|
||
const calls: Call[] = [];
|
||
const claimed = await claimOwnOrderReservationForProcessingPick(makeDb([], calls) as never, {
|
||
orderCodes: ['ORD_A'],
|
||
skuCode: 'SKU1',
|
||
branchCode: 'BR002',
|
||
qty: 5,
|
||
planned: 5,
|
||
});
|
||
expect(claimed).toBe(0);
|
||
// 允许 SELECT(要判断有没有预留),但**不许**有任何写语句
|
||
expect(calls.some((c) => c.sql.includes('UPDATE inventory_reservations'))).toBe(false);
|
||
});
|
||
|
||
it('只查**入参订单自己**的预留(他单预留不在 SQL 范围内)', async () => {
|
||
const calls: Call[] = [];
|
||
await claimOwnOrderReservationForProcessingPick(
|
||
makeDb([{ id: '66666666-6666-6666-6666-666666666666', qty: 1 }], calls) as never,
|
||
{ orderCodes: ['ORD_A', 'ORD_B'], skuCode: 'SKU1', branchCode: 'BR002', qty: 1, planned: 1 }
|
||
);
|
||
const select = calls.find((c) => c.sql.includes('FOR UPDATE'));
|
||
expect(select!.sql).toContain('order_code = ANY($2::text[])');
|
||
expect(select!.params?.[1]).toEqual(['ORD_A', 'ORD_B']);
|
||
});
|
||
});
|
||
|
||
describe('② 成对性守卫:放行必须同时消费预留(防"库存已扣、预留仍在")', () => {
|
||
const src = read('services/processingDemandSheetService.ts');
|
||
|
||
it('需求单领料:校验按「毛可用 − 他单预留」,且领料后消费本单预留', () => {
|
||
const at = src.indexOf('ownOrderCodes');
|
||
expect(at).toBeGreaterThan(-1);
|
||
const body = src.slice(at, at + 4000);
|
||
expect(body).toContain('sumAvailableQty');
|
||
expect(body).toContain('sumOtherOrderReservations');
|
||
expect(body).toContain('claimOwnOrderReservationForProcessingPick');
|
||
// 扣库必须带上本单订单号(内部 getClaimableStockQty 才会豁免本单预留)
|
||
expect(body).toContain('orderCode: ownOrderCodes[0] || null');
|
||
// 不得再回退成"扣全部在用预留"
|
||
expect(body).not.toContain('getClaimableStockQty(client, skuCode, branchCode, null)');
|
||
});
|
||
|
||
it('需求单领料在**扣库之后**才消费预留(顺序错会漏消费失败回滚点)', () => {
|
||
const body = src.slice(src.indexOf('ownOrderCodes'));
|
||
const deductAt = body.indexOf('await fifoDeductInventory');
|
||
const claimAt = body.indexOf('claimOwnOrderReservationForProcessingPick');
|
||
expect(deductAt).toBeGreaterThan(-1);
|
||
expect(claimAt).toBeGreaterThan(deductAt);
|
||
});
|
||
|
||
it('候选列表按 ownOrderCodes 排除本单预留(SQL 里 NOT ... = ANY)', () => {
|
||
const svc = read('services/processingStandaloneService.ts');
|
||
expect(svc).toContain('excludeOwnSql');
|
||
// 编号**动态取号**(写死 $4 会在"搜索为空"时留空洞 ⇒ PG 报 parameter type 错误,2026-10-14 事故)
|
||
expect(svc).toContain("excludeOwnSql = ` AND NOT (r.order_code = ANY($${params.length}::text[]))`");
|
||
expect(svc).toContain('ownOrderCodes');
|
||
});
|
||
|
||
it('路由:需求单领料传 demand_task_id 解析本单订单号', () => {
|
||
for (const rel of [
|
||
'modules/workflow/routes/inspectionRoutes.ts',
|
||
'routes/mobile/mobilePurchaseRoutes.ts',
|
||
]) {
|
||
const route = read(rel);
|
||
expect(route).toContain('demand_task_id');
|
||
expect(route).toContain('listDemandSheetOwnOrderCodes');
|
||
}
|
||
});
|
||
});
|
||
|
||
describe('③ sumActiveReservationsForOrders:领料前取 planned', () => {
|
||
it('无订单 ⇒ 0 且不查库', async () => {
|
||
const calls: Call[] = [];
|
||
expect(
|
||
await sumActiveReservationsForOrders(makeDb([], calls) as never, {
|
||
orderCodes: [],
|
||
skuCode: 'SKU1',
|
||
})
|
||
).toBe(0);
|
||
expect(calls.length).toBe(0);
|
||
});
|
||
|
||
it('取合计值', async () => {
|
||
const db = {
|
||
query: vi.fn(async () => ({ rowCount: 1, rows: [{ total: 7.5 }] })),
|
||
};
|
||
expect(
|
||
await sumActiveReservationsForOrders(db as never, {
|
||
orderCodes: ['ORD_A'],
|
||
skuCode: 'SKU1',
|
||
})
|
||
).toBe(7.5);
|
||
});
|
||
});
|