Files
math-quiz/lib/engine/parenthesis-sign.test.ts
T

67 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
generateProblem,
generateSession,
gradeAnswer,
} from "./parenthesis-sign";
describe("parenthesis sign problem engine", () => {
it("generates a problem within the configured ranges", () => {
for (let index = 0; index < 100; index += 1) {
const problem = generateProblem();
expect(Number.isInteger(problem.multiplier)).toBe(true);
expect(problem.multiplier).toBeGreaterThanOrEqual(-9);
expect(problem.multiplier).toBeLessThanOrEqual(-1);
expect(problem.a).toBeGreaterThanOrEqual(1);
expect(problem.a).toBeLessThanOrEqual(9);
expect(problem.b).toBeGreaterThanOrEqual(1);
expect(problem.b).toBeLessThanOrEqual(9);
expect(problem.a).not.toBe(problem.b);
expect(["+", "-"]).toContain(problem.operator);
}
});
it("distributes the multiplier correctly for both operators", () => {
for (let index = 0; index < 100; index += 1) {
const problem = generateProblem();
const signedB = problem.operator === "-" ? -problem.b : problem.b;
expect(problem.distributedA).toBe(problem.multiplier * problem.a);
expect(problem.distributedB).toBe(problem.multiplier * signedB);
}
});
it("grades correct and incorrect answers", () => {
const problem = generateProblem();
expect(
gradeAnswer(problem, {
a: problem.distributedA,
b: problem.distributedB,
}).correct,
).toBe(true);
expect(
gradeAnswer(problem, {
a: problem.distributedB,
b: problem.distributedA,
}).correct,
).toBe(false);
});
it("generates a session with unique problem keys", () => {
const problems = generateSession(10);
const keys = new Set(problems.map((problem) => problem.key));
expect(problems).toHaveLength(10);
expect(keys.size).toBe(10);
});
it("rejects impossible session sizes", () => {
expect(() => generateSession(0)).toThrow();
expect(() => generateSession(145)).toThrow();
});
});