terminal.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. toArray
  9. } from '@phosphor/algorithm';
  10. import {
  11. JSONExt
  12. } from '@phosphor/coreutils';
  13. import {
  14. TerminalSession
  15. } from '../../../lib/terminal';
  16. import {
  17. TerminalTester
  18. } from '../utils';
  19. describe('terminal', () => {
  20. let tester: TerminalTester;
  21. let session: TerminalSession.ISession;
  22. beforeEach(() => {
  23. tester = new TerminalTester();
  24. });
  25. afterEach(() => {
  26. if (session) {
  27. session.dispose();
  28. }
  29. tester.dispose();
  30. });
  31. describe('TerminalSession', () => {
  32. describe('.isAvailable()', () => {
  33. it('should test whether terminal sessions are available', () => {
  34. expect(TerminalSession.isAvailable()).to.be(true);
  35. });
  36. });
  37. describe('.startNew()', () => {
  38. it('should startNew a terminal session', (done) => {
  39. TerminalSession.startNew().then(s => {
  40. session = s;
  41. expect(session.name).to.be.ok();
  42. done();
  43. }).catch(done);
  44. });
  45. });
  46. describe('.connectTo', () => {
  47. it('should give back an existing session', () => {
  48. return TerminalSession.startNew().then(s => {
  49. session = s;
  50. return TerminalSession.connectTo(s.name);
  51. }).then(newSession => {
  52. expect(newSession).to.be(session);
  53. });
  54. });
  55. it('should give back a session that exists on the server', () => {
  56. tester.onRequest = () => {
  57. tester.respond(200, [{ name: 'foo' }]);
  58. };
  59. return TerminalSession.connectTo('foo').then(s => {
  60. expect(s.name).to.be('foo');
  61. s.dispose();
  62. });
  63. });
  64. it('should reject if the session does not exist on the server', () => {
  65. tester.onRequest = () => {
  66. tester.respond(200, [{ name: 'foo' }]);
  67. };
  68. return TerminalSession.connectTo('bar').then(
  69. () => { throw Error('should not get here'); },
  70. () => undefined
  71. );
  72. });
  73. });
  74. describe('.shutdown()', () => {
  75. it('should shut down a terminal session by name', (done) => {
  76. TerminalSession.startNew().then(s => {
  77. session = s;
  78. return TerminalSession.shutdown(s.name);
  79. }).then(() => {
  80. done();
  81. }).catch(done);
  82. });
  83. it('should handle a 404 status', (done) => {
  84. tester.onRequest = () => {
  85. tester.respond(404, { });
  86. };
  87. TerminalSession.shutdown('foo').then(done, done);
  88. });
  89. });
  90. describe('.listRunning()', () => {
  91. it('should list the running session models', (done) => {
  92. let data: TerminalSession.IModel[] = [{ name: 'foo'}, { name: 'bar' }];
  93. tester.runningTerminals = data;
  94. TerminalSession.listRunning().then(models => {
  95. expect(JSONExt.deepEqual(data, toArray(models))).to.be(true);
  96. done();
  97. }).catch(done);
  98. });
  99. });
  100. });
  101. describe('.ISession', () => {
  102. beforeEach(() => {
  103. return TerminalSession.startNew().then(s => {
  104. session = s;
  105. });
  106. });
  107. afterEach(() => {
  108. session.dispose();
  109. });
  110. describe('#terminated', () => {
  111. it('should be emitted when the session is shut down', (done) => {
  112. session.terminated.connect((sender, args) => {
  113. expect(sender).to.be(session);
  114. expect(args).to.be(void 0);
  115. done();
  116. });
  117. session.shutdown();
  118. });
  119. });
  120. describe('#messageReceived', () => {
  121. it('should be emitted when a message is received', (done) => {
  122. session.messageReceived.connect((sender, msg) => {
  123. expect(sender).to.be(session);
  124. expect(msg.type).to.be('stdout');
  125. expect(toArray(msg.content)).to.eql(['foo bar']);
  126. done();
  127. });
  128. tester.sendRaw(JSON.stringify(['stdout', 'foo bar']));
  129. });
  130. });
  131. describe('#name', () => {
  132. it('should be the name of the session', (done) => {
  133. session.dispose();
  134. TerminalSession.startNew().then(s => {
  135. session = s;
  136. expect(session.name).to.be.ok();
  137. done();
  138. }).catch(done);
  139. });
  140. });
  141. context('#serverSettings', () => {
  142. it('should be the server settings of the server', () => {
  143. expect(session.serverSettings.baseUrl).to.be(PageConfig.getBaseUrl());
  144. });
  145. });
  146. describe('#isDisposed', () => {
  147. it('should test whether the object is disposed', () => {
  148. expect(session.isDisposed).to.be(false);
  149. session.dispose();
  150. expect(session.isDisposed).to.be(true);
  151. });
  152. });
  153. describe('#dispose()', () => {
  154. it('should dispose of the resources used by the session', () => {
  155. session.dispose();
  156. expect(session.isDisposed).to.be(true);
  157. });
  158. it('should be safe to call more than once', () => {
  159. session.dispose();
  160. session.dispose();
  161. expect(session.isDisposed).to.be(true);
  162. });
  163. });
  164. context('#isReady', () => {
  165. it('should test whether the terminal is ready', (done) => {
  166. session.shutdown();
  167. TerminalSession.startNew().then(s => {
  168. session = s;
  169. expect(session.isReady).to.be(false);
  170. return session.ready;
  171. }).then(() => {
  172. expect(session.isReady).to.be(true);
  173. done();
  174. }).catch(done);
  175. });
  176. });
  177. describe('#ready', () => {
  178. it('should resolve when the terminal is ready', (done) => {
  179. session.ready.then(done, done);
  180. });
  181. });
  182. describe('#send()', () => {
  183. it('should send a message to the socket', (done) => {
  184. tester.onMessage(msg => {
  185. expect(msg.type).to.be('stdin');
  186. done();
  187. });
  188. session.ready.then(() => {
  189. session.send({ type: 'stdin', content: [1, 2] });
  190. }).catch(done);
  191. });
  192. });
  193. describe('#reconnect()', () => {
  194. it('should reconnect to the socket', (done) => {
  195. session.ready.then(() => {
  196. let promise = session.reconnect();
  197. expect(session.isReady).to.be(false);
  198. return promise;
  199. }).then(() => {
  200. expect(session.isReady).to.be(true);
  201. }).then(done, done);
  202. });
  203. });
  204. describe('#shutdown()', () => {
  205. it('should shut down the terminal session', (done) => {
  206. session.shutdown().then(done, done);
  207. });
  208. it('should handle a 404 status', (done) => {
  209. tester.onRequest = () => {
  210. tester.respond(404, { });
  211. };
  212. session.shutdown().then(done, done);
  213. });
  214. });
  215. });
  216. });