File size: 4,659 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 | 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");
});
});
|