debugger.spec.ts 6.9 KB

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