SPB Git

Repository Public

quiz.test.ts 6.4 KB 107 lines typescript

1import { readFileSync } from "node:fs";2import { describe, expect, it } from "vitest";3import { Quiz, type Question } from "@/modules/content-schema";4import { emptyAnswer, grade, gradeQuiz, isAnswered, parseNumber, shuffle, type Answer } from "@/modules/learning-engine";56const demo = Quiz.parse(JSON.parse(readFileSync("content/preview/overlay.json", "utf8")).demoQuiz);7const q = <T extends Question["type"]>(type: T) => demo.questions.find((x) => x.type === type) as Extract<Question, { type: T }>;89describe("number parsing", () => {10  it("accepts French and English decimals, fractions and signs", () => {11    expect(parseNumber("0,20")).toBeCloseTo(0.2);12    expect(parseNumber("0.2")).toBeCloseTo(0.2);13    expect(parseNumber("1/5")).toBeCloseTo(0.2);14    expect(parseNumber("−3")).toBe(-3);15    expect(parseNumber("1 250,5")).toBe(1250.5);16    expect(parseNumber(" 24 ")).toBe(24);17  });18  it("rejects what is not a number", () => {19    for (const bad of ["", "abc", "1/0", "1,2,3", "12 jetons", "--1"]) expect(parseNumber(bad), bad).toBeNull();20  });21});2223describe("grading, one test per question type", () => {24  it("single choice", () => {25    expect(grade(q("single"), { type: "single", value: "B" })).toMatchObject({ correct: true, score: 1 });26    expect(grade(q("single"), { type: "single", value: "A" })).toMatchObject({ correct: false, score: 0 });27    expect(isAnswered(q("single"), { type: "single", value: null })).toBe(false);28  });29  it("true / false", () => {30    expect(grade(q("true-false"), { type: "true-false", value: false }).correct).toBe(true);31    expect(grade(q("true-false"), { type: "true-false", value: true }).correct).toBe(false);32  });33  it("multiple choice: exact set required, partial credit minus wrong picks", () => {34    expect(grade(q("multiple"), { type: "multiple", value: ["A", "B", "D"] })).toMatchObject({ correct: true, score: 1 });35    const partial = grade(q("multiple"), { type: "multiple", value: ["A", "B"] });36    expect(partial.correct).toBe(false); expect(partial.score).toBeCloseTo(2 / 3);37    const withWrong = grade(q("multiple"), { type: "multiple", value: ["A", "C"] });38    expect(withWrong.score).toBe(0); expect(withWrong.parts.C).toBe(false);39    expect(grade(q("multiple"), { type: "multiple", value: ["C"] }).score).toBe(0); // never negative40  });41  it("ordering", () => {42    expect(grade(q("ordering"), { type: "ordering", value: ["a", "b", "c", "d"] }).correct).toBe(true);43    const g = grade(q("ordering"), { type: "ordering", value: ["a", "c", "b", "d"] });44    expect(g.correct).toBe(false); expect(g.score).toBe(0.5); expect(g.parts).toEqual({ a: true, b: false, c: false, d: true });45    // The initial order shown to the learner is not already the answer.46    expect(grade(q("ordering"), emptyAnswer(q("ordering"))).correct).toBe(false);47  });48  it("matching", () => {49    expect(grade(q("matching"), { type: "matching", value: { espace: "lecture", m: "muet", f: "plein", c: "st" } }).correct).toBe(true);50    const g = grade(q("matching"), { type: "matching", value: { espace: "lecture", m: "plein", f: "muet", c: "st" } });51    expect(g.score).toBe(0.5);52    expect(isAnswered(q("matching"), { type: "matching", value: { espace: "lecture" } })).toBe(false);53  });54  it("numeric with tolerance", () => {55    expect(grade(q("numeric"), { type: "numeric", value: "24" }).correct).toBe(true);56    expect(grade(q("numeric"), { type: "numeric", value: "24,0" }).correct).toBe(true);57    expect(grade(q("numeric"), { type: "numeric", value: "25" }).correct).toBe(false);58    const tol: Question = { id: "t", type: "numeric", prompt: "", explanation: "", answer: 0.2, tolerance: 0.01 };59    expect(grade(tol, { type: "numeric", value: "0,21" }).correct).toBe(true);60    expect(grade(tol, { type: "numeric", value: "0,19" }).correct).toBe(true);61    expect(grade(tol, { type: "numeric", value: "0,22" }).correct).toBe(false);62    expect(grade(tol, { type: "numeric", value: "1/5" }).correct).toBe(true);63    expect(grade(tol, { type: "numeric", value: "vingt" }).correct).toBe(false);64  });65  it("fill in the blank: case, accents and spacing are forgiven", () => {66    expect(grade(q("fill-blank"), { type: "fill-blank", value: { "1": " Mire ", "2": "vtt" } }).correct).toBe(true);67    const g = grade(q("fill-blank"), { type: "fill-blank", value: { "1": "mire", "2": "srt" } });68    expect(g.correct).toBe(false); expect(g.score).toBe(0.5); expect(g.parts).toEqual({ "1": true, "2": false });69  });70  it("debugging scenario: the line and the fix are both required", () => {71    expect(grade(q("debug"), { type: "debug", value: { line: 4, fix: "A" } }).correct).toBe(true);72    expect(grade(q("debug"), { type: "debug", value: { line: 5, fix: "A" } })).toMatchObject({ correct: false, score: 0.5 });73    expect(isAnswered(q("debug"), { type: "debug", value: { line: 4, fix: null } })).toBe(false);74  });75  it("reflection is self-assessed, never auto-graded", () => {76    expect(grade(q("reflection"), { type: "reflection", value: { text: "…", checks: [true, true, true] } }).correct).toBeNull();77  });78  it("an answer of the wrong shape is simply wrong", () => {79    expect(grade(q("single"), { type: "numeric", value: "1" } as Answer).correct).toBe(false);80  });81});8283describe("whole quiz", () => {84  it("counts auto-graded questions only and treats unanswered ones as wrong", () => {85    const r = gradeQuiz(demo, { "demo-single": { type: "single", value: "B" }, "demo-numerique": { type: "numeric", value: "24" } });86    expect(r.total).toBe(8); expect(r.selfAssessed).toBe(1); expect(r.correct).toBe(2); expect(r.passed).toBeNull();87  });88  it("applies the passing score of the imported S01 quiz", () => {89    const s01 = Quiz.parse(JSON.parse(readFileSync("content/import/sessions/S01/quiz.json", "utf8")));90    expect(s01.questions.length).toBe(11);91    const all: Record<string, Answer> = {};92    for (const x of s01.questions) all[x.id] = x.type === "single" ? { type: "single", value: x.answer } : x.type === "true-false" ? { type: "true-false", value: x.answer } : emptyAnswer(x);93    const perfect = gradeQuiz(s01, all);94    expect(perfect.correct).toBe(11); expect(perfect.passed).toBe(true);95    expect(gradeQuiz(s01, {}).passed).toBe(false);96  });97});9899describe("shuffle", () => {100  it("is deterministic for a seed and keeps every item", () => {101    const items = ["a", "b", "c", "d", "e"];102    expect(shuffle(items, 7)).toEqual(shuffle(items, 7));103    expect([...shuffle(items, 7)].sort()).toEqual(items);104    expect(items).toEqual(["a", "b", "c", "d", "e"]);105  });106});107
Files main
Menu

Theme