mock.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import * as Mock from '../src/mock';
  4. import { KernelMessage } from '@jupyterlab/services';
  5. import { toArray } from '@lumino/algorithm';
  6. describe('mock', () => {
  7. describe('createSimpleSessionContext()', () => {
  8. it('should create a session context', () => {
  9. const context = Mock.createSimpleSessionContext();
  10. expect(context.session!.kernel!.name).toEqual(Mock.DEFAULT_NAME);
  11. });
  12. it('should accept a session model', () => {
  13. const context = Mock.createSimpleSessionContext({
  14. name: 'hi',
  15. path: 'foo',
  16. type: 'bar',
  17. kernel: { name: 'fizz' }
  18. });
  19. expect(context.name).toEqual('hi');
  20. expect(context.path).toEqual('foo');
  21. expect(context.type).toEqual('bar');
  22. expect(context.session!.kernel!.name).toEqual('fizz');
  23. });
  24. });
  25. describe('updateKernelStatus()', () => {
  26. it('should update the kernel status', () => {
  27. const context = Mock.createSimpleSessionContext();
  28. let called = false;
  29. context.statusChanged.connect((_, status) => {
  30. if (status === 'dead') {
  31. called = true;
  32. }
  33. });
  34. Mock.updateKernelStatus(context, 'dead');
  35. expect(context.session!.kernel!.status).toEqual('dead');
  36. expect(called).toEqual(true);
  37. });
  38. });
  39. describe('emitIopubMessage', () => {
  40. it('should emit an iopub message', () => {
  41. const context = Mock.createSimpleSessionContext();
  42. const source = KernelMessage.createMessage({
  43. channel: 'iopub',
  44. msgType: 'execute_input',
  45. session: 'foo',
  46. username: 'bar',
  47. msgId: 'fizz',
  48. content: {
  49. code: 'hello, world!',
  50. execution_count: 0
  51. }
  52. });
  53. let called = false;
  54. context.iopubMessage.connect((_, msg) => {
  55. expect(msg).toBe(source);
  56. called = true;
  57. });
  58. Mock.emitIopubMessage(context, source);
  59. expect(called).toBe(true);
  60. });
  61. });
  62. describe('cloneKernel()', () => {
  63. it('should clone a kernel', () => {
  64. const kernel0 = new Mock.KernelMock({});
  65. const kernel1 = Mock.cloneKernel(kernel0);
  66. expect(kernel0.id).toBe(kernel1.id);
  67. expect(kernel0.clientId).not.toBe(kernel1.clientId);
  68. });
  69. });
  70. describe('KernelMock', () => {
  71. describe('.constructor()', () => {
  72. it('should create a mock kernel', () => {
  73. const kernel = new Mock.KernelMock({});
  74. expect(kernel.name).toBe(Mock.DEFAULT_NAME);
  75. });
  76. it('should take options', () => {
  77. const kernel = new Mock.KernelMock({ model: { name: 'foo' } });
  78. expect(kernel.name).toBe('foo');
  79. });
  80. });
  81. describe('.spec()', () => {
  82. it('should resolve with a kernel spec', async () => {
  83. const kernel = new Mock.KernelMock({});
  84. const spec = await kernel.spec;
  85. expect(spec!.name).toBe(Mock.DEFAULT_NAME);
  86. });
  87. });
  88. describe('.dispose()', () => {
  89. it('should be a no-op', () => {
  90. const kernel = new Mock.KernelMock({});
  91. kernel.dispose();
  92. });
  93. });
  94. describe('.clone()', () => {
  95. it('should clone the kernel', () => {
  96. const kernel0 = new Mock.KernelMock({});
  97. const kernel1 = kernel0.clone();
  98. expect(kernel0.id).toBe(kernel1.id);
  99. expect(kernel0.clientId).not.toBe(kernel1.clientId);
  100. });
  101. });
  102. describe('.info', () => {
  103. it('should resolve with info', async () => {
  104. const kernel = new Mock.KernelMock({});
  105. const info = await kernel.info;
  106. expect(info.language_info.name).toBe(Mock.DEFAULT_NAME);
  107. });
  108. });
  109. describe('.shutdown()', () => {
  110. it('should be a no-op', async () => {
  111. const kernel = new Mock.KernelMock({});
  112. await kernel.shutdown();
  113. });
  114. });
  115. describe('.requestHistory()', () => {
  116. it('should get the history info', async () => {
  117. const kernel = new Mock.KernelMock({});
  118. const reply = await kernel.requestHistory({} as any);
  119. expect(reply.content.status).toBe('ok');
  120. });
  121. });
  122. describe('.restart()', () => {
  123. it('should be a no-op', async () => {
  124. const kernel = new Mock.KernelMock({});
  125. await kernel.restart();
  126. });
  127. });
  128. describe('.requestExecute()', () => {
  129. it('should request execution', async () => {
  130. const kernel = new Mock.KernelMock({});
  131. let called = false;
  132. kernel.iopubMessage.connect((_, msg) => {
  133. if (msg.header.msg_type === 'execute_input') {
  134. called = true;
  135. }
  136. });
  137. const future = kernel.requestExecute({ code: 'foo ' });
  138. await future.done;
  139. expect(called).toBe(true);
  140. });
  141. });
  142. });
  143. describe('SessionConnectionMock', () => {
  144. describe('.constructor()', () => {
  145. it('should create a new SessionConnectionMock', () => {
  146. const session = new Mock.SessionConnectionMock({}, null);
  147. expect(session.kernel!.name).toBe(Mock.DEFAULT_NAME);
  148. });
  149. it('should take options', () => {
  150. const kernel = new Mock.KernelMock({});
  151. const session = new Mock.SessionConnectionMock(
  152. { model: { name: 'foo' } },
  153. kernel
  154. );
  155. expect(session.kernel).toBe(kernel);
  156. expect(session.name).toBe('foo');
  157. });
  158. });
  159. describe('.dispose()', () => {
  160. it('should be a no-op', () => {
  161. const session = new Mock.SessionConnectionMock({}, null);
  162. session.dispose();
  163. });
  164. });
  165. describe('.changeKernel()', () => {
  166. it('should change the kernel', async () => {
  167. const session = new Mock.SessionConnectionMock({}, null);
  168. const oldId = session.kernel!.id;
  169. const kernel = await session.changeKernel({ name: Mock.DEFAULT_NAME });
  170. expect(kernel!.id).not.toBe(oldId);
  171. });
  172. });
  173. describe('.shutdown()', () => {
  174. it('should be a no-op', async () => {
  175. const session = new Mock.SessionConnectionMock({}, null);
  176. await session.shutdown();
  177. });
  178. });
  179. describe('.setPath()', () => {
  180. it('should set the path', async () => {
  181. const session = new Mock.SessionConnectionMock({}, null);
  182. let called = false;
  183. session.propertyChanged.connect((_, args) => {
  184. if (args === 'path') {
  185. called = true;
  186. }
  187. });
  188. await session.setPath('foo');
  189. expect(session.path).toBe('foo');
  190. expect(called).toBe(true);
  191. });
  192. });
  193. describe('.setType()', () => {
  194. it('should set the type', async () => {
  195. const session = new Mock.SessionConnectionMock({}, null);
  196. let called = false;
  197. session.propertyChanged.connect((_, args) => {
  198. if (args === 'type') {
  199. called = true;
  200. }
  201. });
  202. await session.setType('foo');
  203. expect(session.type).toBe('foo');
  204. expect(called).toBe(true);
  205. });
  206. });
  207. describe('.setName()', () => {
  208. it('should set the name', async () => {
  209. const session = new Mock.SessionConnectionMock({}, null);
  210. let called = false;
  211. session.propertyChanged.connect((_, args) => {
  212. if (args === 'name') {
  213. called = true;
  214. }
  215. });
  216. await session.setName('foo');
  217. expect(session.name).toBe('foo');
  218. expect(called).toBe(true);
  219. });
  220. });
  221. });
  222. describe('SessionContextMock', () => {
  223. describe('.constructor()', () => {
  224. it('should create a new context', () => {
  225. const context = new Mock.SessionContextMock({}, null);
  226. expect(context.session!.kernel!.name).toBe(Mock.DEFAULT_NAME);
  227. });
  228. it('should accept options', () => {
  229. const session = new Mock.SessionConnectionMock({}, null);
  230. const context = new Mock.SessionContextMock({ path: 'foo' }, session);
  231. expect(context.session).toBe(session);
  232. expect(context.path).toBe('foo');
  233. });
  234. });
  235. describe('.dispose()', () => {
  236. it('should be a no-op', () => {
  237. const context = new Mock.SessionContextMock({}, null);
  238. context.dispose();
  239. });
  240. });
  241. describe('.initialize()', () => {
  242. it('should be a no-op', async () => {
  243. const context = new Mock.SessionContextMock({}, null);
  244. await context.initialize();
  245. });
  246. });
  247. describe('.ready', () => {
  248. it('should be a no-op', async () => {
  249. const context = new Mock.SessionContextMock({}, null);
  250. await context.ready;
  251. });
  252. });
  253. describe('.changeKernel()', () => {
  254. it('should change the kernel', async () => {
  255. const context = new Mock.SessionContextMock({}, null);
  256. const oldId = context.session!.kernel!.id;
  257. const kernel = await context.changeKernel({ name: Mock.DEFAULT_NAME });
  258. expect(kernel!.id).not.toBe(oldId);
  259. });
  260. });
  261. describe('.shutdown()', () => {
  262. it('should be a no-op', async () => {
  263. const context = new Mock.SessionContextMock({}, null);
  264. await context.shutdown();
  265. });
  266. });
  267. });
  268. describe('ContentsManagerMock', () => {
  269. describe('.constructor()', () => {
  270. it('should create a new mock', () => {
  271. const manager = new Mock.ContentsManagerMock();
  272. expect(manager.localPath('foo')).toBe('foo');
  273. });
  274. });
  275. describe('.newUntitled', () => {
  276. it('should create a new text file', async () => {
  277. const manager = new Mock.ContentsManagerMock();
  278. let called = false;
  279. manager.fileChanged.connect((_, args) => {
  280. if (args.type === 'new') {
  281. called = true;
  282. }
  283. });
  284. const contents = await manager.newUntitled();
  285. expect(contents.type).toBe('file');
  286. expect(called).toBe(true);
  287. });
  288. it('should create a new notebook', async () => {
  289. const manager = new Mock.ContentsManagerMock();
  290. const contents = await manager.newUntitled({ type: 'notebook' });
  291. expect(contents.type).toBe('notebook');
  292. });
  293. });
  294. describe('.createCheckpoint()', () => {
  295. it('should create a checkpoint', async () => {
  296. const manager = new Mock.ContentsManagerMock();
  297. const content = await manager.newUntitled();
  298. const checkpoint = await manager.createCheckpoint(content.path);
  299. expect(checkpoint.id).toBeTruthy();
  300. });
  301. });
  302. describe('.listCheckpoints()', () => {
  303. it('should list the checkpoints', async () => {
  304. const manager = new Mock.ContentsManagerMock();
  305. const content = await manager.newUntitled();
  306. const checkpoint = await manager.createCheckpoint(content.path);
  307. const checkpoints = await manager.listCheckpoints(content.path);
  308. expect(checkpoints[0].id).toBe(checkpoint.id);
  309. });
  310. });
  311. describe('.deleteCheckpoint', () => {
  312. it('should delete a checkpoints', async () => {
  313. const manager = new Mock.ContentsManagerMock();
  314. const content = await manager.newUntitled();
  315. const checkpoint = await manager.createCheckpoint(content.path);
  316. await manager.deleteCheckpoint(content.path, checkpoint.id);
  317. const checkpoints = await manager.listCheckpoints(content.path);
  318. expect(checkpoints.length).toBe(0);
  319. });
  320. });
  321. describe('.restoreCheckpoint()', () => {
  322. it('should restore the contents', async () => {
  323. const manager = new Mock.ContentsManagerMock();
  324. const content = await manager.newUntitled();
  325. await manager.save(content.path, { content: 'foo' });
  326. const checkpoint = await manager.createCheckpoint(content.path);
  327. await manager.save(content.path, { content: 'bar' });
  328. await manager.restoreCheckpoint(content.path, checkpoint.id);
  329. const newContent = await manager.get(content.path);
  330. expect(newContent.content).toBe('foo');
  331. });
  332. });
  333. describe('.getModelDBFactory()', () => {
  334. it('should return null', () => {
  335. const manager = new Mock.ContentsManagerMock();
  336. expect(manager.getModelDBFactory('foo')).toBe(null);
  337. });
  338. });
  339. describe('.normalize()', () => {
  340. it('should normalize a path', () => {
  341. const manager = new Mock.ContentsManagerMock();
  342. expect(manager.normalize('foo/bar/../baz')).toBe('foo/baz');
  343. });
  344. });
  345. describe('.localPath', () => {
  346. it('should get the local path of a file', () => {
  347. const manager = new Mock.ContentsManagerMock();
  348. const defaultDrive = manager.driveName('foo');
  349. expect(manager.localPath(`${defaultDrive}foo/bar`)).toBe('foo/bar');
  350. });
  351. });
  352. describe('.get()', () => {
  353. it('should get the file contents', async () => {
  354. const manager = new Mock.ContentsManagerMock();
  355. const content = await manager.newUntitled();
  356. await manager.save(content.path, { content: 'foo' });
  357. const newContent = await manager.get(content.path);
  358. expect(newContent.content).toBe('foo');
  359. });
  360. });
  361. describe('.driveName()', () => {
  362. it('should get the drive name of the path', () => {
  363. const manager = new Mock.ContentsManagerMock();
  364. const defaultDrive = manager.driveName('foo');
  365. expect(manager.driveName(`${defaultDrive}/bar`)).toBe(defaultDrive);
  366. });
  367. });
  368. describe('.rename()', () => {
  369. it('should rename the file', async () => {
  370. const manager = new Mock.ContentsManagerMock();
  371. let called = false;
  372. manager.fileChanged.connect((_, args) => {
  373. if (args.type !== 'rename') {
  374. return;
  375. }
  376. expect(args.newValue!.path).toBe('foo');
  377. called = true;
  378. });
  379. const contents = await manager.newUntitled();
  380. await manager.rename(contents.path, 'foo');
  381. expect(called).toBe(true);
  382. });
  383. });
  384. describe('.delete()', () => {
  385. it('should delete the file', async () => {
  386. const manager = new Mock.ContentsManagerMock();
  387. let called = false;
  388. manager.fileChanged.connect((_, args) => {
  389. if (args.type !== 'delete') {
  390. return;
  391. }
  392. expect(args.newValue).toBe(null);
  393. called = true;
  394. });
  395. const contents = await manager.newUntitled();
  396. await manager.delete(contents.path);
  397. expect(called).toBe(true);
  398. });
  399. });
  400. describe('.save()', () => {
  401. it('should save the file', async () => {
  402. const manager = new Mock.ContentsManagerMock();
  403. let called = false;
  404. manager.fileChanged.connect((_, args) => {
  405. if (args.type !== 'save') {
  406. return;
  407. }
  408. expect(args.newValue!.content).toBe('bar');
  409. called = true;
  410. });
  411. const contents = await manager.newUntitled();
  412. await manager.save(contents.path, { content: 'bar' });
  413. expect(called).toBe(true);
  414. });
  415. });
  416. describe('.dispose()', () => {
  417. it('should be a no-op', () => {
  418. const manager = new Mock.ContentsManagerMock();
  419. manager.dispose();
  420. });
  421. });
  422. });
  423. describe('SessionManagerMock', () => {
  424. describe('.constructor()', () => {
  425. it('should create a new session manager', () => {
  426. const manager = new Mock.SessionManagerMock();
  427. expect(manager.isReady).toBe(true);
  428. });
  429. });
  430. describe('.startNew()', () => {
  431. it('should start a new session', async () => {
  432. const manager = new Mock.SessionManagerMock();
  433. const session = await manager.startNew({
  434. path: 'foo',
  435. name: 'foo',
  436. type: 'bar',
  437. kernel: { name: Mock.DEFAULT_NAME }
  438. });
  439. expect(session.kernel!.name).toBe(Mock.DEFAULT_NAME);
  440. });
  441. });
  442. describe('.connectTo()', () => {
  443. it('should connect to a session', async () => {
  444. const manager = new Mock.SessionManagerMock();
  445. const session = await manager.connectTo({
  446. model: {
  447. id: 'fizz',
  448. path: 'foo',
  449. type: 'bar',
  450. name: 'baz',
  451. kernel: { name: Mock.DEFAULT_NAME, id: 'fuzz' }
  452. }
  453. });
  454. expect(session.kernel!.name).toBe(Mock.DEFAULT_NAME);
  455. });
  456. });
  457. describe('.stopIfNeeded()', () => {
  458. it('should remove a running kernel', async () => {
  459. const manager = new Mock.SessionManagerMock();
  460. const session = await manager.startNew({
  461. path: 'foo',
  462. name: 'foo',
  463. type: 'bar',
  464. kernel: { name: Mock.DEFAULT_NAME }
  465. });
  466. expect(toArray(manager.running()).length).toBe(1);
  467. await manager.stopIfNeeded(session.path);
  468. expect(toArray(manager.running()).length).toBe(0);
  469. });
  470. });
  471. describe('.refreshRunning()', () => {
  472. it('should be a no-op', async () => {
  473. const manager = new Mock.SessionManagerMock();
  474. await manager.refreshRunning();
  475. });
  476. });
  477. describe('.running()', () => {
  478. it('should be an iterable of running sessions', async () => {
  479. const manager = new Mock.SessionManagerMock();
  480. await manager.startNew({
  481. path: 'foo',
  482. name: 'foo',
  483. type: 'bar',
  484. kernel: { name: Mock.DEFAULT_NAME }
  485. });
  486. expect(toArray(manager.running()).length).toBe(1);
  487. });
  488. });
  489. });
  490. describe('KernelSpecManagerMock', () => {
  491. describe('.constructor', () => {
  492. it('should create a new mock', () => {
  493. const manager = new Mock.KernelSpecManagerMock();
  494. expect(manager.isReady).toBe(true);
  495. });
  496. });
  497. describe('.specs', () => {
  498. it('should be the kernel specs', () => {
  499. const manager = new Mock.KernelSpecManagerMock();
  500. expect(manager.specs!.default).toBe(Mock.DEFAULT_NAME);
  501. });
  502. });
  503. describe('.refreshSpecs()', () => {
  504. it('should be a no-op', async () => {
  505. const manager = new Mock.KernelSpecManagerMock();
  506. await manager.refreshSpecs();
  507. });
  508. });
  509. });
  510. describe('ServiceManagerMock', () => {
  511. describe('.constructor()', () => {
  512. it('should create a new mock', () => {
  513. const manager = new Mock.ServiceManagerMock();
  514. expect(manager.isReady).toBe(true);
  515. });
  516. });
  517. describe('.ready', () => {
  518. it('should resolve', async () => {
  519. const manager = new Mock.ServiceManagerMock();
  520. await manager.ready;
  521. });
  522. });
  523. describe('.contents', () => {
  524. it('should be a contents manager', () => {
  525. const manager = new Mock.ServiceManagerMock();
  526. expect(manager.contents.normalize).toBeTruthy();
  527. });
  528. });
  529. describe('.sessions', () => {
  530. it('should be a sessions manager', () => {
  531. const manager = new Mock.ServiceManagerMock();
  532. expect(manager.sessions.isReady).toBe(true);
  533. });
  534. });
  535. describe('.kernelspecs', () => {
  536. it('should be a kernelspecs manager', () => {
  537. const manager = new Mock.ServiceManagerMock();
  538. expect(manager.kernelspecs.isReady).toBe(true);
  539. });
  540. });
  541. describe('.dispose()', () => {
  542. it('should be a no-op', () => {
  543. const manager = new Mock.ServiceManagerMock();
  544. manager.dispose();
  545. });
  546. });
  547. });
  548. describe('MockShellFuture', () => {
  549. it('should create a new mock', async () => {
  550. const msg = KernelMessage.createMessage({
  551. channel: 'shell',
  552. msgType: 'execute_reply',
  553. session: 'foo',
  554. username: 'bar',
  555. msgId: 'fizz',
  556. content: {
  557. user_expressions: {},
  558. execution_count: 0,
  559. status: 'ok'
  560. }
  561. });
  562. const future = new Mock.MockShellFuture(msg);
  563. const reply = await future.done;
  564. expect(reply).toBe(msg);
  565. future.dispose();
  566. });
  567. });
  568. describe('createFileContext()', () => {
  569. it('should create a context without a kernel', async () => {
  570. const context = await Mock.createFileContext();
  571. expect(context.sessionContext.session).toBe(null);
  572. });
  573. it('should create a context with a kernel', async () => {
  574. const context = await Mock.createFileContext(true);
  575. expect(context.sessionContext.session!.kernel!.name).toBe(
  576. Mock.DEFAULT_NAME
  577. );
  578. });
  579. });
  580. });