Download src/lib/coding/__tests__/judge.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/__tests__/judge.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/coding/__tests__/judge.test.ts
-
curl -L -o judge.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/coding/__tests__/judge.test.ts
4.66 kB
| import { | |
| heuristicCodeReviewVerdict, | |
| heuristicArchitectureVerdict, | |
| judgeWritten, | |
| } from "../judge"; | |
| import type { | |
| CodeIssue, | |
| RubricPoint, | |
| CodeReviewProblem, | |
| ArchitectureProblem, | |
| } from "../types"; | |
| const issues: CodeIssue[] = [ | |
| { | |
| id: "off-by-one", | |
| title: "Loop overruns the array", | |
| description: "The loop reads one past the end of the array.", | |
| severity: "critical", | |
| }, | |
| { | |
| id: "empty-divide", | |
| title: "Division by zero on empty input", | |
| description: "Dividing by length zero returns NaN.", | |
| severity: "minor", | |
| }, | |
| ]; | |
| describe("heuristicCodeReviewVerdict", () => { | |
| it("identifies an issue when the answer describes it", () => { | |
| const v = heuristicCodeReviewVerdict( | |
| "The loop overruns the array because it reads past the end.", | |
| issues, | |
| ); | |
| expect(v.identifiedIssueIds).toContain("off-by-one"); | |
| }); | |
| it("does not credit an issue the answer never mentions", () => { | |
| const v = heuristicCodeReviewVerdict( | |
| "The loop overruns the array.", | |
| issues, | |
| ); | |
| expect(v.identifiedIssueIds).not.toContain("empty-divide"); | |
| }); | |
| it("identifies multiple issues", () => { | |
| const v = heuristicCodeReviewVerdict( | |
| "The loop overruns the array, and there is a division by zero on empty input.", | |
| issues, | |
| ); | |
| expect(v.identifiedIssueIds).toContain("off-by-one"); | |
| expect(v.identifiedIssueIds).toContain("empty-divide"); | |
| }); | |
| it("reports no false positive when correctly stating clean code is fine", () => { | |
| const v = heuristicCodeReviewVerdict( | |
| "This function looks correct, there are no issues.", | |
| [], | |
| ); | |
| expect(v.falsePositives).toBe(0); | |
| expect(v.identifiedIssueIds).toHaveLength(0); | |
| }); | |
| it("flags a false positive when inventing a bug in clean code", () => { | |
| const v = heuristicCodeReviewVerdict( | |
| "There is a serious bug here that will crash.", | |
| [], | |
| ); | |
| expect(v.falsePositives).toBeGreaterThan(0); | |
| }); | |
| }); | |
| describe("heuristicArchitectureVerdict", () => { | |
| const rubric: RubricPoint[] = [ | |
| { id: "algorithm", point: "Selects an algorithm like token bucket or sliding window", weight: 2 }, | |
| { id: "state", point: "Decides where counters live such as Redis shared store", weight: 2 }, | |
| ]; | |
| it("covers a rubric point the answer addresses", () => { | |
| const v = heuristicArchitectureVerdict( | |
| "I would use a token bucket algorithm to smooth bursts.", | |
| rubric, | |
| ); | |
| expect(v.coveredPointIds).toContain("algorithm"); | |
| }); | |
| it("does not cover a point the answer ignores", () => { | |
| const v = heuristicArchitectureVerdict( | |
| "I would use a token bucket algorithm.", | |
| rubric, | |
| ); | |
| expect(v.coveredPointIds).not.toContain("state"); | |
| }); | |
| }); | |
| describe("judgeWritten dispatcher", () => { | |
| const reviewProblem: CodeReviewProblem = { | |
| id: "r", | |
| type: "code-review", | |
| language: "javascript", | |
| difficulty: "medium", | |
| title: "t", | |
| prompt: "p", | |
| code: "code", | |
| issues, | |
| }; | |
| it("uses the heuristic when AI is disabled", async () => { | |
| const res = await judgeWritten(reviewProblem, "the loop overruns the array", { | |
| useAI: false, | |
| }); | |
| expect(res.usedAI).toBe(false); | |
| expect(res.verdict).toHaveProperty("identifiedIssueIds"); | |
| }); | |
| it("uses the injected AI judge when enabled", async () => { | |
| const aiJudge = async () => ({ | |
| verdict: { identifiedIssueIds: ["off-by-one", "empty-divide"], falsePositives: 0 }, | |
| feedback: "great", | |
| }); | |
| const res = await judgeWritten(reviewProblem, "anything", { useAI: true, aiJudge }); | |
| expect(res.usedAI).toBe(true); | |
| expect(res.feedback).toBe("great"); | |
| expect((res.verdict as { identifiedIssueIds: string[] }).identifiedIssueIds).toHaveLength(2); | |
| }); | |
| it("falls back to the heuristic when the AI judge throws", async () => { | |
| const aiJudge = async () => { | |
| throw new Error("no api key"); | |
| }; | |
| const res = await judgeWritten(reviewProblem, "the loop overruns the array", { | |
| useAI: true, | |
| aiJudge, | |
| }); | |
| expect(res.usedAI).toBe(false); | |
| expect(res.verdict).toHaveProperty("identifiedIssueIds"); | |
| }); | |
| it("dispatches architecture problems to the rubric verdict shape", async () => { | |
| const archProblem: ArchitectureProblem = { | |
| id: "a", | |
| type: "architecture", | |
| language: "javascript", | |
| difficulty: "hard", | |
| title: "t", | |
| prompt: "p", | |
| rubric: [{ id: "x", point: "uses token bucket algorithm", weight: 1 }], | |
| }; | |
| const res = await judgeWritten(archProblem, "token bucket algorithm", { useAI: false }); | |
| expect(res.verdict).toHaveProperty("coveredPointIds"); | |
| }); | |
| }); | |