Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing randomly failing EoS report tests #1601

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/server/controllers/eosReport.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions lib/server/services/eosReport/EosReportService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 20 additions & 11 deletions lib/server/services/shift/ShiftService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -25,21 +24,23 @@ 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
*/
class ShiftService {
/**
* Returns the shift of the user for which EOS can be created
*
* @param {UserIdentifier} shifterUserIdentifier the identifier of the shifter
* @return {Promise<Shift>} 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);
Expand All @@ -50,16 +51,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<object>} the shift data
*/
async getShiftData(shifterUserIdentifier, shiftType) {
const shift = await this.getUserPendingShiftOrFail(shifterUserIdentifier);
const shift = this.getUserPendingShiftOrFail();

const ret = {
shift,
Expand All @@ -69,7 +78,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);
Comment on lines 80 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accoriding to docs, it doesn't return Promise

const expectedPreviousReportTitle = formatEosReportTitle(pastShift, shiftType);

const previousShiftReports = await getLogsByTitle(expectedPreviousReportTitle, { rootOnly: true });
Expand Down
8 changes: 4 additions & 4 deletions test/api/shift.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
24 changes: 9 additions & 15 deletions test/lib/server/services/eosReport/EosReportService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -331,10 +330,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,
Expand All @@ -346,11 +343,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: '',
};

Expand All @@ -361,15 +357,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 });
Expand All @@ -380,13 +375,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,
};

Expand Down
6 changes: 3 additions & 3 deletions test/public/eosReport/ecs-creation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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 { shiftService } = require('../../../lib/server/services/shift/ShiftService.js');

module.exports = () => {
let page;
Expand Down Expand Up @@ -92,11 +92,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,
};

Expand Down
2 changes: 1 addition & 1 deletion test/public/eosReport/shift-leader-creation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading