import express from 'express'; import request from 'supertest'; import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * 「按 proposal id 取明细」路由层边界(2026-10-19 增量,只读): * · `GET /api/workflow/quote/price-proposals/:id` 仅查看权限 `customer_manage | order_manage`; * · 开关关闭 ⇒ 503 `feature_disabled`,且服务零调用; * · 非法 id ⇒ 400 `invalid_request`,服务零调用; * · 缺分公司上下文 ⇒ 400 `branch_required`; * · 服务层返回 null(不存在 / 不属于本分公司)⇒ 404 `not_found`; * · 成功 ⇒ 200 `{ data }`,服务只收到 `{ id, branchCode }`(body/query 一律不采信)。 * * 鉴权/租户上下文/服务层都 mock,数据库不参与。 */ const H = vi.hoisted(() => ({ ctx: null as Record | null, enabled: true, getPriceProposalDetail: 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 () => H.ctx }; }); vi.mock('../src/services/quotePriceProposalService', () => ({ isQuotePriceProposalEnabled: () => H.enabled, getPriceProposalDetail: (...args: unknown[]) => H.getPriceProposalDetail(...args), })); const { mountQuoteWorkflowRoutes } = await import('../src/modules/workflow/routes/quoteRoutes'); const ID = '11111111-1111-4111-8111-111111111111'; function buildApp() { const app = express(); app.use(express.json()); const router = express.Router(); mountQuoteWorkflowRoutes(router); app.use('/api/workflow', router); return app; } const app = buildApp(); function ctxWith(permissions: string[], patch: Record = {}) { return { userId: 'user-1', roles: ['admin'], permissions, branchCode: 'BR002', supplierCode: null, purchaserCode: null, isSuperAdmin: false, isBranchAdmin: false, isInternalStaff: true, isSupplierAccount: false, ...patch, }; } const detail = { id: ID, branch_code: 'BR002', quote_sheet_code: 'QS1', main_branch_code: 'BR002', direction: 'to_delivery', items: [ { sku_code: 'SKU1', unit: '斤', price: 11.125 }, { sku_code: 'SKU2', unit: '袋', price: 9.9 }, ], status: 'pending', lines: [ { sku_code: 'SKU1', sku_name: '草鱼', unit: '斤', before_sale_price: 10, after_sale_price: 11.125, change: 'updated', status: 'pending', }, ], }; beforeEach(() => { H.ctx = ctxWith(['customer_manage', 'order_manage']); H.enabled = true; H.getPriceProposalDetail.mockReset(); H.getPriceProposalDetail.mockResolvedValue(detail); }); describe('GET /quote/price-proposals/:id(只读明细)', () => { it('有查看权限 ⇒ 200,只把 { id, branchCode } 传给服务', async () => { const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(200); expect(res.body.data).toMatchObject({ id: ID, direction: 'to_delivery' }); expect(res.body.data.lines[0]).toMatchObject({ sku_name: '草鱼', status: 'pending' }); expect(H.getPriceProposalDetail).toHaveBeenCalledTimes(1); expect(H.getPriceProposalDetail).toHaveBeenCalledWith({ id: ID, branchCode: 'BR002' }); // body/query 里的花招不采信 const args = H.getPriceProposalDetail.mock.calls[0][0] as Record; expect(Object.keys(args).sort()).toEqual(['branchCode', 'id']); }); it('只有 order_manage ⇒ 也可查看(与列表同权限)', async () => { H.ctx = ctxWith(['order_manage']); const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(200); expect(H.getPriceProposalDetail).toHaveBeenCalledTimes(1); }); it('无查看权限 ⇒ 403,服务零调用', async () => { H.ctx = ctxWith([]); const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(403); expect(H.getPriceProposalDetail).not.toHaveBeenCalled(); }); it('开关关闭 ⇒ 503 feature_disabled,服务零调用', async () => { H.enabled = false; const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(503); expect(res.body.error).toBe('feature_disabled'); expect(H.getPriceProposalDetail).not.toHaveBeenCalled(); }); it('非法 id ⇒ 400 invalid_request,服务零调用', async () => { const res = await request(app).get('/api/workflow/quote/price-proposals/not-a-uuid'); expect(res.status).toBe(400); expect(res.body.error).toBe('invalid_request'); expect(H.getPriceProposalDetail).not.toHaveBeenCalled(); }); it('缺分公司上下文 ⇒ 400 branch_required,服务零调用', async () => { H.ctx = ctxWith(['customer_manage'], { branchCode: null }); const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(400); expect(res.body.error).toBe('branch_required'); expect(H.getPriceProposalDetail).not.toHaveBeenCalled(); }); it('提议不存在 / 不属于本分公司 ⇒ 404 not_found', async () => { H.getPriceProposalDetail.mockResolvedValue(null); const res = await request(app).get(`/api/workflow/quote/price-proposals/${ID}`); expect(res.status).toBe(404); expect(res.body.error).toBe('not_found'); }); });