File size: 3,934 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 | import { PROBLEM_BANK, pickProblem, bankStats } from "../bank";
import { validateProblem } from "../validate";
import {
CODING_PROBLEM_TYPES,
CODING_DIFFICULTIES,
isExecutableType,
} from "../types";
describe("PROBLEM_BANK integrity", () => {
it("is non-empty", () => {
expect(PROBLEM_BANK.length).toBeGreaterThan(0);
});
it("every problem has a known, verifiable answer (validateProblem passes)", () => {
for (const problem of PROBLEM_BANK) {
const res = validateProblem(problem);
expect(res.valid, `${problem.id}: ${res.reason ?? ""}`).toBe(true);
}
});
it("has globally unique ids", () => {
const ids = PROBLEM_BANK.map((p) => p.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("only tags executable problems with executable-friendly content", () => {
for (const p of PROBLEM_BANK) {
if (p.type === "code-creation") {
expect(p.entryFunction.length).toBeGreaterThan(0);
expect(p.tests.length).toBeGreaterThan(0);
}
if (p.type === "debugging") {
expect(p.buggyCode.length).toBeGreaterThan(0);
expect(p.tests.length).toBeGreaterThan(0);
}
}
});
it("includes at least one hidden test per executable problem", () => {
for (const p of PROBLEM_BANK) {
if (isExecutableType(p.type)) {
const tests = (p as { tests: { hidden?: boolean }[] }).tests;
expect(tests.some((t) => t.hidden)).toBe(true);
}
}
});
it("covers every problem type", () => {
for (const type of CODING_PROBLEM_TYPES) {
expect(PROBLEM_BANK.some((p) => p.type === type)).toBe(true);
}
});
it("covers every difficulty", () => {
for (const diff of CODING_DIFFICULTIES) {
expect(PROBLEM_BANK.some((p) => p.difficulty === diff)).toBe(true);
}
});
it("offers a JavaScript executable problem for each difficulty", () => {
for (const diff of CODING_DIFFICULTIES) {
const match = PROBLEM_BANK.some(
(p) =>
p.language === "javascript" &&
isExecutableType(p.type) &&
p.difficulty === diff,
);
expect(match, `no executable JS problem for ${diff}`).toBe(true);
}
});
});
describe("pickProblem", () => {
it("returns a problem matching the requested type and difficulty", () => {
const p = pickProblem({
type: "code-creation",
difficulty: "easy",
language: "javascript",
});
expect(p).not.toBeNull();
expect(p!.type).toBe("code-creation");
expect(p!.difficulty).toBe("easy");
});
it("honors the requested language when a match exists", () => {
const p = pickProblem({
type: "code-review",
difficulty: "medium",
language: "python",
});
if (p) expect(p.language).toBe("python");
});
it("is deterministic when given a fixed rng", () => {
const opts = {
type: "code-creation" as const,
difficulty: "easy" as const,
language: "javascript" as const,
rng: () => 0,
};
const a = pickProblem(opts);
const b = pickProblem(opts);
expect(a!.id).toBe(b!.id);
});
it("returns null for an impossible combination", () => {
const p = pickProblem({
// no executable problems exist in Go in the bank
type: "code-creation",
difficulty: "easy",
language: "go",
strictLanguage: true,
});
expect(p).toBeNull();
});
it("falls back to a JS executable problem when language is non-executable and not strict", () => {
const p = pickProblem({
type: "debugging",
difficulty: "medium",
language: "go",
});
expect(p).not.toBeNull();
expect(p!.language).toBe("javascript");
});
});
describe("bankStats", () => {
it("reports counts that sum to the bank size", () => {
const stats = bankStats();
const total = Object.values(stats.byType).reduce((a, b) => a + b, 0);
expect(total).toBe(PROBLEM_BANK.length);
});
});
|