- 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 超长修复
217 lines
8.5 KiB
TypeScript
217 lines
8.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* BIZ-10(2026-10-19):报价提议**审批通过后必须物化 `customer_quote_versions`**。
|
||
*
|
||
* 缺陷:审批路径只 `UPDATE customer_quote_sheet_items.sale_price`,不物化客户报价版本 ⇒
|
||
* 「报价版本管理」里看不到这次改价(版本号停在改价前)。
|
||
*
|
||
* 验收(AC-T1.5.2):假 client 捕获 SQL —— 审批成功后出现 `INSERT INTO customer_quote_versions`,
|
||
* 且发生在 `COMMIT` **之前**;零写入闸门(全 skipped)场景**不物化**。
|
||
* 复用 `quoteSheetService.materializeQuoteVersions` 同一实现(不复制)。
|
||
*/
|
||
|
||
const H = vi.hoisted(() => ({
|
||
poolQuery: vi.fn(),
|
||
clientQuery: vi.fn(),
|
||
release: vi.fn(),
|
||
decideRemote: vi.fn(),
|
||
listRemote: vi.fn(),
|
||
resolveSupplier: vi.fn(),
|
||
mapping: vi.fn(),
|
||
audit: vi.fn(),
|
||
}));
|
||
|
||
const BRANCH = 'BR002';
|
||
const SHEET = 'QS1';
|
||
const PROPOSAL_ID = '11111111-1111-4111-8111-111111111111';
|
||
|
||
vi.mock('../src/db', () => ({
|
||
pool: {
|
||
query: (...args: unknown[]) => H.poolQuery(...args),
|
||
connect: async () => ({
|
||
query: (...args: unknown[]) => H.clientQuery(...args),
|
||
release: () => H.release(),
|
||
}),
|
||
},
|
||
getRawPoolQuery: () => H.poolQuery,
|
||
}));
|
||
|
||
vi.mock('../src/services/mainSystemQuoteCostSync', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/services/mainSystemQuoteCostSync')>();
|
||
return { ...actual, resolveMainSupplierCode: (...args: unknown[]) => H.resolveSupplier(...args) };
|
||
});
|
||
|
||
vi.mock('../src/services/mainSystem/api/quotePriceProposals', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/services/mainSystem/api/quotePriceProposals')>();
|
||
return {
|
||
...actual,
|
||
listQuotePriceProposals: (...args: unknown[]) => H.listRemote(...args),
|
||
decideQuotePriceProposal: (...args: unknown[]) => H.decideRemote(...args),
|
||
};
|
||
});
|
||
|
||
vi.mock('../src/services/skuMappingService', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/services/skuMappingService')>();
|
||
return {
|
||
...actual,
|
||
getMappingByDeliverySku: (...args: unknown[]) => H.mapping(...args),
|
||
getSheetMappingStats: async () => ({ quote_sheet_code: SHEET, total: 1, mapped: 1, unmapped: 0, unmapped_skus: [] }),
|
||
};
|
||
});
|
||
|
||
vi.mock('../src/services/auditService', () => ({
|
||
writeAuditLog: (...args: unknown[]) => H.audit(...args),
|
||
}));
|
||
|
||
import { approvePriceProposal } from '../src/services/quotePriceProposalService';
|
||
|
||
type Row = Record<string, any>;
|
||
|
||
const state = {
|
||
proposal: null as Row,
|
||
/** 审批事务内按顺序执行的 SQL(用于断言物化发生在 COMMIT 之前) */
|
||
clientSql: [] as string[],
|
||
};
|
||
|
||
function proposal(overrides: Row = {}): Row {
|
||
return {
|
||
id: PROPOSAL_ID,
|
||
branch_code: BRANCH,
|
||
quote_sheet_code: SHEET,
|
||
main_branch_code: BRANCH,
|
||
direction: 'from_delivery',
|
||
items: [{ sku_code: 'SKU1', unit: '斤', price: 11 }],
|
||
payload_hash: 'fp',
|
||
status: 'pending',
|
||
reject_reason: null,
|
||
client_request_id: 'cr-local',
|
||
remote_proposal_id: 'remote-1',
|
||
requested_by: 'u1',
|
||
requested_at: '2026-10-18T01:00:00.000Z',
|
||
reviewed_by: null,
|
||
reviewed_at: null,
|
||
applied_at: null,
|
||
updated_at: '2026-10-18T01:00:00.000Z',
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
function makeHandler(recordClient: boolean) {
|
||
return (sql: string, params: unknown[] = []) => {
|
||
const text = String(sql);
|
||
if (recordClient) state.clientSql.push(text);
|
||
if (/^\s*(BEGIN|COMMIT|ROLLBACK)\s*;?\s*$/i.test(text)) return { rows: [], rowCount: 0 };
|
||
if (text.includes('FROM quote_price_proposals') && text.includes('FOR UPDATE')) {
|
||
return { rows: [state.proposal], rowCount: 1 };
|
||
}
|
||
if (text.includes('FROM quote_price_proposals') && text.includes('WHERE id = $1::uuid')) {
|
||
return { rows: [state.proposal], rowCount: 1 };
|
||
}
|
||
if (text.includes('FROM customer_quote_sheets qs')) return { rows: [{ quote_sheet_code: SHEET }], rowCount: 1 };
|
||
if (text.includes('FROM customer_quote_sheets')) {
|
||
return {
|
||
rows: [
|
||
{
|
||
quote_sheet_code: SHEET,
|
||
branch_code: BRANCH,
|
||
status: 1,
|
||
approval_status: 30,
|
||
expires_at: null,
|
||
quote_cycle: '202610',
|
||
created_by: 'u1',
|
||
},
|
||
],
|
||
rowCount: 1,
|
||
};
|
||
}
|
||
if (text.includes('SELECT id::text AS id, sku_code, unit, sale_price, price')) {
|
||
return {
|
||
rows: [{ id: 'item-SKU1', sku_code: 'SKU1', unit: '斤', sale_price: '10.00', price: '10.00' }],
|
||
rowCount: 1,
|
||
};
|
||
}
|
||
if (text.includes('UPDATE customer_quote_sheet_items') && text.includes('SET sale_price = $3, price = $3')) {
|
||
return { rows: [{ id: 'item-SKU1' }], rowCount: 1 };
|
||
}
|
||
// ── materializeQuoteVersions 的取数与写入 ──
|
||
if (text.includes('SELECT * FROM customer_quote_sheet_items WHERE quote_sheet_code = $1')) {
|
||
return { rows: [{ sku_code: 'SKU1', unit: '斤', sale_price: '11', price: '11' }], rowCount: 1 };
|
||
}
|
||
if (text.includes('SELECT customer_code FROM customer_quote_sheet_customers WHERE quote_sheet_code = $1')) {
|
||
return { rows: [{ customer_code: 'C1' }], rowCount: 1 };
|
||
}
|
||
if (text.includes('SELECT COALESCE(MAX(version_no), 0) + 1 AS next_version')) {
|
||
return { rows: [{ next_version: 2 }], rowCount: 1 };
|
||
}
|
||
if (text.includes('INSERT INTO customer_quote_versions')) return { rows: [], rowCount: 1 };
|
||
if (text.includes('UPDATE customer_quote_versions')) return { rows: [], rowCount: 0 };
|
||
if (text.includes("SET status = 'approved'")) {
|
||
state.proposal.status = 'approved';
|
||
return { rows: [state.proposal], rowCount: 1 };
|
||
}
|
||
return { rows: [], rowCount: 0 };
|
||
};
|
||
}
|
||
|
||
beforeEach(() => {
|
||
process.env.MAIN_SYSTEM_QUOTE_PRICE_PROPOSAL = '1';
|
||
state.proposal = proposal();
|
||
state.clientSql = [];
|
||
H.poolQuery.mockReset();
|
||
H.clientQuery.mockReset();
|
||
H.release.mockReset();
|
||
H.decideRemote.mockReset();
|
||
H.listRemote.mockReset();
|
||
H.mapping.mockReset();
|
||
H.audit.mockReset();
|
||
H.resolveSupplier.mockReset();
|
||
H.audit.mockResolvedValue(undefined);
|
||
H.resolveSupplier.mockResolvedValue('SUP1');
|
||
H.decideRemote.mockResolvedValue({ kind: 'ok', status: 200 });
|
||
H.listRemote.mockResolvedValue({
|
||
kind: 'ok',
|
||
status: 200,
|
||
rows: [{ proposal_id: 'remote-1', status: 'pending' }],
|
||
summary: null,
|
||
nextCursor: null,
|
||
});
|
||
H.mapping.mockResolvedValue({ supplier_sku_code: 'SKU1', supplier_sku_name: '草鱼', supplier_unit: '斤' });
|
||
const poolHandler = makeHandler(false);
|
||
const clientHandler = makeHandler(true);
|
||
H.poolQuery.mockImplementation((sql: string, params?: unknown[]) => poolHandler(sql, params || []));
|
||
H.clientQuery.mockImplementation((sql: string, params?: unknown[]) => clientHandler(sql, params || []));
|
||
});
|
||
|
||
describe('BIZ-10:审批通过 ⇒ 同一事务内物化 customer_quote_versions', () => {
|
||
it('写价之后、COMMIT 之前出现 INSERT INTO customer_quote_versions', async () => {
|
||
await approvePriceProposal({ id: PROPOSAL_ID, ctx: { branchCode: BRANCH } as any });
|
||
|
||
const itemUpdateIdx = state.clientSql.findIndex((sql) =>
|
||
sql.includes('UPDATE customer_quote_sheet_items') && sql.includes('SET sale_price = $3, price = $3')
|
||
);
|
||
const insertIdx = state.clientSql.findIndex((sql) => sql.includes('INSERT INTO customer_quote_versions'));
|
||
const commitIdx = state.clientSql.findIndex((sql) => /^\s*COMMIT\s*;?\s*$/i.test(sql));
|
||
|
||
expect(itemUpdateIdx, '审批必须先写价').toBeGreaterThan(-1);
|
||
expect(insertIdx, '审批通过后必须物化客户报价版本').toBeGreaterThan(-1);
|
||
expect(commitIdx, '必须提交').toBeGreaterThan(-1);
|
||
expect(insertIdx, '物化必须在写价之后').toBeGreaterThan(itemUpdateIdx);
|
||
expect(insertIdx, '物化必须在 COMMIT 之前(同一事务)').toBeLessThan(commitIdx);
|
||
// 版本号来自 MAX(version_no)+1
|
||
expect(state.clientSql.some((sql) => sql.includes('MAX(version_no)'))).toBe(true);
|
||
});
|
||
|
||
it('零写入闸门(全 skipped)⇒ 回滚且不物化版本', async () => {
|
||
H.mapping.mockResolvedValue(null);
|
||
state.proposal = proposal({ items: [{ sku_code: 'D-999', unit: '斤', price: 11 }] });
|
||
|
||
const error = await approvePriceProposal({ id: PROPOSAL_ID, ctx: { branchCode: BRANCH } as any }).catch(
|
||
(err) => err
|
||
);
|
||
expect(error).toMatchObject({ status: 409, code: 'nothing_applied' });
|
||
expect(state.clientSql.some((sql) => sql.includes('INSERT INTO customer_quote_versions'))).toBe(false);
|
||
expect(state.clientSql.some((sql) => /^\s*COMMIT\s*;?\s*$/i.test(sql))).toBe(false);
|
||
});
|
||
});
|