debugger.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. breakpointsCommands: {
  82. registry,
  83. pause: ''
  84. },
  85. editorServices: {
  86. factoryService,
  87. mimeTypeService
  88. }
  89. });
  90. await act(async () => {
  91. Widget.attach(sidebar, document.body);
  92. MessageLoop.sendMessage(sidebar, Widget.Msg.UpdateRequest);
  93. await service.restoreState(true);
  94. });
  95. path = service.getCodeId(code);
  96. breakpoints = lines.map((line: number, id: number) => {
  97. return {
  98. id,
  99. line,
  100. verified: true,
  101. source: {
  102. path
  103. }
  104. };
  105. });
  106. const model = service.model as DebuggerModel;
  107. const currentFrameChanged = signalToPromise(
  108. model.callstack.currentFrameChanged
  109. );
  110. await act(async () => {
  111. await service.updateBreakpoints(code, breakpoints);
  112. connection!.kernel!.requestExecute({ code });
  113. await currentFrameChanged;
  114. });
  115. });
  116. afterAll(async () => {
  117. await connection.shutdown();
  118. connection.dispose();
  119. session.dispose();
  120. sidebar.dispose();
  121. });
  122. describe('#constructor()', () => {
  123. it('should create a new debugger sidebar', () => {
  124. expect(sidebar).toBeInstanceOf(Debugger.Sidebar);
  125. });
  126. });
  127. describe('Panel', () => {
  128. describe('Variable toolbar', () => {
  129. let toolbar: Element;
  130. beforeEach(() => {
  131. toolbar = sidebar.variables.node;
  132. });
  133. it('should have title', () => {
  134. const title = toolbar.querySelectorAll(
  135. 'div.jp-stack-panel-header > h2'
  136. );
  137. expect(title.length).toBe(1);
  138. expect(title[0].innerHTML).toContain('Variables');
  139. });
  140. it('should have two buttons', () => {
  141. const buttons = toolbar.querySelectorAll('button');
  142. expect(buttons.length).toBe(2);
  143. expect(buttons[0].title).toBe('Tree View');
  144. expect(buttons[1].title).toBe('Table View');
  145. });
  146. });
  147. describe('Callstack toolbar', () => {
  148. let toolbar: Element;
  149. beforeEach(() => {
  150. toolbar = sidebar.callstack.node;
  151. });
  152. it('should have title', () => {
  153. const title = toolbar.querySelectorAll(
  154. 'div.jp-stack-panel-header > h2'
  155. );
  156. console;
  157. expect(title.length).toBe(1);
  158. expect(title[0].innerHTML).toContain('Callstack');
  159. });
  160. it('should have six buttons', () => {
  161. const buttons = toolbar.querySelectorAll('button');
  162. expect(buttons.length).toBe(6);
  163. });
  164. });
  165. describe('Breakpoints toolbar', () => {
  166. let toolbar: Element;
  167. beforeEach(() => {
  168. toolbar = sidebar.breakpoints.node;
  169. });
  170. it('should have title', () => {
  171. const title = toolbar.querySelectorAll(
  172. 'div.jp-stack-panel-header > h2'
  173. );
  174. expect(title.length).toBe(1);
  175. expect(title[0].innerHTML).toContain('Breakpoints');
  176. });
  177. it('should have two buttons', () => {
  178. const buttons = toolbar.querySelectorAll('button');
  179. expect(buttons.length).toBe(2);
  180. });
  181. });
  182. describe('Source toolbar', () => {
  183. let toolbar: Element;
  184. beforeEach(() => {
  185. toolbar = sidebar.sources.node;
  186. });
  187. it('should have title', () => {
  188. const title = toolbar.querySelectorAll(
  189. 'div.jp-stack-panel-header > h2'
  190. );
  191. expect(title.length).toBe(1);
  192. expect(title[0].innerHTML).toContain('Source');
  193. });
  194. it('should have one button', () => {
  195. const buttons = toolbar.querySelectorAll('button');
  196. expect(buttons.length).toBe(1);
  197. });
  198. });
  199. });
  200. describe('#callstack', () => {
  201. it('should have a header and a body', () => {
  202. expect(sidebar.callstack.widgets.length).toEqual(2);
  203. });
  204. it('should have the jp-DebuggerCallstack class', () => {
  205. expect(sidebar.callstack.hasClass('jp-DebuggerCallstack')).toBe(true);
  206. });
  207. it('should have the debug buttons', () => {
  208. const node = sidebar.callstack.node;
  209. const items = node.querySelectorAll('button');
  210. expect(items.length).toEqual(6);
  211. items.forEach(item => {
  212. expect(Array.from(items[0].classList)).toEqual(
  213. expect.arrayContaining(['jp-ToolbarButtonComponent'])
  214. );
  215. });
  216. });
  217. it('should display the stack frames', () => {
  218. const node = sidebar.callstack.node;
  219. const items = node.querySelectorAll('.jp-DebuggerCallstack-body li');
  220. expect(items).toHaveLength(1);
  221. expect(items[0].innerHTML).toContain('module');
  222. expect(items[0].innerHTML).toContain('3'); // line for the first breakpoint
  223. });
  224. });
  225. describe('#breakpoints', () => {
  226. it('should have the jp-DebuggerBreakpoints class', () => {
  227. expect(sidebar.breakpoints.hasClass('jp-DebuggerBreakpoints')).toBe(true);
  228. });
  229. it('should contain the list of breakpoints', async () => {
  230. const node = sidebar.breakpoints.node;
  231. const items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  232. expect(items).toHaveLength(2);
  233. });
  234. it('should contain the path to the breakpoints', async () => {
  235. const node = sidebar.breakpoints.node;
  236. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-source');
  237. items.forEach(item => {
  238. // TODO: replace by toEqual when there is an alternative to the rtl
  239. // breakpoint display
  240. expect(item.innerHTML).toContain(path.slice(1));
  241. });
  242. });
  243. it('should contain the line number', async () => {
  244. const node = sidebar.breakpoints.node;
  245. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-line');
  246. await act(() => service.updateBreakpoints(code, breakpoints));
  247. items.forEach((item, i) => {
  248. const parsed = parseInt(item.innerHTML, 10);
  249. expect(parsed).toEqual(lines[i]);
  250. });
  251. });
  252. it('should be updated when new breakpoints are added', async () => {
  253. const node = sidebar.breakpoints.node;
  254. let items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  255. const len1 = items.length;
  256. const bps = breakpoints.concat([
  257. {
  258. id: 3,
  259. line: 4,
  260. verified: true,
  261. source: {
  262. path
  263. }
  264. }
  265. ]);
  266. await act(() => service.updateBreakpoints(code, bps));
  267. items = node.querySelectorAll('.jp-DebuggerBreakpoint');
  268. const len2 = items.length;
  269. expect(len2).toEqual(len1 + 1);
  270. });
  271. it('should contain the path after a restore', async () => {
  272. await service.restoreState(true);
  273. const node = sidebar.breakpoints.node;
  274. const items = node.querySelectorAll('.jp-DebuggerBreakpoint-source');
  275. items.forEach(item => {
  276. // TODO: replace by toEqual when there is an alternative to the rtl
  277. // breakpoint display
  278. expect(item.innerHTML).toContain(path.slice(1));
  279. });
  280. });
  281. });
  282. describe('#sources', () => {
  283. it('should have a header and a body', () => {
  284. expect(sidebar.sources.widgets.length).toEqual(2);
  285. });
  286. it('should display the source path in the header', () => {
  287. const body = sidebar.sources.widgets[0] as SourcesHeader;
  288. const children = toArray(body.children());
  289. const sourcePath = children[2].node.querySelector('span');
  290. expect(sourcePath!.innerHTML).toEqual(path);
  291. });
  292. it('should display the source code in the body', () => {
  293. const body = sidebar.sources.widgets[1] as SourcesBody;
  294. const children = toArray(body.children());
  295. const editor = children[0] as CodeEditorWrapper;
  296. expect(editor.model.value.text).toEqual(code);
  297. });
  298. });
  299. });