foreign.spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { UUID } from '@lumino/coreutils';
  5. import { KernelMessage } from '@jupyterlab/services';
  6. import { Signal } from '@lumino/signaling';
  7. import { Panel } from '@lumino/widgets';
  8. import { CodeCellModel, CodeCell } from '@jupyterlab/cells';
  9. import { defaultRenderMime, NBTestUtils, Mock } from '@jupyterlab/testutils';
  10. import { ISessionContext } from '@jupyterlab/apputils';
  11. import { ForeignHandler } from '../src';
  12. class TestParent extends Panel implements ForeignHandler.IReceiver {
  13. addCell(cell: CodeCell, msgId?: string): void {
  14. this.addWidget(cell);
  15. if (msgId) {
  16. this._cells.set(msgId, cell);
  17. }
  18. }
  19. createCodeCell(): CodeCell {
  20. const contentFactory = NBTestUtils.createCodeCellFactory();
  21. const model = new CodeCellModel({});
  22. const cell = new CodeCell({
  23. model,
  24. rendermime,
  25. contentFactory
  26. }).initializeState();
  27. return cell;
  28. }
  29. getCell(msgId: string) {
  30. return this._cells.get(msgId);
  31. }
  32. private _cells = new Map<string, CodeCell>();
  33. }
  34. class TestHandler extends ForeignHandler {
  35. injected = new Signal<this, KernelMessage.IIOPubMessage>(this);
  36. received = new Signal<this, KernelMessage.IIOPubMessage>(this);
  37. rejected = new Signal<this, KernelMessage.IIOPubMessage>(this);
  38. methods: string[] = [];
  39. protected onIOPubMessage(
  40. sender: ISessionContext,
  41. msg: KernelMessage.IIOPubMessage
  42. ): boolean {
  43. const injected = super.onIOPubMessage(sender, msg);
  44. if (injected) {
  45. this.injected.emit(msg);
  46. } else {
  47. // If the message was not injected but otherwise would have been, emit
  48. // a rejected signal. This should only happen if `enabled` is `false`.
  49. const session = (msg.parent_header as KernelMessage.IHeader).session;
  50. const msgType = msg.header.msg_type;
  51. if (
  52. session !== this.sessionContext.session!.kernel!.clientId &&
  53. relevantTypes.has(msgType)
  54. ) {
  55. this.rejected.emit(msg);
  56. } else {
  57. console.log(session, this.sessionContext.session?.kernel?.clientId);
  58. }
  59. }
  60. this.received.emit(msg);
  61. return injected;
  62. }
  63. }
  64. const rendermime = defaultRenderMime();
  65. const relevantTypes = [
  66. 'execute_input',
  67. 'execute_result',
  68. 'display_data',
  69. 'stream',
  70. 'error',
  71. 'clear_output'
  72. ].reduce((acc, val) => {
  73. acc.add(val);
  74. return acc;
  75. }, new Set<string>());
  76. describe('@jupyterlab/console', () => {
  77. describe('ForeignHandler', () => {
  78. let foreign: ISessionContext;
  79. let handler: TestHandler;
  80. let sessionContext: ISessionContext;
  81. const streamMsg = KernelMessage.createMessage({
  82. session: 'foo',
  83. channel: 'iopub',
  84. msgType: 'stream',
  85. content: { name: 'stderr', text: 'foo' }
  86. });
  87. const clearMsg = KernelMessage.createMessage({
  88. session: 'foo',
  89. channel: 'iopub',
  90. msgType: 'clear_output',
  91. content: { wait: false }
  92. });
  93. beforeAll(async function() {
  94. const path = UUID.uuid4();
  95. const kernel0 = new Mock.KernelMock({});
  96. const kernel1 = Mock.cloneKernel(kernel0);
  97. const connection0 = new Mock.SessionConnectionMock(
  98. { model: { path, type: 'test' } },
  99. kernel0
  100. );
  101. sessionContext = new Mock.SessionContextMock({}, connection0);
  102. const connection1 = new Mock.SessionConnectionMock(
  103. { model: { path, type: 'test2' } },
  104. kernel1
  105. );
  106. foreign = new Mock.SessionContextMock({}, connection1);
  107. await sessionContext.initialize();
  108. await sessionContext.session!.kernel!.info;
  109. });
  110. beforeEach(() => {
  111. const parent = new TestParent();
  112. handler = new TestHandler({ sessionContext, parent });
  113. });
  114. afterEach(() => {
  115. handler.dispose();
  116. });
  117. afterAll(async () => {
  118. foreign.dispose();
  119. await sessionContext.shutdown();
  120. sessionContext.dispose();
  121. });
  122. describe('#constructor()', () => {
  123. it('should create a new foreign handler', () => {
  124. expect(handler).toBeInstanceOf(ForeignHandler);
  125. });
  126. });
  127. describe('#enabled', () => {
  128. it('should default to `false`', () => {
  129. expect(handler.enabled).toBe(false);
  130. });
  131. it('should allow foreign cells to be injected if `true`', async () => {
  132. handler.enabled = true;
  133. let called = false;
  134. handler.injected.connect(() => {
  135. called = true;
  136. });
  137. await foreign.session!.kernel!.requestExecute({ code: 'foo' }).done;
  138. Mock.emitIopubMessage(foreign, streamMsg);
  139. expect(called).toBe(true);
  140. });
  141. it('should reject foreign cells if `false`', async () => {
  142. handler.enabled = false;
  143. let called = false;
  144. handler.rejected.connect(() => {
  145. called = true;
  146. });
  147. await foreign.session!.kernel!.requestExecute({ code: 'foo' }).done;
  148. Mock.emitIopubMessage(foreign, streamMsg);
  149. expect(called).toBe(true);
  150. });
  151. });
  152. describe('#isDisposed', () => {
  153. it('should indicate whether the handler is disposed', () => {
  154. expect(handler.isDisposed).toBe(false);
  155. handler.dispose();
  156. expect(handler.isDisposed).toBe(true);
  157. });
  158. });
  159. describe('#session', () => {
  160. it('should be a client session object', () => {
  161. expect(handler.sessionContext.session!.path).toBeTruthy();
  162. });
  163. });
  164. describe('#parent', () => {
  165. it('should be set upon instantiation', () => {
  166. const parent = new TestParent();
  167. handler = new TestHandler({
  168. sessionContext: handler.sessionContext,
  169. parent
  170. });
  171. expect(handler.parent).toBe(parent);
  172. });
  173. });
  174. describe('#dispose()', () => {
  175. it('should dispose the resources held by the handler', () => {
  176. expect(handler.isDisposed).toBe(false);
  177. handler.dispose();
  178. expect(handler.isDisposed).toBe(true);
  179. });
  180. it('should be safe to call multiple times', () => {
  181. expect(handler.isDisposed).toBe(false);
  182. handler.dispose();
  183. handler.dispose();
  184. expect(handler.isDisposed).toBe(true);
  185. });
  186. });
  187. describe('#onIOPubMessage()', () => {
  188. it('should be called when messages come through', async () => {
  189. handler.enabled = false;
  190. let called = false;
  191. handler.received.connect(() => {
  192. called = true;
  193. });
  194. await foreign.session!.kernel!.requestExecute({ code: 'foo' }).done;
  195. Mock.emitIopubMessage(foreign, streamMsg);
  196. expect(called).toBe(true);
  197. });
  198. it('should inject relevant cells into the parent', async () => {
  199. handler.enabled = true;
  200. const parent = handler.parent as TestParent;
  201. expect(parent.widgets.length).toBe(0);
  202. let called = false;
  203. handler.injected.connect(() => {
  204. expect(parent.widgets.length).toBeGreaterThan(0);
  205. called = true;
  206. });
  207. await foreign.session!.kernel!.requestExecute({ code: 'foo' }).done;
  208. Mock.emitIopubMessage(foreign, streamMsg);
  209. expect(called).toBe(true);
  210. });
  211. it('should not reject relevant iopub messages', async () => {
  212. let called = false;
  213. let errored = false;
  214. handler.enabled = true;
  215. handler.rejected.connect(() => {
  216. errored = true;
  217. });
  218. handler.received.connect((sender, msg) => {
  219. if (KernelMessage.isClearOutputMsg(msg)) {
  220. called = true;
  221. }
  222. });
  223. await foreign.session!.kernel!.requestExecute({ code: 'foo' }).done;
  224. Mock.emitIopubMessage(foreign, clearMsg);
  225. expect(called).toBe(true);
  226. expect(errored).toBe(false);
  227. });
  228. });
  229. });
  230. });