81 lines
2.5 KiB
TypeScript
81 lines
2.5 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.terms).toHaveLength(2);
|
|
expect(problem.operators).toHaveLength(1);
|
|
expect(problem.terms.every((term) => term >= 1 && term <= 9)).toBe(true);
|
|
expect(new Set(problem.terms).size).toBe(problem.terms.length);
|
|
expect(["+", "-"]).toContain(problem.operators[0]);
|
|
}
|
|
});
|
|
|
|
it("distributes the multiplier correctly for both operators", () => {
|
|
for (let index = 0; index < 100; index += 1) {
|
|
const problem = generateProblem();
|
|
const signedTerms = problem.terms.map((term, termIndex) => {
|
|
if (termIndex === 0) return term;
|
|
|
|
return problem.operators[termIndex - 1] === "-" ? -term : term;
|
|
});
|
|
|
|
expect(problem.distributedTerms).toEqual(
|
|
signedTerms.map((term) => problem.multiplier * term),
|
|
);
|
|
}
|
|
});
|
|
|
|
it("grades correct and incorrect answers", () => {
|
|
const problem = generateProblem();
|
|
|
|
const correctResult = gradeAnswer(problem, {
|
|
terms: problem.distributedTerms,
|
|
});
|
|
|
|
expect(correctResult.correct).toBe(true);
|
|
expect(correctResult.userAnswer).toEqual(problem.distributedTerms);
|
|
|
|
expect(
|
|
gradeAnswer(problem, {
|
|
terms: [...problem.distributedTerms].reverse(),
|
|
}).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("generates the selected number of terms inside parentheses", () => {
|
|
const problem = generateProblem(4);
|
|
|
|
expect(problem.terms).toHaveLength(4);
|
|
expect(problem.operators).toHaveLength(3);
|
|
expect(problem.distributedTerms).toHaveLength(4);
|
|
expect(
|
|
gradeAnswer(problem, { terms: problem.distributedTerms }).correct,
|
|
).toBe(true);
|
|
});
|
|
|
|
it("rejects impossible session sizes", () => {
|
|
expect(() => generateSession(0)).toThrow();
|
|
expect(() => generateSession(1297)).toThrow();
|
|
});
|
|
});
|