- 实现 82 个集成测试用例覆盖所有 API 端点 - 使用真实数据库连接,无 mock 逻辑 - 修复 PUT 请求只更新提供字段的逻辑 - 配置 Vitest 串行执行避免测试竞态 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { beforeAll, afterAll, describe, it, expect } from 'vitest';
|
|
import { createTestApp } from './helpers/app';
|
|
import { cleanupDatabase, prisma } from './setup';
|
|
|
|
describe('健康检查 API', () => {
|
|
let app: Awaited<ReturnType<typeof createTestApp>>;
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
await cleanupDatabase();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
await cleanupDatabase();
|
|
});
|
|
|
|
describe('GET /api/v1/health', () => {
|
|
it('应返回健康状态', async () => {
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/health',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.success).toBe(true);
|
|
expect(body.data.status).toBe('ok');
|
|
expect(body.data.timestamp).toBeDefined();
|
|
expect(body.data.version).toBe('1.0.0');
|
|
});
|
|
});
|
|
|
|
describe('GET /api/v1/health/detailed', () => {
|
|
it('应返回详细的健康状态包括数据库检查', async () => {
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/health/detailed',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.success).toBe(true);
|
|
expect(body.data.status).toBe('ok');
|
|
expect(body.data.checks).toBeDefined();
|
|
expect(body.data.checks.server).toBe('ok');
|
|
expect(body.data.checks.database).toBe('ok');
|
|
});
|
|
});
|
|
});
|