terminal.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { expect } from 'chai';
  4. import { PageConfig } from '@jupyterlab/coreutils';
  5. import { UUID } from '@phosphor/coreutils';
  6. import { TerminalSession } from '@jupyterlab/services';
  7. import { handleRequest, testEmission } from '../utils';
  8. describe('terminal', () => {
  9. let defaultSession: TerminalSession.ISession;
  10. let session: TerminalSession.ISession;
  11. beforeAll(async () => {
  12. defaultSession = await TerminalSession.startNew();
  13. });
  14. afterEach(async () => {
  15. if (session) {
  16. await session.shutdown();
  17. }
  18. });
  19. describe('TerminalSession', () => {
  20. describe('.isAvailable()', () => {
  21. it('should test whether terminal sessions are available', () => {
  22. expect(TerminalSession.isAvailable()).to.equal(true);
  23. });
  24. });
  25. describe('.startNew()', () => {
  26. it('should startNew a terminal session', async () => {
  27. session = await TerminalSession.startNew();
  28. expect(session.name).to.be.ok;
  29. });
  30. });
  31. describe('.connectTo', () => {
  32. it('should give back an existing session', async () => {
  33. const newSession = await TerminalSession.connectTo(defaultSession.name);
  34. expect(newSession.name).to.equal(defaultSession.name);
  35. expect(newSession).to.not.equal(defaultSession);
  36. });
  37. it('should reject if the session does not exist on the server', async () => {
  38. try {
  39. await TerminalSession.connectTo(UUID.uuid4());
  40. throw Error('should not get here');
  41. } catch (e) {
  42. expect(e.message).to.not.equal('should not get here');
  43. }
  44. });
  45. });
  46. describe('.shutdown()', () => {
  47. it('should shut down a terminal session by name', async () => {
  48. session = await TerminalSession.startNew();
  49. await TerminalSession.shutdown(session.name);
  50. });
  51. it('should handle a 404 status', () => {
  52. return TerminalSession.shutdown('ThisTerminalDoesNotExist');
  53. });
  54. });
  55. describe('.listRunning()', () => {
  56. it('should list the running session models', async () => {
  57. const models = await TerminalSession.listRunning();
  58. expect(models.length).to.be.greaterThan(0);
  59. });
  60. });
  61. });
  62. describe('.ISession', () => {
  63. describe('#terminated', () => {
  64. it('should be emitted when the session is disposed', async () => {
  65. session = await TerminalSession.startNew();
  66. let called = false;
  67. session.terminated.connect((sender, args) => {
  68. expect(sender).to.equal(session);
  69. expect(args).to.be.undefined;
  70. called = true;
  71. });
  72. await session.dispose();
  73. expect(called).to.equal(true);
  74. });
  75. });
  76. describe('#messageReceived', () => {
  77. it('should be emitted when a message is received', async () => {
  78. session = await TerminalSession.startNew();
  79. await testEmission(session.messageReceived, {
  80. test: (sender, msg) => {
  81. return msg.type === 'stdout';
  82. }
  83. });
  84. });
  85. });
  86. describe('#name', () => {
  87. it('should be the name of the session', () => {
  88. expect(defaultSession.name).to.be.ok;
  89. });
  90. });
  91. describe('#serverSettings', () => {
  92. it('should be the server settings of the server', () => {
  93. expect(defaultSession.serverSettings.baseUrl).to.equal(
  94. PageConfig.getBaseUrl()
  95. );
  96. });
  97. });
  98. describe('#isDisposed', () => {
  99. it('should test whether the object is disposed', async () => {
  100. session = await TerminalSession.startNew();
  101. const name = session.name;
  102. expect(session.isDisposed).to.equal(false);
  103. session.dispose();
  104. expect(session.isDisposed).to.equal(true);
  105. await TerminalSession.shutdown(name);
  106. });
  107. });
  108. describe('#dispose()', () => {
  109. it('should dispose of the resources used by the session', async () => {
  110. session = await TerminalSession.startNew();
  111. const name = session.name;
  112. session.dispose();
  113. expect(session.isDisposed).to.equal(true);
  114. await TerminalSession.shutdown(name);
  115. });
  116. it('should be safe to call more than once', async () => {
  117. session = await TerminalSession.startNew();
  118. const name = session.name;
  119. session.dispose();
  120. session.dispose();
  121. expect(session.isDisposed).to.equal(true);
  122. await TerminalSession.shutdown(name);
  123. });
  124. });
  125. describe('#isReady', () => {
  126. it('should test whether the terminal is ready', async () => {
  127. session = await TerminalSession.startNew();
  128. expect(session.isReady).to.equal(false);
  129. await session.ready;
  130. expect(session.isReady).to.equal(true);
  131. });
  132. });
  133. describe('#ready', () => {
  134. it('should resolve when the terminal is ready', () => {
  135. return defaultSession.ready;
  136. });
  137. });
  138. describe('#send()', () => {
  139. it('should send a message to the socket', async () => {
  140. await defaultSession.ready;
  141. session.send({ type: 'stdin', content: [1, 2] });
  142. });
  143. });
  144. describe('#reconnect()', () => {
  145. it('should reconnect to the socket', async () => {
  146. const session = await TerminalSession.startNew();
  147. const promise = session.reconnect();
  148. expect(session.isReady).to.equal(false);
  149. await promise;
  150. expect(session.isReady).to.equal(true);
  151. });
  152. });
  153. describe('#shutdown()', () => {
  154. it('should shut down the terminal session', async () => {
  155. session = await TerminalSession.startNew();
  156. await session.shutdown();
  157. });
  158. it('should handle a 404 status', () => {
  159. handleRequest(defaultSession, 404, {});
  160. return defaultSession.shutdown();
  161. });
  162. });
  163. });
  164. });