terminal.spec.ts 3.7 KB

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