-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
46 lines (37 loc) · 1.58 KB
/
app.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const request = require('supertest');
const app = require('./app');
const fs = require('fs').promises;
describe('GET /monitoring', () => {
beforeEach(async () => {
// Create a sample monitoring data file
const monitoringData = {
lastBlockNumber: 123,
timeSinceLastBlock: 1000,
timeSinceUpgrade: 5000,
freeDiskSpace: 100000,
freeMemory: 8000,
cpuUsage: 10.5,
status: 'OK',
timestamp: new Date().toISOString(),
};
await fs.writeFile('monitoring_data.json', JSON.stringify(monitoringData, null, 2));
});
afterEach(async () => {
// Clean up the sample monitoring data file
await fs.unlink('monitoring_data.json');
});
test('should return monitoring data with status OK', async () => {
const res = await request(app).get('/monitoring');
expect(res.statusCode).toEqual(200);
expect(res.body.status).toEqual('OK');
});
test('should return monitoring data with status ERROR - stale data', async () => {
// Update the timestamp in the monitoring data file to make it stale
const monitoringData = JSON.parse(await fs.readFile('monitoring_data.json', 'utf-8'));
monitoringData.timestamp = new Date(Date.now() - 30000).toISOString();
await fs.writeFile('monitoring_data.json', JSON.stringify(monitoringData, null, 2));
const res = await request(app).get('/monitoring');
expect(res.statusCode).toEqual(200);
expect(res.body.status).toEqual('ERROR - stale data');
});
});