repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/the-system/__tests__/markdown.test.ts

Raw
File outline
import assert from "node:assert/strict";
import { describe, it } from "vitest";
import { MarkdownDocumentError, parseRichMarkdown } from "../markdown.ts";

const rich = `---
id: PX-SPEC-001
type: spec
title: Rich documents
owners: [human, the-system]
review:
  approved: 2026-07-15
  flags: [visual, editable]
---

# Rich documents

> [!IMPORTANT]
> Canonical source remains editable.

Term
:   Definition

| A | B |
| - | - |
| 1 | 2 |

Inline $E = mc^2$.

\`\`\`mermaid
graph LR
  A --> B
\`\`\`

<span data-example="raw">raw HTML</span>
`;

describe("rich Markdown parsing", () => {
	it("parses the complete profile and preserves frontmatter order", () => {
		const result = parseRichMarkdown(rich);
		assert.deepEqual(
			[...result.frontmatter.keys()],
			["id", "type", "title", "owners", "review"],
		);
		assert.deepEqual(
			result.frontmatter.get("review"),
			new Map([
				["approved", "2026-07-15"],
				["flags", ["visual", "editable"]],
			]),
		);
		const tree = JSON.stringify(result.tree);
		for (const nodeType of [
			'"type":"table"',
			'"type":"defList"',
			'"type":"inlineMath"',
			'"lang":"mermaid"',
			'"type":"html"',
		])
			assert.ok(tree.includes(nodeType), nodeType);
	});

	it.each([
		["missing", "# No frontmatter", "frontmatter.missing"],
		["duplicate", "---\nid: A\nid: B\n---\n", "frontmatter.invalid"],
		["alias", "---\na: &a [1]\nb: *a\n---\n", "frontmatter.unsupported"],
	])("rejects %s frontmatter", (_name, source, code) => {
		assert.throws(
			() => parseRichMarkdown(source),
			(error: unknown) =>
				error instanceof MarkdownDocumentError && error.code === code,
		);
	});
});