123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import expect = require('expect.js');
- import {
- PageConfig
- } from '@jupyterlab/coreutils';
- import {
- UUID
- } from '@phosphor/coreutils';
- import {
- Signal
- } from '@phosphor/signaling';
- import {
- TerminalSession
- } from '../../../lib/terminal';
- import {
- handleRequest
- } from '../utils';
- describe('terminal', () => {
- let defaultSession: TerminalSession.ISession;
- let session: TerminalSession.ISession;
- before(() => {
- TerminalSession.startNew().then(s => {
- defaultSession = s;
- });
- });
- afterEach(() => {
- if (session) {
- return session.shutdown();
- }
- });
- describe('TerminalSession', () => {
- describe('.isAvailable()', () => {
- it('should test whether terminal sessions are available', () => {
- expect(TerminalSession.isAvailable()).to.be(true);
- });
- });
- describe('.startNew()', () => {
- it('should startNew a terminal session', () => {
- return TerminalSession.startNew().then(s => {
- session = s;
- expect(session.name).to.be.ok();
- });
- });
- });
- describe('.connectTo', () => {
- it('should give back an existing session', () => {
- return TerminalSession.connectTo(defaultSession.name).then(newSession => {
- expect(newSession.name).to.be(defaultSession.name);
- expect(newSession).to.not.be(defaultSession);
- });
- });
- it('should reject if the session does not exist on the server', () => {
- return TerminalSession.connectTo(UUID.uuid4()).then(
- () => { throw Error('should not get here'); },
- () => undefined
- );
- });
- });
- describe('.shutdown()', () => {
- it('should shut down a terminal session by name', () => {
- return TerminalSession.startNew().then(s => {
- session = s;
- return TerminalSession.shutdown(s.name);
- });
- });
- it('should handle a 404 status', () => {
- return TerminalSession.shutdown(UUID.uuid4());
- });
- });
- describe('.listRunning()', () => {
- it('should list the running session models', () => {
- return TerminalSession.listRunning().then(models => {
- expect(models.length).to.be.greaterThan(0);
- });
- });
- });
- });
- describe('.ISession', () => {
- describe('#terminated', () => {
- it('should be emitted when the session is shut down', (done) => {
- TerminalSession.startNew().then(s => {
- session = s;
- session.terminated.connect((sender, args) => {
- expect(sender).to.be(session);
- expect(args).to.be(void 0);
- done();
- });
- return session.shutdown();
- }).catch(done);
- });
- });
- describe('#messageReceived', () => {
- it('should be emitted when a message is received', (done) => {
- const object = {};
- TerminalSession.startNew().then(s => {
- session = s;
- session.messageReceived.connect((sender, msg) => {
- expect(sender).to.be(session);
- if (msg.type === 'stdout') {
- Signal.disconnectReceiver(object);
- done();
- }
- }, object);
- }).catch(done);
- });
- });
- describe('#name', () => {
- it('should be the name of the session', () => {
- expect(defaultSession.name).to.be.ok();
- });
- });
- context('#serverSettings', () => {
- it('should be the server settings of the server', () => {
- expect(defaultSession.serverSettings.baseUrl).to.be(PageConfig.getBaseUrl());
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the object is disposed', () => {
- return TerminalSession.startNew().then(session => {
- let name = session.name;
- expect(session.isDisposed).to.be(false);
- session.dispose();
- expect(session.isDisposed).to.be(true);
- return TerminalSession.shutdown(name);
- });
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources used by the session', () => {
- TerminalSession.startNew().then(session => {
- let name = session.name;
- session.dispose();
- expect(session.isDisposed).to.be(true);
- return TerminalSession.shutdown(name);
- });
- });
- it('should be safe to call more than once', () => {
- TerminalSession.startNew().then(s => {
- let name = session.name;
- session.dispose();
- session.dispose();
- expect(session.isDisposed).to.be(true);
- return TerminalSession.shutdown(name);
- });
- });
- });
- context('#isReady', () => {
- it('should test whether the terminal is ready', () => {
- return TerminalSession.startNew().then(s => {
- session = s;
- expect(session.isReady).to.be(false);
- return session.ready;
- }).then(() => {
- expect(session.isReady).to.be(true);
- });
- });
- });
- describe('#ready', () => {
- it('should resolve when the terminal is ready', () => {
- return defaultSession.ready;
- });
- });
- describe('#send()', () => {
- it('should send a message to the socket', () => {
- return defaultSession.ready.then(() => {
- session.send({ type: 'stdin', content: [1, 2] });
- });
- });
- });
- describe('#reconnect()', () => {
- it('should reconnect to the socket', () => {
- let session: TerminalSession.ISession;
- return TerminalSession.startNew().then(s => {
- session = s;
- let promise = session.reconnect();
- expect(session.isReady).to.be(false);
- return promise;
- }).then(() => {
- expect(session.isReady).to.be(true);
- });
- });
- });
- describe('#shutdown()', () => {
- it('should shut down the terminal session', () => {
- TerminalSession.startNew().then(session => {
- return session.shutdown();
- });
- });
- it('should handle a 404 status', () => {
- handleRequest(defaultSession, 404, {});
- return defaultSession.shutdown();
- });
- });
- });
- });
|