forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.spec.ts
442 lines (394 loc) · 13.3 KB
/
model.spec.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { StateDB, PageConfig } from '@jupyterlab/coreutils';
import { UUID } from '@phosphor/coreutils';
import { DocumentManager, IDocumentManager } from '@jupyterlab/docmanager';
import { DocumentRegistry, TextModelFactory } from '@jupyterlab/docregistry';
import { ServiceManager } from '@jupyterlab/services';
import {
FileBrowserModel,
LARGE_FILE_SIZE,
CHUNK_SIZE
} from '@jupyterlab/filebrowser';
import {
acceptDialog,
dismissDialog,
signalToPromises
} from '@jupyterlab/testutils';
import { toArray } from '@phosphor/algorithm';
describe('filebrowser/model', () => {
let manager: IDocumentManager;
let serviceManager: ServiceManager.IManager;
let registry: DocumentRegistry;
let model: FileBrowserModel;
let name: string;
let state: StateDB;
before(() => {
const opener: DocumentManager.IWidgetOpener = {
open: widget => {
/* no op */
}
};
registry = new DocumentRegistry({
textModelFactory: new TextModelFactory()
});
serviceManager = new ServiceManager();
manager = new DocumentManager({
registry,
opener,
manager: serviceManager
});
state = new StateDB({ namespace: 'filebrowser/model' });
});
beforeEach(async () => {
state.clear();
model = new FileBrowserModel({ manager, state });
const contents = await manager.newUntitled({ type: 'file' });
name = contents.name;
return model.cd();
});
afterEach(() => {
model.dispose();
});
describe('FileBrowserModel', () => {
describe('#constructor()', () => {
it('should construct a new file browser model', () => {
model = new FileBrowserModel({ manager });
expect(model).to.be.an.instanceof(FileBrowserModel);
});
});
describe('#pathChanged', () => {
it('should be emitted when the path changes', async () => {
let called = false;
model.pathChanged.connect((sender, args) => {
expect(sender).to.equal(model);
expect(args.name).to.equal('path');
expect(args.oldValue).to.equal('');
expect(args.newValue).to.equal('src');
called = true;
});
await model.cd('src');
expect(called).to.equal(true);
});
});
describe('#refreshed', () => {
it('should be emitted after a refresh', async () => {
let called = false;
model.refreshed.connect((sender, arg) => {
expect(sender).to.equal(model);
expect(arg).to.be.undefined;
called = true;
});
await model.cd();
expect(called).to.equal(true);
});
it('should be emitted when the path changes', async () => {
let called = false;
model.refreshed.connect((sender, arg) => {
expect(sender).to.equal(model);
expect(arg).to.be.undefined;
called = true;
});
await model.cd('src');
expect(called).to.equal(true);
});
});
describe('#fileChanged', () => {
it('should be emitted when a file is created', async () => {
let called = false;
model.fileChanged.connect((sender, args) => {
expect(sender).to.equal(model);
expect(args.type).to.equal('new');
expect(args.oldValue).to.be.null;
expect(args.newValue.type).to.equal('file');
called = true;
});
await manager.newUntitled({ type: 'file' });
expect(called).to.equal(true);
});
it('should be emitted when a file is renamed', async () => {
let called = false;
model.fileChanged.connect((sender, args) => {
expect(sender).to.equal(model);
expect(args.type).to.equal('rename');
expect(args.oldValue.path).to.equal(name);
expect(args.newValue.path).to.equal(name + '.bak');
called = true;
});
await manager.rename(name, name + '.bak');
expect(called).to.equal(true);
});
it('should be emitted when a file is deleted', async () => {
let called = false;
model.fileChanged.connect((sender, args) => {
expect(sender).to.equal(model);
expect(args.type).to.equal('delete');
expect(args.oldValue.path).to.equal(name);
expect(args.newValue).to.be.null;
called = true;
});
await manager.deleteFile(name);
expect(called).to.equal(true);
});
});
describe('#path', () => {
it('should be the current path of the model', async () => {
expect(model.path).to.equal('');
await model.cd('src/');
expect(model.path).to.equal('src');
});
});
describe('#items()', () => {
it('should get an iterator of items in the current path', () => {
const items = model.items();
expect(items.next()).to.be.ok;
});
});
describe('#isDisposed', () => {
it('should test whether the model is disposed', () => {
expect(model.isDisposed).to.equal(false);
model.dispose();
expect(model.isDisposed).to.equal(true);
});
});
describe('#sessions()', () => {
it('should be the session models for the active notebooks', async () => {
const contents = await manager.newUntitled({ type: 'notebook' });
const session = await serviceManager.sessions.startNew({
path: contents.path
});
await model.cd();
expect(model.sessions().next()).to.be.ok;
await session.shutdown();
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the model', () => {
model.dispose();
expect(model.isDisposed).to.equal(true);
});
it('should be safe to call more than once', () => {
model.dispose();
model.dispose();
expect(model.isDisposed).to.equal(true);
});
});
describe('#refresh()', () => {
it('should refresh the contents', () => {
return model.refresh();
});
});
describe('#cd()', () => {
it('should change directory', async () => {
await model.cd('src');
expect(model.path).to.equal('src');
});
it('should accept a relative path', async () => {
await model.cd('./src');
expect(model.path).to.equal('src');
});
it('should accept a parent directory', async () => {
await model.cd('src');
await model.cd('..');
expect(model.path).to.equal('');
});
});
describe('#restore()', () => {
it('should restore based on ID', async () => {
const id = 'foo';
const model2 = new FileBrowserModel({ manager, state });
await model.restore(id);
await model.cd('src');
expect(model.path).to.equal('src');
expect(model2.path).to.equal('');
await model2.restore(id);
expect(model2.path).to.equal('src');
model2.dispose();
});
it('should be safe to call multiple times', async () => {
const id = 'bar';
const model2 = new FileBrowserModel({ manager, state });
await model.restore(id);
await model.cd('src');
expect(model.path).to.equal('src');
expect(model2.path).to.equal('');
await model2.restore(id);
await model2.restore(id);
expect(model2.path).to.equal('src');
model2.dispose();
});
});
describe('#download()', () => {
it('should download the file without error', () => {
// TODO: how to test this?
});
});
describe('#upload()', () => {
it('should upload a file object', async () => {
const fname = UUID.uuid4() + '.html';
const file = new File(['<p>Hello world!</p>'], fname, {
type: 'text/html'
});
const contents = await model.upload(file);
expect(contents.name).to.equal(fname);
});
it('should overwrite', async () => {
const fname = UUID.uuid4() + '.html';
const file = new File(['<p>Hello world!</p>'], fname, {
type: 'text/html'
});
const contents = await model.upload(file);
expect(contents.name).to.equal(fname);
const promise = model.upload(file);
await acceptDialog();
await promise;
expect(contents.name).to.equal(fname);
});
it('should not overwrite', async () => {
const fname = UUID.uuid4() + '.html';
const file = new File(['<p>Hello world!</p>'], fname, {
type: 'text/html'
});
const contents = await model.upload(file);
expect(contents.name).to.equal(fname);
const promise = model.upload(file);
await dismissDialog();
try {
await promise;
} catch (e) {
expect(e).to.equal('File not uploaded');
}
});
it('should emit the fileChanged signal', async () => {
const fname = UUID.uuid4() + '.html';
let called = false;
model.fileChanged.connect((sender, args) => {
expect(sender).to.equal(model);
expect(args.type).to.equal('save');
expect(args.oldValue).to.be.null;
expect(args.newValue.path).to.equal(fname);
called = true;
});
const file = new File(['<p>Hello world!</p>'], fname, {
type: 'text/html'
});
await model.upload(file);
expect(called).to.equal(true);
});
describe('older notebook version', () => {
let prevNotebookVersion: string;
before(() => {
prevNotebookVersion = PageConfig.setOption(
'notebookVersion',
JSON.stringify([5, 0, 0])
);
});
it('should not upload large file', async () => {
const fname = UUID.uuid4() + '.html';
const file = new File([new ArrayBuffer(LARGE_FILE_SIZE + 1)], fname);
try {
await model.upload(file);
throw new Error('Upload should have failed');
} catch (err) {
expect(err).to.equal(`Cannot upload file (>15 MB). ${fname}`);
}
});
after(() => {
PageConfig.setOption('notebookVersion', prevNotebookVersion);
});
});
describe('newer notebook version', () => {
let prevNotebookVersion: string;
before(() => {
prevNotebookVersion = PageConfig.setOption(
'notebookVersion',
JSON.stringify([5, 1, 0])
);
});
it('should not upload large notebook file', async () => {
const fname = UUID.uuid4() + '.ipynb';
const file = new File([new ArrayBuffer(LARGE_FILE_SIZE + 1)], fname);
try {
await model.upload(file);
throw Error('Upload should have failed');
} catch (err) {
expect(err).to.equal(`Cannot upload file (>15 MB). ${fname}`);
}
});
for (const size of [
CHUNK_SIZE - 1,
CHUNK_SIZE,
CHUNK_SIZE + 1,
2 * CHUNK_SIZE
]) {
it(`should upload a large file of size ${size}`, async () => {
const fname = UUID.uuid4() + '.txt';
const content = 'a'.repeat(size);
const file = new File([content], fname);
await model.upload(file);
const contentsModel = await model.manager.services.contents.get(
fname
);
expect(contentsModel.content).to.equal(content);
});
}
it(`should produce progress as a large file uploads`, async () => {
const fname = UUID.uuid4() + '.txt';
const file = new File([new ArrayBuffer(2 * CHUNK_SIZE)], fname);
const [start, first, second, finished] = signalToPromises(
model.uploadChanged,
4
);
model.upload(file);
expect(toArray(model.uploads())).to.deep.equal([]);
expect(await start).to.deep.equal([
model,
{
name: 'start',
oldValue: null,
newValue: { path: fname, progress: 0 }
}
]);
expect(toArray(model.uploads())).to.deep.equal([
{ path: fname, progress: 0 }
]);
expect(await first).to.deep.equal([
model,
{
name: 'update',
oldValue: { path: fname, progress: 0 },
newValue: { path: fname, progress: 0 }
}
]);
expect(toArray(model.uploads())).to.deep.equal([
{ path: fname, progress: 0 }
]);
expect(await second).to.deep.equal([
model,
{
name: 'update',
oldValue: { path: fname, progress: 0 },
newValue: { path: fname, progress: 1 / 2 }
}
]);
expect(toArray(model.uploads())).to.deep.equal([
{ path: fname, progress: 1 / 2 }
]);
expect(await finished).to.deep.equal([
model,
{
name: 'finish',
oldValue: { path: fname, progress: 1 / 2 },
newValue: null
}
]);
expect(toArray(model.uploads())).to.deep.equal([]);
});
after(() => {
PageConfig.setOption('notebookVersion', prevNotebookVersion);
});
});
});
});
});