debugger.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. JupyterServer,
  15. signalToPromise
  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. evaluate: ''
  80. },
  81. editorServices: {
  82. factoryService,
  83. mimeTypeService
  84. }
  85. });
  86. await act(async () => {
  87. Widget.attach(sidebar, document.body);
  88. MessageLoop.sendMessage(sidebar, Widget.Msg.UpdateRequest);
  89. await service.restoreState(true);
  90. });
  91. path = service.getCodeId(code);
  92. breakpoints = lines.map((line: number, id: number) => {
  93. return {
  94. id,
  95. line,
  96. verified: true,
  97. source: {
  98. path
  99. }
  100. };
  101. });
  102. const model = service.model as DebuggerModel;
  103. const currentFrameChanged = signalToPromise(
  104. model.callstack.currentFrameChanged
  105. );
  106. await act(async () => {
  107. await service.updateBreakpoints(code, breakpoints);
  108. connection!.kernel!.requestExecute({ code });
  109. await currentFrameChanged;
  110. });
  111. });
  112. afterAll(async () => {
  113. await connection.shutdown();
  114. connection.dispose();
  115. session.dispose();
  116. sidebar.dispose();
  117. });
  118. describe('#constructor()', () => {
  119. it('should create a new debugger sidebar', () => {
  120. expect(sidebar).toBeInstanceOf(Debugger.Sidebar);
  121. });
  122. });
  123. describe('#callstack', () => {
  124. it('should have a header and a body', () => {
  125. expect(sidebar.callstack.widgets.length).toEqual(2);
  126. });
  127. it('should have the jp-DebuggerCallstack class', () => {
  128. expect(sidebar.callstack.hasClass('jp-DebuggerCallstack')).toBe(true);
  129. });
  130. it('should have the debug buttons', () => {
  131. const node = sidebar.callstack.node;
  132. const items = node.querySelectorAll('button');
  133. expect(items.length).toEqual(6);
  134. items.forEach(item => {
  135. expect(Array.from(items[0].classList)).toEqual(
  136. expect.arrayContaining(['jp-ToolbarButtonComponent'])
  137. );
  138. });
  139. });
  140. it('should display the stack frames', () => {
  141. const node = sidebar.callstack.node;
  142. const items = node.querySelectorAll('.jp-DebuggerCallstack-body li');
  143. expect(items).toHaveLength(1);
  144. expect(items[0].innerHTML).toContain('module');
  145. expect(items[0].innerHTML).toContain('3'); // line for the first breakpoint
  146. });
  147. });
  148. describe('#breakpoints', () => {
  149. it('should have the jp-DebuggerBreakpoints class', () => {
  150. expect(sidebar.breakpoints.hasClass('jp-DebuggerBreakpoints')).toBe(true);
  151. });
  152. it('should contain the list of breakpoints', async () => {
  153. const node = sidebar.breakpoints.node;
  154. const items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  155. expect(items).toHaveLength(2);
  156. });
  157. it('should contain the path to the breakpoints', async () => {
  158. const node = sidebar.breakpoints.node;
  159. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-source');
  160. items.forEach(item => {
  161. // TODO: replace by toEqual when there is an alternative to the rtl
  162. // breakpoint display
  163. expect(item.innerHTML).toContain(path.slice(1));
  164. });
  165. });
  166. it('should contain the line number', async () => {
  167. const node = sidebar.breakpoints.node;
  168. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-line');
  169. await act(() => service.updateBreakpoints(code, breakpoints));
  170. items.forEach((item, i) => {
  171. const parsed = parseInt(item.innerHTML, 10);
  172. expect(parsed).toEqual(lines[i]);
  173. });
  174. });
  175. it('should be updated when new breakpoints are added', async () => {
  176. const node = sidebar.breakpoints.node;
  177. let items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  178. const len1 = items.length;
  179. const bps = breakpoints.concat([
  180. {
  181. id: 3,
  182. line: 4,
  183. verified: true,
  184. source: {
  185. path
  186. }
  187. }
  188. ]);
  189. await act(() => service.updateBreakpoints(code, bps));
  190. items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  191. const len2 = items.length;
  192. expect(len2).toEqual(len1 + 1);
  193. });
  194. it('should contain the path after a restore', async () => {
  195. await service.restoreState(true);
  196. const node = sidebar.breakpoints.node;
  197. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-source');
  198. items.forEach(item => {
  199. // TODO: replace by toEqual when there is an alternative to the rtl
  200. // breakpoint display
  201. expect(item.innerHTML).toContain(path.slice(1));
  202. });
  203. });
  204. });
  205. describe('#sources', () => {
  206. it('should have a header and a body', () => {
  207. expect(sidebar.sources.widgets.length).toEqual(2);
  208. });
  209. it('should display the source path in the header', () => {
  210. const body = sidebar.sources.widgets[0] as SourcesHeader;
  211. const children = toArray(body.children());
  212. const sourcePath = children[2].node.querySelector('span');
  213. expect(sourcePath!.innerHTML).toEqual(path);
  214. });
  215. it('should display the source code in the body', () => {
  216. const body = sidebar.sources.widgets[1] as SourcesBody;
  217. const children = toArray(body.children());
  218. const editor = children[0] as CodeEditorWrapper;
  219. expect(editor.model.value.text).toEqual(code);
  220. });
  221. });
  222. });