import express from 'express'; import request from 'supertest'; import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * 竞价邀约删除/恢复:**前端传参名必须被后端识别**(2026-10-18 反向核对)。 * * 事故:前端 `workflowApi` 按详情接口同款字段名传 `?supplier=SUP-A`,后端 delete/restore * 只读 `supplier_code` ⇒ `supplierCode=null` ⇒ service 的 `scope=''` 会把同一轮**所有供应商** * 的 pending/expired 行一起软删(或一起恢复)。 * * 修复:后端同时接受 `supplier` 与 `supplier_code`。本文件只钉 HTTP 参数链路。 */ const H = vi.hoisted(() => ({ softDelete: vi.fn(), restore: vi.fn(), })); vi.mock('../src/middleware/auth', () => ({ authMiddleware: (req: { userId?: string }, _res: unknown, next: () => void) => { req.userId = 'user-1'; next(); }, })); vi.mock('../src/modules/workflow/context', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, getWorkflowCtx: async () => ({ userId: 'user-1', roles: ['admin'], permissions: ['order_manage'], branchCode: 'BR002', isSuperAdmin: false, }), requireAdminRole: () => undefined, requireWorkflowPermission: () => undefined, }; }); vi.mock('../src/services/tenantScope', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, getEffectiveBranchCode: () => 'BR002', readOperatingBranchCode: () => null }; }); vi.mock('../src/services/mainSystemBidQuotes', () => ({ listBidInvitations: vi.fn(), refreshBidInvitation: vi.fn(), submitBidQuotes: vi.fn(), syncBidInvitations: vi.fn(), })); vi.mock('../src/services/mainSystemBidInbox', () => ({ softDeleteBidInvitation: (...args: unknown[]) => H.softDelete(...args), restoreBidInvitation: (...args: unknown[]) => H.restore(...args), })); const { mountMainSystemBidWorkflowRoutes } = await import('../src/modules/workflow/routes/mainSystemBidRoutes'); function buildApp() { const app = express(); app.use(express.json()); const router = express.Router(); mountMainSystemBidWorkflowRoutes(router); app.use('/api/workflow', router); return app; } const app = buildApp(); beforeEach(() => { H.softDelete.mockReset(); H.restore.mockReset(); H.softDelete.mockResolvedValue({ ok: true, roundCode: 'BID001' }); H.restore.mockResolvedValue({ ok: true, roundCode: 'BID001' }); }); describe('竞价邀约删除/恢复:supplier 参数链路', () => { it('删除:前端 `?supplier=SUP-A` 必须精确传到 service(否则会误删同轮所有供应商)', async () => { const res = await request(app) .post('/api/workflow/main-system/bid-invitations/round-1/delete?supplier=SUP-A') .send({}); expect(res.status).toBe(200); expect(H.softDelete).toHaveBeenCalledWith( expect.objectContaining({ branchCode: 'BR002', roundId: 'round-1', supplierCode: 'SUP-A' }) ); }); it('恢复:前端 `?supplier=SUP-A` 同样必须精确传到 service', async () => { const res = await request(app) .post('/api/workflow/main-system/bid-invitations/round-1/restore?supplier=SUP-A') .send({}); expect(res.status).toBe(200); expect(H.restore).toHaveBeenCalledWith( expect.objectContaining({ branchCode: 'BR002', roundId: 'round-1', supplierCode: 'SUP-A' }) ); }); it('兼容旧字段 `supplier_code`(query 与 body 均可)', async () => { await request(app) .post('/api/workflow/main-system/bid-invitations/round-1/delete?supplier_code=SUP-B') .send({}); expect(H.softDelete).toHaveBeenLastCalledWith(expect.objectContaining({ supplierCode: 'SUP-B' })); await request(app) .post('/api/workflow/main-system/bid-invitations/round-1/restore') .send({ supplier_code: 'SUP-C' }); expect(H.restore).toHaveBeenLastCalledWith(expect.objectContaining({ supplierCode: 'SUP-C' })); }); });