Files
pplus-web/tests/curriculum.test.mjs
T

118 lines
7.0 KiB
JavaScript

// The lesson corpus verifies the interpreter: the dependency map must be strict and every program must run.
import test from 'node:test';
import assert from 'node:assert/strict';
import { LESSONS, CONCEPTS, CONCEPT_BY_ID, FLAVORS, teacherOf, prerequisitesOf, depthOf, allPrerequisitesOf, statusOf, solutionOf, starterOf } from '../dist/curriculum/index.js';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { problemById } from '../dist/core/problems.js';
const conceptIds = new Set(CONCEPTS.map((c) => c.id));
const flavorIds = new Set(FLAVORS.map((f) => f.id));
test('lesson ids are unique and every concept id is real', () => {
const ids = new Set();
for (const L of LESSONS) {
assert.ok(!ids.has(L.id), `duplicate lesson id ${L.id}`); ids.add(L.id);
for (const c of [...L.teaches, ...L.requires]) assert.ok(conceptIds.has(c), `${L.id} names unknown concept ${c}`);
assert.ok(L.teaches.length >= 1, `${L.id} teaches nothing`);
assert.ok(L.purpose.length > 20 && L.explanation.length >= 3 && L.checks.length >= 2 && L.flavors.length >= 2, `${L.id} is thin`);
for (const f of L.flavors) assert.ok(flavorIds.has(f.id), `${L.id} has unknown flavor ${f.id}`);
for (const c of L.checks) assert.ok(c.answer >= 0 && c.answer < c.options.length, `${L.id} check answer out of range`);
for (const p of L.problems || []) assert.ok(problemById(p), `${L.id} points at missing problem ${p}`);
if (L.showAsmOf) assert.ok(problemById(L.showAsmOf)?.sources.asm, `${L.id} shows asm of a problem without any`);
}
});
test('every concept is taught by exactly one lesson, and every concept is used', () => {
for (const c of CONCEPTS) {
const teachers = LESSONS.filter((l) => l.teaches.includes(c.id));
assert.equal(teachers.length, 1, `${c.id} is taught by ${teachers.map((l) => l.id).join(', ') || 'nobody'}`);
}
// every foundations and core concept must feed a later lesson; only advanced concepts may be frontier
const required = new Set(LESSONS.flatMap((l) => l.requires));
for (const L of LESSONS.filter((l) => l.stage !== 'advanced')) for (const c of L.teaches) assert.ok(required.has(c), `${c} (from ${L.id}) is never required by a later lesson`);
});
test('the dependency map is strict: every requirement is taught by an earlier lesson, and there are no cycles', () => {
const seen = new Set();
for (const L of LESSONS) {
for (const c of L.requires) {
const t = teacherOf(c);
assert.ok(t, `${L.id} requires ${c}, which nothing teaches`);
assert.ok(seen.has(t.id), `${L.id} requires ${c}, taught by ${t.id}, which comes later in the order`);
assert.ok(!L.teaches.includes(c), `${L.id} both teaches and requires ${c}`);
}
seen.add(L.id);
}
for (const L of LESSONS) assert.ok(depthOf(L) >= 0, `${L.id} is in a cycle`);
const roots = LESSONS.filter((l) => l.requires.length === 0);
assert.equal(roots.length, 1, `exactly one lesson should assume nothing: ${roots.map((l) => l.id).join(', ')}`);
for (const L of LESSONS.slice(1)) assert.ok(allPrerequisitesOf(L).some((p) => p.id === roots[0].id), `${L.id} does not trace back to ${roots[0].id}`);
});
test('a lesson is available exactly when every prerequisite lesson is done', () => {
const none = { done: [], checks: {} };
const first = LESSONS[0];
assert.equal(statusOf(first, none), 'available');
assert.equal(statusOf(LESSONS[LESSONS.length - 1], none), 'locked');
const last = LESSONS[LESSONS.length - 1];
const all = { done: allPrerequisitesOf(last).map((l) => l.id), checks: {} };
assert.equal(statusOf(last, all), 'available');
const direct = prerequisitesOf(last)[0].id;
const butOne = { done: all.done.filter((id) => id !== direct), checks: {} };
assert.notEqual(statusOf(last, butOne), 'available');
const order = ['foundations', 'core', 'advanced'];
for (const L of LESSONS) for (const p of prerequisitesOf(L)) assert.ok(order.indexOf(p.stage) <= order.indexOf(L.stage), `${L.id} depends on a later stage`);
});
function runProgram(label, prog) {
const st = studyOf(solutionOf(prog));
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${label}: ${errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')}`);
const tr = runPseudo(st, { input: prog.input || [], maxSteps: 200000 });
const out = tr.output.filter((l) => !/^\w+ returned /.test(l));
if (prog.stops) { assert.equal(tr.error?.kind, prog.stops, `${label}: expected a ${prog.stops} stop, got ${tr.error?.kind || 'none'}`); }
else assert.equal(tr.error, null, `${label}: ${tr.error?.message}`);
assert.deepEqual(out, prog.expect, label);
return st;
}
for (const L of LESSONS) {
test(`lesson ${L.id}: the example and every flavor run and print what they say`, () => {
runProgram(`${L.id}/example`, L.example);
for (const f of L.flavors) {
runProgram(`${L.id}/${f.id}`, f.solution);
const starter = f.starter ?? starterOf(f.solution);
assert.ok(starter.length > 0);
if (!f.starter) assert.ok(/\/\/ TODO:/.test(starter) || !/\/\/ ~/.test(f.solution.pseudo), `${L.id}/${f.id}: starter has no blanks`);
assert.ok(!/\/\/ ~/.test(starter), `${L.id}/${f.id}: marker leaked into the starter`);
assert.ok(!/\/\/ ~/.test(solutionOf(f.solution)), `${L.id}/${f.id}: marker leaked into the solution`);
}
});
}
test('a starter blanks exactly the marked lines', () => {
const p = { pseudo: 'A <- 1\nB <- 2 // ~ set B\nC <- 3', expect: [] };
assert.equal(starterOf(p), 'A <- 1\n// TODO: set B\nC <- 3');
assert.equal(solutionOf(p), 'A <- 1\nB <- 2\nC <- 3');
});
test('every lesson uses only keywords its prerequisites (or it) have introduced', () => {
// a rough but real check: the statement keyword that introduces a concept must not appear
// in a lesson that neither teaches nor (transitively) requires that concept
const introduces = {
'IF': 'selection', 'FOR': 'for-loop', 'WHILE': 'while', 'REPEAT': 'repeat', 'FUNCTION': 'function', 'PROCEDURE': 'procedure-sub', 'BYREF': 'byref', 'CASE': 'case',
'HASHMAP': 'hashmap', 'SET': 'set', 'TYPE': 'record', 'ARRAY': 'array', 'INVARIANT': 'invariant', 'ENSURE': 'specification', 'BREAK': 'early-exit', 'CONTINUE': 'early-exit', 'INPUT': 'io',
};
const exempt = { 'sets': ['TYPE'], 'what-stops-a-run': ['FUNCTION'] };
const violations = []; // TYPE … = SET OF is the set lesson's own form; the stops lesson reviews a function
for (const L of LESSONS) {
const held = new Set([...L.teaches, ...allPrerequisitesOf(L).flatMap((p) => p.teaches), ...L.requires]);
const programs = [L.example.pseudo, ...L.flavors.flatMap((f) => [f.solution.pseudo, f.starter || ''])].join('\n');
for (const [kw, concept] of Object.entries(introduces)) {
if ((exempt[L.id] || []).includes(kw)) continue;
if (new RegExp(`(^|[^\\w])${kw}\\b`, 'm').test(programs.replace(/"[^"]*"/g, '""').replace(/\/\/.*$/gm, '')) && !held.has(concept)) violations.push(`${L.id} uses ${kw} but neither teaches nor requires ${concept}`);
}
}
assert.deepEqual(violations, []);
});