import { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types'; import { PrismaThrottlerStorage } from '../src/common/throttler/prisma-throttler.storage'; import { createTestApp } from './utils/create-test-app'; describe('Health (e2e)', () => { let app: INestApplication; beforeAll(async () => { app = await createTestApp(); }); afterAll(async () => { await app.close(); }); it('GET /health returns ok without auth', () => { return request(app.getHttpServer()) .get('/health') .expect(200) .expect({ status: 'ok' }); }); it('GET /health/ready returns ok when the database is reachable', () => { return request(app.getHttpServer()) .get('/health/ready') .expect(200) .expect({ status: 'ok' }); }); describe('when the throttler storage is down', () => { afterEach(() => jest.restoreAllMocks()); it('GET /health stays 200 (liveness must not depend on the database)', async () => { jest .spyOn(app.get(PrismaThrottlerStorage), 'increment') .mockRejectedValue(new Error('db down')); await request(app.getHttpServer()).get('/health').expect(200); }); it('GET /health/ready stays throttled (fails with the storage)', async () => { jest .spyOn(app.get(PrismaThrottlerStorage), 'increment') .mockRejectedValue(new Error('db down')); await request(app.getHttpServer()).get('/health/ready').expect(500); }); it('other routes fail closed instead of being allowed', async () => { jest .spyOn(app.get(PrismaThrottlerStorage), 'increment') .mockRejectedValue(new Error('db down')); await request(app.getHttpServer()) .get('/api/v1/public/batches/NONE-x') .expect(500); }); }); });