terminal.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { Terminal, TerminalManager } from '@jupyterlab/services';
  6. import { testEmission } from '@jupyterlab/testutils';
  7. import { handleRequest } from '../utils';
  8. describe('terminal', () => {
  9. let defaultSession: Terminal.ITerminalConnection;
  10. let session: Terminal.ITerminalConnection;
  11. let manager = new TerminalManager();
  12. beforeAll(async () => {
  13. defaultSession = await manager.startNew();
  14. });
  15. afterEach(async () => {
  16. if (session) {
  17. await session.shutdown();
  18. }
  19. });
  20. describe('Terminal', () => {
  21. describe('.isAvailable()', () => {
  22. it('should test whether terminal sessions are available', () => {
  23. expect(Terminal.isAvailable()).to.equal(true);
  24. });
  25. });
  26. });
  27. describe('.ITerminalConnection', () => {
  28. describe('#messageReceived', () => {
  29. it('should be emitted when a message is received', async () => {
  30. session = await manager.startNew();
  31. let emission = testEmission(session.messageReceived, {
  32. test: (sender, msg) => {
  33. return msg.type === 'stdout';
  34. }
  35. });
  36. session.send({ type: 'stdin', content: ['cd\r'] });
  37. await emission;
  38. });
  39. });
  40. describe('#name', () => {
  41. it('should be the name of the session', () => {
  42. expect(defaultSession.name).to.be.ok;
  43. });
  44. });
  45. describe('#serverSettings', () => {
  46. it('should be the server settings of the server', () => {
  47. expect(defaultSession.serverSettings.baseUrl).to.equal(
  48. PageConfig.getBaseUrl()
  49. );
  50. });
  51. });
  52. describe('#isDisposed', () => {
  53. it('should test whether the object is disposed', async () => {
  54. session = await manager.startNew();
  55. const name = session.name;
  56. expect(session.isDisposed).to.equal(false);
  57. session.dispose();
  58. expect(session.isDisposed).to.equal(true);
  59. await manager.shutdown(name);
  60. });
  61. });
  62. describe('#dispose()', () => {
  63. it('should dispose of the resources used by the session', async () => {
  64. session = await manager.startNew();
  65. const name = session.name;
  66. session.dispose();
  67. expect(session.isDisposed).to.equal(true);
  68. await manager.shutdown(name);
  69. });
  70. it('should be safe to call more than once', async () => {
  71. session = await manager.startNew();
  72. const name = session.name;
  73. session.dispose();
  74. session.dispose();
  75. expect(session.isDisposed).to.equal(true);
  76. await manager.shutdown(name);
  77. });
  78. });
  79. describe('#send()', () => {
  80. it('should send a message to the socket', async () => {
  81. session.send({ type: 'stdin', content: [1, 2] });
  82. });
  83. });
  84. describe('#reconnect()', () => {
  85. it('should reconnect to the socket', async () => {
  86. const session = await manager.startNew();
  87. const promise = session.reconnect();
  88. expect(session.connectionStatus).to.equal('connecting');
  89. await promise;
  90. expect(session.connectionStatus).to.equal('connected');
  91. });
  92. });
  93. describe('#shutdown()', () => {
  94. it('should shut down the terminal session', async () => {
  95. session = await manager.startNew();
  96. await session.shutdown();
  97. });
  98. it('should handle a 404 status', () => {
  99. handleRequest(defaultSession, 404, {});
  100. return defaultSession.shutdown();
  101. });
  102. });
  103. });
  104. });