context.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { UUID } from '@lumino/coreutils';
  4. import { Contents, ServiceManager } from '@jupyterlab/services';
  5. import { Widget } from '@lumino/widgets';
  6. import {
  7. Context,
  8. DocumentRegistry,
  9. TextModelFactory
  10. } from '@jupyterlab/docregistry';
  11. import { RenderMimeRegistry } from '@jupyterlab/rendermime';
  12. import {
  13. waitForDialog,
  14. acceptDialog,
  15. dismissDialog,
  16. initNotebookContext,
  17. NBTestUtils
  18. } from '@jupyterlab/testutils';
  19. import { SessionContext } from '@jupyterlab/apputils';
  20. import * as Mock from '@jupyterlab/testutils/lib/mock';
  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('#save()', () => {
  239. it('should save the contents of the file to disk', async () => {
  240. await context.initialize(true);
  241. context.model.fromString('foo');
  242. await context.save();
  243. const opts: Contents.IFetchOptions = {
  244. format: factory.fileFormat,
  245. type: factory.contentType,
  246. content: true
  247. };
  248. const model = await manager.contents.get(context.path, opts);
  249. expect(model.content).toBe('foo');
  250. });
  251. it('should should preserve LF line endings upon save', async () => {
  252. await context.initialize(true);
  253. await manager.contents.save(context.path, {
  254. type: factory.contentType,
  255. format: factory.fileFormat,
  256. content: 'foo\nbar'
  257. });
  258. await context.revert();
  259. await context.save();
  260. const opts: Contents.IFetchOptions = {
  261. format: factory.fileFormat,
  262. type: factory.contentType,
  263. content: true
  264. };
  265. const model = await manager.contents.get(context.path, opts);
  266. expect(model.content).toBe('foo\nbar');
  267. });
  268. it('should should preserve CRLF line endings upon save', async () => {
  269. await context.initialize(true);
  270. await manager.contents.save(context.path, {
  271. type: factory.contentType,
  272. format: factory.fileFormat,
  273. content: 'foo\r\nbar'
  274. });
  275. await context.revert();
  276. await context.save();
  277. const opts: Contents.IFetchOptions = {
  278. format: factory.fileFormat,
  279. type: factory.contentType,
  280. content: true
  281. };
  282. const model = await manager.contents.get(context.path, opts);
  283. expect(model.content).toBe('foo\r\nbar');
  284. });
  285. });
  286. describe('#saveAs()', () => {
  287. it('should save the document to a different path chosen by the user', async () => {
  288. const initialize = context.initialize(true);
  289. const newPath = UUID.uuid4() + '.txt';
  290. const func = async () => {
  291. await initialize;
  292. await waitForDialog();
  293. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  294. const input = dialog.getElementsByTagName('input')[0];
  295. input.value = newPath;
  296. await acceptDialog();
  297. };
  298. const promise = func();
  299. await initialize;
  300. const oldPath = context.path;
  301. await context.saveAs();
  302. await promise;
  303. expect(context.path).toBe(newPath);
  304. // Make sure the both files are there now.
  305. const model = await manager.contents.get('', { content: true });
  306. expect(model.content.find((x: any) => x.name === oldPath)).toBeTruthy();
  307. expect(model.content.find((x: any) => x.name === newPath)).toBeTruthy();
  308. });
  309. it('should bring up a conflict dialog', async () => {
  310. const newPath = UUID.uuid4() + '.txt';
  311. const func = async () => {
  312. await waitForDialog();
  313. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  314. const input = dialog.getElementsByTagName('input')[0];
  315. input.value = newPath;
  316. await acceptDialog(); // Accept rename dialog
  317. await acceptDialog(); // Accept conflict dialog
  318. };
  319. await manager.contents.save(newPath, {
  320. type: factory.contentType,
  321. format: factory.fileFormat,
  322. content: 'foo'
  323. });
  324. await context.initialize(true);
  325. const promise = func();
  326. await context.saveAs();
  327. await promise;
  328. expect(context.path).toBe(newPath);
  329. });
  330. it('should keep the file if overwrite is aborted', async () => {
  331. const oldPath = context.path;
  332. const newPath = UUID.uuid4() + '.txt';
  333. const func = async () => {
  334. await waitForDialog();
  335. const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  336. const input = dialog.getElementsByTagName('input')[0];
  337. input.value = newPath;
  338. await acceptDialog(); // Accept rename dialog
  339. await dismissDialog(); // Reject conflict dialog
  340. };
  341. await manager.contents.save(newPath, {
  342. type: factory.contentType,
  343. format: factory.fileFormat,
  344. content: 'foo'
  345. });
  346. await context.initialize(true);
  347. const promise = func();
  348. await context.saveAs();
  349. await promise;
  350. expect(context.path).toBe(oldPath);
  351. });
  352. it('should just save if the file name does not change', async () => {
  353. const path = context.path;
  354. await context.initialize(true);
  355. const promise = context.saveAs();
  356. await acceptDialog();
  357. await promise;
  358. expect(context.path).toBe(path);
  359. });
  360. });
  361. describe('#revert()', () => {
  362. it('should revert the contents of the file to the disk', async () => {
  363. await context.initialize(true);
  364. context.model.fromString('foo');
  365. await context.save();
  366. context.model.fromString('bar');
  367. await context.revert();
  368. expect(context.model.toString()).toBe('foo');
  369. });
  370. it('should normalize CRLF line endings to LF', async () => {
  371. await context.initialize(true);
  372. await manager.contents.save(context.path, {
  373. type: factory.contentType,
  374. format: factory.fileFormat,
  375. content: 'foo\r\nbar'
  376. });
  377. await context.revert();
  378. expect(context.model.toString()).toBe('foo\nbar');
  379. });
  380. });
  381. describe('#createCheckpoint()', () => {
  382. it('should create a checkpoint for the file', async () => {
  383. await context.initialize(true);
  384. const model = await context.createCheckpoint();
  385. expect(model.id).toBeTruthy();
  386. expect(model.last_modified).toBeTruthy();
  387. });
  388. });
  389. describe('#deleteCheckpoint()', () => {
  390. it('should delete the given checkpoint', async () => {
  391. await context.initialize(true);
  392. const model = await context.createCheckpoint();
  393. await context.deleteCheckpoint(model.id);
  394. const models = await context.listCheckpoints();
  395. expect(models.length).toBe(0);
  396. });
  397. });
  398. describe('#restoreCheckpoint()', () => {
  399. it('should restore the value to the last checkpoint value', async () => {
  400. context.model.fromString('bar');
  401. await context.initialize(true);
  402. const model = await context.createCheckpoint();
  403. context.model.fromString('foo');
  404. const id = model.id;
  405. await context.save();
  406. await context.restoreCheckpoint(id);
  407. await context.revert();
  408. expect(context.model.toString()).toBe('bar');
  409. });
  410. });
  411. describe('#listCheckpoints()', () => {
  412. it('should list the checkpoints for the file', async () => {
  413. await context.initialize(true);
  414. const model = await context.createCheckpoint();
  415. const id = model.id;
  416. const models = await context.listCheckpoints();
  417. let found = false;
  418. for (const model of models) {
  419. if (model.id === id) {
  420. found = true;
  421. }
  422. }
  423. expect(found).toBe(true);
  424. });
  425. });
  426. describe('#urlResolver', () => {
  427. it('should be a url resolver', () => {
  428. expect(context.urlResolver).toBeInstanceOf(
  429. RenderMimeRegistry.UrlResolver
  430. );
  431. });
  432. });
  433. describe('#addSibling()', () => {
  434. it('should add a sibling widget', () => {
  435. let called = false;
  436. const opener = (widget: Widget) => {
  437. called = true;
  438. };
  439. context = new Context({
  440. manager,
  441. factory,
  442. path: UUID.uuid4() + '.txt',
  443. opener
  444. });
  445. context.addSibling(new Widget());
  446. expect(called).toBe(true);
  447. });
  448. });
  449. });
  450. });