debugger.spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { init } from './utils';
  4. init();
  5. import { act } from 'react-dom/test-utils';
  6. import { CodeEditorWrapper } from '@jupyterlab/codeeditor';
  7. import {
  8. CodeMirrorEditorFactory,
  9. CodeMirrorMimeTypeService
  10. } from '@jupyterlab/codemirror';
  11. import { KernelSpecManager, Session } from '@jupyterlab/services';
  12. import {
  13. createSession,
  14. signalToPromise,
  15. JupyterServer
  16. } from '@jupyterlab/testutils';
  17. import { toArray } from '@lumino/algorithm';
  18. import { CommandRegistry } from '@lumino/commands';
  19. import { UUID } from '@lumino/coreutils';
  20. import { MessageLoop } from '@lumino/messaging';
  21. import { Widget } from '@lumino/widgets';
  22. import { Debugger } from '../src/debugger';
  23. import { DebuggerService } from '../src/service';
  24. import { DebuggerModel } from '../src/model';
  25. import { SourcesBody } from '../src/panels/sources/body';
  26. import { SourcesHeader } from '../src/panels/sources/header';
  27. import { IDebugger } from '../src/tokens';
  28. /**
  29. * A test sidebar.
  30. */
  31. class TestSidebar extends Debugger.Sidebar {}
  32. const server = new JupyterServer();
  33. beforeAll(async () => {
  34. jest.setTimeout(20000);
  35. await server.start();
  36. });
  37. afterAll(async () => {
  38. await server.shutdown();
  39. });
  40. describe('Debugger', () => {
  41. const specsManager = new KernelSpecManager();
  42. const config = new Debugger.Config();
  43. const service = new DebuggerService({ specsManager, config });
  44. const registry = new CommandRegistry();
  45. const factoryService = new CodeMirrorEditorFactory();
  46. const mimeTypeService = new CodeMirrorMimeTypeService();
  47. const lines = [3, 5];
  48. const code = [
  49. 'i = 0',
  50. 'i += 1',
  51. 'i += 1',
  52. 'j = i**2',
  53. 'j += 1',
  54. 'print(i, j)'
  55. ].join('\n');
  56. let breakpoints: IDebugger.IBreakpoint[];
  57. let session: Debugger.Session;
  58. let path: string;
  59. let connection: Session.ISessionConnection;
  60. let sidebar: TestSidebar;
  61. beforeAll(async () => {
  62. connection = await createSession({
  63. name: '',
  64. type: 'test',
  65. path: UUID.uuid4()
  66. });
  67. await connection.changeKernel({ name: 'xpython' });
  68. session = new Debugger.Session({ connection });
  69. service.session = session;
  70. sidebar = new TestSidebar({
  71. service,
  72. callstackCommands: {
  73. registry,
  74. continue: '',
  75. terminate: '',
  76. next: '',
  77. stepIn: '',
  78. stepOut: ''
  79. },
  80. editorServices: {
  81. factoryService,
  82. mimeTypeService
  83. }
  84. });
  85. await act(async () => {
  86. Widget.attach(sidebar, document.body);
  87. MessageLoop.sendMessage(sidebar, Widget.Msg.UpdateRequest);
  88. await service.restoreState(true);
  89. });
  90. path = service.getCodeId(code);
  91. breakpoints = lines.map((line: number, id: number) => {
  92. return {
  93. id,
  94. line,
  95. verified: true,
  96. source: {
  97. path
  98. }
  99. };
  100. });
  101. const model = service.model as DebuggerModel;
  102. const currentFrameChanged = signalToPromise(
  103. model.callstack.currentFrameChanged
  104. );
  105. await act(async () => {
  106. await service.updateBreakpoints(code, breakpoints);
  107. connection!.kernel!.requestExecute({ code });
  108. await currentFrameChanged;
  109. });
  110. });
  111. afterAll(async () => {
  112. await connection.shutdown();
  113. connection.dispose();
  114. session.dispose();
  115. sidebar.dispose();
  116. });
  117. describe('#constructor()', () => {
  118. it('should create a new debugger sidebar', () => {
  119. expect(sidebar).toBeInstanceOf(Debugger.Sidebar);
  120. });
  121. });
  122. describe('#callstack', () => {
  123. it('should have a header and a body', () => {
  124. expect(sidebar.callstack.widgets.length).toEqual(2);
  125. });
  126. it('should have the jp-DebuggerCallstack class', () => {
  127. expect(sidebar.callstack.hasClass('jp-DebuggerCallstack')).toBe(true);
  128. });
  129. it('should have the debug buttons', () => {
  130. const node = sidebar.callstack.node;
  131. const items = node.querySelectorAll('button');
  132. expect(items.length).toEqual(5);
  133. items.forEach(item => {
  134. expect(Array.from(items[0].classList)).toEqual(
  135. expect.arrayContaining(['jp-ToolbarButtonComponent'])
  136. );
  137. });
  138. });
  139. it('should display the stack frames', () => {
  140. const node = sidebar.callstack.node;
  141. const items = node.querySelectorAll('.jp-DebuggerCallstack-body li');
  142. expect(items).toHaveLength(1);
  143. expect(items[0].innerHTML).toContain('module');
  144. expect(items[0].innerHTML).toContain('3'); // line for the first breakpoint
  145. });
  146. });
  147. describe('#breakpoints', () => {
  148. it('should have the jp-DebuggerBreakpoints class', () => {
  149. expect(sidebar.breakpoints.hasClass('jp-DebuggerBreakpoints')).toBe(true);
  150. });
  151. it('should contain the list of breakpoints', async () => {
  152. const node = sidebar.breakpoints.node;
  153. const items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  154. expect(items).toHaveLength(2);
  155. });
  156. it('should contain the path to the breakpoints', async () => {
  157. const node = sidebar.breakpoints.node;
  158. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-source');
  159. items.forEach(item => {
  160. // TODO: replace by toEqual when there is an alternative to the rtl
  161. // breakpoint display
  162. expect(item.innerHTML).toContain(path.slice(1));
  163. });
  164. });
  165. it('should contain the line number', async () => {
  166. const node = sidebar.breakpoints.node;
  167. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-line');
  168. await act(() => service.updateBreakpoints(code, breakpoints));
  169. items.forEach((item, i) => {
  170. const parsed = parseInt(item.innerHTML, 10);
  171. expect(parsed).toEqual(lines[i]);
  172. });
  173. });
  174. it('should be updated when new breakpoints are added', async () => {
  175. const node = sidebar.breakpoints.node;
  176. let items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  177. const len1 = items.length;
  178. const bps = breakpoints.concat([
  179. {
  180. id: 3,
  181. line: 4,
  182. verified: true,
  183. source: {
  184. path
  185. }
  186. }
  187. ]);
  188. await act(() => service.updateBreakpoints(code, bps));
  189. items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  190. const len2 = items.length;
  191. expect(len2).toEqual(len1 + 1);
  192. });
  193. });
  194. describe('#sources', () => {
  195. it('should have a header and a body', () => {
  196. expect(sidebar.sources.widgets.length).toEqual(2);
  197. });
  198. it('should display the source path in the header', () => {
  199. const body = sidebar.sources.widgets[0] as SourcesHeader;
  200. const children = toArray(body.children());
  201. const sourcePath = children[2].node.querySelector('span');
  202. expect(sourcePath!.innerHTML).toEqual(path);
  203. });
  204. it('should display the source code in the body', () => {
  205. const body = sidebar.sources.widgets[1] as SourcesBody;
  206. const children = toArray(body.children());
  207. const editor = children[0] as CodeEditorWrapper;
  208. expect(editor.model.value.text).toEqual(code);
  209. });
  210. });
  211. });