Files
SupplierSystem/server/tests/selfPurchaseReattributeSingleWriter.test.ts
Ubuntu 5fa2bbfd55
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(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

146 lines
5.9 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 { beforeEach, describe, expect, it, vi } from 'vitest';
/**
* BIZ-14(2026-10-19):入库后转挂到自采,**不得由转挂服务直写 `purchase_status=30`/`actual_qty`**。
*
* 缺陷:`inboundFulfillmentReattributeService.ensureCompletedSelfPurchase` 自己 UPDATE/INSERT
* `purchase_status = 30` 并预填 `actual_qty = 计划量`,破坏方案 C「验收是实收唯一写入方」。
*
* 修复(AC-T1.7.1/2/4):
* ① 转挂服务的 UPDATE/INSERT 不再写 30、不再写 actual_qty(新单落 20 待验收);
* ② 实收由验收模块 `writeReattributedSelfPurchaseActual` 按**累计入库量**落 30 + actual_qty;
* ③ actual_qty 必须真的被写入(不是只改状态)。
*/
const H = vi.hoisted(() => ({
poolQuery: vi.fn(),
upsertCost: vi.fn(),
recalcStatements: vi.fn(),
}));
vi.mock('../src/db', () => ({
pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() },
query: (...args: unknown[]) => H.poolQuery(...args),
getRawPoolQuery: () => H.poolQuery,
}));
vi.mock('../src/services/orderCostPostingService', () => ({
upsertOrderSkuPurchaseCostFromUnitCost: (...args: unknown[]) => H.upsertCost(...args),
}));
vi.mock('../src/services/financeReconciliationService', () => ({
recalculateStatementsAffectedByOrder: (...args: unknown[]) => H.recalcStatements(...args),
}));
import { reattributeInboundFulfillment } from '../src/services/inboundFulfillmentReattributeService';
type Row = Record<string, any>;
type Call = { sql: string; params: unknown[] };
const state = {
calls: [] as Call[],
existingPurchase: true,
/** 累计入库量(inspection actual_qty 合计) */
accumulatedInbound: 5,
};
function makeDb() {
return {
query: async (sql: string, params: unknown[] = []) => {
const text = String(sql);
state.calls.push({ sql: text, params });
if (text.includes('FROM inspections') && text.includes('inbound_batch_no') && text.includes('LIMIT 1')) {
return { rowCount: 1, rows: [{ 1: 1 }] };
}
if (text.includes('SELECT DISTINCT NULLIF(TRIM(inbound_batch_no)')) {
return { rowCount: 1, rows: [{ batch_no: 'B1' }] };
}
if (text.includes('FROM order_item_fulfillments f')) {
return { rowCount: 0, rows: [] };
}
if (text.includes('FROM purchase_orders') && text.includes('SELECT purchase_code')) {
return state.existingPurchase
? { rowCount: 1, rows: [{ purchase_code: 'PURC1' }] }
: { rowCount: 0, rows: [] };
}
// 验收模块按累计入库量取批次
if (text.includes('FROM inspections') && text.includes('actual_qty') && text.includes('parent_id')) {
return {
rowCount: 1,
rows: [{ id: 'insp-1', actual_qty: state.accumulatedInbound, created_at: new Date(), parent_id: null }],
};
}
return { rowCount: 0, rows: [] };
},
};
}
async function runReattribute(): Promise<void> {
await reattributeInboundFulfillment(makeDb() as never, {
orderCode: 'ORD1',
itemId: 'item-1',
skuCode: 'SKU1',
itemName: '草鱼',
unit: '斤',
previousSupplierCode: 'SUP1',
nextSupplierCode: null, // 改派到自采
quantity: 4,
unitCost: 3,
branchCode: 'BR002',
});
}
beforeEach(() => {
state.calls = [];
state.existingPurchase = true;
state.accumulatedInbound = 5;
H.poolQuery.mockReset();
H.upsertCost.mockReset();
H.recalcStatements.mockReset();
H.upsertCost.mockResolvedValue(undefined);
H.recalcStatements.mockResolvedValue(undefined);
});
describe('BIZ-14:转挂自采不再直写 purchase_status=30 / actual_qty', () => {
it('既有自采单:转挂 UPDATE 不写 30/actual_qty;验收模块按累计入库量写 30 + actual_qty', async () => {
await runReattribute();
const purchaseUpdates = state.calls
.filter((call) => call.sql.includes('UPDATE purchase_orders'))
.map((call) => call);
const handoverUpdate = purchaseUpdates.find((call) => call.sql.includes('SET supplier_code = NULL'));
expect(handoverUpdate, '转挂服务应更新自采单基础字段').toBeTruthy();
expect(handoverUpdate!.sql).not.toMatch(/purchase_status\s*=\s*30/);
expect(handoverUpdate!.sql).not.toMatch(/actual_qty\s*=/);
const inspectionWrite = purchaseUpdates.find((call) => /purchase_status\s*=\s*30/.test(call.sql));
expect(inspectionWrite, '必须由验收模块统一写 30').toBeTruthy();
expect(inspectionWrite!.sql).toMatch(/actual_qty\s*=\s*\$2::numeric/);
// 实收量来自累计入库量(5),不是转挂入参的计划量(4)
expect(inspectionWrite!.params[1]).toBe(5);
// 全仓 SQL 里 30 只出现于验收模块那一条;转挂自己的 INSERT 也不写 30
const status30Calls = state.calls.filter((call) => /purchase_status\s*=\s*30/.test(call.sql));
expect(status30Calls).toHaveLength(1);
expect(state.calls.some((call) => /INSERT INTO purchase_orders/.test(call.sql))).toBe(false);
expect(H.upsertCost).toHaveBeenCalledTimes(1);
});
it('无既有自采单:INSERT 落 20(待验收)且 actual_qty 为空,随后由验收模块写 30 + 实收', async () => {
state.existingPurchase = false;
await runReattribute();
const insert = state.calls.find((call) => call.sql.includes('INSERT INTO purchase_orders'));
expect(insert, '应新建自采单').toBeTruthy();
expect(insert!.sql).not.toMatch(/,\s*30\s*,/); // 不得把 30 塞进状态列
expect(insert!.sql).toMatch(/NULL,\s*\$5,\s*20,/); // actual_qty=NULL、purchase_status=20
const insertArgs = insert!.params;
expect(insertArgs[3]).toBe(4); // purchase_qty = 计划量
expect(insertArgs[4]).toBe(3); // purchase_price
const inspectionWrite = state.calls.find((call) => /purchase_status\s*=\s*30/.test(call.sql));
expect(inspectionWrite).toBeTruthy();
expect(inspectionWrite!.params[1]).toBe(state.accumulatedInbound);
});
});