terminal.spec.ts 6.2 KB

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