import { test, expect, Page } from "@playwright/test"; // End-to-end coverage of the Coding Training module, exercising the real app: // navigation → selection → generation → sandbox code submission (TDD) and // LLM-judge written submission, with separate XP awards. // Correct solutions for every executable problem in the bank, keyed by the // entry function name (read from sessionStorage at runtime so the test is // robust to which problem is randomly served). const SOLUTIONS: Record = { sumOfEvens: "function sumOfEvens(nums){ return nums.filter(n=>n%2===0).reduce((a,b)=>a+b,0); }", reverse: "function reverse(s){ return s.split('').reverse().join(''); }", twoSum: "function twoSum(nums,target){ const m=new Map(); for(let i=0;i { const raw = await page.evaluate(() => sessionStorage.getItem("coding:problem")); expect(raw).toBeTruthy(); const parsed = JSON.parse(raw!); return parsed.problem.entryFunction as string; } async function generate(page: Page, type: string, difficulty: string, language: string) { await page.goto("/coding"); await expect(page.getByRole("heading", { name: "Coding Training" })).toBeVisible(); await page.getByTestId(`type-${type}`).click(); // Difficulty await page.getByTestId("difficulty-select").click(); await page.getByRole("option", { name: new RegExp(difficulty, "i") }).click(); // Language await page.getByTestId("language-select").click(); await page.getByRole("option", { name: new RegExp(`^${language}$`, "i") }).click(); await page.getByTestId("generate-btn").click(); await expect(page).toHaveURL(/\/coding\/problem/); await expect(page.getByTestId("problem-title")).toBeVisible(); } test("navigation: Coding Training tab is reachable from the sidebar", async ({ page }) => { await page.goto("/"); await page.getByRole("link", { name: "Coding Training" }).first().click(); await expect(page).toHaveURL(/\/coding$/); await expect(page.getByRole("heading", { name: "Coding Training" })).toBeVisible(); }); test("generates a problem and shows the new problem page with formatted code", async ({ page }) => { await generate(page, "debugging", "easy", "JavaScript"); // The buggy code is shown with syntax highlighting (line-numbered code block). await expect(page.getByTestId("code-editor")).toBeVisible(); await expect(page.getByTestId("problem-prompt")).toBeVisible(); }); test("code submission: correct code passes the hidden tests and awards code XP", async ({ page }) => { await generate(page, "code-creation", "easy", "JavaScript"); const fn = await entryFunctionFromSession(page); const solution = SOLUTIONS[fn]; expect(solution, `missing E2E solution for ${fn}`).toBeTruthy(); // Type the correct solution into the sandbox editor. const editor = page.getByTestId("code-editor"); await editor.fill(solution); // Run against the example tests first. await page.getByTestId("run-btn").click(); await expect(page.getByTestId("run-output")).toContainText(/example tests passed/i); // Submit for grading against the full hidden suite. await page.getByTestId("submit-code-btn").click(); await expect(page.getByTestId("code-result-message")).toContainText(/solved|tests pass/i); await expect(page.getByTestId("xp-results-code")).toBeVisible(); await expect(page.getByTestId("xp-results-code")).toContainText(/XP/i); }); test("debugging: applying the fix turns the suite green and awards XP", async ({ page }) => { await generate(page, "debugging", "easy", "JavaScript"); const fn = await entryFunctionFromSession(page); const solution = SOLUTIONS[fn]; expect(solution).toBeTruthy(); await page.getByTestId("code-editor").fill(solution); await page.getByTestId("submit-code-btn").click(); await expect(page.getByTestId("xp-results-code")).toBeVisible(); }); test("incorrect code is rejected without awarding XP", async ({ page }) => { await generate(page, "code-creation", "easy", "JavaScript"); await page.getByTestId("code-editor").fill("function wrongName() { return -1; }"); await page.getByTestId("submit-code-btn").click(); await expect(page.getByTestId("code-result-message")).toContainText(/passed|did not compile/i); await expect(page.getByTestId("xp-results-code")).toHaveCount(0); }); test("written submission: correctly reviewing clean code awards written XP", async ({ page }) => { // The only easy code-review problem in the bank is the clean clamp(). await generate(page, "code-review", "easy", "JavaScript"); // The code under review is shown with syntax highlighting. await expect(page.getByTestId("review-code")).toBeVisible(); await page .getByTestId("written-answer") .fill("This function looks correct. There are no issues — it clamps the value properly."); await page.getByTestId("submit-written-btn").click(); await expect(page.getByTestId("written-result-message")).toContainText(/correct|accepted/i); await expect(page.getByTestId("xp-results-written")).toBeVisible(); await expect(page.getByTestId("xp-results-written")).toContainText(/WRITTEN XP/i); }); test("written submission: a thorough architecture answer is graded and awards XP", async ({ page }) => { await generate(page, "architecture", "medium", "JavaScript"); await page .getByTestId("written-answer") .fill( "I'd use a token bucket algorithm. The limit key is per API key granularity. " + "Counters live in a shared Redis store so it works across instances. Bursts are " + "smoothed by bucket capacity. If Redis is down we fail open to stay available.", ); await page.getByTestId("submit-written-btn").click(); await expect(page.getByTestId("written-result")).toBeVisible(); await expect(page.getByTestId("xp-results-written")).toBeVisible(); }); test("language selection is honoured in the generated problem", async ({ page }) => { await generate(page, "code-review", "medium", "Python"); const raw = await page.evaluate(() => sessionStorage.getItem("coding:problem")); const parsed = JSON.parse(raw!); expect(parsed.problem.language).toBe("python"); });