File size: 7,792 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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);
  });
});