File size: 7,238 Bytes
9580dce | 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 | 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<string, string> = {
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<nums.length;i++){ const n=target-nums[i]; if(m.has(n)) return [m.get(n),i]; m.set(nums[i],i);} return []; }",
isBalanced:
"function isBalanced(s){ const p={')':'(',']':'[','}':'{'}; const st=[]; for(const c of s){ if(c==='('||c==='['||c==='{') st.push(c); else if(p[c]){ if(st.pop()!==p[c]) return false; } } return st.length===0; }",
lengthOfLongestSubstring:
"function lengthOfLongestSubstring(s){ const seen=new Set(); let l=0,best=0; for(let r=0;r<s.length;r++){ while(seen.has(s[r])){ seen.delete(s[l]); l++; } seen.add(s[r]); best=Math.max(best,r-l+1);} return best; }",
factorial: "function factorial(n){ let r=1; for(let i=1;i<=n;i++) r*=i; return r; }",
binarySearch:
"function binarySearch(arr,t){ let lo=0,hi=arr.length-1; while(lo<=hi){ const m=Math.floor((lo+hi)/2); if(arr[m]===t) return m; if(arr[m]<t) lo=m+1; else hi=m-1; } return -1; }",
maxSubArray:
"function maxSubArray(nums){ let best=-Infinity,cur=0; for(const n of nums){ cur=Math.max(n,cur+n); best=Math.max(best,cur);} return best; }",
};
async function entryFunctionFromSession(page: Page): Promise<string> {
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");
});
|