Download source/test/browser/debugger.spec.js from FormaLau/microduck-reachy-simulator: direct link, hf CLI and curl.
- Browser
- Download file 12.7 kB
-
https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/browser/debugger.spec.js
- Command line
-
hf download hf://spaces/FormaLau/microduck-reachy-simulator/source/test/browser/debugger.spec.js
-
curl -L -o debugger.spec.js https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/browser/debugger.spec.js
12.7 kB
| import { test, expect } from '@playwright/test'; | |
| const program = `#include <stdio.h> | |
| int twice(int n) { | |
| int result = n * 2; | |
| return result; | |
| } | |
| int main(void) { | |
| int x = 2; | |
| x = twice(x); | |
| int values[2] = {x, 7}; | |
| x += values[1]; | |
| printf("%d\\n", x); | |
| return 0; | |
| }`; | |
| test('real debug controls, breakpoints, watches and assessment without 3D', async ({ page }) => { | |
| test.setTimeout(240000); | |
| const errors = []; page.on('pageerror', error => errors.push(error.message)); | |
| await page.goto('/?boot=1'); | |
| await page.waitForFunction(() => window.__store?.getState().bootDone || window.__store?.getState().bootFailed); | |
| expect(await page.evaluate(() => window.__store.getState().bootFailed)).toBe(false); | |
| await page.evaluate(() => window.__gameApi.startEntrance()); | |
| await page.waitForFunction(() => window.rl && !window.rl.inputLocked); | |
| await page.getByRole('button', { name: 'CODE TRAINING', exact: true }).click(); | |
| await page.getByLabel('Analysis language').selectOption('fr'); | |
| const editor = page.getByLabel('C source code'); | |
| async function debug() { | |
| await editor.fill(program); | |
| const breakpoint = page.getByRole('button', { name: 'Breakpoint line 11', exact: true }); | |
| if (await breakpoint.getAttribute('aria-pressed') !== 'true') await breakpoint.click(); | |
| await page.getByRole('button', { name: 'Compile and debug', exact: true }).click(); | |
| const status = page.locator('.code-status'); | |
| await expect(status).toContainText('main : 7', { timeout: 90000 }); | |
| await expect(editor).toHaveAttribute('readonly', ''); | |
| await page.getByRole('button', { name: 'Ligne suivante', exact: true }).click(); | |
| await expect(status).toContainText('main : 8'); | |
| await expect(page.getByRole('table', { name: 'Variables', exact: true })).toContainText('xint2'); | |
| await page.getByLabel('Watch variable').fill('x'); | |
| await page.getByRole('button', { name: 'Ajouter', exact: true }).click(); | |
| await expect(page.locator('.debug-watch')).toContainText('x = 2'); | |
| await page.getByRole('button', { name: 'Entrer dans la fonction', exact: true }).click(); | |
| await expect(status).toContainText('twice : 3'); | |
| await expect(page.locator('.debug-watch')).toContainText('hors portée'); | |
| await page.getByRole('button', { name: 'Ligne suivante', exact: true }).click(); | |
| await expect(status).toContainText('twice : 4'); | |
| await expect(page.getByRole('table', { name: 'Variables', exact: true })).toContainText('resultint4'); | |
| await page.getByRole('button', { name: 'Sortir de la fonction', exact: true }).click(); | |
| await expect(status).toContainText('main : 9'); | |
| await expect(page.locator('.debug-watch')).toContainText('x = 4'); | |
| await page.getByRole('button', { name: 'Continuer', exact: true }).click(); | |
| await expect(status).toContainText('main : 11'); | |
| await expect(page.getByRole('table', { name: 'Variables', exact: true })).toContainText('[4, 7]'); | |
| await expect(page.locator('.debug-watch')).toContainText('x = 11'); | |
| await page.screenshot({ path: `test-results/debug-${await page.locator('body').evaluate(el => el.classList.contains('exam-active') ? 'exam' : 'training')}.png` }); | |
| await breakpoint.click(); | |
| await page.getByRole('button', { name: 'Continuer', exact: true }).click(); | |
| await expect(page.getByLabel('Program terminal')).toContainText('11\n'); | |
| await expect(page.getByLabel('Program terminal')).toContainText('exited with code 0'); | |
| await expect(editor).not.toHaveAttribute('readonly', ''); | |
| } | |
| await debug(); | |
| // Step over a call executes it entirely, and Stop releases a paused program. | |
| await page.getByRole('button', { name: 'Compile and debug', exact: true }).click(); | |
| await expect(page.locator('.code-status')).toContainText('main : 7', { timeout: 90000 }); | |
| await page.keyboard.press('F10'); | |
| await expect(page.locator('.code-status')).toContainText('main : 8'); | |
| await page.keyboard.press('F10'); | |
| await expect(page.locator('.code-status')).toContainText('main : 9'); | |
| await expect(page.locator('.debug-watch')).toContainText('x = 4'); | |
| await page.getByRole('button', { name: 'Stop', exact: true }).click(); | |
| await expect(editor).not.toHaveAttribute('readonly', ''); | |
| await editor.fill('int main(void) { for (;;) {} }'); | |
| await page.getByRole('button', { name: 'Compile and debug', exact: true }).click(); | |
| await expect(page.getByRole('button', { name: 'Continuer', exact: true })).toBeEnabled({ timeout: 90000 }); | |
| await page.getByRole('button', { name: 'Continuer', exact: true }).click(); | |
| await page.getByRole('button', { name: 'Stop', exact: true }).click(); | |
| await expect(editor).not.toHaveAttribute('readonly', ''); | |
| const trainingWidth = (await editor.boundingBox()).width; | |
| await page.getByRole('button', { name: 'Évaluation', exact: true }).click(); | |
| await page.getByLabel('Nom et prénom').fill('Debug Test'); | |
| await page.getByLabel('Code d’accès').fill('browser-test-code'); | |
| await page.getByRole('button', { name: 'Accéder à l’examen' }).click(); | |
| await page.getByRole('button', { name: '1. Bonjour', exact: true }).click(); | |
| await expect(page.locator('.game-canvas')).toHaveCSS('visibility', 'hidden'); | |
| expect((await editor.boundingBox()).width).toBeGreaterThan(trainingWidth); | |
| const before = await page.evaluate(() => ({ time: window.rl.data.time, frames: window.rl.renderFrameCount })); | |
| await debug(); | |
| expect(await page.evaluate(() => ({ time: window.rl.data.time, frames: window.rl.renderFrameCount }))).toEqual(before); | |
| await editor.fill('int main() {\nint x = 1;\nif (x) {\nx++;\n}\n}'); | |
| await editor.press('Control+f'); | |
| await expect(editor).toHaveValue('int main() {\n int x = 1;\n if (x) {\n x++;\n }\n}'); | |
| await editor.fill('if (1)'); await editor.press('End'); await editor.press('Enter'); | |
| await expect(editor).toHaveValue('if (1)\n '); | |
| await editor.press('Tab'); await expect(editor).toHaveValue('if (1)\n '); | |
| expect(errors).toEqual([]); | |
| }); | |
| test('instrumentation preserves C control flow, recursion, scopes and interactive input', async ({ page }) => { | |
| test.setTimeout(180000); | |
| await page.goto('/'); | |
| const result = await page.evaluate(async () => { | |
| const { default: DebugWorker } = await import('/src/training/compiler.worker.js?worker'); | |
| const worker = new DebugWorker(); | |
| const source = `#include <stdio.h> | |
| int total = 0; | |
| int factorial(int n) { if(n <= 1) return 1; return n * factorial(n - 1); } | |
| int main() { | |
| int n; scanf("%d", &n); | |
| for (int i=0;i<4;i++) { if(i==1) continue; total += i; } | |
| int i=0; while(i<2) i++; | |
| do { i--; } while(i>0); | |
| for (;;) { total++; break; } | |
| if (n) if (n==4) total++; else total--; | |
| { int total=50; printf("inner=%d\\n",total); } | |
| switch(n) { case 4: total+=2; break; default: total=0; } | |
| printf("%d %d %d\\n", total, factorial(n), i); | |
| return 0; | |
| }`; | |
| const points = []; let output = ''; | |
| try { | |
| return await new Promise((resolve, reject) => { | |
| const timeout = setTimeout(() => reject(new Error('Debug worker timed out')), 90000); | |
| worker.onerror = e => reject(new Error(e.message)); | |
| worker.onmessage = ({ data }) => { | |
| if (data.type === 'compiled') { if (!data.ok) reject(new Error(data.diagnostics)); else worker.postMessage({ type: 'run' }); } | |
| if (data.type === 'debug-paused') { points.push(data); worker.postMessage({ type: 'reply', value: 'into' }); } | |
| if (data.type === 'output') { output += data.text; worker.postMessage({ type: 'reply' }); } | |
| if (data.type === 'input') worker.postMessage({ type: 'reply', value: '4\n' }); | |
| if (data.type === 'flush') worker.postMessage({ type: 'reply' }); | |
| if (data.type === 'error') reject(new Error(data.text)); | |
| if (data.type === 'exited') { clearTimeout(timeout); resolve({ points, output, code: data.code }); } | |
| }; | |
| worker.postMessage({ type: 'compile', debug: true, source, baseUrl: location.origin + '/' }); | |
| }); | |
| } finally { worker.terminate(); } | |
| }); | |
| expect(result.code).toBe(0); | |
| expect(result.output).toBe('inner=50\n9 24 0\n'); | |
| expect(Math.max(...result.points.map(p => p.depth))).toBe(5); | |
| expect(result.points.some(p => p.variables.some(v => v.name === 'total' && v.value === 50))).toBe(true); | |
| expect(result.points.filter(p => p.function === 'factorial').map(p => p.variables.find(v => v.name === 'n')?.value)).toContain(1); | |
| }); | |
| test('compact debugger and expandable arrays expose every index in training and assessment', async ({ page }) => { | |
| test.setTimeout(240000); | |
| await page.goto('/?boot=1'); | |
| await page.waitForFunction(() => window.__store?.getState().bootDone); | |
| await page.evaluate(() => window.__gameApi.startEntrance()); | |
| await page.waitForFunction(() => window.rl && !window.rl.inputLocked); | |
| await page.getByRole('button', { name: 'CODE TRAINING', exact: true }).click(); | |
| await page.getByLabel('Analysis language').selectOption('fr'); | |
| const editor = page.getByLabel('C source code'); | |
| const source = `int main(void) { | |
| int values[130]; | |
| for (int i=0; i<130; i++) values[i] = i * 3; | |
| int matrix[2][3] = {{1,2,3},{4,5,6}}; | |
| int marker = 0; | |
| values[129] += 1; | |
| marker++; | |
| return 0; | |
| }`; | |
| for (const assessment of [false, true]) { | |
| if (assessment) { | |
| await page.getByRole('button', { name: 'Évaluation', exact: true }).click(); | |
| await page.getByLabel('Nom et prénom').fill('Array Test'); | |
| await page.getByLabel('Code d’accès').fill('browser-test-code'); | |
| await page.getByRole('button', { name: 'Accéder à l’examen' }).click(); | |
| await page.getByRole('button', { name: '1. Bonjour', exact: true }).click(); | |
| } | |
| await editor.fill(source); | |
| const bp = page.getByRole('button', { name: 'Breakpoint line 6', exact: true }); | |
| if (await bp.getAttribute('aria-pressed') !== 'true') await bp.click(); | |
| await page.getByRole('button', { name: 'Compile and debug', exact: true }).click(); | |
| await expect(page.locator('.code-status')).toContainText('main : 2', { timeout: 90000 }); | |
| await page.getByRole('button', { name: 'Continuer', exact: true }).click(); | |
| await expect(page.locator('.code-status')).toContainText('main : 6'); | |
| const height = (await editor.boundingBox()).height; | |
| expect(height).toBeGreaterThan(420); | |
| expect((await page.getByLabel('Breakpoints', { exact: true }).boundingBox()).width).toBeLessThan(50); | |
| expect((await page.getByLabel('Program terminal').boundingBox()).height).toBeLessThanOrEqual(50); | |
| expect((await page.getByLabel('Debugger', { exact: true }).boundingBox()).height).toBeLessThanOrEqual(140); | |
| for (const name of ['values', 'matrix']) { | |
| await page.getByLabel('Watch variable').fill(name); | |
| await page.getByRole('button', { name: 'Ajouter', exact: true }).click(); | |
| } | |
| const watch = page.locator('.debug-watch').filter({ has: page.getByRole('button', { name: 'Remove watch values', exact: true }) }); | |
| await watch.getByRole('button', { name: 'Déplier values', exact: true }).click(); | |
| await expect(watch).toContainText('[0] = 0'); | |
| await expect(watch).toContainText('[63] = 189'); | |
| await watch.getByRole('button', { name: 'Éléments suivants de values', exact: true }).click(); | |
| await expect(watch).toContainText('[64] = 192'); | |
| await watch.getByLabel('Indice dans values', { exact: true }).fill('129'); | |
| await watch.getByRole('button', { name: 'Aller', exact: true }).click(); | |
| await expect(watch).toContainText('[129] = 387'); | |
| const matrix = page.locator('.debug-watch').filter({ has: page.getByRole('button', { name: 'Remove watch matrix', exact: true }) }); | |
| await matrix.getByRole('button', { name: 'Déplier matrix', exact: true }).click(); | |
| await matrix.getByRole('button', { name: 'Déplier matrix[1]', exact: true }).click(); | |
| await expect(matrix).toContainText('[2] = 6'); | |
| expect((await editor.boundingBox()).height).toBeGreaterThanOrEqual(height - 2); | |
| await page.screenshot({ path: `test-results/debug-arrays-${assessment ? 'exam' : 'training'}.png` }); | |
| await page.getByRole('button', { name: 'Ligne suivante', exact: true }).click(); | |
| await expect(page.locator('.code-status')).toContainText('main : 7'); | |
| // Expanded paths survive stepping, but values are read from the new pause. | |
| await watch.getByLabel('Indice dans values', { exact: true }).fill('129'); | |
| await watch.getByRole('button', { name: 'Aller', exact: true }).click(); | |
| await expect(watch).toContainText('[129] = 388'); | |
| await watch.getByRole('button', { name: 'Replier values', exact: true }).click(); | |
| await expect(watch.locator('.debug-array-element')).toHaveCount(0); | |
| await page.getByRole('button', { name: 'Stop', exact: true }).click(); | |
| } | |
| }); | |