|
1 | 1 | const request = require('supertest') |
| 2 | +const { generateStaticReports } = require('../src/reports') |
2 | 3 | const serverModule = require('../src/httpServer') |
3 | | -const server = serverModule() |
4 | | -const app = request(server) |
| 4 | +let server |
| 5 | +let serverStop |
| 6 | +let app |
5 | 7 |
|
6 | | -// Cleanup after all tests |
7 | | -afterAll(() => { |
8 | | - server?.close() |
| 8 | +// Mocks |
| 9 | +jest.mock('../src/reports', () => ({ |
| 10 | + generateStaticReports: jest.fn() |
| 11 | +})) |
| 12 | + |
| 13 | +beforeAll(async () => { |
| 14 | + // Initialize server asynchronously |
| 15 | + const serverInstance = serverModule() |
| 16 | + server = await serverInstance.start() |
| 17 | + serverStop = serverInstance.stop |
| 18 | + app = request(server) |
| 19 | +}) |
| 20 | + |
| 21 | +afterAll(async () => { |
| 22 | + // Cleanup after all tests |
| 23 | + await serverStop?.() |
| 24 | +}) |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + jest.clearAllMocks() |
9 | 28 | }) |
10 | 29 |
|
11 | 30 | describe('HTTP Server API', () => { |
12 | | - test('health check endpoint should return status ok', async () => { |
13 | | - const response = await app.get('/api/v1/__health') |
| 31 | + describe('GET /api/v1/__health', () => { |
| 32 | + test('should return status ok', async () => { |
| 33 | + const response = await app.get('/api/v1/__health') |
14 | 34 |
|
15 | | - expect(response.status).toBe(200) |
16 | | - expect(response.body).toHaveProperty('status', 'ok') |
17 | | - expect(response.body).toHaveProperty('timestamp') |
| 35 | + expect(response.status).toBe(200) |
| 36 | + expect(response.body).toHaveProperty('status', 'ok') |
| 37 | + expect(response.body).toHaveProperty('timestamp') |
18 | 38 |
|
19 | | - const timestamp = new Date(response.body.timestamp) |
20 | | - expect(timestamp.toISOString()).toBe(response.body.timestamp) |
| 39 | + const timestamp = new Date(response.body.timestamp) |
| 40 | + expect(timestamp.toISOString()).toBe(response.body.timestamp) |
| 41 | + }) |
21 | 42 | }) |
22 | 43 |
|
23 | | - test('non-existent API endpoint should return 404', async () => { |
24 | | - const response = await app.get('/api/v1/non-existent-endpoint') |
| 44 | + describe('POST /api/v1/generate-reports', () => { |
| 45 | + test('should return status completed when report generation succeeds', async () => { |
| 46 | + generateStaticReports.mockResolvedValueOnce() |
| 47 | + |
| 48 | + const response = await app.post('/api/v1/generate-reports') |
| 49 | + |
| 50 | + expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true }) |
| 51 | + expect(response.status).toBe(202) |
| 52 | + expect(response.body).toHaveProperty('status', 'completed') |
| 53 | + expect(response.body).toHaveProperty('startedAt') |
| 54 | + expect(response.body).toHaveProperty('finishedAt') |
| 55 | + |
| 56 | + const startedAt = new Date(response.body.startedAt) |
| 57 | + const finishedAt = new Date(response.body.finishedAt) |
| 58 | + expect(startedAt.toISOString()).toBe(response.body.startedAt) |
| 59 | + expect(finishedAt.toISOString()).toBe(response.body.finishedAt) |
| 60 | + expect(finishedAt >= startedAt).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + test('should return status failed when report generation fails', async () => { |
| 64 | + generateStaticReports.mockRejectedValueOnce(new Error('Report generation failed')) |
| 65 | + |
| 66 | + const response = await app.post('/api/v1/generate-reports') |
| 67 | + |
| 68 | + expect(generateStaticReports).toHaveBeenCalledWith(expect.anything(), { clearPreviousReports: true }) |
| 69 | + expect(response.status).toBe(500) |
| 70 | + expect(response.body).toHaveProperty('status', 'failed') |
| 71 | + expect(response.body).toHaveProperty('startedAt') |
| 72 | + expect(response.body).toHaveProperty('finishedAt') |
25 | 73 |
|
26 | | - expect(response.status).toBe(404) |
| 74 | + const startedAt = new Date(response.body.startedAt) |
| 75 | + const finishedAt = new Date(response.body.finishedAt) |
| 76 | + expect(startedAt.toISOString()).toBe(response.body.startedAt) |
| 77 | + expect(finishedAt.toISOString()).toBe(response.body.finishedAt) |
| 78 | + expect(finishedAt >= startedAt).toBe(true) |
| 79 | + }) |
27 | 80 | }) |
28 | 81 | }) |
0 commit comments