|
| 1 | +const request = require('request-promise'); |
| 2 | + |
| 3 | +const db = require('../src/db'); |
| 4 | +const { listen } = require('./utils'); |
| 5 | + |
| 6 | +let mockStatus = 'ok'; |
| 7 | +jest.mock('micro-analytics-adapter-flat-file-db', () => ({ |
| 8 | + version: '42.0.0', |
| 9 | + subscribe: () => {}, |
| 10 | + healthcheck: async () => { |
| 11 | + await new Promise(resolve => setTimeout(resolve, 20)); |
| 12 | + return mockStatus; |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('micro-analytics-adapter-memory', () => ({ |
| 17 | + version: '42.0.0', |
| 18 | +})); |
| 19 | + |
| 20 | +const service = require('../src/handler')({ adapter: 'flat-file-db' }); |
| 21 | + |
| 22 | +let url; |
| 23 | + |
| 24 | +beforeAll(() => { |
| 25 | + db.initDbAdapter({ adapter: 'flat-file-db' }); |
| 26 | +}); |
| 27 | + |
| 28 | +beforeEach(async () => { |
| 29 | + url = await listen(service); |
| 30 | +}); |
| 31 | + |
| 32 | +test('GET /_healtcheck adapter healthcheck returns "ok"', async () => { |
| 33 | + mockStatus = 'ok'; |
| 34 | + const body = JSON.parse(await request(`${url}/_healthcheck`)); |
| 35 | + |
| 36 | + expect(body).toMatchObject({ |
| 37 | + health: 'ok', |
| 38 | + adapter: { |
| 39 | + name: 'flat-file-db', |
| 40 | + version: '42.0.0', |
| 41 | + features: { |
| 42 | + realtime: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +test('GET /_healtcheck adapter healthcheck returns "critical"', async () => { |
| 49 | + mockStatus = 'critical'; |
| 50 | + let error; |
| 51 | + |
| 52 | + try { |
| 53 | + const body = JSON.parse(await request(`${url}/_healthcheck`)); |
| 54 | + } catch (e) { |
| 55 | + error = e; |
| 56 | + } |
| 57 | + |
| 58 | + expect(error.statusCode).toEqual(500); |
| 59 | + expect(JSON.parse(error.error)).toMatchObject({ |
| 60 | + health: 'critical', |
| 61 | + adapter: { |
| 62 | + name: 'flat-file-db', |
| 63 | + version: '42.0.0', |
| 64 | + features: { |
| 65 | + realtime: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +test('GET /_healtcheck when adapter has no healthcheck', async () => { |
| 72 | + let error; |
| 73 | + url = await listen(require('../src/handler')({ adapter: 'memory' })); |
| 74 | + db.initDbAdapter({ adapter: 'memory' }); |
| 75 | + |
| 76 | + try { |
| 77 | + const body = JSON.parse(await request(`${url}/_healthcheck`)); |
| 78 | + } catch (e) { |
| 79 | + error = e; |
| 80 | + } |
| 81 | + |
| 82 | + expect(error.statusCode).toEqual(500); |
| 83 | + expect(JSON.parse(error.error)).toMatchObject({ |
| 84 | + health: 'unknown', |
| 85 | + adapter: { |
| 86 | + name: 'memory', |
| 87 | + version: '42.0.0', |
| 88 | + features: { |
| 89 | + realtime: false, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }); |
| 93 | +}); |
0 commit comments