microduck-reachy-simulator / source /test /browser /code-training.spec.js
laureen
Publish static Space from 6953707
b968036
Raw History Blame Contribute Delete
8.12 kB
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([]);
});