123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- // 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 {
- toArray
- } from '@phosphor/algorithm';
- import {
- JSONExt
- } from '@phosphor/coreutils';
- import {
- TerminalSession
- } from '../../../lib/terminal';
- import {
- TerminalTester
- } from '../utils';
- describe('terminal', () => {
- let tester: TerminalTester;
- let session: TerminalSession.ISession;
- beforeEach(() => {
- tester = new TerminalTester();
- });
- afterEach(() => {
- if (session) {
- session.dispose();
- }
- tester.dispose();
- });
- 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', (done) => {
- TerminalSession.startNew().then(s => {
- session = s;
- expect(session.name).to.be.ok();
- done();
- }).catch(done);
- });
- });
- describe('.connectTo', () => {
- it('should give back an existing session', () => {
- return TerminalSession.startNew().then(s => {
- session = s;
- return TerminalSession.connectTo(s.name);
- }).then(newSession => {
- expect(newSession).to.be(session);
- });
- });
- it('should give back a session that exists on the server', () => {
- tester.onRequest = () => {
- tester.respond(200, [{ name: 'foo' }]);
- };
- return TerminalSession.connectTo('foo').then(s => {
- expect(s.name).to.be('foo');
- s.dispose();
- });
- });
- it('should reject if the session does not exist on the server', () => {
- tester.onRequest = () => {
- tester.respond(200, [{ name: 'foo' }]);
- };
- return TerminalSession.connectTo('bar').then(
- () => { throw Error('should not get here'); },
- () => undefined
- );
- });
- });
- describe('.shutdown()', () => {
- it('should shut down a terminal session by name', (done) => {
- TerminalSession.startNew().then(s => {
- session = s;
- return TerminalSession.shutdown(s.name);
- }).then(() => {
- done();
- }).catch(done);
- });
- it('should handle a 404 status', (done) => {
- tester.onRequest = () => {
- tester.respond(404, { });
- };
- TerminalSession.shutdown('foo').then(done, done);
- });
- });
- describe('.listRunning()', () => {
- it('should list the running session models', (done) => {
- let data: TerminalSession.IModel[] = [{ name: 'foo'}, { name: 'bar' }];
- tester.runningTerminals = data;
- TerminalSession.listRunning().then(models => {
- expect(JSONExt.deepEqual(data, toArray(models))).to.be(true);
- done();
- }).catch(done);
- });
- });
- });
- describe('.ISession', () => {
- beforeEach(() => {
- return TerminalSession.startNew().then(s => {
- session = s;
- });
- });
- afterEach(() => {
- session.dispose();
- });
- describe('#terminated', () => {
- it('should be emitted when the session is shut down', (done) => {
- session.terminated.connect((sender, args) => {
- expect(sender).to.be(session);
- expect(args).to.be(void 0);
- done();
- });
- session.shutdown();
- });
- });
- describe('#messageReceived', () => {
- it('should be emitted when a message is received', (done) => {
- session.messageReceived.connect((sender, msg) => {
- expect(sender).to.be(session);
- expect(msg.type).to.be('stdout');
- expect(toArray(msg.content)).to.eql(['foo bar']);
- done();
- });
- tester.sendRaw(JSON.stringify(['stdout', 'foo bar']));
- });
- });
- describe('#name', () => {
- it('should be the name of the session', (done) => {
- session.dispose();
- TerminalSession.startNew().then(s => {
- session = s;
- expect(session.name).to.be.ok();
- done();
- }).catch(done);
- });
- });
- context('#serverSettings', () => {
- it('should be the server settings of the server', () => {
- expect(session.serverSettings.baseUrl).to.be(PageConfig.getBaseUrl());
- });
- });
- describe('#isDisposed', () => {
- it('should test whether the object is disposed', () => {
- expect(session.isDisposed).to.be(false);
- session.dispose();
- expect(session.isDisposed).to.be(true);
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources used by the session', () => {
- session.dispose();
- expect(session.isDisposed).to.be(true);
- });
- it('should be safe to call more than once', () => {
- session.dispose();
- session.dispose();
- expect(session.isDisposed).to.be(true);
- });
- });
- context('#isReady', () => {
- it('should test whether the terminal is ready', (done) => {
- session.shutdown();
- TerminalSession.startNew().then(s => {
- session = s;
- expect(session.isReady).to.be(false);
- return session.ready;
- }).then(() => {
- expect(session.isReady).to.be(true);
- done();
- }).catch(done);
- });
- });
- describe('#ready', () => {
- it('should resolve when the terminal is ready', (done) => {
- session.ready.then(done, done);
- });
- });
- describe('#send()', () => {
- it('should send a message to the socket', (done) => {
- tester.onMessage(msg => {
- expect(msg.type).to.be('stdin');
- done();
- });
- session.ready.then(() => {
- session.send({ type: 'stdin', content: [1, 2] });
- }).catch(done);
- });
- });
- describe('#reconnect()', () => {
- it('should reconnect to the socket', (done) => {
- session.ready.then(() => {
- let promise = session.reconnect();
- expect(session.isReady).to.be(false);
- return promise;
- }).then(() => {
- expect(session.isReady).to.be(true);
- }).then(done, done);
- });
- });
- describe('#shutdown()', () => {
- it('should shut down the terminal session', (done) => {
- session.shutdown().then(done, done);
- });
- it('should handle a 404 status', (done) => {
- tester.onRequest = () => {
- tester.respond(404, { });
- };
- session.shutdown().then(done, done);
- });
- });
- });
- });
|