来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
196 lines
6.9 KiB
TypeScript
196 lines
6.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* 采购单实收回写归属判定。
|
||
*
|
||
* 背景与三次踩坑(详见 docs/design/20260913-self-purchase-partial-inbound-qty.md):
|
||
* 1. 采购单**入库即置 purchase_status=30**,所以不能用状态当写入门槛,
|
||
* 否则首批写入后补收批次会被永久挡掉;
|
||
* 2. 单据上的实收可能来自前序环节——备货完成(`completeSelfPurchase`,status 20)预填的备货量、
|
||
* 或分批入库首批的缺量前值——这些都必须被验收结果覆盖;
|
||
* 3. 但人工改量(`purchaseAmendService`)也直接写 actual_qty,不能抹掉。
|
||
*
|
||
* 因此判定收敛为「只在本流程能确定这个值是自己写的时才覆盖」。
|
||
*/
|
||
|
||
const PO = 'PUR_TEST_1';
|
||
const ORDER = 'ORD_TEST_1';
|
||
const SKU = 'SKU1';
|
||
const CURRENT_INSPECTION = 'insp-current';
|
||
const PARENT_INSPECTION = 'insp-parent';
|
||
|
||
type InspectionRow = {
|
||
id: string;
|
||
actual_qty: string;
|
||
created_at: Date;
|
||
parent_id: string | null;
|
||
/** 分批入库链路行:首批 partial_inbound / 补收 partial_follow_up_of */
|
||
is_chain_row: boolean;
|
||
};
|
||
|
||
/** 记录所有执行过的 SQL,便于断言最终写没写 */
|
||
function makeClient(opts: { poActual: string | null; purchaseStatus?: number; batches: InspectionRow[] }) {
|
||
const executed: Array<{ sql: string; params: unknown[] }> = [];
|
||
const query = vi.fn(async (sql: string, params: unknown[] = []) => {
|
||
executed.push({ sql, params });
|
||
if (/FROM purchase_orders/.test(sql)) {
|
||
return {
|
||
rows: [{ purchase_status: opts.purchaseStatus ?? 30, actual_qty: opts.poActual }],
|
||
rowCount: 1,
|
||
};
|
||
}
|
||
if (/FROM inspections/.test(sql)) {
|
||
return { rows: opts.batches, rowCount: opts.batches.length };
|
||
}
|
||
return { rows: [], rowCount: 1 };
|
||
});
|
||
return { client: { query } as never, executed };
|
||
}
|
||
|
||
/** chain 默认跟随 parentId 推断:分批链路必然有父子关系,单批普通验收为 false */
|
||
function batch(
|
||
id: string,
|
||
qty: string,
|
||
parentId: string | null = null,
|
||
minute = 0,
|
||
chain = parentId !== null
|
||
): InspectionRow {
|
||
return {
|
||
id,
|
||
actual_qty: qty,
|
||
created_at: new Date(Date.UTC(2026, 8, 13, 3, minute)),
|
||
parent_id: parentId,
|
||
is_chain_row: chain,
|
||
};
|
||
}
|
||
|
||
function updateStatements(executed: Array<{ sql: string; params: unknown[] }>) {
|
||
return executed.filter((e) => /UPDATE purchase_orders/.test(e.sql));
|
||
}
|
||
|
||
async function run(client: never) {
|
||
const { writeAccumulatedPurchaseActual } = await import('../src/services/inspectionComplianceService');
|
||
return writeAccumulatedPurchaseActual(client, {
|
||
purchaseCode: PO,
|
||
orderCode: ORDER,
|
||
skuCode: SKU,
|
||
supplierCode: null,
|
||
isRejected: false,
|
||
actualPrice: 2,
|
||
});
|
||
}
|
||
|
||
describe('writeAccumulatedPurchaseActual 归属判定', () => {
|
||
it('首批已写入 8、补收 2 到达时,即使采购单已是入库状态也累计成 10', async () => {
|
||
const { client, executed } = makeClient({
|
||
poActual: '8.00',
|
||
purchaseStatus: 30,
|
||
batches: [
|
||
batch(PARENT_INSPECTION, '8.00', null, 0, true),
|
||
batch(CURRENT_INSPECTION, '2.00', PARENT_INSPECTION, 1),
|
||
],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBe(10);
|
||
const updates = updateStatements(executed);
|
||
expect(updates).toHaveLength(1);
|
||
expect(updates[0].params[1]).toBe(10);
|
||
});
|
||
|
||
it('备货完成预填 15、验收 13 + 补 2 → 覆盖为 15 并推进状态', async () => {
|
||
// 真实场景:/purchase/complete(备货完成)先写备货量 15 且置 status=20,
|
||
// 随后验收 13 斤、补收 2 斤。分批链路存在,验收结果为准。
|
||
const { client, executed } = makeClient({
|
||
poActual: '15.00',
|
||
purchaseStatus: 20,
|
||
batches: [
|
||
batch(PARENT_INSPECTION, '15.00', null, 0, true),
|
||
batch(CURRENT_INSPECTION, '13.00', PARENT_INSPECTION, 1),
|
||
],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBe(28);
|
||
const updates = updateStatements(executed);
|
||
expect(updates).toHaveLength(1);
|
||
expect(updates[0].params[1]).toBe(28);
|
||
expect(updates[0].sql).toContain('purchase_status = 30');
|
||
});
|
||
|
||
it('单批验收(链路首批)覆盖备货完成预填值并推进状态', async () => {
|
||
// 真实故障 ORD_WEB_20260913_BR004_0009:备货完成写 13 且 status=20,
|
||
// 验收同为 13。若不覆盖则单据永远停在 20(列表显示「进行中」)。
|
||
const { client, executed } = makeClient({
|
||
poActual: '13.00',
|
||
purchaseStatus: 20,
|
||
batches: [batch(CURRENT_INSPECTION, '13.00', null, 0, true)],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBe(13);
|
||
const updates = updateStatements(executed);
|
||
expect(updates).toHaveLength(1);
|
||
expect(updates[0].params[1]).toBe(13);
|
||
expect(updates[0].sql).toContain('purchase_status = 30');
|
||
});
|
||
|
||
it('实收为空(新单首批)→ 写入本批数量', async () => {
|
||
const { client, executed } = makeClient({
|
||
poActual: null,
|
||
purchaseStatus: 20,
|
||
batches: [batch(CURRENT_INSPECTION, '8.00', null, 0)],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBe(8);
|
||
expect(updateStatements(executed)[0].params[1]).toBe(8);
|
||
});
|
||
|
||
it('三批连续入库逐层累计(8 → +2 → +5)', async () => {
|
||
const { client, executed } = makeClient({
|
||
poActual: '10.00', // 前两批已写入
|
||
purchaseStatus: 30,
|
||
batches: [
|
||
batch(CURRENT_INSPECTION, '8.00', null, 0, true),
|
||
batch('insp-2', '2.00', CURRENT_INSPECTION, 1, true),
|
||
batch('insp-3', '5.00', 'insp-2', 2, true),
|
||
],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBe(15);
|
||
expect(updateStatements(executed)[0].params[1]).toBe(15);
|
||
});
|
||
|
||
it('普通单批验收且单据数量被人为改过 → 保留人工值', async () => {
|
||
// 真实例:香蕉 采购单 25、验收 22;刺黄瓜 采购单 17、验收 43(均单批、非分批链路)
|
||
const { client, executed } = makeClient({
|
||
poActual: '25.00',
|
||
purchaseStatus: 30,
|
||
batches: [batch(CURRENT_INSPECTION, '22.00', null, 0, false)],
|
||
});
|
||
const got = await run(client);
|
||
expect(got).toBeNull();
|
||
expect(updateStatements(executed)).toHaveLength(0);
|
||
});
|
||
|
||
it('普通单批验收且单据一致 → 幂等重放,不改动', async () => {
|
||
const { client } = makeClient({
|
||
poActual: '22.00',
|
||
purchaseStatus: 30,
|
||
batches: [batch(CURRENT_INSPECTION, '22.00', null, 0, false)],
|
||
});
|
||
expect(await run(client)).toBe(22);
|
||
});
|
||
|
||
it('采购单不存在时安全返回', async () => {
|
||
const query = vi.fn(async () => ({ rows: [], rowCount: 0 }));
|
||
const { writeAccumulatedPurchaseActual } = await import('../src/services/inspectionComplianceService');
|
||
const got = await writeAccumulatedPurchaseActual({ query } as never, {
|
||
purchaseCode: 'PUR_MISSING',
|
||
orderCode: ORDER,
|
||
skuCode: SKU,
|
||
supplierCode: null,
|
||
isRejected: false,
|
||
actualPrice: 2,
|
||
});
|
||
expect(got).toBeNull();
|
||
});
|
||
});
|