Files
TeleUploader/test/health.test.ts
T

42 lines
1.3 KiB
TypeScript
Raw Normal View History

import { beforeEach, describe, expect, it, mock } from 'bun:test';
2026-05-18 07:10:55 +07:00
// Mock database layer
const mockExecute = mock(() => Promise.resolve());
mock.module('../src/db/index', () => ({
2026-05-18 07:10:55 +07:00
db: {
execute: mockExecute,
},
2026-05-18 07:10:55 +07:00
}));
describe('Health Route Handler', () => {
let handleHealth: typeof import('../src/routes/health').handleHealth;
2026-05-18 07:10:55 +07:00
beforeEach(async () => {
mockExecute.mockClear();
const healthRoute = await import('../src/routes/health');
2026-05-18 07:10:55 +07:00
handleHealth = healthRoute.handleHealth;
});
it('should return status 200 and ok when DB is healthy', async () => {
const req = new Request('http://localhost:3000/health');
2026-05-18 07:10:55 +07:00
const res = await handleHealth(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual({ status: 'ok' });
2026-05-18 07:10:55 +07:00
expect(mockExecute).toHaveBeenCalled();
});
it('should return status 500 and error details when DB health check fails', async () => {
mockExecute.mockImplementationOnce(() => Promise.reject(new Error('DB Connection Failed')));
const req = new Request('http://localhost:3000/health');
2026-05-18 07:10:55 +07:00
const res = await handleHealth(req);
expect(res.status).toBe(500);
const body = await res.json();
expect(body.status).toBe('error');
expect(body.error).toBe('DB Connection Failed');
2026-05-18 07:10:55 +07:00
});
});