From ef94d8c1b54ca13771382c576549d02d88ebacff Mon Sep 17 00:00:00 2001 From: Martin Boulais <31805063+martinboulais@users.noreply.github.com> Date: Thu, 6 Jun 2024 16:05:10 +0200 Subject: [PATCH 1/2] Fixing randomly failing EoS report tests --- .../controllers/eosReport.controller.js | 2 +- .../services/eosReport/EosReportService.js | 7 ----- lib/server/services/shift/ShiftService.js | 30 ++++++++++++------- test/api/shift.test.js | 8 ++--- .../eosReport/EosReportService.test.js | 23 ++++++-------- test/public/eosReport/ecs-creation.test.js | 7 +++-- .../eosReport/shift-leader-creation.test.js | 2 +- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/lib/server/controllers/eosReport.controller.js b/lib/server/controllers/eosReport.controller.js index c0e3d40aaa..af7a1ee32a 100644 --- a/lib/server/controllers/eosReport.controller.js +++ b/lib/server/controllers/eosReport.controller.js @@ -28,7 +28,7 @@ const createEosReportHandler = async (request, response) => { const content = await eosReportService.createLogEntry( value.query.reportType, { - shiftStart: (await shiftService.getUserPendingShiftOrFail(userIdentifier)).start, + shiftStart: shiftService.getUserPendingShiftOrFail().start, ...value.body, }, userIdentifier, diff --git a/lib/server/services/eosReport/EosReportService.js b/lib/server/services/eosReport/EosReportService.js index ec3fec81ec..221afebeb1 100644 --- a/lib/server/services/eosReport/EosReportService.js +++ b/lib/server/services/eosReport/EosReportService.js @@ -23,13 +23,6 @@ const { getShiftTagsFiltersByType } = require('../shift/getShiftTagsFiltersByTyp const { getShiftRuns } = require('../shift/getShiftRuns.js'); const { getShiftIssuesTagsOccurrences } = require('../log/getShiftIssuesTagsOccurrences.js'); -/** - * @typedef Shift - * @property {number} start the start of the shift (UNIX timestamp in ms) - * @property {number} end the end of the shift (UNIX timestamp in ms) - * @property {'Morning'|'Afternoon'|'Night'} period the period of the shift - */ - /** * @typedef EosReport * @property {string} type diff --git a/lib/server/services/shift/ShiftService.js b/lib/server/services/shift/ShiftService.js index cf3e69b7d5..ac971c7201 100644 --- a/lib/server/services/shift/ShiftService.js +++ b/lib/server/services/shift/ShiftService.js @@ -25,6 +25,13 @@ const { getLogsByTitle } = require('../log/getLogsByTitle.js'); // Time after the end of the shift during which one the user is allowed to fill the EOS report (currently 30 minutes) const PENDING_SHIFT_MARGIN = 30 * 60 * 1000; +/** + * @typedef Shift + * @property {number} start the start of the shift (UNIX timestamp in ms) + * @property {number} end the end of the shift (UNIX timestamp in ms) + * @property {'Morning'|'Afternoon'|'Night'} period the period of the shift + */ + /** * Service to generate end of shift report */ @@ -32,14 +39,9 @@ class ShiftService { /** * Returns the shift of the user for which EOS can be created * - * @param {UserIdentifier} shifterUserIdentifier the identifier of the shifter - * @return {Promise} resolves with the user's the shift if it exists, else reject + * @return {Shift} Returns the pending shift (offset by pending shift margin) */ - async getUserPendingShiftOrFail(shifterUserIdentifier) { - // For now, do not use user's shift role - // eslint-disable-next-line no-unused-vars - const shifter = await getUserOrFail(shifterUserIdentifier); - + getUserPendingShiftOrFail() { const now = Date.now(); const currentShift = getShiftFromTimestamp(now); @@ -50,16 +52,24 @@ class ShiftService { } } + /** + * Return the shift right before the current user's shift + * + * @return {Shift} resolves with the shift before the current user's shift + */ + getUserPreviousShiftOrFail() { + return getShiftFromTimestamp(this.getUserPendingShiftOrFail().start - SHIFT_DURATION); + } + /** * Return all the data for the pending shift of a specific type for a given user * * @param {UserIdentifier} shifterUserIdentifier the identifier of the user for which shift information must be retrieved * @param {string} shiftType the type of the shift for which information must be retrieved - * @param {Date} shiftStart the start time of the shift (to check for previous information) * @return {Promise} the shift data */ async getShiftData(shifterUserIdentifier, shiftType) { - const shift = await this.getUserPendingShiftOrFail(shifterUserIdentifier); + const shift = this.getUserPendingShiftOrFail(); const ret = { shift, @@ -69,7 +79,7 @@ class ShiftService { }; // Find EoS reports from the last shift with the correct title - const pastShift = getShiftFromTimestamp(shift.start - SHIFT_DURATION); + const pastShift = await this.getUserPreviousShiftOrFail(shifterUserIdentifier); const expectedPreviousReportTitle = formatEosReportTitle(pastShift, shiftType); const previousShiftReports = await getLogsByTitle(expectedPreviousReportTitle, { rootOnly: true }); diff --git a/test/api/shift.test.js b/test/api/shift.test.js index 2105a086a7..07a42d2730 100644 --- a/test/api/shift.test.js +++ b/test/api/shift.test.js @@ -26,7 +26,7 @@ module.exports = () => { // Create some logs for the shift const logIds = []; - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); + const currentShift = shiftService.getUserPendingShiftOrFail(); const defaultLogData = { title: 'Title', text: 'Text', @@ -53,7 +53,7 @@ module.exports = () => { // Create some logs for the shift const logIds = []; - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); + const currentShift = shiftService.getUserPendingShiftOrFail(); const defaultLogData = { title: 'Title', text: 'Text', @@ -80,7 +80,7 @@ module.exports = () => { // Create some logs for the shift const logIds = []; - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); + const currentShift = shiftService.getUserPendingShiftOrFail(); const defaultLogData = { title: 'Title', text: 'Text', @@ -107,7 +107,7 @@ module.exports = () => { // Create some logs for the shift const logIds = []; - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); + const currentShift = shiftService.getUserPendingShiftOrFail(); const defaultLogData = { title: 'Title', text: 'Text', diff --git a/test/lib/server/services/eosReport/EosReportService.test.js b/test/lib/server/services/eosReport/EosReportService.test.js index a137692cb1..34187de317 100644 --- a/test/lib/server/services/eosReport/EosReportService.test.js +++ b/test/lib/server/services/eosReport/EosReportService.test.js @@ -331,10 +331,8 @@ module.exports = () => { }); it ('should throw an error if the previous EoS report has no information transfer field', async () => { - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); - const past = new Date(currentShift.start - SHIFT_DURATION); - const pastShift = getShiftFromTimestamp(past); - const title = formatEosReportTitle(pastShift, ShiftTypes.DCS); + const past = shiftService.getUserPreviousShiftOrFail(); + const title = formatEosReportTitle(past, ShiftTypes.DCS); await logService.create({ userId: 1, title: title, @@ -346,11 +344,10 @@ module.exports = () => { }); it ('should throw an error if the previous EoS report has no information for the next shifter', async () => { - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); - const past = new Date(currentShift.start - SHIFT_DURATION); + const past = shiftService.getUserPreviousShiftOrFail(); const request = { ...emptySlimosEosReportRequest, - shiftStart: past, + shiftStart: past.start, infoForNextShifter: '', }; @@ -361,15 +358,14 @@ module.exports = () => { }); it ('should throw an error if multiple previous EoS reports are found for autofilling', async () => { - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); - const past = new Date(currentShift.start - SHIFT_DURATION); + const past = shiftService.getUserPreviousShiftOrFail(); const request1 = { ...emptySlimosEosReportRequest, - shiftStart: past, + shiftStart: past.start, }; const request2 = { ...emptySlimosEosReportRequest, - shiftStart: past, + shiftStart: past.start, }; await eosReportService.createLogEntry(ShiftTypes.SLIMOS, request1, { userId: 1 }); @@ -380,13 +376,12 @@ module.exports = () => { }); it ('should autofill new EoS reports with information from the previous shifter', async () => { - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); - const past = new Date(currentShift.start - SHIFT_DURATION); + const past = shiftService.getUserPreviousShiftOrFail(); const info = `Important information for the next tester containing new lines and #punctuation...`; const request = { ...emptyECSEosReportRequest, - shiftStart: past, + shiftStart: past.start, infoForNextShifter: info, }; diff --git a/test/public/eosReport/ecs-creation.test.js b/test/public/eosReport/ecs-creation.test.js index 75b7008967..d2a7d60db3 100644 --- a/test/public/eosReport/ecs-creation.test.js +++ b/test/public/eosReport/ecs-creation.test.js @@ -33,7 +33,8 @@ const { getOrCreateAllDetectorsByName } = require('../../../lib/server/services/ const EorReasonRepository = require('../../../lib/database/repositories/EorReasonRepository.js'); const { ShiftTypes } = require('../../../lib/domain/enums/ShiftTypes.js'); const { eosReportService } = require('../../../lib/server/services/eosReport/EosReportService.js'); -const { SHIFT_DURATION } = require('../../../lib/server/services/shift/getShiftFromTimestamp.js'); +const { SHIFT_DURATION, getShiftFromTimestamp } = require('../../../lib/server/services/shift/getShiftFromTimestamp.js'); +const { shiftService } = require('../../../lib/server/services/shift/ShiftService.js'); module.exports = () => { let page; @@ -92,11 +93,11 @@ module.exports = () => { } // Create the expected previous EoS report - const past = new Date(Date.now() - SHIFT_DURATION); + const past = shiftService.getUserPreviousShiftOrFail(); const info = 'Important information for the next tester'; const request = { ...emptyECSEosReportRequest, - shiftStart: past, + shiftStart: past.start, infoForNextShifter: info, }; diff --git a/test/public/eosReport/shift-leader-creation.test.js b/test/public/eosReport/shift-leader-creation.test.js index 57622772d8..afb027eca5 100644 --- a/test/public/eosReport/shift-leader-creation.test.js +++ b/test/public/eosReport/shift-leader-creation.test.js @@ -77,7 +77,7 @@ module.exports = () => { await reloadPage(page); - const currentShift = await shiftService.getUserPendingShiftOrFail({ userId: 1 }); + const currentShift = shiftService.getUserPendingShiftOrFail(); const magnetStart = formatShiftDate(currentShift.start, { time: true }); const { formatTimestampForDateTimeInput } = await import('../../../lib/public/utilities/formatting/dateTimeInputFormatters.mjs'); From ca81970316576f7c678c20d8fa584a8bdb70dbd6 Mon Sep 17 00:00:00 2001 From: martinboulais <31805063+martinboulais@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:17:02 +0200 Subject: [PATCH 2/2] Fix linter --- lib/server/services/shift/ShiftService.js | 1 - test/lib/server/services/eosReport/EosReportService.test.js | 1 - test/public/eosReport/ecs-creation.test.js | 1 - 3 files changed, 3 deletions(-) diff --git a/lib/server/services/shift/ShiftService.js b/lib/server/services/shift/ShiftService.js index ac971c7201..c15b74153f 100644 --- a/lib/server/services/shift/ShiftService.js +++ b/lib/server/services/shift/ShiftService.js @@ -11,7 +11,6 @@ * or submit itself to any jurisdiction. */ const { getShiftFromTimestamp, SHIFT_DURATION } = require('./getShiftFromTimestamp.js'); -const { getUserOrFail } = require('../user/getUserOrFail.js'); const { logAdapter, environmentAdapter, runAdapter } = require('../../../database/adapters/index.js'); const { getShiftIssues } = require('../eosReport/getShiftIssues.js'); const { ShiftTypes } = require('../../../domain/enums/ShiftTypes.js'); diff --git a/test/lib/server/services/eosReport/EosReportService.test.js b/test/lib/server/services/eosReport/EosReportService.test.js index 34187de317..4a48077ba0 100644 --- a/test/lib/server/services/eosReport/EosReportService.test.js +++ b/test/lib/server/services/eosReport/EosReportService.test.js @@ -51,7 +51,6 @@ const { } = 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'); -const { getShiftFromTimestamp, SHIFT_DURATION } = require('../../../../../lib/server/services/shift/getShiftFromTimestamp.js'); module.exports = () => { it('should successfully create a log containing ECS EoS report', async () => { diff --git a/test/public/eosReport/ecs-creation.test.js b/test/public/eosReport/ecs-creation.test.js index d2a7d60db3..5d3e78eba7 100644 --- a/test/public/eosReport/ecs-creation.test.js +++ b/test/public/eosReport/ecs-creation.test.js @@ -33,7 +33,6 @@ const { getOrCreateAllDetectorsByName } = require('../../../lib/server/services/ const EorReasonRepository = require('../../../lib/database/repositories/EorReasonRepository.js'); const { ShiftTypes } = require('../../../lib/domain/enums/ShiftTypes.js'); const { eosReportService } = require('../../../lib/server/services/eosReport/EosReportService.js'); -const { SHIFT_DURATION, getShiftFromTimestamp } = require('../../../lib/server/services/shift/getShiftFromTimestamp.js'); const { shiftService } = require('../../../lib/server/services/shift/ShiftService.js'); module.exports = () => {