- 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 超长修复
285 lines
11 KiB
TypeScript
285 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* T1 · BIZ-4 独立边界复核(2026-10-19,tester)。
|
||
*
|
||
* 与 dev 的 `quotePriceProposalNothingApplied.test.ts` **独立**:不覆盖单一路径,而是钉住
|
||
* ① 全 skipped(四种原因混合)⇒ 409 `nothing_applied`、ROLLBACK、本地仍 pending、**无 approved 回执**、不物化版本;
|
||
* ② `unchanged>0` 且 `updated===0`(同时含 skipped)⇒ 允许通过,回执 `applied.updated===0` 且 skipped 一致;
|
||
* ③ `updated>0` 且 `skipped>0` ⇒ 允许通过,回执 `applied` 与 `result.applied` **逐字段一致**(不吞 skipped)。
|
||
*/
|
||
|
||
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: {} as Row,
|
||
/** 目标报价单明细行(我方 SKU) */
|
||
sheetItems: [] as Row[],
|
||
events: [] as string[],
|
||
};
|
||
|
||
function proposal(items: Row[]): Row {
|
||
return {
|
||
id: PROPOSAL_ID,
|
||
branch_code: BRANCH,
|
||
quote_sheet_code: SHEET,
|
||
main_branch_code: BRANCH,
|
||
direction: 'from_delivery',
|
||
items,
|
||
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',
|
||
};
|
||
}
|
||
|
||
function sheetItem(skuCode: string, unit: string, price: number): Row {
|
||
return { id: `item-${skuCode}`, sku_code: skuCode, unit, sale_price: String(price), price: String(price) };
|
||
}
|
||
|
||
function installDb(): void {
|
||
const handler = (sql: string, params: unknown[] = []) => {
|
||
const text = String(sql);
|
||
if (/^\s*(BEGIN|COMMIT|ROLLBACK)\s*;?\s*$/i.test(text)) {
|
||
state.events.push(text.trim().toUpperCase());
|
||
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: state.sheetItems, rowCount: state.sheetItems.length };
|
||
}
|
||
if (text.includes('UPDATE customer_quote_sheet_items') && text.includes('SET sale_price = $3, price = $3')) {
|
||
state.events.push('ITEM_UPDATE');
|
||
return { rows: [{ id: `item-${String(params[1])}` }], rowCount: 1 };
|
||
}
|
||
// ── materializeQuoteVersions ──
|
||
if (text.includes('SELECT * FROM customer_quote_sheet_items WHERE quote_sheet_code = $1')) {
|
||
return { rows: state.sheetItems, rowCount: state.sheetItems.length };
|
||
}
|
||
if (text.includes('SELECT customer_code FROM customer_quote_sheet_customers')) {
|
||
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')) {
|
||
state.events.push('VERSION_INSERT');
|
||
return { rows: [], rowCount: 1 };
|
||
}
|
||
if (text.includes('UPDATE customer_quote_versions')) {
|
||
state.events.push('VERSION_EXPIRE');
|
||
return { rows: [], rowCount: 0 };
|
||
}
|
||
if (text.includes("SET status = 'approved'")) {
|
||
state.events.push('PROPOSAL_APPROVED');
|
||
state.proposal.status = 'approved';
|
||
return { rows: [state.proposal], rowCount: 1 };
|
||
}
|
||
return { rows: [], rowCount: 0 };
|
||
};
|
||
H.poolQuery.mockImplementation((sql: string, params?: unknown[]) => handler(sql, params || []));
|
||
H.clientQuery.mockImplementation((sql: string, params?: unknown[]) => handler(sql, params || []));
|
||
}
|
||
|
||
beforeEach(() => {
|
||
process.env.MAIN_SYSTEM_QUOTE_PRICE_PROPOSAL = '1';
|
||
state.proposal = proposal([{ sku_code: 'SKU1', unit: '斤', price: 11 }]);
|
||
state.sheetItems = [sheetItem('S1', '斤', 10), sheetItem('S-UNIT', '斤', 20)];
|
||
state.events = [];
|
||
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.mockImplementation(async (_branch: string, deliverySku: string) => {
|
||
if (deliverySku === 'D-NOTIN') return { supplier_sku_code: 'S-MISSING', supplier_sku_name: null, supplier_unit: null };
|
||
if (deliverySku === 'D-UNIT') return { supplier_sku_code: 'S-UNIT', supplier_sku_name: null, supplier_unit: null };
|
||
if (deliverySku === 'SKU1') return { supplier_sku_code: 'S1', supplier_sku_name: null, supplier_unit: null };
|
||
return null;
|
||
});
|
||
installDb();
|
||
});
|
||
|
||
describe('BIZ-4 边界①:全 skipped(多原因)⇒ nothing_applied,零写入', () => {
|
||
it('invalid_price + sku_unmapped + sku_not_in_sheet + unit_mismatch ⇒ 409、ROLLBACK、pending、无回执、不物化', async () => {
|
||
state.proposal = proposal([
|
||
{ sku_code: 'D-INV', unit: '斤', price: 0 },
|
||
{ sku_code: 'D-UNMAP', unit: '斤', price: 9 },
|
||
{ sku_code: 'D-NOTIN', unit: '斤', price: 12 },
|
||
{ sku_code: 'D-UNIT', unit: '袋', price: 13 },
|
||
]);
|
||
|
||
const error = await approvePriceProposal({ id: PROPOSAL_ID, ctx: { branchCode: BRANCH } as any }).catch(
|
||
(err) => err
|
||
);
|
||
|
||
expect(error).toMatchObject({ status: 409, code: 'nothing_applied' });
|
||
for (const reason of ['invalid_price', 'sku_unmapped', 'sku_not_in_sheet', 'unit_mismatch']) {
|
||
expect(String(error.message), reason).toContain(reason);
|
||
}
|
||
expect(state.events).not.toContain('ITEM_UPDATE');
|
||
expect(state.events).not.toContain('PROPOSAL_APPROVED');
|
||
expect(state.events).not.toContain('VERSION_INSERT');
|
||
expect(state.events).toContain('ROLLBACK');
|
||
expect(state.events).not.toContain('COMMIT');
|
||
expect(state.proposal.status).toBe('pending');
|
||
expect(H.decideRemote).not.toHaveBeenCalled();
|
||
});
|
||
});
|
||
|
||
describe('BIZ-4 边界②:unchanged>0 且 updated=0 ⇒ 允许(幂等无操作)', () => {
|
||
it('一行价格已一致 + 一行未映射 ⇒ approved;回执 applied.updated=0 且带 skipped', async () => {
|
||
state.proposal = proposal([
|
||
{ sku_code: 'SKU1', unit: '斤', price: 10 }, // 与 S1 一致 ⇒ unchanged
|
||
{ sku_code: 'D-UNMAP', unit: '斤', price: 9 }, // skipped
|
||
]);
|
||
|
||
const result = await approvePriceProposal({ id: PROPOSAL_ID, ctx: { branchCode: BRANCH } as any });
|
||
|
||
expect(result.applied).toEqual({
|
||
updated: 0,
|
||
unchanged: 1,
|
||
skipped: [{ sku_code: 'D-UNMAP', reason: 'sku_unmapped' }],
|
||
});
|
||
expect(state.proposal.status).toBe('approved');
|
||
expect(state.events).toContain('COMMIT');
|
||
expect(state.events).not.toContain('ROLLBACK');
|
||
expect(H.decideRemote).toHaveBeenCalledTimes(1);
|
||
const receipt = H.decideRemote.mock.calls[0][0] as Row;
|
||
expect(receipt).toMatchObject({ decision: 'approved', proposalId: 'remote-1' });
|
||
expect(receipt.applied).toEqual({
|
||
updated: 0,
|
||
skipped: [{ sku_code: 'D-UNMAP', reason: 'sku_unmapped' }],
|
||
});
|
||
});
|
||
});
|
||
|
||
describe('BIZ-4 边界③:updated>0 + skipped>0 ⇒ 允许且回执 applied 一致', () => {
|
||
it('一行更新 + 两行跳过 ⇒ approved;回执 applied 与 result.applied 逐字段一致', async () => {
|
||
state.proposal = proposal([
|
||
{ sku_code: 'SKU1', unit: '斤', price: 11 }, // 10 → 11,updated
|
||
{ sku_code: 'D-UNIT', unit: '袋', price: 13 }, // unit_mismatch
|
||
{ sku_code: 'D-UNMAP', unit: '斤', price: 9 }, // sku_unmapped
|
||
]);
|
||
|
||
const result = await approvePriceProposal({ id: PROPOSAL_ID, ctx: { branchCode: BRANCH } as any });
|
||
|
||
expect(result.applied.updated).toBe(1);
|
||
expect(result.applied.unchanged).toBe(0);
|
||
expect(result.applied.skipped).toEqual([
|
||
{ sku_code: 'D-UNIT', reason: 'unit_mismatch', detail: '袋 vs 斤' },
|
||
{ sku_code: 'D-UNMAP', reason: 'sku_unmapped' },
|
||
]);
|
||
expect(state.proposal.status).toBe('approved');
|
||
expect(state.events.filter((event) => event === 'ITEM_UPDATE')).toHaveLength(1);
|
||
expect(state.events).toContain('COMMIT');
|
||
const receipt = H.decideRemote.mock.calls[0][0] as Row;
|
||
// 回执必须与返回的统计一致,不得吞掉 skipped(否则配送侧看到的 applied 是残缺的)
|
||
expect(receipt.applied).toEqual({
|
||
updated: result.applied.updated,
|
||
skipped: result.applied.skipped,
|
||
});
|
||
// 同时确认 BIZ-10:审批通过确实物化了版本(同一事务)
|
||
expect(state.events).toContain('VERSION_INSERT');
|
||
});
|
||
});
|