前端(Vite + React)、API/worker(Fastify + BullMQ)、api-contracts 契约包、 迁移脚本、测试与自托管 docker-compose 示例。 开源准备:运维脚本与机器信息已拆到私有仓库;公司名、备案号、隐私邮箱改读 publicCompliance.local.json(仓库只保留占位模板);站点地址与客服二维码改为 VITE_ 环境变量;fixtures 中的真实用户标识已匿名化;三个 package.json 标注 Apache-2.0。 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
94 lines
2.7 KiB
TypeScript
94 lines
2.7 KiB
TypeScript
import fastify from 'fastify';
|
|
import {
|
|
serializerCompiler,
|
|
validatorCompiler,
|
|
} from 'fastify-type-provider-zod';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { z } from 'zod';
|
|
import { setApiErrorHandler } from '../src/app.js';
|
|
|
|
function contractTestApp() {
|
|
const app = fastify({ logger: false });
|
|
app.setValidatorCompiler(validatorCompiler);
|
|
app.setSerializerCompiler(serializerCompiler);
|
|
setApiErrorHandler(app);
|
|
return app;
|
|
}
|
|
|
|
describe('API 契约运行时', () => {
|
|
it('严格请求 Schema 拒绝未声明字段并返回统一 400', async () => {
|
|
const app = contractTestApp();
|
|
app.post('/strict-request', {
|
|
schema: {
|
|
body: z.object({ name: z.string() }).strict(),
|
|
response: { 200: z.object({ ok: z.literal(true) }) },
|
|
},
|
|
}, async () => ({ ok: true as const }));
|
|
|
|
try {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/strict-request',
|
|
payload: { name: 'Lindyson', legacyName: 'old' },
|
|
});
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
expect(response.json()).toEqual({
|
|
message: '请求参数不正确',
|
|
code: 'VALIDATION_ERROR',
|
|
requestId: expect.any(String),
|
|
issues: [expect.objectContaining({
|
|
code: 'unrecognized_keys',
|
|
path: [],
|
|
})],
|
|
});
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
});
|
|
|
|
it('成功响应不符合 Schema 时返回不泄露字段细节的统一 500', async () => {
|
|
const app = contractTestApp();
|
|
app.get('/invalid-response', {
|
|
schema: {
|
|
response: { 200: z.object({ item: z.object({ id: z.string().uuid() }) }) },
|
|
},
|
|
}, async () => ({ item: { id: 123 } }));
|
|
|
|
try {
|
|
const response = await app.inject({ method: 'GET', url: '/invalid-response' });
|
|
const body = response.json();
|
|
|
|
expect(response.statusCode).toBe(500);
|
|
expect(body).toEqual({
|
|
message: '服务器响应格式错误',
|
|
code: 'API_RESPONSE_SCHEMA_MISMATCH',
|
|
requestId: expect.any(String),
|
|
});
|
|
expect(body).not.toHaveProperty('issues');
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
});
|
|
|
|
it('响应 Schema 会过滤未声明字段', async () => {
|
|
const app = contractTestApp();
|
|
app.get('/filtered-response', {
|
|
schema: {
|
|
response: { 200: z.object({ user: z.object({ id: z.string() }) }) },
|
|
},
|
|
}, async () => ({
|
|
user: { id: 'user-1', passwordHash: 'must-not-leak' },
|
|
databaseDebug: true,
|
|
}));
|
|
|
|
try {
|
|
const response = await app.inject({ method: 'GET', url: '/filtered-response' });
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json()).toEqual({ user: { id: 'user-1' } });
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
});
|
|
});
|