- 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 超长修复
254 lines
10 KiB
TypeScript
254 lines
10 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* **BIZ-11:待报价邀约软删后不得再报价**(2026-10-19 T10a)。
|
||
*
|
||
* 缺陷(审计 BIZ-11):
|
||
* · `findInvitationByRound` 的**无 `supplierCode` 分支**不过滤 `deleted_at`(带 code 的分支过了);
|
||
* · `getBidInvitationDetail` 的 `canQuote` 不看 `deletedAt`;
|
||
* · ⇒ 管理员「删除待报价」后,供应商从旧详情页/接口仍能把这轮报到主系统。
|
||
*
|
||
* 口径:软删是"本地隐藏、可恢复"的台账操作(不通知主系统)。因此**全链路**都要把它当不存在:
|
||
* 列表(默认不含已删)→ 定位 `findInvitationByRound`(两个分支)→ 详情(null)→
|
||
* 刷新/提交(显式 409 `bid_invitation_deleted`,且**零出站**)。
|
||
*
|
||
* 造数方式:`vi.mock('../src/db')` 用内存版两张表接管 SQL(过滤语义按 SQL 里有没有
|
||
* `deleted_at IS NULL` 真做),对方接口/绑定列表全部 mock ⇒ 一个用例里"有没有出站"可数。
|
||
*/
|
||
|
||
const H = vi.hoisted(() => ({
|
||
poolQuery: vi.fn(),
|
||
submitQuotes: vi.fn(),
|
||
getDetail: vi.fn(),
|
||
listCodes: vi.fn(),
|
||
}));
|
||
|
||
vi.mock('../src/db', () => ({
|
||
pool: { query: (...args: unknown[]) => H.poolQuery(...args) },
|
||
getRawPoolQuery: () => H.poolQuery,
|
||
}));
|
||
|
||
/** 绑定列表:默认空(⇒ `syncBidInvitations` 早退,不打库);用例可覆盖 */
|
||
vi.mock('../src/services/mainSystemBranchApi', () => ({
|
||
listSupplierCodesForBranch: (...args: unknown[]) => H.listCodes(...args),
|
||
}));
|
||
|
||
/** 对方接口:提交/详情都是"出站",本文件要证明**已删行一次都不发** */
|
||
vi.mock('../src/services/mainSystem/api/bids', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/services/mainSystem/api/bids')>();
|
||
return {
|
||
...actual,
|
||
submitMainSupplierBidQuotes: (...args: unknown[]) => H.submitQuotes(...args),
|
||
getMainSupplierBidDetail: (...args: unknown[]) => H.getDetail(...args),
|
||
listMainSupplierBids: vi.fn(async () => ({ kind: 'ok', enabled: true, rows: [] })),
|
||
};
|
||
});
|
||
|
||
const inbox = await import('../src/services/mainSystemBidInbox');
|
||
const quotes = await import('../src/services/mainSystemBidQuotes');
|
||
|
||
const BRANCH = 'BR002';
|
||
const ROUND = 'round-soft-del-1';
|
||
const SUPPLIER = 'SUP-A';
|
||
|
||
type InvitationRow = Record<string, unknown>;
|
||
|
||
function makeRow(overrides: Partial<InvitationRow> = {}): InvitationRow {
|
||
return {
|
||
id: 'inv-1',
|
||
branch_code: BRANCH,
|
||
supplier_code: SUPPLIER,
|
||
round_id: ROUND,
|
||
round_code: 'BID-001',
|
||
round_title: '今日竞价',
|
||
order_code: 'ORD-1',
|
||
respond_deadline: new Date(Date.now() + 3600_000).toISOString(),
|
||
item_count: 1,
|
||
round_status: 'open',
|
||
main_invitation_status: 'open',
|
||
local_status: 'pending',
|
||
quoted_at: null,
|
||
webhook_received_at: null,
|
||
last_synced_at: null,
|
||
sync_error: null,
|
||
deleted_at: null,
|
||
deleted_by: null,
|
||
payload: null,
|
||
created_at: new Date().toISOString(),
|
||
updated_at: new Date().toISOString(),
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 内存版两张表:只实现本文件用到的 SELECT。
|
||
* `deleted_at IS NULL` 出现在 SQL 里时**真的**过滤(模拟 Postgres),
|
||
* 这样"SQL 过滤"和"代码分区"两种实现都能被同一份用例验证。
|
||
*/
|
||
function makeFakeDb(rows: InvitationRow[], itemRows: Array<Record<string, unknown>> = []) {
|
||
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
||
const query = async (sql: string, params: unknown[] = []) => {
|
||
const text = String(sql);
|
||
calls.push({ sql: text, params });
|
||
if (text.includes('FROM public.main_system_bid_invitations')) {
|
||
const [branch, round, supplier] = params as [string, string, string | undefined];
|
||
const withSupplier = text.includes('upper(supplier_code) = upper($3)');
|
||
const onlyActive = text.includes('deleted_at IS NULL');
|
||
const matched = rows.filter(
|
||
(row) =>
|
||
String(row.branch_code) === String(branch) &&
|
||
String(row.round_id) === String(round) &&
|
||
(!withSupplier || String(row.supplier_code).toUpperCase() === String(supplier).toUpperCase()) &&
|
||
(!onlyActive || !row.deleted_at)
|
||
);
|
||
return { rowCount: matched.length, rows: matched };
|
||
}
|
||
if (text.includes('FROM public.main_system_bid_invitation_items')) {
|
||
const matched = itemRows.filter((row) => String(row.invitation_id) === String(params[0]));
|
||
return { rowCount: matched.length, rows: matched };
|
||
}
|
||
return { rowCount: 0, rows: [] };
|
||
};
|
||
return { calls, query };
|
||
}
|
||
|
||
beforeEach(() => {
|
||
H.poolQuery.mockReset();
|
||
H.submitQuotes.mockReset();
|
||
H.getDetail.mockReset();
|
||
H.listCodes.mockReset();
|
||
H.listCodes.mockResolvedValue([]);
|
||
});
|
||
|
||
describe('BIZ-11 软删后的邀约:找不到 / 无详情 / 提交与刷新都被拒(零出站)', () => {
|
||
const deleted = () => makeRow({ deleted_at: new Date().toISOString(), deleted_by: 'user-9' });
|
||
|
||
it('findInvitationByRound:两个分支都不得把已删行当 found(无 supplierCode 分支曾经漏过滤)', async () => {
|
||
const db = makeFakeDb([deleted()]);
|
||
H.poolQuery.mockImplementation(db.query);
|
||
const withoutSupplier = await inbox.findInvitationByRound({ branchCode: BRANCH, roundId: ROUND });
|
||
expect(withoutSupplier.kind, '不带 supplierCode 的分支必须过滤 deleted_at').not.toBe('found');
|
||
expect(withoutSupplier.kind).toBe('deleted');
|
||
|
||
const withSupplier = await inbox.findInvitationByRound({
|
||
branchCode: BRANCH,
|
||
roundId: ROUND,
|
||
supplierCode: SUPPLIER,
|
||
});
|
||
expect(withSupplier.kind, '带 supplierCode 的分支同样不得返回已删行').not.toBe('found');
|
||
expect(withSupplier.kind).toBe('deleted');
|
||
});
|
||
|
||
it('getBidInvitationDetail:已删行返回 null(两个入口都不给详情 ⇒ 页面无从报价)', async () => {
|
||
const fake = makeFakeDb([deleted()], [
|
||
{ id: 'item-1', invitation_id: 'inv-1', sku_code: 'SKU-1', sku_name: '苹果', unit: '斤' },
|
||
]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
expect(await inbox.getBidInvitationDetail({ branchCode: BRANCH, roundId: ROUND })).toBeNull();
|
||
expect(
|
||
await inbox.getBidInvitationDetail({ branchCode: BRANCH, roundId: ROUND, supplierCode: SUPPLIER })
|
||
).toBeNull();
|
||
// 拿不到详情 ⇒ 连明细都不该去查
|
||
expect(fake.calls.some((c) => c.sql.includes('main_system_bid_invitation_items'))).toBe(false);
|
||
});
|
||
|
||
it('getBidInvitationDetail:即使定位函数把已删行漏了回来,详情层也必须自己拦住(双保险)', async () => {
|
||
const fake = makeFakeDb([deleted()], [
|
||
{ id: 'item-1', invitation_id: 'inv-1', sku_code: 'SKU-1', sku_name: '苹果', unit: '斤' },
|
||
]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
const spy = vi
|
||
.spyOn(inbox, 'findInvitationByRound')
|
||
.mockResolvedValueOnce({ kind: 'found', row: deleted() as never });
|
||
try {
|
||
expect(await inbox.getBidInvitationDetail({ branchCode: BRANCH, roundId: ROUND })).toBeNull();
|
||
} finally {
|
||
spy.mockRestore();
|
||
}
|
||
// canQuote 自身也不得因 `deletedAt` 缺失而给出 true —— 由上面的 null 覆盖
|
||
});
|
||
|
||
it('submitBidQuotes:已删行 ⇒ 409 bid_invitation_deleted,且**没有**推到主系统', async () => {
|
||
const fake = makeFakeDb([deleted()]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
const outcome = await quotes.submitBidQuotes({
|
||
branchCode: BRANCH,
|
||
roundId: ROUND,
|
||
supplierCode: SUPPLIER,
|
||
quotes: [{ skuCode: 'SKU-1', unitPrice: 5.5 }],
|
||
});
|
||
expect(outcome.kind).toBe('rejected');
|
||
if (outcome.kind !== 'rejected') throw new Error('unreachable');
|
||
expect(outcome.status).toBe(409);
|
||
expect(outcome.code).toBe('bid_invitation_deleted');
|
||
expect(outcome.message).toContain('已删除');
|
||
expect(H.submitQuotes, '已删行不得出站提交').not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('submitBidQuotes:不带 supplierCode(旧页面)同样被拒', async () => {
|
||
const fake = makeFakeDb([deleted()]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
const outcome = await quotes.submitBidQuotes({
|
||
branchCode: BRANCH,
|
||
roundId: ROUND,
|
||
quotes: [{ skuCode: 'SKU-1', unitPrice: 5.5 }],
|
||
});
|
||
expect(outcome.kind).toBe('rejected');
|
||
if (outcome.kind !== 'rejected') throw new Error('unreachable');
|
||
expect(outcome.code).toBe('bid_invitation_deleted');
|
||
expect(H.submitQuotes).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('refreshBidInvitation:已删行 ⇒ 409,且不向主系统拉详情', async () => {
|
||
const fake = makeFakeDb([deleted()]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
const outcome = await quotes.refreshBidInvitation({
|
||
branchCode: BRANCH,
|
||
roundId: ROUND,
|
||
supplierCode: SUPPLIER,
|
||
});
|
||
expect(outcome.kind).toBe('rejected');
|
||
if (outcome.kind !== 'rejected') throw new Error('unreachable');
|
||
expect(outcome.code).toBe('bid_invitation_deleted');
|
||
expect(H.getDetail, '已删行不得出站拉详情').not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('对照:未删除的行照旧可看详情、可提交(修复不得误伤正常链路)', async () => {
|
||
const active = makeRow();
|
||
const fake = makeFakeDb([active], [
|
||
{
|
||
id: 'item-1',
|
||
invitation_id: 'inv-1',
|
||
branch_code: BRANCH,
|
||
round_id: ROUND,
|
||
sku_code: 'SKU-1',
|
||
sku_name: '苹果',
|
||
unit: '斤',
|
||
unit_price: null,
|
||
remark: null,
|
||
quoted_at: null,
|
||
award_price: null,
|
||
award_qty: null,
|
||
award_status: null,
|
||
},
|
||
]);
|
||
H.poolQuery.mockImplementation(fake.query);
|
||
const detail = await inbox.getBidInvitationDetail({ branchCode: BRANCH, roundId: ROUND, supplierCode: SUPPLIER });
|
||
expect(detail).not.toBeNull();
|
||
expect(detail?.canQuote).toBe(true);
|
||
expect(detail?.items).toHaveLength(1);
|
||
expect(detail?.invitation.deletedAt).toBeNull();
|
||
|
||
// 提交链路确实走到了出站(用"对方拒绝"收口,证明本地不再提前拦截)
|
||
H.submitQuotes.mockResolvedValue({ kind: 'rejected', status: 409, code: 'bid_not_open', reason: '本轮已结束' });
|
||
const outcome = await quotes.submitBidQuotes({
|
||
branchCode: BRANCH,
|
||
roundId: ROUND,
|
||
supplierCode: SUPPLIER,
|
||
quotes: [{ skuCode: 'SKU-1', unitPrice: 5.5 }],
|
||
});
|
||
expect(outcome.kind).toBe('rejected');
|
||
expect(H.submitQuotes).toHaveBeenCalledTimes(1);
|
||
});
|
||
});
|