Unit tests and fixtures
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
2b0f2b1f01
commit
1b4642ec35
21 files changed
+9430
No files matched your search
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Every starter document in library/ parses and builds. A failure names the file, the line, the
|
||||
* section and the quote, all of them at once, so the files can be fixed in one pass.
|
||||
*/
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { FormatError } from "../src/core/format";
|
||||
import { Doc, descendants, isStale } from "../src/core/model";
|
||||
import { writeText } from "../src/core/seed";
|
||||
import { slugify } from "../src/core/text";
|
||||
|
||||
const dir = new URL("../library/", import.meta.url);
|
||||
const id0 = (f: string) => slugify(f.slice(0, -3));
|
||||
const files = (() => {
|
||||
try {
|
||||
return readdirSync(dir).filter((f) => f.endsWith(".md")).sort();
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
})();
|
||||
|
||||
describe("library", () => {
|
||||
it("parses and builds every file into one library, with nothing stale", () => {
|
||||
const failures: string[] = [];
|
||||
const lib = Doc.library("test");
|
||||
for (const f of files) {
|
||||
const id = f.slice(0, -3);
|
||||
if (slugify(id) !== id) failures.push(`${f}: the file name is not a valid id (lowercase letters, digits, hyphens); it would be imported as "${slugify(id)}"`);
|
||||
try {
|
||||
const id = writeText(lib, readFileSync(new URL(f, dir), "utf8"), f, { slug: id0(f) });
|
||||
const snap = lib.snap();
|
||||
const stale = [id, ...descendants(snap, id)].filter((n) => isStale(snap, n));
|
||||
if (stale.length) failures.push(`${f}: ${stale.length} nodes start out stale`);
|
||||
if (!snap.nodes[id].title) failures.push(`${f}: the document has no title`);
|
||||
} catch (err) {
|
||||
if (!(err instanceof FormatError)) throw err;
|
||||
failures.push(...err.problems);
|
||||
}
|
||||
}
|
||||
console.log(`library: ${files.length} files, ${failures.length} problems${failures.length ? `\n ${failures.join("\n ")}` : ""}`);
|
||||
expect(failures).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user