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