import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals'; import type { NextRequest } from 'next/server'; const mockFetch = jest.fn<(input: unknown, init?: unknown) => Promise>(); jest.unmock('./route'); import { POST, LANDING_CONTACT_RATE_LIMIT_MAX, landingContactRateLimiter } from './route'; /** * `/api/v1/contact` 的契约测试。 * * 这个路由的存在理由是**landing 的判据**:`public/novavis/js/main.js` 只认 * `ok && data.code === 200` 为成功。所以本文件的断言核心不是「有没有发邮件」, * 而是**响应形状是否让 landing 走进成功分支**——判据错位时表单会静默落进失败兜底, * 而那种失败在浏览器里只表现为一句「服务器返回异常」,没人能从文案定位到是 code 字段。 */ function createJsonRequest(body: unknown, ip = '1.2.3.4'): NextRequest { return { url: 'http://localhost:3000/api/v1/contact', headers: new Headers({ 'x-forwarded-for': ip }), json: async () => body, } as unknown as NextRequest; } function createBadJsonRequest(ip = '1.2.3.4'): NextRequest { return { url: 'http://localhost:3000/api/v1/contact', headers: new Headers({ 'x-forwarded-for': ip }), json: async () => { throw new SyntaxError('Unexpected token'); }, } as unknown as NextRequest; } const validBody = { name: '张三', org: '某某律师事务所', phone: '13812345678', email: 'zhang@example.com', message: '想了解睿视在资金流水研判上的能力', }; beforeEach(() => { jest.clearAllMocks(); // 每个用例从空桶开始:限流器是进程内单例,跨用例共享会让第 6 个用例莫名 429 // (本文件里就有一条用例要故意打满额度,不清空会污染后面所有用例)。 landingContactRateLimiter.reset(); global.fetch = mockFetch as unknown as typeof fetch; mockFetch.mockResolvedValue( new Response(JSON.stringify({ success: 'true' }), { status: 200, headers: { 'Content-Type': 'application/json' }, }), ); }); afterEach(() => { jest.restoreAllMocks(); }); describe('POST /api/v1/contact —— landing 契约', () => { it('合法请求返回 HTTP 200 且 code === 200(landing 的唯一成功判据)', async () => { const res = await POST(createJsonRequest(validBody)); const data = await res.json(); expect(res.status).toBe(200); expect(data.code).toBe(200); }); it('把 landing 的 org 合成为 subject 投递(表单里没有 subject 字段)', async () => { await POST(createJsonRequest(validBody)); expect(mockFetch).toHaveBeenCalledTimes(1); const [, init] = mockFetch.mock.calls[0]!; const body = String((init as { body: string }).body); const params = new URLSearchParams(body); expect(params.get('name')).toBe('张三'); expect(params.get('phone')).toBe('13812345678'); expect(params.get('subject')).toBe('睿视试用申请 · 某某律师事务所'); expect(params.get('_captcha')).toBe('false'); }); it('email / message 选填:缺失时仍成功,且不投递空 email', async () => { const res = await POST( createJsonRequest({ name: '李四', org: '某公司', phone: '13912345678' }), ); const data = await res.json(); expect(data.code).toBe(200); const body = String((mockFetch.mock.calls[0]![1] as { body: string }).body); const params = new URLSearchParams(body); expect(params.get('email')).toBeNull(); expect(params.get('message')).toBe('(未填写)'); }); it('缺必填字段:code !== 200 且 message 指出具体字段(不是笼统的"无效")', async () => { const res = await POST(createJsonRequest({ name: '王五', phone: '13812345678' })); const data = await res.json(); expect(data.code).not.toBe(200); expect(data.message).toContain('单位名称'); expect(mockFetch).not.toHaveBeenCalled(); }); it('手机号格式错:code !== 200(与 landing 的 validatePhone 同口径)', async () => { const res = await POST(createJsonRequest({ ...validBody, phone: '12345' })); const data = await res.json(); expect(data.code).not.toBe(200); expect(data.message).toContain('手机'); }); it('请求体不是 JSON:明确报「格式」而不是把锅推给用户字段', async () => { const res = await POST(createBadJsonRequest()); const data = await res.json(); expect(data.code).not.toBe(200); expect(data.message).toContain('JSON'); }); it('危险字符被过滤后才投递', async () => { await POST(createJsonRequest({ ...validBody, name: '张