forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.ts
387 lines (374 loc) · 12.1 KB
/
fixtures.ts
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import type { Session, TerminalAPI, User } from '@jupyterlab/services';
import {
test as base,
Page,
PlaywrightTestArgs,
PlaywrightTestOptions,
PlaywrightWorkerArgs,
PlaywrightWorkerOptions,
TestType
} from '@playwright/test';
import * as path from 'path';
import { ContentsHelper } from './contents';
import { galata } from './galata';
import { IJupyterLabPage, IJupyterLabPageFixture } from './jupyterlabpage';
/**
* Galata test arguments
*/
export interface IGalataTestArgs extends PlaywrightTestArgs {
/**
* JupyterLab test page.
*
* It brings the following feature on top of Playwright Page object:
* - Goto to JupyterLab URL and wait for the application to be ready
* - Helpers for JupyterLab
* - Settings mock-up
* - State mock-up
* - Track sessions and terminals opened during a test to close them at the end
*
* Note: If autoGoto is true, the filebrowser will be set inside tmpPath.
* Nothing is preventing you to navigate to some other folders.
* So you must avoid creating files outside that directory to avoid
* coupling effects between tests.
*
*/
page: IJupyterLabPageFixture;
}
/**
* Galata test configuration
*/
export type GalataOptions = {
/**
* Application URL path fragment.
*
* Default: /lab
*/
appPath: string;
/**
* Whether to go to JupyterLab page within the fixture or not.
*
* Default: true
*/
autoGoto: boolean;
/**
* Mock JupyterLab config in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab config will be mocked on a per test basis
* - false: JupyterLab config won't be mocked (Be careful it will write config in local files)
* - Record<string, JSONObject>: Initial JupyterLab data config - Mapping (config section, value).
*
* By default the config is stored in-memory.
*/
mockConfig: boolean | Record<string, unknown>;
/**
* Mock JupyterLab state in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab state will be mocked on a per test basis
* - false: JupyterLab state won't be mocked (Be careful it will write state in local files)
* - Record<string, unknown>: Initial JupyterLab data state - Mapping (state key, value).
*
* By default the state is stored in-memory.
*/
mockState: boolean | Record<string, unknown>;
/**
* Mock JupyterLab settings in-memory or not.
*
* Possible values are:
* - true: JupyterLab settings will be mocked on a per test basis
* - false: JupyterLab settings won't be mocked (Be careful it will read & write settings local files)
* - Record<string, unknown>: Mapping {pluginId: settings} that will be default user settings
*
* The default value is `galata.DEFAULT_SETTINGS`
*
* By default the settings are stored in-memory. However the
* they are still initialized with the hard drive values.
*/
mockSettings: boolean | Record<string, unknown>;
/**
* Mock JupyterLab user in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab user will be mocked on a per test basis
* - false: JupyterLab user won't be mocked (It will be a random user so snapshots won't match)
* - Record<string, unknown>: Initial JupyterLab user - Mapping (user attribute, value).
*
* By default the user is stored in-memory.
*/
mockUser: boolean | Partial<User.IUser>;
/**
* Galata can keep the uploaded and created files in ``tmpPath`` on
* the server root for debugging purpose. By default the files are
* always deleted
*
* - 'off' - ``tmpPath`` is deleted after each tests
* - 'on' - ``tmpPath`` is never deleted
* - 'only-on-failure' - ``tmpPath`` is deleted except if a test failed or timed out.
*/
serverFiles: 'on' | 'off' | 'only-on-failure';
/**
* Sessions created during the test.
*
* Possible values are:
* - null: The sessions API won't be mocked
* - Map<string, Session.IModel>: The sessions created during a test.
*
* By default the sessions created during a test will be tracked and disposed at the end.
*/
sessions: Map<string, Session.IModel> | null;
/**
* Terminals created during the test.
*
* Possible values are:
* - null: The Terminals API won't be mocked
* - Map<string, TerminalsAPI.IModel>: The Terminals created during a test.
*
* By default the Terminals created during a test will be tracked and disposed at the end.
*/
terminals: Map<string, TerminalAPI.IModel> | null;
/**
* Unique test temporary path created on the server.
*
* Note: if you override this string, you will need to take care of creating the
* folder and cleaning it.
*/
tmpPath: string;
/**
* Wait for the application page to be ready.
*
* @param page Playwright Page model
* @param helpers JupyterLab helpers
*/
waitForApplication: (page: Page, helpers: IJupyterLabPage) => Promise<void>;
};
/**
* JupyterLab customized test.
*/
// @ts-ignore
export const test: TestType<
IGalataTestArgs & GalataOptions & PlaywrightTestOptions,
PlaywrightWorkerArgs & PlaywrightWorkerOptions
> = base.extend<GalataOptions>({
/**
* `baseURL` used for all pages in the test. Takes priority over `contextOptions`.
* @see BrowserContextOptions
*
* It can also be set with `TARGET_URL` environment variable and default to `http://localhost:8888`.
*/
baseURL: async ({ baseURL }, use) => {
await use(baseURL ?? process.env.TARGET_URL ?? 'http://localhost:8888');
},
/**
* Application URL path fragment.
*
* Default: /lab
*/
appPath: ['/lab', { option: true }],
/**
* Whether to go to JupyterLab page within the fixture or not.
*
* Default: true.
*
* Note: Setting it to false allows to register new route mock-ups for example.
*/
autoGoto: [true, { option: true }],
/**
* Mock JupyterLab config in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab config will be mocked on a per test basis
* - false: JupyterLab config won't be mocked (Be careful it will write config in local files)
* - Record<string, JSONObject>: Initial JupyterLab data config - Mapping (config section, value).
*
* By default the config is stored in-memory.
*/
mockConfig: [true, { option: true }],
/**
* Mock JupyterLab state in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab state will be mocked on a per test basis
* - false: JupyterLab state won't be mocked (Be careful it will write state in local files)
* - Record<string, unknown>: Initial JupyterLab data state - Mapping (state key, value).
*
* By default the state is stored in-memory
*/
mockState: [true, { option: true }],
/**
* Mock JupyterLab settings in-memory or not.
*
* Possible values are:
* - true: JupyterLab settings will be mocked on a per test basis
* - false: JupyterLab settings won't be mocked (Be careful it may write settings local files)
* - Record<string, unknown>: Mapping {pluginId: settings} that will be default user settings
*
* The default value is `galata.DEFAULT_SETTINGS`
*
* By default the settings are stored in-memory. However the
* they are still initialized with the hard drive values.
*/
mockSettings: [galata.DEFAULT_SETTINGS, { option: true }],
/**
* Mock JupyterLab user in-memory or not.
*
* Possible values are:
* - true (default): JupyterLab user will be mocked on a per test basis
* - false: JupyterLab user won't be mocked (It will be a random user so snapshots won't match)
* - Record<string, unknown>: Initial JupyterLab user - Mapping (user attribute, value).
*
* By default the user is stored in-memory.
*/
mockUser: [true, { option: true }],
/**
* Galata can keep the uploaded and created files in ``tmpPath`` on
* the server root for debugging purpose. By default the files are
* always deleted.
*
* - 'off' - ``tmpPath`` is deleted after each tests
* - 'on' - ``tmpPath`` is never deleted
* - 'only-on-failure' - ``tmpPath`` is deleted except if a test failed or timed out.
*/
serverFiles: ['off', { option: true }],
/**
* Sessions created during the test.
*
* Possible values are:
* - null: The sessions API won't be mocked
* - Map<string, Session.IModel>: The sessions created during a test.
*
* By default the sessions created during a test will be tracked and disposed at the end.
*/
sessions: async ({ request }, use) => {
const sessions = new Map<string, Session.IModel>();
await use(sessions);
if (sessions.size > 0) {
await galata.Mock.clearRunners(request, [...sessions.keys()], 'sessions');
}
},
/**
* Terminals created during the test.
*
* Possible values are:
* - null: The Terminals API won't be mocked
* - Map<string, TerminalAPI.IModel>: The Terminals created during a test.
*
* By default the Terminals created during a test will be tracked and disposed at the end.
*/
terminals: async ({ request }, use) => {
const terminals = new Map<string, TerminalAPI.IModel>();
await use(terminals);
if (terminals.size > 0) {
await galata.Mock.clearRunners(
request,
[...terminals.keys()],
'terminals'
);
}
},
/**
* Unique test temporary path created on the server.
*
* Note: if you override this string, you will need to take care of creating the
* folder and cleaning it.
*/
tmpPath: async ({ request, serverFiles }, use, testInfo) => {
// Remove appended retry part for reproducibility
const testFolder = path
.basename(testInfo.outputDir)
.replace(/-retry\d+$/i, '');
const contents = new ContentsHelper(request);
if (await contents.directoryExists(testFolder)) {
await contents.deleteDirectory(testFolder);
}
// Create the test folder on the server
await contents.createDirectory(testFolder);
await use(testFolder);
// Delete the test folder on the server
// If serverFiles is 'on' or 'only-on-failure', keep the server files for the test
if (
serverFiles === 'off' ||
(serverFiles === 'only-on-failure' &&
(testInfo.status === 'passed' || testInfo.status === 'skipped'))
) {
await contents.deleteDirectory(testFolder);
}
},
/**
* Wait for the application page to be ready
*
* @param page Playwright Page model
* @param helpers JupyterLab helpers
*/
waitForApplication: async ({ baseURL }, use) => {
const waitIsReady = async (
page: Page,
helpers: IJupyterLabPage
): Promise<void> => {
await page.waitForSelector('#jupyterlab-splash', {
state: 'detached'
});
await helpers.waitForCondition(() => {
return helpers.activity.isTabActive('Launcher');
});
// Oddly current tab is not always set to active
if (!(await helpers.isInSimpleMode())) {
await helpers.activity.activateTab('Launcher');
}
};
await use(waitIsReady);
},
/**
* JupyterLab test page.
*
* It brings the following feature on top of Playwright Page object:
* - Goto to JupyterLab URL and wait for the application to be ready (autoGoto == true)
* - Helpers for JupyterLab
* - Settings mock-up
* - State mock-up
* - Track sessions and terminals opened during a test to close them at the end
*
* Note: If autoGoto is true, the filebrowser will be set inside tmpPath.
* Nothing is preventing you to navigate to some other folders.
* So you must avoid creating files outside that directory to avoid
* coupling effects between tests.
*/
// @ts-ignore
page: async (
{
appPath,
autoGoto,
baseURL,
mockConfig,
mockSettings,
mockState,
mockUser,
page,
sessions,
terminals,
tmpPath,
waitForApplication
},
use
) => {
await use(
await galata.initTestPage(
appPath,
autoGoto,
baseURL!,
mockConfig,
mockSettings,
mockState,
mockUser,
page,
sessions,
terminals,
tmpPath,
waitForApplication
)
);
}
});