File size: 8,120 Bytes
022cb3a
 
 
 
 
 
 
 
 
 
 
eb24e49
022cb3a
 
16215cb
022cb3a
d6a8383
 
 
022cb3a
 
 
 
 
 
 
 
 
 
 
f5375f2
022cb3a
 
d6a8383
 
 
 
16215cb
 
 
b09157d
 
16215cb
022cb3a
 
 
16215cb
022cb3a
 
 
 
 
 
 
 
 
 
d6a8383
022cb3a
 
 
 
 
 
 
 
73bd3c5
022cb3a
d6a8383
 
022cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6a8383
16215cb
 
 
 
 
 
 
 
 
d6a8383
 
 
 
 
 
 
 
 
16215cb
 
 
022cb3a
 
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
import { test, expect } from "@playwright/test";

test("real C compilation, interactive input, diagnostics and cancellation", 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);
  // Keep the real Microduck animation. Replace Qwen speech with a deterministic
  // completion callback so we can verify every utterance and its ordering.
  await page.evaluate(() => {
    window.trainingPageIdentity = crypto.randomUUID();
    window.trainingEvents = [];
    window.trainingReachyEmotions = [];
    const reachyEmotion = window.__gameApi.playReachyEmotion;
    window.__gameApi.playReachyEmotion = async (name) => { window.trainingReachyEmotions.push(name); return reachyEmotion(name); };
    const emotion = window.__gameApi.playMicroduckEmotion;
    window.__gameApi.playMicroduckEmotion = async (name) => {
      window.trainingEvents.push(`emotion:${name}`);
      await emotion(name);
      window.trainingEvents.push(`done:${name}`);
    };
    window.__gameApi.reachySay = async (text) => { window.trainingEvents.push(`say:${text}`); };
  });
  await page.getByRole("button", { name: "CODE TRAINING", exact: true }).click();
  const editor = page.getByRole("textbox", { name: "C source code" });
  const compile = page.getByRole("button", { name: "Compile", exact: true });
  const run = page.getByRole("button", { name: "Compile and run", exact: true });
  await expect(page.getByLabel("Programming language")).toHaveValue("c");
  await expect(compile).toBeEnabled();
  for (const title of await page.locator(".sim-panel-title").all()) { await expect(title).toHaveAttribute("aria-expanded", "false"); await expect(title).toBeDisabled(); }
  await page.screenshot({ path: "test-results/code-training-staging.png" });
  await expect(page.getByLabel("Analysis language").locator("option")).toHaveText(["En", "Fr", "Es", "Zh"]);
  await expect(page.locator('select[aria-label="Personnalité de Reachy"]')).toHaveCount(0);
  const identity = await page.evaluate(() => window.trainingPageIdentity);
  for (const button of await page.locator(".sim-panel-content button").all()) await expect(button).toBeDisabled();
  const cameraBefore = await page.evaluate(() => window.rl.camera.position.toArray());
  await page.mouse.move(1400, 280); await page.mouse.down();
  await page.mouse.move(1320, 310, { steps: 10 }); await page.mouse.up();
  await expect.poll(() => page.evaluate((before) => window.rl.camera.position.distanceTo({ x: before[0], y: before[1], z: before[2] }), cameraBefore)).toBeGreaterThan(0.01);
  await editor.fill('#include <stdio.h>\nint main(void) { int n; char word[32]; printf("Number: "); if(scanf("%d", &n)!=1) return 1; printf("Word: "); if(scanf("%31s", word)!=1) return 2; printf("%s %d\\n", word, n * 2); puts("Goodbye!"); return 0; }');
  await compile.click();
  await expect(run).toBeEnabled({ timeout: 45000 });
  expect(await page.evaluate(() => window.trainingPageIdentity)).toBe(identity);
  await run.click();
  const input = page.getByRole("textbox", { name: "Terminal input" });
  await expect(input).toBeEnabled({ timeout: 15000 });
  await page.screenshot({ path: "test-results/code-training-interactive.png" });
  await input.fill("21"); await input.press("Enter");
  await expect(input).toBeEnabled();
  await input.fill("Kwack"); await input.press("Enter");
  await expect(page.getByLabel("Program terminal")).toContainText("Kwack 42");
  await expect(page.getByLabel("Program terminal")).toContainText("exited with code 0");
  const events = await page.evaluate(() => window.trainingEvents);
  expect(events.slice(0, 2)).toEqual(["emotion:excited", "done:excited"]);
  expect(events.filter((value) => value.startsWith("say:")).map((value) => value.slice(4)).join("")).toBe("Number: Word: Kwack 42\nGoodbye!\n");
  await page.screenshot({ path: "test-results/code-training.png" });

  await editor.fill('int main(void) { return 0 }');
  await expect(run).toBeDisabled();
  await compile.click();
  await expect(page.getByRole("dialog").getByRole("status")).toContainText("Fix the errors", { timeout: 30000 });
  await expect(page.getByText("Compiler diagnostics", { exact: true })).toBeVisible();
  await expect(page.locator(".code-diagnostics")).toContainText("semicolon is missing");
  const failureEvents = await page.evaluate(() => window.trainingEvents.slice(-3));
  expect(failureEvents[0]).toBe("emotion:mock");
  expect(failureEvents[1]).toBe("done:mock");
  expect(failureEvents[2]).toContain("say:Compilation failed");
  await expect(run).toBeDisabled();

  await editor.fill('#include <stdio.h>\nint main(void) { int c = getchar(); printf("EOF: %d\\n", c == EOF); return 7; }');
  await compile.click(); await expect(run).toBeEnabled({ timeout: 30000 });
  await run.click(); await expect(input).toBeEnabled();
  await page.getByRole("button", { name: "EOF", exact: true }).click();
  await expect(page.getByLabel("Program terminal")).toContainText("EOF: 1");
  await expect(page.getByLabel("Program terminal")).toContainText("exited with code 7");

  await editor.fill('int main(void) { __builtin_trap(); }');
  await compile.click(); await expect(run).toBeEnabled({ timeout: 30000 });
  await run.click();
  await expect(page.getByRole("dialog").getByRole("status")).toContainText("unreachable");
  await expect(compile).toBeEnabled();

  await editor.fill("int main(void) { for (;;) {} }");
  await compile.click(); await expect(run).toBeEnabled({ timeout: 30000 });
  await run.click();
  await page.getByRole("dialog").getByRole("button", { name: "Stop", exact: true }).click();
  await expect(compile).toBeEnabled();
  await expect(run).toBeDisabled();
  await page.getByRole("button", { name: "Close code training" }).click();
  await expect(page.getByRole("dialog")).not.toBeVisible();
  await page.getByRole("button", { name: "CODE TRAINING", exact: true }).click();
  await expect(editor).toHaveValue("int main(void) { for (;;) {} }");
  await expect(page.locator(".sim-panel-content").first()).toBeHidden();
  const downloadSource = '#include <stdio.h>\nint main(void) { printf("été\\n"); return 0; }\n';
  await editor.fill(downloadSource);
  const downloadEvent = page.waitForEvent("download");
  await page.getByRole("button", { name: "Enregistrer .c" }).click();
  const download = await downloadEvent;
  expect(download.suggestedFilename()).toBe("main.c");
  const { readFile } = await import("node:fs/promises");
  expect(await readFile(await download.path(), "utf8")).toBe(downloadSource);
  await editor.fill('#include <stdio.h>\nint main(void) { int unused; char s[32]; scanf("%s", s); return 0; }');
  await page.getByRole("button", { name: "Analyse", exact: true }).click();
  await expect(page.locator(".code-status")).toContainText("Analyse terminée", { timeout: 45000 });
  await expect(page.locator(".code-analysis")).toContainText("Limit the input width");
  await expect(page.locator(".code-analysis")).toContainText("Check scanf's return value");
  await expect(page.locator(".code-analysis")).toContainText("Remove the unused value");
  expect(await page.evaluate(() => window.trainingReachyEmotions)).toContain("proud1");
  expect(await page.evaluate(() => window.trainingReachyEmotions)).toContain("reprimand1");
  const analysisEvents = await page.evaluate(() => window.trainingEvents.filter((event) => event.startsWith("say:") && event.includes("Let's improve")));
  expect(analysisEvents).toHaveLength(1);
  await page.screenshot({ path: "test-results/code-training-analysis.png" });
  await page.getByRole("button", { name: "Close code training" }).click();
  await expect(page.getByRole("button", { name: "Home", exact: true })).toBeEnabled();
  expect(errors).toEqual([]);
});