session.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) Jupyter Development Team.
  2. import 'jest';
  3. import { UUID } from '@lumino/coreutils';
  4. import { toArray } from '@lumino/algorithm';
  5. import { SessionAPI } from '../../src';
  6. import { Session } from '../../src';
  7. import {
  8. expectFailure,
  9. JupyterServer,
  10. flakyIt as it
  11. } from '@jupyterlab/testutils';
  12. import {
  13. makeSettings,
  14. createSessionModel,
  15. getRequestHandler,
  16. init
  17. } from '../utils';
  18. init();
  19. const server = new JupyterServer();
  20. beforeAll(async () => {
  21. await server.start();
  22. });
  23. afterAll(async () => {
  24. await server.shutdown();
  25. });
  26. describe('session', () => {
  27. let session: Session.IModel;
  28. beforeAll(async () => {
  29. const sessions = await SessionAPI.listRunning();
  30. await Promise.all(sessions.map(s => SessionAPI.shutdownSession(s.id)));
  31. });
  32. afterEach(async () => {
  33. const sessions = await SessionAPI.listRunning();
  34. await Promise.all(sessions.map(s => SessionAPI.shutdownSession(s.id)));
  35. });
  36. describe('Session.listRunning()', () => {
  37. it('should yield a list of valid session models', async () => {
  38. expect(toArray(await SessionAPI.listRunning()).length).toBe(0);
  39. const session = await SessionAPI.startSession({
  40. name: UUID.uuid4(),
  41. path: UUID.uuid4(),
  42. type: 'test'
  43. });
  44. expect(toArray(await SessionAPI.listRunning())).toEqual([session]);
  45. });
  46. it('should throw an error for an invalid model', async () => {
  47. const data = { id: '1234', path: 'test' };
  48. const serverSettings = getRequestHandler(200, data);
  49. const list = SessionAPI.listRunning(serverSettings);
  50. await expectFailure(list);
  51. });
  52. it('should throw an error for another invalid model', async () => {
  53. const data = [{ id: '1234', kernel: { id: '', name: '' }, path: '' }];
  54. const serverSettings = getRequestHandler(200, data);
  55. const list = SessionAPI.listRunning(serverSettings);
  56. await expectFailure(list);
  57. });
  58. it('should fail for wrong response status', async () => {
  59. const serverSettings = getRequestHandler(201, [createSessionModel()]);
  60. const list = SessionAPI.listRunning(serverSettings);
  61. await expectFailure(list);
  62. });
  63. it('should fail for error response status', async () => {
  64. const serverSettings = getRequestHandler(500, {});
  65. const list = SessionAPI.listRunning(serverSettings);
  66. await expectFailure(list, '');
  67. });
  68. });
  69. describe('SessionAPI.startNew', () => {
  70. it('should start a session', async () => {
  71. session = await SessionAPI.startSession({
  72. path: UUID.uuid4(),
  73. name: UUID.uuid4(),
  74. type: 'test'
  75. });
  76. expect(session.id).toBeTruthy();
  77. });
  78. it('should accept ajax options', async () => {
  79. const serverSettings = makeSettings();
  80. session = await SessionAPI.startSession(
  81. {
  82. path: UUID.uuid4(),
  83. name: UUID.uuid4(),
  84. type: 'test'
  85. },
  86. serverSettings
  87. );
  88. expect(session.id).toBeTruthy();
  89. });
  90. it('should fail for wrong response status', async () => {
  91. const sessionModel = createSessionModel();
  92. const serverSettings = getRequestHandler(200, sessionModel);
  93. const sessionPromise = SessionAPI.startSession(
  94. sessionModel as any,
  95. serverSettings
  96. );
  97. await expectFailure(sessionPromise);
  98. });
  99. it('should fail for error response status', async () => {
  100. const serverSettings = getRequestHandler(500, {});
  101. const sessionModel = createSessionModel();
  102. const sessionPromise = SessionAPI.startSession(
  103. sessionModel as any,
  104. serverSettings
  105. );
  106. await expectFailure(sessionPromise, '');
  107. });
  108. it('should fail for wrong response model', async () => {
  109. const sessionModel = createSessionModel();
  110. (sessionModel as any).path = 1;
  111. const serverSettings = getRequestHandler(201, sessionModel);
  112. const sessionPromise = SessionAPI.startSession(
  113. sessionModel as any,
  114. serverSettings
  115. );
  116. const msg = `Property 'path' is not of type 'string'`;
  117. await expectFailure(sessionPromise, msg);
  118. });
  119. it('should handle a deprecated response model', async () => {
  120. const sessionModel = createSessionModel();
  121. const data = {
  122. id: sessionModel.id,
  123. kernel: sessionModel.kernel,
  124. notebook: { path: sessionModel.path }
  125. };
  126. const serverSettings = getRequestHandler(201, data);
  127. const model = await SessionAPI.startSession(
  128. sessionModel as any,
  129. serverSettings
  130. );
  131. expect(model).toHaveProperty('id');
  132. expect(model.path).toBeTruthy();
  133. });
  134. });
  135. describe('Session.shutdown()', () => {
  136. it('should shut down a kernel by id', async () => {
  137. session = await SessionAPI.startSession({
  138. path: UUID.uuid4(),
  139. name: UUID.uuid4(),
  140. type: 'test'
  141. });
  142. await SessionAPI.shutdownSession(session.id);
  143. });
  144. it('should handle a 404 status', () => {
  145. return SessionAPI.shutdownSession(UUID.uuid4());
  146. });
  147. });
  148. });