import { gradeCodeRun, gradeCodeReview, gradeArchitecture, difficultyWeight, } from "../grade"; import type { RubricPoint } from "../types"; describe("gradeCodeRun", () => { it("is fully correct when all tests pass", () => { const g = gradeCodeRun({ passed: 5, total: 5, allPassed: true }); expect(g.isCorrect).toBe(true); expect(g.qualityScore).toBe(1); }); it("gives partial quality for partial passes but is not correct", () => { const g = gradeCodeRun({ passed: 3, total: 5, allPassed: false }); expect(g.isCorrect).toBe(false); expect(g.qualityScore).toBeCloseTo(0.6); }); it("is zero when nothing passes", () => { const g = gradeCodeRun({ passed: 0, total: 5, allPassed: false }); expect(g.isCorrect).toBe(false); expect(g.qualityScore).toBe(0); }); it("handles an empty test set safely", () => { const g = gradeCodeRun({ passed: 0, total: 0, allPassed: false }); expect(g.isCorrect).toBe(false); expect(g.qualityScore).toBe(0); }); }); describe("gradeCodeReview", () => { it("is correct when the user finds all known issues with no false positives", () => { const g = gradeCodeReview(3, { identifiedIssueIds: ["a", "b", "c"], falsePositives: 0 }); expect(g.isCorrect).toBe(true); expect(g.qualityScore).toBe(1); }); it("gives partial credit and counts as correct when a majority are found", () => { const g = gradeCodeReview(4, { identifiedIssueIds: ["a", "b", "c"], falsePositives: 0 }); expect(g.qualityScore).toBeCloseTo(0.75); expect(g.isCorrect).toBe(true); }); it("is not correct when fewer than half the issues are found", () => { const g = gradeCodeReview(4, { identifiedIssueIds: ["a"], falsePositives: 0 }); expect(g.isCorrect).toBe(false); }); it("penalizes false positives", () => { const clean = gradeCodeReview(4, { identifiedIssueIds: ["a", "b", "c", "d"], falsePositives: 0 }); const noisy = gradeCodeReview(4, { identifiedIssueIds: ["a", "b", "c", "d"], falsePositives: 2 }); expect(noisy.qualityScore).toBeLessThan(clean.qualityScore); }); it("rewards correctly reporting clean code (zero known issues)", () => { const g = gradeCodeReview(0, { identifiedIssueIds: [], falsePositives: 0 }); expect(g.isCorrect).toBe(true); expect(g.qualityScore).toBe(1); }); it("marks clean code wrong when the user invents issues", () => { const g = gradeCodeReview(0, { identifiedIssueIds: [], falsePositives: 2 }); expect(g.isCorrect).toBe(false); expect(g.qualityScore).toBeLessThan(1); }); it("dedupes and ignores unknown identified ids beyond the known count", () => { // Judge should only pass real ids, but be defensive: cap recall at 1. const g = gradeCodeReview(2, { identifiedIssueIds: ["a", "a", "b"], falsePositives: 0 }); expect(g.qualityScore).toBeLessThanOrEqual(1); expect(g.isCorrect).toBe(true); }); }); describe("gradeArchitecture", () => { const rubric: RubricPoint[] = [ { id: "scale", point: "Addresses horizontal scaling", weight: 2 }, { id: "consistency", point: "Discusses consistency tradeoffs", weight: 1 }, { id: "failure", point: "Handles failure modes", weight: 1 }, ]; it("is fully correct when every weighted point is covered", () => { const g = gradeArchitecture(rubric, { coveredPointIds: ["scale", "consistency", "failure"] }); expect(g.qualityScore).toBe(1); expect(g.isCorrect).toBe(true); }); it("weights points by importance", () => { // Covering only the weight-2 point = 2/4 = 0.5 const g = gradeArchitecture(rubric, { coveredPointIds: ["scale"] }); expect(g.qualityScore).toBeCloseTo(0.5); }); it("is not correct when coverage is below half", () => { const g = gradeArchitecture(rubric, { coveredPointIds: ["consistency"] }); expect(g.qualityScore).toBeCloseTo(0.25); expect(g.isCorrect).toBe(false); }); it("handles an empty rubric safely", () => { const g = gradeArchitecture([], { coveredPointIds: [] }); expect(g.qualityScore).toBe(0); expect(g.isCorrect).toBe(false); }); }); describe("difficultyWeight", () => { it("increases with difficulty", () => { expect(difficultyWeight("easy")).toBeLessThan(difficultyWeight("medium")); expect(difficultyWeight("medium")).toBeLessThan(difficultyWeight("hard")); }); it("stays within (0, 1]", () => { for (const d of ["easy", "medium", "hard"] as const) { expect(difficultyWeight(d)).toBeGreaterThan(0); expect(difficultyWeight(d)).toBeLessThanOrEqual(1); } }); });