Files
SupplierSystem/server/tests/quotePriceProposalDetailRoute.test.ts
Ubuntu 492ae97cc6
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(报价单): 配送商销售价确认——展开「我方提交」待审记录可见商品明细
原来展开只显示摘要(提交人/时间/N 个商品),看不到具体哪些商品在待审。
· 后端新增只读 GET /api/workflow/quote/price-proposals/:id(权限同列表、开关关 503、
  404/400 齐备、全程只 SELECT,不触发审批动作),返回 lines[](商品名/我方售价/建议售价/差额/状态);
· 前端展开 A 方向时按 id 取明细并渲染表格:差额=建议−我方(4 位小数、涨红跌绿),
  max-h-72 内部滚动、显示「共 N 条」、加载中/加载失败三态,失败不白屏、摘要仍在;
· 三语仅新增 5 个文案键(商品名/我方售价/建议售价/共 N 条/加载中…/加载失败);
· 用例:前端 14 条(渲染/滚动/加载中/失败/B 方向不额外请求)+ 后端路由 7 条;
  tsc(根+server)0 错、后端 quotePriceProposal 相关 57 通过。

注:src/pages/customers 里 QuoteVersionManagement.priceProposal.contract.test.ts 有 1 条既有失败
(HEAD 上页面 view 白名单与测试就不一致),非本次引入,未动。
2026-10-19 11:17:05 +08:00

159 lines
5.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, unknown> | 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<typeof import('../src/modules/workflow/context')>();
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<string, unknown> = {}) {
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<string, unknown>;
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');
});
});