File size: 5,272 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 | import { validateProblem } from "../validate";
import type {
CodeCreationProblem,
DebuggingProblem,
CodeReviewProblem,
ArchitectureProblem,
} from "../types";
function baseCreation(): CodeCreationProblem {
return {
id: "p1",
type: "code-creation",
language: "javascript",
difficulty: "easy",
title: "Sum two numbers",
prompt: "Implement add(a, b).",
entryFunction: "add",
starterCode: "function add(a, b) {\n // your code\n}",
referenceSolution: "function add(a, b) { return a + b; }",
tests: [
{ name: "1+1", input: [1, 1], expected: 2 },
{ name: "2+2", input: [2, 2], expected: 4 },
],
};
}
describe("validateProblem — code-creation", () => {
it("accepts a problem whose reference solution passes all tests", () => {
const res = validateProblem(baseCreation());
expect(res.valid).toBe(true);
});
it("rejects a problem whose reference solution fails its own tests", () => {
const p = baseCreation();
p.referenceSolution = "function add(a, b) { return a - b; }";
const res = validateProblem(p);
expect(res.valid).toBe(false);
expect(res.reason).toMatch(/reference/i);
});
it("rejects a problem with no tests", () => {
const p = baseCreation();
p.tests = [];
const res = validateProblem(p);
expect(res.valid).toBe(false);
});
it("skips execution for non-executable languages but still requires tests", () => {
const p = baseCreation();
p.language = "python";
p.referenceSolution = "def add(a, b):\n return a + b";
const res = validateProblem(p);
expect(res.valid).toBe(true); // structural only — cannot run python
});
});
describe("validateProblem — debugging", () => {
function baseDebug(): DebuggingProblem {
return {
id: "d1",
type: "debugging",
language: "javascript",
difficulty: "medium",
title: "Fix the off-by-one",
prompt: "Fix max().",
entryFunction: "max",
buggyCode: "function max(a, b) { return a < b ? a : b; }",
referenceSolution: "function max(a, b) { return a > b ? a : b; }",
tests: [
{ name: "max(1,2)", input: [1, 2], expected: 2 },
{ name: "max(5,3)", input: [5, 3], expected: 5 },
],
};
}
it("accepts when the fix passes and the buggy code fails at least one test", () => {
const res = validateProblem(baseDebug());
expect(res.valid).toBe(true);
});
it("rejects when the buggy code already passes every test (nothing to fix)", () => {
const p = baseDebug();
p.buggyCode = p.referenceSolution;
const res = validateProblem(p);
expect(res.valid).toBe(false);
expect(res.reason).toMatch(/buggy/i);
});
it("rejects when the reference fix does not pass", () => {
const p = baseDebug();
p.referenceSolution = "function max(a, b) { return 0; }";
const res = validateProblem(p);
expect(res.valid).toBe(false);
});
});
describe("validateProblem — code-review", () => {
function baseReview(): CodeReviewProblem {
return {
id: "r1",
type: "code-review",
language: "javascript",
difficulty: "medium",
title: "Review this",
prompt: "Find the issues.",
code: "function f(a){ return a.map(x => x.id) }",
issues: [
{ id: "null-deref", title: "No null check", description: "...", severity: "major" },
],
};
}
it("accepts a well-formed review problem", () => {
expect(validateProblem(baseReview()).valid).toBe(true);
});
it("accepts clean code with zero issues", () => {
const p = baseReview();
p.issues = [];
expect(validateProblem(p).valid).toBe(true);
});
it("rejects duplicate issue ids", () => {
const p = baseReview();
p.issues = [
{ id: "dup", title: "A", description: "x", severity: "minor" },
{ id: "dup", title: "B", description: "y", severity: "minor" },
];
expect(validateProblem(p).valid).toBe(false);
});
it("rejects empty code", () => {
const p = baseReview();
p.code = " ";
expect(validateProblem(p).valid).toBe(false);
});
});
describe("validateProblem — architecture", () => {
function baseArch(): ArchitectureProblem {
return {
id: "a1",
type: "architecture",
language: "javascript",
difficulty: "hard",
title: "Design a URL shortener",
prompt: "How would you design it?",
rubric: [
{ id: "hashing", point: "Key generation strategy", weight: 1 },
{ id: "scale", point: "Read/write scaling", weight: 2 },
],
};
}
it("accepts a well-formed architecture problem", () => {
expect(validateProblem(baseArch()).valid).toBe(true);
});
it("rejects an empty rubric", () => {
const p = baseArch();
p.rubric = [];
expect(validateProblem(p).valid).toBe(false);
});
it("rejects duplicate rubric ids", () => {
const p = baseArch();
p.rubric = [
{ id: "x", point: "a", weight: 1 },
{ id: "x", point: "b", weight: 1 },
];
expect(validateProblem(p).valid).toBe(false);
});
it("rejects non-positive weights", () => {
const p = baseArch();
p.rubric = [{ id: "x", point: "a", weight: 0 }];
expect(validateProblem(p).valid).toBe(false);
});
});
|