|
| 1 | +const request = require('supertest'); |
| 2 | +const express = require('express'); |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs').promises; |
| 5 | +const jest = require('jest'); |
| 6 | + |
| 7 | +const { updateParameters, getParameters } = require('../controllers/parametersController'); |
| 8 | +const {describe, it, expect, beforeEach} = jest; |
| 9 | + |
| 10 | +// Mock the fs and path modules |
| 11 | +jest.mock('fs').promises; |
| 12 | +jest.mock('path'); |
| 13 | + |
| 14 | +const app = express(); |
| 15 | +app.use(express.json()); |
| 16 | + |
| 17 | +// Set up routes for testing |
| 18 | +app.post('/api/parameters/update', updateParameters); |
| 19 | +app.get('/api/parameters', getParameters); |
| 20 | + |
| 21 | +// Mock data |
| 22 | +const mockParameters = { |
| 23 | + parameters: { |
| 24 | + "preset1": { threshold1: 0.5, threshold2: 0.8 }, |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +describe('Parameters Controller', () => { |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + // Mock the path to parameters.json |
| 32 | + path.join.mockReturnValue('/fake/path/parameters.json'); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('updateParameters', () => { |
| 36 | + it('should update parameters successfully when valid data is provided', async () => { |
| 37 | + const newParameters = { threshold1: 0.6, threshold2: 0.9 }; |
| 38 | + |
| 39 | + fs.readFile.mockResolvedValue(JSON.stringify(mockParameters)); |
| 40 | + fs.writeFile.mockResolvedValue(undefined); // No return value expected from writeFile |
| 41 | + |
| 42 | + const res = await request(app) |
| 43 | + .post('/api/parameters/update') |
| 44 | + .send({ |
| 45 | + preset_name: 'preset1', |
| 46 | + defekt_proportion_thresholds: newParameters, |
| 47 | + }); |
| 48 | + |
| 49 | + expect(res.statusCode).toBe(200); |
| 50 | + expect(res.body).toEqual({ message: 'Parameters updated successfully' }); |
| 51 | + expect(fs.writeFile).toHaveBeenCalledWith( |
| 52 | + '/fake/path/parameters.json', |
| 53 | + JSON.stringify({ parameters: { preset1: newParameters } }, null, 4), |
| 54 | + 'utf8' |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return 400 if the preset is not found', async () => { |
| 59 | + const newParameters = { threshold1: 0.6, threshold2: 0.9 }; |
| 60 | + |
| 61 | + fs.readFile.mockResolvedValue(JSON.stringify(mockParameters)); |
| 62 | + |
| 63 | + const res = await request(app) |
| 64 | + .post('/api/parameters/update') |
| 65 | + .send({ |
| 66 | + preset_name: 'nonexistent_preset', |
| 67 | + defekt_proportion_thresholds: newParameters, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(res.statusCode).toBe(400); |
| 71 | + expect(res.body).toEqual({ message: 'Preset is not found' }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return 400 if the parameters are invalid', async () => { |
| 75 | + const invalidParameters = { threshold1: 1.5, threshold2: 0.9 }; |
| 76 | + |
| 77 | + fs.readFile.mockResolvedValue(JSON.stringify(mockParameters)); |
| 78 | + |
| 79 | + const res = await request(app) |
| 80 | + .post('/api/parameters/update') |
| 81 | + .send({ |
| 82 | + preset_name: 'preset1', |
| 83 | + defekt_proportion_thresholds: invalidParameters, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(res.statusCode).toBe(400); |
| 87 | + expect(res.body).toEqual({ message: 'Invalid parameters format or values out of range' }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return 500 if there is an error reading or writing the file', async () => { |
| 91 | + fs.readFile.mockRejectedValue(new Error('File read error')); |
| 92 | + |
| 93 | + const res = await request(app) |
| 94 | + .post('/api/parameters/update') |
| 95 | + .send({ |
| 96 | + preset_name: 'preset1', |
| 97 | + defekt_proportion_thresholds: { threshold1: 0.6, threshold2: 0.9 }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(res.statusCode).toBe(500); |
| 101 | + expect(res.body).toEqual({ message: 'Error reading or writing file', error: {} }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('getParameters', () => { |
| 106 | + it('should return the parameters successfully', async () => { |
| 107 | + fs.readFile.mockResolvedValue(JSON.stringify(mockParameters)); |
| 108 | + |
| 109 | + const res = await request(app).get('/api/parameters'); |
| 110 | + |
| 111 | + expect(res.statusCode).toBe(200); |
| 112 | + expect(res.body).toEqual(mockParameters.parameters); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return 500 if there is an error reading the file', async () => { |
| 116 | + fs.readFile.mockRejectedValue(new Error('File read error')); |
| 117 | + |
| 118 | + const res = await request(app).get('/api/parameters'); |
| 119 | + |
| 120 | + expect(res.statusCode).toBe(500); |
| 121 | + expect(res.body).toEqual({ message: 'Error reading parameters file', error: {} }); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments