-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrunMode.test.js
More file actions
262 lines (227 loc) · 11.5 KB
/
runMode.test.js
File metadata and controls
262 lines (227 loc) · 11.5 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
/**
* @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.
*/
/* eslint-disable @stylistic/js/max-len */
import { strictEqual, ok, deepStrictEqual } from 'node:assert';
import { delay } from '../../testUtils/delay.js';
import { IntegratedServices } from '../../../common/library/enums/Status/integratedServices.enum.js';
import { ServiceStatus } from '../../../common/library/enums/Status/serviceStatus.enum.js';
import { integratedServiceInterceptor } from '../../testUtils/interceptors/integratedServiceInterceptor.js';
import { ONGOING_RUN_NUMBER } from '../../setup/mockKafkaEvents.js';
// If using nock for HTTP mocking (uncomment if available)
// import nock from 'nock';
export const runModeTests = async (url, page, timeout = 5000, testParent) => {
const mockedTestRunNumber = ONGOING_RUN_NUMBER;
let countRunStatusCalls = 0;
let expectCountRunStatusCalls = 0;
let countObjectsCalls = 0;
page.on('request', (req) => {
const url = req.url();
const decodedUrl = decodeURIComponent(url);
if (url.includes('/api/objects') && decodedUrl.includes(`filters[RunNumber]=${mockedTestRunNumber}`)) {
countObjectsCalls++;
} else if (url.includes(`/api/filter/run-status/${mockedTestRunNumber}`)) {
countRunStatusCalls++;
}
});
await testParent.test('when kafka service is not configured the run mode toggle should be hidden', { timeout }, async () => {
const requestHandler = (request) => integratedServiceInterceptor(request, IntegratedServices.KAFKA, ServiceStatus.NOT_CONFIGURED);
try {
// Enable interception and attach the handler
await page.setRequestInterception(true);
page.on('request', requestHandler);
await page.goto(
`${url}?page=objectTree`,
{ waitUntil: 'networkidle0' },
);
await delay(100);
// Prevent the 'get run status' from re-triggering mid test
await page.evaluate(() => {
window.model.filterModel.ONGOING_RUN_INTERVAL_MS = 12000000;
});
const runsModeToggleNoExist = await page.evaluate(() => document.querySelector('#run-mode-switch') === null);
ok(runsModeToggleNoExist, 'The RunMode switch should not be displayed');
const runsModeErrorNoExist = await page.evaluate(() => document.querySelector('#run-mode-failure') === null);
ok(runsModeErrorNoExist, 'The RunMode switch should not be displayed');
} finally {
// Cleanup: remove listener and disable interception
page.off('request', requestHandler);
await page.setRequestInterception(false);
}
});
await testParent.test('when kafka service is unavailable nothing should be displayed (rely on about page)', { timeout }, async () => {
const requestHandler = (request) => integratedServiceInterceptor(request, IntegratedServices.KAFKA, ServiceStatus.ERROR, {
message: 'test error',
});
try {
// Enable interception and attach the handler
await page.setRequestInterception(true);
page.on('request', requestHandler);
await page.goto(
`${url}?page=objectTree`,
{ waitUntil: 'networkidle0' },
);
await delay(100);
// Prevent the 'get run status' from re-triggering mid test
await page.evaluate(() => {
window.model.filterModel.ONGOING_RUN_INTERVAL_MS = 12000000;
});
const runsModeNoExist = await page.evaluate(() => document.querySelector('#run-mode-switch') === null);
ok(runsModeNoExist, 'The RunMode switch should not be displayed');
} finally {
// Cleanup: remove listener and disable interception
page.off('request', requestHandler);
await page.setRequestInterception(false);
}
});
await testParent.test('should have a switch to enable run mode when kafka service is available', { timeout }, async () => {
// The kafka service is required for run mode to be available
const requestHandler = (request) => integratedServiceInterceptor(request, IntegratedServices.KAFKA, ServiceStatus.SUCCESS);
try {
// Enable interception and attach the handler
await page.setRequestInterception(true);
page.on('request', requestHandler);
await page.goto(
`${url}?page=objectTree`,
{ waitUntil: 'networkidle0' },
);
await delay(100);
// Prevent the 'get run status' from re-triggering mid test
await page.evaluate(() => {
window.model.filterModel.ONGOING_RUN_INTERVAL_MS = 12000000;
});
await page.locator('#run-mode-switch > .switch');
const runsModeTitle = await page.evaluate(() => document.querySelector('#run-mode-switch')?.textContent);
strictEqual(runsModeTitle, 'Run mode', 'The text displayed is not `Run mode`');
} finally {
// Cleanup: remove listener and disable interception
page.off('request', requestHandler);
await page.setRequestInterception(false);
}
});
await testParent.test('should have a switch to enable run mode', { timeout }, async () => {
// The kafka service is required for run mode to be available
const requestHandler = (request) => integratedServiceInterceptor(request, IntegratedServices.KAFKA, ServiceStatus.SUCCESS);
try {
// Enable interception and attach the handler
await page.setRequestInterception(true);
page.on('request', requestHandler);
await page.goto(
`${url}?page=objectTree`,
{ waitUntil: 'networkidle0' },
);
await delay(100);
// Prevent the 'get run status' from re-triggering mid test
await page.evaluate(() => {
window.model.filterModel.ONGOING_RUN_INTERVAL_MS = 12000000;
});
await page.locator('#run-mode-switch > .switch');
const runsModeTitle = await page.evaluate(() =>
document.querySelector('#run-mode-switch')?.textContent);
strictEqual(runsModeTitle, 'Run mode', 'The text displayed is not `Run mode`');
} finally {
// Cleanup: remove listener and disable interception
page.off('request', requestHandler);
await page.setRequestInterception(false);
}
});
await testParent.test('should activate run mode', { timeout }, async () => {
await page.locator('#run-mode-switch > .switch').click();
await delay(500);
expectCountRunStatusCalls ++;
const isRunModeActivated = await page.evaluate(() => window.model.filterModel.isRunModeActivated);
ok(isRunModeActivated, 'Run mode should be activated');
});
await testParent.test('should display ongoing runs selector', { timeout }, async () => {
await page.waitForSelector('#ongoingRunsFilter', { timeout: 1000 });
const selector = await page.locator('#ongoingRunsFilter');
ok(selector, 'Ongoing runs selector should be present');
});
await testParent.test('should have correct options in ongoing runs selector', { timeout }, async () => {
const availableOptions = await page.evaluate(() => {
const selector = document.querySelector('#ongoingRunsFilter');
return Array.from(selector.options)
.map((option) => option.value)
.filter((value) => value !== '');
});
deepStrictEqual(availableOptions, ['500001', '500002', '500003'], 'Ongoing runs selector should have correct options');
});
await testParent.test('should automatically select first run and update URL', { timeout }, async () => {
await delay(500);
const currentUrl = await page.evaluate(() => window.location.href);
ok(currentUrl.includes(`RunNumber=${mockedTestRunNumber}`), `URL should contain RunNumber=${mockedTestRunNumber} parameter`);
const selectedRunNumber = await page.evaluate(() => {
const selector = document.querySelector('#ongoingRunsFilter');
return selector.value;
});
strictEqual(selectedRunNumber, mockedTestRunNumber.toString(), 'First ongoing run should be automatically selected');
});
await testParent.test('should show ENDED status when run finishes', { timeout }, async () => {
await delay(500);
// Manually trigger refresh, we need to do this since periodic refresh is off (high delay) due to inconsistant tests.
await page.evaluate(async (mockedTestRunNumber) => {
await model.filterModel._refreshRunsModeStatus(model.layoutListModel, mockedTestRunNumber);
}, mockedTestRunNumber);
expectCountRunStatusCalls++;
await delay(1000);
await page.waitForSelector('#runStatusPanel');
const runStatusInfo = await page.evaluate(() => {
const status = document.querySelector('#runStatusBadge')?.textContent;
return { status };
});
// Verify the run status changed to ENDED
strictEqual(runStatusInfo.status, 'ENDED', 'Run status should be ENDED');
});
await testParent.test('should persist runs mode between pages', { timeout }, async () => {
await page.locator('.menu-item:nth-child(3) > .ph2').click();
expectCountRunStatusCalls++;// fetches run information on page load
await page.waitForSelector('#runStatusPanel');
const runInfo = await page.evaluate(() => {
const status = document.querySelector('#runStatusBadge').textContent;
const selector = document.querySelector('#ongoingRunsFilter');
const [, firstOption] = selector.options;
const isSelected = firstOption.selected;
const { value } = firstOption;
return { status, isSelected, value };
});
const isRunModeActivated = await page.evaluate(() => window.model.filterModel.isRunModeActivated);
ok(isRunModeActivated);
ok(runInfo.isSelected);
ok(runInfo.value, mockedTestRunNumber.toString());
ok(runInfo.status, 'ENDED');
});
await testParent.test('should exit runs mode successfully', { timeout }, async () => {
// Verify run mode is currently active
let isRunModeActive = await page.evaluate(() => window.model.filterModel.isRunModeActivated);
ok(isRunModeActive, 'Run mode should be active before disabling');
// Click the run mode checkbox to disable it
await page.locator('#run-mode-switch > .switch').click();
await delay(100);
// Verify run mode is now deactivated
isRunModeActive = await page.evaluate(() => window.model.filterModel.isRunModeActivated);
ok(!isRunModeActive, 'Run mode should be deactivated after clicking checkbox');
//check the filters element is back again and run number and URL are cleared
await page.locator('#filterElement');
const currentUrl = await page.evaluate(() => window.location.href);
ok(!currentUrl.includes(`RunNumber=${mockedTestRunNumber}`), 'URL should not contain RunNumber parameter after disabling run mode');
//filterElement to be empty
const filterElementContent = await page.evaluate(() => {
const filterElement = document.querySelector('#runNumberFilter');
return filterElement.textContent.trim();
});
strictEqual(filterElementContent, '', 'Filter element should be empty after disabling run mode');
});
await testParent.test('should make requests for run status and objects of selected run', { timeout }, () => {
strictEqual(countRunStatusCalls, expectCountRunStatusCalls, `Expected: ${expectCountRunStatusCalls} req to /api/filter/run-status, actual: ${countRunStatusCalls}`);
strictEqual(countObjectsCalls, 2, `Expected: 2 req to /api/objects, actual: ${countObjectsCalls}`);
});
};