context.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { SessionContext } from '@jupyterlab/apputils';
  4. import {
  5. Context,
  6. DocumentRegistry,
  7. TextModelFactory
  8. } from '@jupyterlab/docregistry';
  9. import { RenderMimeRegistry } from '@jupyterlab/rendermime';
  10. import { Contents, ServiceManager } from '@jupyterlab/services';
  11. import {
  12. acceptDialog,
  13. dismissDialog,
  14. initNotebookContext,
  15. NBTestUtils,
  16. waitForDialog
  17. } from '@jupyterlab/testutils';
  18. import * as Mock from '@jupyterlab/testutils/lib/mock';
  19. import { UUID } from '@lumino/coreutils';
  20. import { Widget } from '@lumino/widgets';
  21. describe('docregistry/context', () => {
  22. let manager: ServiceManager.IManager;
  23. const factory = new TextModelFactory();
  24. beforeAll(() => {
  25. manager = new Mock.ServiceManagerMock();
  26. return manager.ready;
  27. });
  28. describe('Context', () => {
  29. let context: Context<DocumentRegistry.IModel>;
  30. beforeEach(() => {
  31. context = new Context({
  32. manager,
  33. factory,
  34. path: UUID.uuid4() + '.txt'
  35. });
  36. });
  37. afterEach(async () => {
  38. await context.sessionContext.shutdown();
  39. context.dispose();
  40. });
  41. describe('#constructor()', () => {
  42. it('should create a new context', () => {
  43. context = new Context({
  44. manager,
  45. factory,
  46. path: UUID.uuid4() + '.txt'
  47. });
  48. expect(context).toBeInstanceOf(Context);
  49. });
  50. });
  51. describe('#pathChanged', () => {
  52. it('should be emitted when the path changes', async () => {
  53. const newPath = UUID.uuid4() + '.txt';
  54. let called = false;
  55. context.pathChanged.connect((sender, args) => {
  56. expect(sender).toBe(context);
  57. expect(args).toBe(newPath);
  58. called = true;
  59. });
  60. await context.initialize(true);
  61. await manager.contents.rename(context.path, newPath);
  62. expect(called).toBe(true);
  63. });
  64. });
  65. describe('#fileChanged', () => {
  66. it('should be emitted when the file is saved', async () => {
  67. const path = context.path;
  68. let called = false;
  69. context.fileChanged.connect((sender, args) => {
  70. expect(sender).toBe(context);
  71. expect(args.path).toBe(path);
  72. called = true;
  73. });
  74. await context.initialize(true);
  75. expect(called).toBe(true);
  76. });
  77. });
  78. describe('#saving', () => {
  79. it("should emit 'starting' when the file starts saving", async () => {
  80. let called = false;
  81. let checked = false;
  82. context.saveState.connect((sender, args) => {
  83. if (!called) {
  84. expect(sender).toBe(context);
  85. expect(args).toBe('started');
  86. checked = true;
  87. }
  88. called = true;
  89. });
  90. await context.initialize(true);
  91. expect(called).toBe(true);
  92. expect(checked).toBe(true);
  93. });
  94. it("should emit 'completed' when the file ends saving", async () => {
  95. let called = 0;
  96. let checked = false;
  97. context.saveState.connect((sender, args) => {
  98. if (called > 0) {
  99. expect(sender).toBe(context);
  100. expect(args).toBe('completed');
  101. checked = true;
  102. }
  103. called += 1;
  104. });
  105. await context.initialize(true);
  106. expect(called).toBe(2);
  107. expect(checked).toBe(true);
  108. });
  109. it("should emit 'failed' when the save operation fails out", async () => {
  110. context = new Context({
  111. manager,
  112. factory,
  113. path: 'readonly.txt'
  114. });
  115. let called = 0;
  116. let checked;
  117. context.saveState.connect((sender, args) => {
  118. if (called > 0) {
  119. expect(sender).toBe(context);
  120. checked = args;
  121. }
  122. called += 1;
  123. });
  124. await expect(context.initialize(true)).rejects.toThrowError(
  125. 'Invalid response: 403 Forbidden'
  126. );
  127. expect(called).toBe(2);
  128. expect(checked).toBe('failed');
  129. await acceptDialog();
  130. });
  131. });
  132. describe('#isReady', () => {
  133. it('should indicate whether the context is ready', async () => {
  134. expect(context.isReady).toBe(false);
  135. const func = async () => {
  136. await context.ready;
  137. expect(context.isReady).toBe(true);
  138. };
  139. const promise = func();
  140. await context.initialize(true);
  141. await promise;
  142. });
  143. });
  144. describe('#ready()', () => {
  145. it('should resolve when the file is saved for the first time', async () => {
  146. await context.initialize(true);
  147. await context.ready;
  148. });
  149. it('should resolve when the file is reverted for the first time', async () => {
  150. await manager.contents.save(context.path, {
  151. type: factory.contentType,
  152. format: factory.fileFormat,
  153. content: 'foo'
  154. });
  155. await context.initialize(false);
  156. await context.ready;
  157. });
  158. it('should initialize the model when the file is saved for the first time', async () => {
  159. const context = await initNotebookContext({ manager });
  160. context.model.fromJSON(NBTestUtils.DEFAULT_CONTENT);
  161. expect(context.model.cells.canUndo).toBe(true);
  162. await context.initialize(true);
  163. await context.ready;
  164. expect(context.model.cells.canUndo).toBe(false);
  165. });
  166. it('should initialize the model when the file is reverted for the first time', async () => {
  167. const context = await initNotebookContext({ manager });
  168. await manager.contents.save(context.path, {
  169. type: 'notebook',
  170. format: 'json',
  171. content: NBTestUtils.DEFAULT_CONTENT
  172. });
  173. context.model.fromJSON(NBTestUtils.DEFAULT_CONTENT);
  174. expect(context.model.cells.canUndo).toBe(true);
  175. await context.initialize(false);
  176. await context.ready;
  177. expect(context.model.cells.canUndo).toBe(false);
  178. });
  179. });
  180. describe('#disposed', () => {
  181. it('should be emitted when the context is disposed', () => {
  182. let called = false;
  183. context.disposed.connect((sender, args) => {
  184. expect(sender).toBe(context);
  185. expect(args).toBeUndefined();
  186. called = true;
  187. });
  188. context.dispose();
  189. expect(called).toBe(true);
  190. });
  191. });
  192. describe('#model', () => {
  193. it('should be the model associated with the document', () => {
  194. expect(context.model.toString()).toBe('');
  195. });
  196. });
  197. describe('#sessionContext', () => {
  198. it('should be a ISessionContext object', () => {
  199. expect(context.sessionContext).toBeInstanceOf(SessionContext);
  200. });
  201. });
  202. describe('#path', () => {
  203. it('should be the current path for the context', () => {
  204. expect(typeof context.path).toBe('string');
  205. });
  206. });
  207. describe('#contentsModel', () => {
  208. it('should be `null` before population', () => {
  209. expect(context.contentsModel).toBeNull();
  210. });
  211. it('should be set after population', async () => {
  212. const { path } = context;
  213. void context.initialize(true);
  214. await context.ready;
  215. expect(context.contentsModel!.path).toBe(path);
  216. });
  217. });
  218. describe('#factoryName', () => {
  219. it('should be the name of the factory used by the context', () => {
  220. expect(context.factoryName).toBe(factory.name);
  221. });
  222. });
  223. describe('#isDisposed', () => {
  224. it('should test whether the context is disposed', () => {
  225. expect(context.isDisposed).toBe(false);
  226. context.dispose();
  227. expect(context.isDisposed).toBe(true);
  228. });
  229. });
  230. describe('#dispose()', () => {
  231. it('should dispose of the resources used by the context', () => {
  232. context.dispose();
  233. expect(context.isDisposed).toBe(true);
  234. context.dispose();
  235. expect(context.isDisposed).toBe(true);
  236. });
  237. });
  238. describe('#rename()', () => {
  239. it('should change the name of the file to the new name', async () => {
  240. await context.initialize(true);
  241. context.model.fromString('foo');
  242. const newName = UUID.uuid4() + '.txt';
  243. await context.rename(newName);
  244. await context.save();
  245. const opts: Contents.IFetchOptions = {
  246. format: factory.fileFormat,
  247. type: factory.contentType,
  248. content: true
  249. };
  250. const model = await manager.contents.get(newName, opts);
  251. expect(model.content).toBe('foo');
  252. });
  253. });
  254. describe('#save()', () => {
  255. it('should save the contents of the file to disk', async () => {
  256. await context.initialize(true);
  257. context.model.fromString('foo');
  258. await context.save();
  259. const opts: Contents.IFetchOptions = {
  260. format: factory.fileFormat,
  261. type: factory.contentType,
  262. content: true
  263. };
  264. const model = await manager.contents.get(context.path, opts);
  265. expect(model.content).toBe('foo');
  266. });
  267. it('should should preserve LF line endings upon save', async () => {
  268. await context.initialize(true);
  269. await manager.contents.save(context.path, {
  270. type: factory.contentType,
  271. format: factory.fileFormat,
  272. content: 'foo\nbar'
  273. });
  274. await context.revert();
  275. await context.save();
  276. const opts: Contents.IFetchOptions = {
  277. format: factory.fileFormat,
  278. type: factory.contentType,
  279. content: true
  280. };
  281. const model = await manager.contents.get(context.path, opts);
  282. expect(model.content).toBe('foo\nbar');
  283. });
  284. it('should should preserve CRLF line endings upon save', async () => {
  285. await context.initialize(true);
  286. await manager.contents.save(context.path, {
  287. type: factory.contentType,
  288. format: factory.fileFormat,
  289. content: 'foo\r\nbar'
  290. });
  291. await context.revert();
  292. await context.save();
  293. const opts: Contents.IFetchOptions = {
  294. format: factory.fileFormat,
  295. type: factory.contentType,
  296. content: true
  297. };
  298. const model = await manager.contents.get(context.path, opts);
  299. expect(model.content).toBe('foo\r\nbar');
  300. });
  301. });
  302. describe('#saveAs()', () => {
  303. it('should save the document to a different path chosen by the user', async () => {
  304. const initialize = context.initialize(true);
  305. const newPath = UUID.uuid4() + '.txt';
  306. const func = async () => {
  307. await initialize;
  308. await waitForDialog();
  309. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  310. const input = dialog.getElementsByTagName('input')[0];
  311. input.value = newPath;
  312. await acceptDialog();
  313. };
  314. const promise = func();
  315. await initialize;
  316. const oldPath = context.path;
  317. await context.saveAs();
  318. await promise;
  319. expect(context.path).toBe(newPath);
  320. // Make sure the both files are there now.
  321. const model = await manager.contents.get('', { content: true });
  322. expect(model.content.find((x: any) => x.name === oldPath)).toBeTruthy();
  323. expect(model.content.find((x: any) => x.name === newPath)).toBeTruthy();
  324. });
  325. it('should bring up a conflict dialog', async () => {
  326. const newPath = UUID.uuid4() + '.txt';
  327. const func = async () => {
  328. await waitForDialog();
  329. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  330. const input = dialog.getElementsByTagName('input')[0];
  331. input.value = newPath;
  332. await acceptDialog(); // Accept rename dialog
  333. await acceptDialog(); // Accept conflict dialog
  334. };
  335. await manager.contents.save(newPath, {
  336. type: factory.contentType,
  337. format: factory.fileFormat,
  338. content: 'foo'
  339. });
  340. await context.initialize(true);
  341. const promise = func();
  342. await context.saveAs();
  343. await promise;
  344. expect(context.path).toBe(newPath);
  345. });
  346. it('should keep the file if overwrite is aborted', async () => {
  347. const oldPath = context.path;
  348. const newPath = UUID.uuid4() + '.txt';
  349. const func = async () => {
  350. await waitForDialog();
  351. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  352. const input = dialog.getElementsByTagName('input')[0];
  353. input.value = newPath;
  354. await acceptDialog(); // Accept rename dialog
  355. await dismissDialog(); // Reject conflict dialog
  356. };
  357. await manager.contents.save(newPath, {
  358. type: factory.contentType,
  359. format: factory.fileFormat,
  360. content: 'foo'
  361. });
  362. await context.initialize(true);
  363. const promise = func();
  364. await context.saveAs();
  365. await promise;
  366. expect(context.path).toBe(oldPath);
  367. });
  368. it('should just save if the file name does not change', async () => {
  369. const path = context.path;
  370. await context.initialize(true);
  371. const promise = context.saveAs();
  372. await acceptDialog();
  373. await promise;
  374. expect(context.path).toBe(path);
  375. });
  376. });
  377. describe('#revert()', () => {
  378. it('should revert the contents of the file to the disk', async () => {
  379. await context.initialize(true);
  380. context.model.fromString('foo');
  381. await context.save();
  382. context.model.fromString('bar');
  383. await context.revert();
  384. expect(context.model.toString()).toBe('foo');
  385. });
  386. it('should normalize CRLF line endings to LF', async () => {
  387. await context.initialize(true);
  388. await manager.contents.save(context.path, {
  389. type: factory.contentType,
  390. format: factory.fileFormat,
  391. content: 'foo\r\nbar'
  392. });
  393. await context.revert();
  394. expect(context.model.toString()).toBe('foo\nbar');
  395. });
  396. });
  397. describe('#createCheckpoint()', () => {
  398. it('should create a checkpoint for the file', async () => {
  399. await context.initialize(true);
  400. const model = await context.createCheckpoint();
  401. expect(model.id).toBeTruthy();
  402. expect(model.last_modified).toBeTruthy();
  403. });
  404. });
  405. describe('#deleteCheckpoint()', () => {
  406. it('should delete the given checkpoint', async () => {
  407. await context.initialize(true);
  408. const model = await context.createCheckpoint();
  409. await context.deleteCheckpoint(model.id);
  410. const models = await context.listCheckpoints();
  411. expect(models.length).toBe(0);
  412. });
  413. });
  414. describe('#restoreCheckpoint()', () => {
  415. it('should restore the value to the last checkpoint value', async () => {
  416. context.model.fromString('bar');
  417. await context.initialize(true);
  418. const model = await context.createCheckpoint();
  419. context.model.fromString('foo');
  420. const id = model.id;
  421. await context.save();
  422. await context.restoreCheckpoint(id);
  423. await context.revert();
  424. expect(context.model.toString()).toBe('bar');
  425. });
  426. });
  427. describe('#listCheckpoints()', () => {
  428. it('should list the checkpoints for the file', async () => {
  429. await context.initialize(true);
  430. const model = await context.createCheckpoint();
  431. const id = model.id;
  432. const models = await context.listCheckpoints();
  433. let found = false;
  434. for (const model of models) {
  435. if (model.id === id) {
  436. found = true;
  437. }
  438. }
  439. expect(found).toBe(true);
  440. });
  441. });
  442. describe('#urlResolver', () => {
  443. it('should be a url resolver', () => {
  444. expect(context.urlResolver).toBeInstanceOf(
  445. RenderMimeRegistry.UrlResolver
  446. );
  447. });
  448. });
  449. describe('#addSibling()', () => {
  450. it('should add a sibling widget', () => {
  451. let called = false;
  452. const opener = (widget: Widget) => {
  453. called = true;
  454. };
  455. context = new Context({
  456. manager,
  457. factory,
  458. path: UUID.uuid4() + '.txt',
  459. opener
  460. });
  461. context.addSibling(new Widget());
  462. expect(called).toBe(true);
  463. });
  464. });
  465. });
  466. });