import { NextRequest } from "next/server"; import { POST as generate } from "../generate/route"; import { POST as runCode } from "../run/route"; import { POST as submitCode } from "../submit-code/route"; import { POST as submitWritten } from "../submit-written/route"; import { PROBLEM_BANK } from "@/lib/coding/bank"; // Integration tests exercise the real route handlers against the seeded // dev.db. They assert on the public JSON interface only. No ANTHROPIC_API_KEY // is set in CI, so written grading exercises the offline heuristic path. function post(url: string, body: unknown): NextRequest { return new NextRequest(`http://localhost${url}`, { method: "POST", body: JSON.stringify(body), headers: { "Content-Type": "application/json" }, }); } async function gen(type: string, difficulty: string, language: string) { const res = await generate(post("/api/coding/generate", { type, difficulty, language })); return res.json(); } /** The bank id embedded in an instanceId (before the "::uuid"). */ function bankIdOf(instanceId: string): string { return instanceId.split("::")[0]; } function referenceFor(instanceId: string): string { const bank = PROBLEM_BANK.find((p) => p.id === bankIdOf(instanceId)); if (!bank || (bank.type !== "code-creation" && bank.type !== "debugging")) { throw new Error("not an executable bank problem"); } return bank.referenceSolution; } describe("POST /api/coding/generate", () => { it("returns a public problem with the answer key stripped", async () => { const body = await gen("code-creation", "easy", "javascript"); expect(body.instanceId).toBeTruthy(); expect(body.problem.type).toBe("code-creation"); expect(body.problem).not.toHaveProperty("referenceSolution"); // only non-hidden example tests are exposed for (const t of body.problem.tests ?? []) expect(t.hidden).toBeFalsy(); expect(body.meta.executable).toBe(true); }); it("strips issues from code-review problems", async () => { const body = await gen("code-review", "medium", "javascript"); expect(body.problem).not.toHaveProperty("issues"); expect(body.problem.code).toBeTruthy(); expect(body.meta.gradedBy).toBe("llm-judge"); }); it("rejects an invalid type with 400", async () => { const res = await generate(post("/api/coding/generate", { type: "nope", difficulty: "easy", language: "javascript" })); expect(res.status).toBe(400); }); }); describe("POST /api/coding/run", () => { it("runs the reference solution against the public tests and passes", async () => { const g = await gen("code-creation", "easy", "javascript"); const res = await runCode(post("/api/coding/run", { instanceId: g.instanceId, code: referenceFor(g.instanceId) })); const body = await res.json(); expect(body.ran).toBe(true); expect(body.allPassed).toBe(true); expect(body.compileError).toBeNull(); }); it("404s for an unknown instance", async () => { const res = await runCode(post("/api/coding/run", { instanceId: "does-not-exist", code: "function x(){}" })); expect(res.status).toBe(404); }); }); describe("POST /api/coding/submit-code", () => { it("accepts the reference solution, marks it correct, and awards XP", async () => { const g = await gen("code-creation", "medium", "javascript"); const res = await submitCode(post("/api/coding/submit-code", { instanceId: g.instanceId, code: referenceFor(g.instanceId), })); const body = await res.json(); expect(body.correct).toBe(true); expect(body.awarded).toBe(true); expect(body.passed).toBe(body.total); expect(body.xp.totalXp).toBeGreaterThan(0); expect(body.categoryXp.level).toBeGreaterThanOrEqual(1); }); it("fixes a debugging problem with the reference fix", async () => { const g = await gen("debugging", "easy", "javascript"); const res = await submitCode(post("/api/coding/submit-code", { instanceId: g.instanceId, code: referenceFor(g.instanceId), })); const body = await res.json(); expect(body.correct).toBe(true); expect(body.awarded).toBe(true); }); it("rejects wrong code without awarding XP", async () => { const g = await gen("code-creation", "easy", "javascript"); const res = await submitCode(post("/api/coding/submit-code", { instanceId: g.instanceId, code: "function wrongName() { return -999; }", })); const body = await res.json(); expect(body.correct).toBe(false); expect(body.awarded).toBe(false); expect(body.xp).toBeUndefined(); }); it("rejects submitting code to a written problem with 400", async () => { const g = await gen("architecture", "medium", "javascript"); const res = await submitCode(post("/api/coding/submit-code", { instanceId: g.instanceId, code: "function x(){}", })); expect(res.status).toBe(400); }); }); describe("POST /api/coding/submit-written", () => { it("grades a strong code-review answer as correct and awards XP (heuristic)", async () => { // Use the known 'average' review problem for a deterministic strong answer. const target = PROBLEM_BANK.find((p) => p.id === "cr-js-medium-average")!; const g = await generate(post("/api/coding/generate", { type: "code-review", difficulty: target.difficulty, language: "javascript", })); const gb = await g.json(); // Keep regenerating deterministically isn't possible; instead submit an // answer that covers both known issues by their distinctive wording. const answer = "The loop overruns the array by reading one past the end, and there is a division by zero on empty input."; const res = await submitWritten(post("/api/coding/submit-written", { instanceId: gb.instanceId, answer, })); const body = await res.json(); expect(body.gradedBy).toBe("heuristic"); expect(typeof body.qualityScore).toBe("number"); // Whatever medium review problem was served, a thorough answer should at // least be graded; correctness depends on the specific problem. expect(body).toHaveProperty("correct"); expect(body).toHaveProperty("expectations"); }); it("awards XP when correctly stating clean code has no issues", async () => { // The only easy code-review problem in the bank is the clean clamp(). const g = await gen("code-review", "easy", "javascript"); const res = await submitWritten(post("/api/coding/submit-written", { instanceId: g.instanceId, answer: "This function looks correct. There are no issues — it clamps properly.", })); const body = await res.json(); expect(body.correct).toBe(true); expect(body.awarded).toBe(true); expect(body.xp.totalXp).toBeGreaterThan(0); }); it("accepts a thorough architecture answer covering the rubric", async () => { const g = await gen("architecture", "medium", "javascript"); const answer = "I would 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 the bucket capacity. If Redis is down we fail open to stay available."; const res = await submitWritten(post("/api/coding/submit-written", { instanceId: g.instanceId, answer, })); const body = await res.json(); expect(body.correct).toBe(true); expect(body.awarded).toBe(true); }); it("rejects a written answer to an executable problem with 400", async () => { const g = await gen("code-creation", "easy", "javascript"); const res = await submitWritten(post("/api/coding/submit-written", { instanceId: g.instanceId, answer: "some prose", })); expect(res.status).toBe(400); }); });