Files
SupplierSystem/server/tests/t1BoundaryBiz14.test.ts
T
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

129 lines
5.1 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 { beforeEach, describe, expect, it, vi } from 'vitest';
/**
* T1 · BIZ-14 独立边界复核(2026-10-19,tester)。
*
* 针对修复后的**唯一写入方** `inspectionComplianceService.writeReattributedSelfPurchaseActual`:
* ① 累计入库 = 0 ⇒ **不写** `purchase_status=30`、不写 `actual_qty`(不留"30 但实收为空"半写);
* ② 累计入库 > 0 ⇒ 一条 UPDATE 同时写 30 + `actual_qty`(值 = 累计入库量,不是计划量);
* ③ 幂等:连调两次结果一致(实收量不累加、不改写第二次);
* ④ 入参不全 ⇒ 直接返回 null,一次查询都不发。
*/
const H = vi.hoisted(() => ({ poolQuery: 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,
}));
import { writeReattributedSelfPurchaseActual } from '../src/services/inspectionComplianceService';
type Row = Record<string, any>;
type Call = { sql: string; params: unknown[] };
const state = {
batches: [] as Array<{ actual_qty: number | string }>,
calls: [] as Call[],
/** 同订单同 SKU 有效订单行数(R-A:无 sourcePurchaseCodes 时先查;>1 跳过写入) */
siblings: 1,
};
const db = {
query: async (sql: string, params: unknown[] = []) => {
const text = String(sql);
state.calls.push({ sql: text, params });
// R-A(2026-10-19):无 sourcePurchaseCodes 时先查同订单同 SKU 有效行数
if (text.includes('COUNT(*)::int AS cnt') && text.includes('FROM order_items')) {
return { rows: [{ cnt: state.siblings }], rowCount: 1 };
}
if (text.includes('FROM inspections')) {
return { rows: state.batches, rowCount: state.batches.length };
}
if (text.includes('UPDATE purchase_orders')) {
return { rows: [], rowCount: 1 };
}
throw new Error(`fake db: 未处理的 SQL: ${text}`);
},
};
function run(overrides: Row = {}): Promise<number | null> {
return writeReattributedSelfPurchaseActual(db as never, {
purchaseCode: 'PURC1',
orderCode: 'ORD1',
skuCode: 'SKU1',
actualPrice: 20,
...overrides,
});
}
beforeEach(() => {
state.batches = [];
state.calls = [];
state.siblings = 1;
});
describe('BIZ-14 边界①:累计入库 = 0 ⇒ 不写 30 / actual_qty', () => {
it('无已完成入库批次 ⇒ 返回 null、零写入', async () => {
const result = await run();
expect(result).toBeNull();
expect(state.calls.some((call) => /UPDATE\s+purchase_orders/i.test(call.sql))).toBe(false);
expect(state.calls.some((call) => /purchase_status\s*=\s*30/.test(call.sql))).toBe(false);
expect(state.calls.some((call) => /actual_qty\s*=/.test(call.sql))).toBe(false);
});
});
describe('BIZ-14 边界②:累计入库 > 0 ⇒ 一次 UPDATE 写 30 + actual_qty', () => {
it('多批次求和 2 + 3.5 = 5.5;actual_qty 用累计量而不是计划量', async () => {
state.batches = [{ actual_qty: 2 }, { actual_qty: '3.5' }];
const result = await run();
expect(result).toBe(5.5);
const writes = state.calls.filter((call) => /UPDATE\s+purchase_orders/i.test(call.sql));
expect(writes).toHaveLength(1);
expect(writes[0].sql).toMatch(/purchase_status\s*=\s*30/);
expect(writes[0].sql).toMatch(/actual_qty\s*=\s*\$2::numeric/);
expect(writes[0].params).toEqual(['PURC1', 5.5, 20]);
// 只写一行采购单(不得顺带改其它表)
expect(state.calls.filter((call) => /\b(INSERT|DELETE)\b/i.test(call.sql))).toHaveLength(0);
});
it('actualPrice 缺省 ⇒ 传 null,SQL 用 COALESCE 保留既有 actual_price', async () => {
state.batches = [{ actual_qty: 1 }];
const result = await run({ actualPrice: null });
expect(result).toBe(1);
const write = state.calls.find((call) => /UPDATE\s+purchase_orders/i.test(call.sql))!;
expect(write.params).toEqual(['PURC1', 1, null]);
expect(write.sql).toMatch(/COALESCE\(\$3::numeric, actual_price\)/);
});
});
describe('BIZ-14 边界③:幂等(连调两次结果一致)', () => {
it('同一累计入库量连调两次 ⇒ 两次 UPDATE 的 actual_qty 都是 5.5,不累加', async () => {
state.batches = [{ actual_qty: 2 }, { actual_qty: 3.5 }];
const first = await run();
const second = await run();
expect(first).toBe(5.5);
expect(second).toBe(5.5);
const writes = state.calls.filter((call) => /UPDATE\s+purchase_orders/i.test(call.sql));
expect(writes).toHaveLength(2);
expect(writes.map((call) => call.params[1])).toEqual([5.5, 5.5]);
expect(writes.map((call) => call.params[0])).toEqual(['PURC1', 'PURC1']);
});
});
describe('BIZ-14 边界④:入参不全 ⇒ 不发任何查询', () => {
it.each([
['purchaseCode', { purchaseCode: '' }],
['orderCode', { orderCode: ' ' }],
['skuCode', { skuCode: '' }],
])('%s 为空 ⇒ 返回 null 且零查询', async (_name, overrides) => {
state.batches = [{ actual_qty: 9 }];
const result = await run(overrides);
expect(result).toBeNull();
expect(state.calls).toHaveLength(0);
});
});