RotNot / src /lib /coding /__tests__ /validate.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
5.27 kB
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);
});
});