import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { borrowingDecision } from "./logical-thinking-rules.mjs"; const expectedReason = ({ active, overdueCount, borrowedCount, limit }) => { if (!active) return "INACTIVE"; if (overdueCount > 0) return "OVERDUE"; if (borrowedCount >= limit) return "AT_LIMIT"; return "ELIGIBLE"; }; let checked = 0; for (const active of [false, true]) { for (const overdueCount of [0, 1]) { for (const borrowedCount of [0, 2, 3]) { const input = { active, overdueCount, borrowedCount, limit: 3 }; const before = JSON.stringify(input); const result = borrowingDecision(input); assert.equal(result.reason, expectedReason(input)); assert.equal(result.allowed, result.reason === "ELIGIBLE"); assert.equal(JSON.stringify(input), before, "input must not change"); checked++; } } } assert.deepStrictEqual( borrowingDecision({ active: true, overdueCount: 0, borrowedCount: 2, limit: 3 }), { allowed: true, reason: "ELIGIBLE" } ); assert.throws(() => borrowingDecision({ active: "yes", overdueCount: 0, borrowedCount: 0, limit: 3 }), /boolean/); assert.throws(() => borrowingDecision({ active: true, overdueCount: -1, borrowedCount: 0, limit: 3 }), /non-negative/); assert.throws(() => borrowingDecision({ active: true, overdueCount: 0, borrowedCount: 0, limit: 0 }), /at least 1/); const pages = JSON.parse(readFileSync(new URL("../../../includes/recovered-learning-pages.json", import.meta.url), "utf8")); assert.equal(pages["how-students-build-logical-thinking-for-coding"].examples[0].code, readFileSync(new URL("./logical-thinking-rules.mjs", import.meta.url), "utf8")); console.log("PASS: " + checked + " decision-table combinations, boundaries, validation, immutability and displayed-code consistency");