import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync, readdirSync, writeFileSync, existsSync, mkdirSync } from "node:fs"; import { parseDocx } from "../server/parser.js"; import { diagnose } from "../server/diagnostics.js"; import { snapshotOf, diffLines } from "../scripts/snapshot-lines.js"; // Every fixture parsed and compared line by line with test/snapshots/.txt, together with // the cells diagnostics flags. After an intended parser change: UPDATE_SNAPSHOTS=1 npm test, // then read the diff of test/snapshots/ before committing it. const fixtures = new URL("./fixtures/", import.meta.url); const snapshots = new URL("./snapshots/", import.meta.url); const update = process.env.UPDATE_SNAPSHOTS === "1"; // The bell schedule and the week table are calendar documents, not timetables. const calendars = new Set(["bells.docx", "weeks.docx"]); for (const file of readdirSync(fixtures).filter((f) => f.endsWith(".docx") && !calendars.has(f)).sort()) { test(`снимок разбора: ${file}`, () => { const meta = existsSync(new URL(`${file}.json`, fixtures)) ? JSON.parse(readFileSync(new URL(`${file}.json`, fixtures), "utf8")) : {}; const schedule = parseDocx(readFileSync(new URL(file, fixtures)), { name: meta.name || file, modified: meta.modified, }); const lines = [ ...snapshotOf(schedule), ...diagnose(schedule).map((p) => `! ${p.code} | ${(p.raw || p.groups?.join(",") || "").replace(/\s+/g, " ")}`), ]; const target = new URL(file.replace(/\.docx$/, ".txt"), snapshots); if (update || !existsSync(target)) { mkdirSync(snapshots, { recursive: true }); writeFileSync(target, lines.join("\n") + "\n"); return; } const saved = readFileSync(target, "utf8").trimEnd().split("\n"); const { removed, added } = diffLines(saved, lines); assert.ok( !removed.length && !added.length, `Разбор ${file} изменился (UPDATE_SNAPSHOTS=1 npm test, если так и задумано):\n` + [...removed.map((l) => ` − ${l}`), ...added.map((l) => ` + ${l}`)].join("\n"), ); }); }