File size: 12,745 Bytes
b09157d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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();
  }
});