1
1
// Copyright (c) Jupyter Development Team.
2
2
// Distributed under the terms of the Modified BSD License.
3
3
4
- import { Session , TerminalAPI } from '@jupyterlab/services' ;
4
+ import { Session , TerminalAPI , Workspace } from '@jupyterlab/services' ;
5
5
import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
6
6
import {
7
7
test as base ,
@@ -49,11 +49,16 @@ export type GalataOptions = {
49
49
*/
50
50
serverFiles : 'on' | 'off' | 'only-on-failure' ;
51
51
/**
52
- * Whether to mock JupyterLab state in-memory or not.
52
+ * Mock JupyterLab state in-memory or not.
53
+ *
54
+ * Possible values are:
55
+ * - true (default): JupyterLab state will be mocked on a per test basis
56
+ * - false: JupyterLab state won't be mocked (Be careful it will write state in local files)
57
+ * - Record<string, unknown>: Initial JupyterLab data state - Mapping (state key, value).
53
58
*
54
59
* By default the state is stored in-memory
55
60
*/
56
- mockState : boolean ; // TODO allow loading custom state
61
+ mockState : boolean | Record < string , unknown > ;
57
62
/**
58
63
* Mock JupyterLab settings in-memory or not.
59
64
*
@@ -65,7 +70,7 @@ export type GalataOptions = {
65
70
* By default the settings are stored in-memory. However the
66
71
* they are still initialized with the hard drive values.
67
72
*/
68
- mockSettings : boolean | Record < string , unknown > ; // TODO test mock with record
73
+ mockSettings : boolean | Record < string , unknown > ;
69
74
/**
70
75
* Unique test temporary path created on the server.
71
76
*
@@ -95,7 +100,12 @@ export const test: TestType<
95
100
await use ( baseURL ?? process . env . TARGET_URL ?? 'http://localhost:8888' ) ;
96
101
} ,
97
102
/**
98
- * Whether to mock JupyterLab state in-memory or not.
103
+ * Mock JupyterLab state in-memory or not.
104
+ *
105
+ * Possible values are:
106
+ * - true (default): JupyterLab state will be mocked on a per test basis
107
+ * - false: JupyterLab state won't be mocked (Be careful it will write state in local files)
108
+ * - Record<string, unknown>: Initial JupyterLab data state - Mapping (state key, value).
99
109
*
100
110
* By default the state is stored in-memory
101
111
*/
@@ -105,8 +115,8 @@ export const test: TestType<
105
115
*
106
116
* Possible values are:
107
117
* - true (default): JupyterLab settings will be mocked on a per test basis
108
- * - false: JupyterLab settings won't be mocked (Be careful it will read & write settings local files)
109
- * - Record<string, any >: Mapping {pluginId: settings} that will override default settings
118
+ * - false: JupyterLab settings won't be mocked (Be careful it may write settings local files)
119
+ * - Record<string, unknown >: Mapping {pluginId: settings} that will override default settings
110
120
*
111
121
* By default the settings are stored in-memory. However the
112
122
* they are still initialized with the hard drive values.
@@ -204,8 +214,14 @@ export const test: TestType<
204
214
) ;
205
215
}
206
216
207
- const workspace = { data : { } , metadata : { } } ;
217
+ const workspace : Workspace . IWorkspace = {
218
+ data : { } ,
219
+ metadata : { id : 'default' }
220
+ } ;
208
221
if ( mockState ) {
222
+ if ( typeof mockState !== 'boolean' ) {
223
+ workspace . data = { ...mockState } as any ;
224
+ }
209
225
// State will be stored in-memory (after loading the initial version from disk)
210
226
await Private . mockState ( page , workspace ) ;
211
227
}
@@ -230,7 +246,17 @@ export const test: TestType<
230
246
}
231
247
} ) ;
232
248
249
+ /**
250
+ * Private methods
251
+ */
233
252
namespace Private {
253
+ /**
254
+ * Clear all wanted sessions.
255
+ *
256
+ * @param baseURL Application base URL
257
+ * @param sessions Set of sessions
258
+ * @returns Whether the sessions were closed or not
259
+ */
234
260
export async function clearSessions (
235
261
baseURL : string ,
236
262
sessions : Set < string >
@@ -243,6 +269,13 @@ namespace Private {
243
269
return responses . every ( response => response . ok ) ;
244
270
}
245
271
272
+ /**
273
+ * Clear all wanted terminals.
274
+ *
275
+ * @param baseURL Application base URL
276
+ * @param terminals Set of terminals
277
+ * @returns Whether the terminals were closed or not
278
+ */
246
279
export async function clearTerminals (
247
280
baseURL : string ,
248
281
terminals : Set < string >
@@ -255,12 +288,15 @@ namespace Private {
255
288
return responses . every ( response => response . ok ) ;
256
289
}
257
290
291
+ /**
292
+ * Mock workspace route.
293
+ *
294
+ * @param page Page model object
295
+ * @param workspace In-memory workspace
296
+ */
258
297
export function mockState (
259
298
page : Page ,
260
- workspace : {
261
- data : Record < string , unknown > ;
262
- metadata : Record < string , unknown > ;
263
- }
299
+ workspace : Workspace . IWorkspace
264
300
) : Promise < void > {
265
301
return page . route ( / .* \/ a p i \/ w o r k s p a c e s [ \/ \w + ] + / , ( route , request ) => {
266
302
switch ( request . method ( ) ) {
@@ -281,8 +317,18 @@ namespace Private {
281
317
} ) ;
282
318
}
283
319
320
+ /**
321
+ * Settings REST API endpoint
322
+ */
284
323
const settingsRegex = / .* \/ a p i \/ s e t t i n g s (?< id > ( \/ [ @ : - \w ] + ) * ) / ;
285
324
325
+ /**
326
+ * Mock settings route.
327
+ *
328
+ * @param page Page model object
329
+ * @param settings In-memory settings
330
+ * @param mockedSettings Test mocked settings
331
+ */
286
332
export function mockSettings (
287
333
page : Page ,
288
334
settings : ISettingRegistry . IPlugin [ ] ,
@@ -388,6 +434,12 @@ namespace Private {
388
434
} ) ;
389
435
}
390
436
437
+ /**
438
+ * Track the sessions created during a test
439
+ *
440
+ * @param page Page model object
441
+ * @param sessions Set of session ids
442
+ */
391
443
export function trackSessions (
392
444
page : Page ,
393
445
sessions : Set < string >
@@ -420,6 +472,12 @@ namespace Private {
420
472
} ) ;
421
473
}
422
474
475
+ /**
476
+ * Track the terminals created during a test
477
+ *
478
+ * @param page Page model object
479
+ * @param terminals Set of terminal names
480
+ */
423
481
export function trackTerminals (
424
482
page : Page ,
425
483
terminals : Set < string >
0 commit comments