Files
TeleUploader/test/health.test.ts
T
asepharyana 5d01a9405f refactor(fase0): shared scaffold — crypto, file-url, schemas, test splits
- Add zod dep; new src/shared/validation/schemas.ts (BucketName,
  JsonUploadPayload, LoginBody, DeleteObjects, CompleteMultipart,
  clampMaxKeys, parseOrNull) + test/validation.test.ts
- Dedup timingSafeCompare -> src/shared/utils/crypto.ts (auth
  middleware, authenticate use-case, s3/auth now import it)
- Dedup AuthSession -> single type in dto/auth.ts
- Dedup telegram file URL builder + filename sanitizer to shared
  modules (file-controller now imports the canonical ones)
- Remove duplicate controllers/home.html (canonical: src/home.html)
- Fix stale bootstrap mocks to real module paths; bootstrap now
  asserts public GET vs guarded POST separately
- Fix health assertion to include version field
- package.json: test -> test:unit alias; new test:quarantine for
  network/live tests; register previously unlisted test files
2026-09-14 17:09:52 +07:00

42 lines
1.4 KiB
TypeScript

import { beforeEach, describe, expect, it, mock } from 'bun:test';
// Mock database layer
const mockExecute = mock(() => Promise.resolve());
mock.module('../src/infrastructure/persistence/drizzle/index', () => ({
db: {
execute: mockExecute,
},
}));
describe('Health Route Handler', () => {
let handleHealth: typeof import('../src/interfaces/http/controllers/health-controller').handleHealth;
beforeEach(async () => {
mockExecute.mockClear();
const healthRoute = await import('../src/interfaces/http/controllers/health-controller');
handleHealth = healthRoute.handleHealth;
});
it('should return status 200 and ok when DB is healthy', async () => {
const req = new Request('http://localhost:4000/health');
const res = await handleHealth(req);
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual({ status: 'ok', version: '1.2.2' });
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:4000/health');
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');
});
});