Files
novalon-website/src/app/api/v1/contact/route.test.ts
T
zhangxiang 99eaa8c66a feat(landing): 睿视 landing 由本仓统一管理——源码子目录化 + 四道门禁移植 + 契约路由
承接 ADR-0014 §4「第二等产品镜像起必须子目录化 + rewrites 前缀映射并回溯睿视」。
上一轮(2026-12-02)只搬了字节,源码落在 public/ 根命名空间;本轮把源码、门禁、
发布职责一并收进本仓,authority 侧退役对应产物。

源码与 URL 契约:
- public/novavis/{index.html,css,js,assets,images/logos} 一棵树自成一体
- index.html 引用保持站点级绝对路径(/css /js /assets /images/logos)不变。
  因为同一份字节要服务两个落点:官网镜像 novalon.cn/novavis/,以及独立子域
  novavis.p.novalon.cn/(页面在根路径)。改绝对路径会把子域首页变成 /novavis/
- 故用 4 条 prefix rewrite 把根命名空间映射进子目录,而非改写 HTML

四道门禁(从 authority 的四个 python 脚本移植为 Playwright,已并入 test:all):
- e2e/landing/{layout,motion,demo,a11y}.spec.ts + 独立 config(单引擎单 project 串行)
- layout:几何与焦点 / motion:js-grow 渐进生长与 reduced-motion 直达终态
- demo:首屏剧本四步 + 徽标一一对应 + 无 JS 降级四层真实可见性
- a11y:结构与全量对比度(同色无条件阻断)+ axe critical/serious=0
- 实测 13 passed / EXIT=0(1.4m)

补一个映射期暴露的真缺陷:landing 的 main.js 固定 POST /api/v1/contact 且只认
code===200,而本仓只有 /api/contact(formData + 必填 subject),镜像侧转化路径
一直 404。新增 src/app/api/v1/contact/route.ts 按 authority 契约响应(JSON +
{code:200}),而非改前端去适配本仓——改前端等于把适配成本转嫁给子域,且会让
两个落点的成功判据不一致。9 个单测覆盖校验、限流、上游失败不透传 5xx。

文档与配置同步:CLAUDE.md → AGENTS.md(单文档真源)、docs/landing.md、
docs/deployment.md §八、ADR-0014、qa-tracker §32、README 与 quality-gates 脚本表、
Jenkinsfile 落地「♿ 可访问性门禁」阶段的 landing 四道门禁、eslint 忽略
.next-dev-live 构建产物目录(未忽略时 eslint . 会扫出 21195 errors)。

门禁:tsc 0 / eslint 0 / jest app/api/v1 9/9 / test:landing 13 passed。
2026-12-03 22:56:17 +08:00

156 lines
5.6 KiB
TypeScript
Raw 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 { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals';
import type { NextRequest } from 'next/server';
const mockFetch = jest.fn<(input: unknown, init?: unknown) => Promise<Response>>();
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: '张<script>三' }));
const body = String((mockFetch.mock.calls[0]![1] as { body: string }).body);
const params = new URLSearchParams(body);
expect(params.get('name')).toBe('张script三');
});
it('上游投递失败:不透传 5xx,仍返回可被 landing 展示的 message', async () => {
mockFetch.mockResolvedValue(new Response('bad gateway', { status: 502 }));
const res = await POST(createJsonRequest(validBody));
const data = await res.json();
expect(data.code).not.toBe(200);
expect(data.message).toBeTruthy();
expect(res.status).not.toBe(502);
});
it('限流:同一 IP 超过额度后返回 429 且 code !== 200', async () => {
const ip = '9.9.9.9';
for (let i = 0; i < LANDING_CONTACT_RATE_LIMIT_MAX; i++) {
const r = await POST(createJsonRequest(validBody, ip));
expect(r.status).toBe(200);
}
const limited = await POST(createJsonRequest(validBody, ip));
const data = await limited.json();
expect(limited.status).toBe(429);
expect(data.code).not.toBe(200);
});
});