-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEosReportService.test.js
More file actions
397 lines (349 loc) · 18.6 KB
/
EosReportService.test.js
File metadata and controls
397 lines (349 loc) · 18.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const { eosReportService } = require('../../../../../lib/server/services/eosReport/EosReportService.js');
const { shiftService } = require('../../../../../lib/server/services/shift/ShiftService.js');
const { expect } = require('chai');
const {
emptyECSEosReportRequest,
formattedCustomizedECSEosReport,
customizedECSEosReport,
eosEcsReportTitle,
customizedECSEosReportRequest,
formattedEmptyECSEosReport,
customizedECSEosReportLogs,
} = require('../../../../mocks/mock-ecs-eos-report.js');
const { resetDatabaseContent } = require('../../../../utilities/resetDatabaseContent.js');
const { createLog } = require('../../../../../lib/server/services/log/createLog.js');
const { ShiftTypes } = require('../../../../../lib/domain/enums/ShiftTypes.js');
const { LogRunsRepository } = require('../../../../../lib/database/repositories/index.js');
const { createEnvironment } = require('../../../../../lib/server/services/environment/createEnvironment.js');
const { createEnvironmentHistoryItem } = require('../../../../../lib/server/services/environmentHistoryItem/createEnvironmentHistoryItem.js');
const { createRun } = require('../../../../../lib/server/services/run/createRun.js');
const EorReasonRepository = require('../../../../../lib/database/repositories/EorReasonRepository.js');
const { getEosReportTagsByType } = require('../../../../../lib/server/services/eosReport/eosTypeSpecificFormatter/getEosReportTagsByType.js');
const {
formattedEmptyQcPdpEosReport, eosQcPdpReportTitle, emptyQcPdpEosReportRequest, customizedQcPdpEosReport, customizedQcPdpEosReportLogs,
customizedQcPdpEosReportRequest, formattedCustomizedQcPdpEosReport,
} = require('../../../../mocks/mock-qc-pdp-eos-report.js');
const { updateRunDetector } = require('../../../../../lib/server/services/runDetector/updateRunDetector.js');
const {
customizedSlimosEosReportLogs, customizedSlimosEosReport, customizedSlimosEosReportRequest, formattedCustomizedSlimosEosReport,
eosSlimosReportTitle, emptySlimosEosReportRequest, formattedEmptySlimosEosReport,
} = require('../../../../mocks/mock-slimos-eos-report.js');
const {
customizedShiftLeaderEosReport, customizedShiftLeaderEosReportLogs, customizedShiftLeaderEosReportRequest,
formattedCustomizedShiftLeaderEosReport, eosShiftLeaderReportTitle, emptyShiftLeaderEosReportRequest, formattedEmptyShiftLeaderEosReport,
} = require('../../../../mocks/mock-shift-leader-eos-report.js');
const {
customizedDcsEosReportLogs, customizedDcsEosReport, customizedDcsEosReportRequest, formattedCustomizedDcsEosReport, eosDcsReportTitle,
emptyDcsEosReportRequest, formattedEmptyDcsEosReport,
} = require('../../../../mocks/mock-dcs-eos-report.js');
const { logService } = require('../../../../../lib/server/services/log/LogService.js');
const { formatEosReportTitle } = require('../../../../../lib/server/services/eosReport/formatEosReport.js');
module.exports = () => {
it('should successfully create a log containing ECS EoS report', async () => {
const expectedRunNumbers = [];
const expectedEnvironmentIds = [];
// Create the expected logs and runs
for (const environment of customizedECSEosReport.typeSpecific.environments) {
await createEnvironment(environment);
await createEnvironmentHistoryItem({
status: 'DESTROYED',
timestamp: environment.createdAt,
timestampNano: 0,
environmentId: environment.id,
});
expectedEnvironmentIds.push(environment.id);
for (const run of environment.runs) {
const runId = await createRun(run);
expectedRunNumbers.push(run.runNumber);
// Create the expected EOR
const eorReasons = [];
for (const eorReason of run.eorReasons) {
eorReasons.push({ reasonTypeId: eorReason.reasonTypeId, description: eorReason.description, runId });
}
if (eorReasons.length > 0) {
await EorReasonRepository.addMany(eorReasons);
}
}
}
// Create the expected logs
const logRuns = [];
for (const log of customizedECSEosReportLogs) {
const logCreationRequest = {
title: log.title,
text: 'This is not important for test',
createdAt: customizedECSEosReport.shiftStart,
subtype: 'comment',
origin: 'human',
};
if (log.parentLogId) {
logCreationRequest.parentLogId = log.parentLogId;
}
const logId = await createLog(logCreationRequest, [200], log.tags.map(({ text }) => text), [], [], []);
logRuns.push({ logId, runId: 1 });
}
LogRunsRepository.bulkInsert(logRuns);
const log = await eosReportService.createLogEntry(ShiftTypes.ECS, customizedECSEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedCustomizedECSEosReport);
expect(log.title).to.equal(eosEcsReportTitle);
expect(log.tags.map(({ text }) => text)).to.have.members(getEosReportTagsByType(ShiftTypes.ECS));
expect(log.runs.map(({ runNumber }) => runNumber)).to.eql(expectedRunNumbers);
expect(log.environments.map(({ id }) => id)).to.eql(expectedEnvironmentIds);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing ECS EoS report with default values', async () => {
await resetDatabaseContent();
const log = await eosReportService.createLogEntry(ShiftTypes.ECS, emptyECSEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedEmptyECSEosReport);
expect(log.title).to.equal(eosEcsReportTitle);
expect(log.runs.length).to.equal(0);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing QC/PDP EoS report', async () => {
await resetDatabaseContent();
const expectedRunNumbers = [];
const expectedEnvironmentIds = [];
// Create the expected logs and runs
for (const runs of Object.values(customizedQcPdpEosReport.typeSpecific.runs)) {
for (const run of runs) {
const runId = await createRun(run, { detectors: run.detectors });
for (const detector of run.detectors) {
await updateRunDetector(
run.runNumber,
detector.RunDetectors.detectorId,
{ quality: detector.RunDetectors.quality },
);
}
expectedRunNumbers.push(run.runNumber);
if (Object.hasOwn(run, 'envId')) {
expectedEnvironmentIds.push(run.envId);
}
// Create the expected EOR
const eorReasons = [];
for (const eorReason of run.eorReasons ?? []) {
eorReasons.push({ reasonTypeId: eorReason.reasonTypeId, description: eorReason.description, runId });
}
if (eorReasons.length > 0) {
await EorReasonRepository.addMany(eorReasons);
}
}
}
// Create the expected logs
const logRuns = [];
for (const log of customizedQcPdpEosReportLogs) {
const logCreationRequest = {
title: log.title,
text: 'This is not important for test',
createdAt: customizedQcPdpEosReport.shiftStart,
subtype: 'comment',
origin: 'human',
};
if (log.parentLogId) {
logCreationRequest.parentLogId = log.parentLogId;
}
const logId = await createLog(logCreationRequest, [200], log.tags.map(({ text }) => text), [], [], []);
logRuns.push({ logId, runId: 1 });
}
LogRunsRepository.bulkInsert(logRuns);
const log = await eosReportService.createLogEntry(ShiftTypes.QC_PDP, customizedQcPdpEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedCustomizedQcPdpEosReport);
expect(log.title).to.equal(eosQcPdpReportTitle);
expect(log.tags.map(({ text }) => text)).to.have.members(getEosReportTagsByType(ShiftTypes.QC_PDP));
expect(log.runs.map(({ runNumber }) => runNumber)).to.eql(expectedRunNumbers);
expect(log.environments.map(({ id }) => id).sort).to.eql(expectedEnvironmentIds.sort);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing QC/PDP EoS report with default values', async () => {
await resetDatabaseContent();
const log = await eosReportService.createLogEntry(ShiftTypes.QC_PDP, emptyQcPdpEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedEmptyQcPdpEosReport);
expect(log.title).to.equal(eosQcPdpReportTitle);
expect(log.runs.length).to.equal(0);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing SLIMOS EoS report', async () => {
await resetDatabaseContent();
// Create the expected logs
for (const log of customizedSlimosEosReportLogs) {
const logCreationRequest = {
title: log.title,
text: 'This is not important for test',
createdAt: customizedSlimosEosReport.shiftStart,
subtype: 'comment',
origin: 'human',
};
if (log.parentLogId) {
logCreationRequest.parentLogId = log.parentLogId;
}
await createLog(logCreationRequest, [], log.tags.map(({ text }) => text), [], [], []);
}
const log = await eosReportService.createLogEntry(ShiftTypes.SLIMOS, customizedSlimosEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedCustomizedSlimosEosReport);
expect(log.title).to.equal(eosSlimosReportTitle);
expect(log.tags.map(({ text }) => text)).to.have.members(getEosReportTagsByType(ShiftTypes.SLIMOS));
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing SLIMOS EoS report with default values', async () => {
await resetDatabaseContent();
const log = await eosReportService.createLogEntry(ShiftTypes.SLIMOS, emptySlimosEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedEmptySlimosEosReport);
expect(log.title).to.equal(eosSlimosReportTitle);
expect(log.runs.length).to.equal(0);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing SL EoS report', async () => {
await resetDatabaseContent();
const expectedRunNumbers = [];
const expectedEnvironmentIds = [];
// Create the expected logs and runs
for (const runs of Object.values(customizedShiftLeaderEosReport.typeSpecific.runs)) {
for (const run of runs) {
await createRun(run, { detectors: run.detectors });
for (const detector of run.detectors) {
await updateRunDetector(
run.runNumber,
detector.RunDetectors.detectorId,
{ quality: detector.RunDetectors.quality },
);
}
expectedRunNumbers.push(run.runNumber);
if (Object.hasOwn(run, 'envId')) {
expectedEnvironmentIds.push(run.envId);
}
}
}
// Create the expected logs
const logRuns = [];
for (const log of customizedShiftLeaderEosReportLogs) {
const logCreationRequest = {
title: log.title,
text: 'This is not important for test',
createdAt: customizedShiftLeaderEosReport.shiftStart,
subtype: 'comment',
origin: 'human',
};
if (log.parentLogId) {
logCreationRequest.parentLogId = log.parentLogId;
}
const logId = await createLog(logCreationRequest, [200], log.tags.map(({ text }) => text), [], [], []);
logRuns.push({ logId, runId: 1 });
}
LogRunsRepository.bulkInsert(logRuns);
const log = await eosReportService.createLogEntry(ShiftTypes.SL, customizedShiftLeaderEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedCustomizedShiftLeaderEosReport);
expect(log.title).to.equal(eosShiftLeaderReportTitle);
expect(log.tags.map(({ text }) => text)).to.have.members(getEosReportTagsByType(ShiftTypes.SL));
expect(log.runs.map(({ runNumber }) => runNumber)).to.eql(expectedRunNumbers);
expect(log.environments.map(({ id }) => id).sort).to.eql(expectedEnvironmentIds.sort);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing Shift Leader EoS report with default values', async () => {
await resetDatabaseContent();
const log = await eosReportService.createLogEntry(ShiftTypes.SL, emptyShiftLeaderEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedEmptyShiftLeaderEosReport);
expect(log.title).to.equal(eosShiftLeaderReportTitle);
expect(log.runs.length).to.equal(0);
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing DCS EoS report', async () => {
await resetDatabaseContent();
// Create the expected logs
for (const log of customizedDcsEosReportLogs) {
const logCreationRequest = {
title: log.title,
text: 'This is not important for test',
createdAt: customizedDcsEosReport.shiftStart,
subtype: 'comment',
origin: 'human',
};
if (log.parentLogId) {
logCreationRequest.parentLogId = log.parentLogId;
}
await createLog(logCreationRequest, [], log.tags.map(({ text }) => text), [], [], []);
}
const log = await eosReportService.createLogEntry(ShiftTypes.DCS, customizedDcsEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedCustomizedDcsEosReport);
expect(log.title).to.equal(eosDcsReportTitle);
expect(log.tags.map(({ text }) => text)).to.have.members(getEosReportTagsByType(ShiftTypes.DCS));
expect(log.author.id).to.equal(1);
});
it('should successfully create a log containing DCS EoS report with default values', async () => {
await resetDatabaseContent();
const log = await eosReportService.createLogEntry(ShiftTypes.DCS, emptyDcsEosReportRequest, { userId: 1 });
expect(log.text).to.equal(formattedEmptyDcsEosReport);
expect(log.title).to.equal(eosDcsReportTitle);
expect(log.runs.length).to.equal(0);
expect(log.author.id).to.equal(1);
});
it ('should throw an error if no previous EoS report is found for autofilling', async () => {
const response = await shiftService.getShiftData({ userId: 1 }, ShiftTypes.SLIMOS);
const { infoFromPreviousShifter } = response;
expect(infoFromPreviousShifter.errorMessage).to.equal('No SLIMOS EoS reports found from the previous shift');
});
it ('should throw an error if the previous EoS report has no information transfer field', async () => {
const past = shiftService.getUserPreviousShiftOrFail();
const title = formatEosReportTitle(past, ShiftTypes.DCS);
await logService.create({
userId: 1,
title: title,
text: 'Missing information',
});
const response = await shiftService.getShiftData({ userId: 1 }, ShiftTypes.DCS);
const { infoFromPreviousShifter } = response;
expect(infoFromPreviousShifter.errorMessage)
.to.equal('EoS report from the previous shift is missing the information transfer field');
});
it ('should throw an error if the previous EoS report has no information for the next shifter', async () => {
const past = shiftService.getUserPreviousShiftOrFail();
const request = {
...emptySlimosEosReportRequest,
shiftStart: past.start,
infoForNextShifter: '',
};
await eosReportService.createLogEntry(ShiftTypes.SLIMOS, request, { userId: 1 });
const response = await shiftService.getShiftData({ userId: 1 }, ShiftTypes.SLIMOS);
const { infoFromPreviousShifter } = response;
expect(infoFromPreviousShifter.errorMessage).to.equal('Previous EoS report contains no information for next shifter');
});
it ('should throw an error if multiple previous EoS reports are found for autofilling', async () => {
const past = shiftService.getUserPreviousShiftOrFail();
const request1 = {
...emptySlimosEosReportRequest,
shiftStart: past.start,
};
const request2 = {
...emptySlimosEosReportRequest,
shiftStart: past.start,
};
await eosReportService.createLogEntry(ShiftTypes.SLIMOS, request1, { userId: 1 });
await eosReportService.createLogEntry(ShiftTypes.SLIMOS, request2, { userId: 1 });
const response = await shiftService.getShiftData({ userId: 1 }, ShiftTypes.SLIMOS);
const { infoFromPreviousShifter } = response;
expect(infoFromPreviousShifter.errorMessage).to.equal('Multiple SLIMOS EoS reports found from the previous shift');
});
it ('should autofill new EoS reports with information from the previous shifter', async () => {
const past = shiftService.getUserPreviousShiftOrFail();
const info = `Important information for the next tester
containing new lines and #punctuation...`;
const request = {
...emptyECSEosReportRequest,
shiftStart: past.start,
infoForNextShifter: info,
};
await eosReportService.createLogEntry(ShiftTypes.ECS, request, { userId: 1 });
const response = await shiftService.getShiftData({ userId: 1 }, ShiftTypes.ECS);
const { infoFromPreviousShifter } = response;
expect(infoFromPreviousShifter.value).to.equal(info);
});
};