Add page identity and frontmatter domain types

PageId (UUIDv7) and PageTitle validate on construction; frontmatter is
split losslessly, read as text-only YAML without anchors or duplicate
keys, and written canonically. All page errors live in domain/errors.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
torbenandClaude Sonnet 5.5 committed 2026-12-04 03:55:01 +01:00
1 parent cce8afada5
commit 6ecdbe856a
5 files changed
+462

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
import sys
import unicodedata
import pytest
from chemenu.domain.errors import FrontmatterError
from chemenu.domain.frontmatter import parse_frontmatter, render_frontmatter, split_document
def test_split_keeps_line_endings() -> None:
assert split_document("---\na: b\n---\nbody\n") == ("a: b\n", "body\n")
assert split_document("---\r\na: b\r\n---\r\nx\r\ny") == ("a: b\r\n", "x\r\ny")
assert split_document("---\ra: b\r---\rx") == ("a: b\r", "x")
assert split_document("---\na: b\n---") == ("a: b\n", "")
assert split_document("---\n---\n") == ("", "")
body = "l1\u2028l2\x0cl3\r\nl4\rl5\n"
assert split_document("---\na: b\u2028c\x0c\n---\r\n" + body) == ("a: b\u2028c\x0c\n", body)
for bad in [
"",
"a\n",
"--- \na\n---\n",
"---\na: b\n",
"\n---\na\n---\n",
"---",
"---\na\n----\n",
]:
with pytest.raises(FrontmatterError):
split_document(bad)
def test_values_stay_text() -> None:
parsed = parse_frontmatter(
"version: 1.10\nflag: no\nday: 2026-11-28\nn: null\nempty:\n"
"tags: [a, 1, true]\nnested:\n k: 0x10\n l:\n - 1e3\n"
)
assert parsed == {
"version": "1.10",
"flag": "no",
"day": "2026-11-28",
"n": "null",
"empty": "",
"tags": ["a", "1", "true"],
"nested": {"k": "0x10", "l": ["1e3"]},
}
@pytest.mark.parametrize(
"block",
[
"",
"\n",
"# only a comment\n",
"a: [\n",
"a: b: c\n",
"- a\n- b\n",
"just text\n",
"~\n",
"a: 1\n---\nb: 2\n",
"a: 1\n...\n---\nb: 2\n",
"? [a, b]\n: c\n",
"? {a: b}\n: c\n",
"a: \x01\n",
"a: \ud800\n",
"a: \ufffe\n",
'a: "\\ud800"\n',
'"\\ud800": a\n',
f"# comment{chr(0x2028)}type: source\n",
f"a: b{chr(0x85)}c\n",
"[" * 100000 + "\n",
],
)
def test_invalid_frontmatter_raises(block: str) -> None:
with pytest.raises(FrontmatterError):
parse_frontmatter(block)
def test_deep_nesting_raises() -> None:
block = "a: " + "[" * 5000 + "]" * 5000 + "\n"
with pytest.raises(FrontmatterError):
parse_frontmatter(block)
# The limit is fixed, not the interpreter's recursion limit.
parse_frontmatter("a: " + "[" * 30 + "]" * 30 + "\n")
with pytest.raises(FrontmatterError, match="nested too deeply"):
parse_frontmatter("a: " + "[" * 40 + "]" * 40 + "\n")
def test_duplicate_key_raises() -> None:
for block in [
"a: 1\na: 2\n",
"x:\n a: 1\n a: 2\n",
"x: {a: 1, a: 2}\n",
"l:\n - a: 1\n a: 2\n",
]:
with pytest.raises(FrontmatterError):
parse_frontmatter(block)
def test_yaml_alias_raises() -> None:
for block in ["a: &x 1\nb: *x\n", "a: &x 1\n", "a: *x\n", "a: [&x 1, *x]\n", "a: &x\n b: c\n"]:
with pytest.raises(FrontmatterError):
parse_frontmatter(block)
def test_render_format() -> None:
assert (
render_frontmatter({"id": "x", "tags": ["a", "b"], "none": []})
== '---\nid: "x"\ntags: ["a", "b"]\nnone: []\n---\n'
)
assert render_frontmatter({"t": 'a"b\\c\u0085\u00e4\U0001f600'}) == (
'---\nt: "a\\"b\\\\c\\u0085\u00e4\U0001f600"\n---\n'
)
def test_render_invalid_input_raises() -> None:
for fields in [{"Bad": "x"}, {"1a": "x"}, {"a-b": "x"}, {"": "x"}, {"a\n": "x"}]:
with pytest.raises(FrontmatterError):
render_frontmatter(fields)
with pytest.raises(FrontmatterError):
render_frontmatter({"a": "x\ud800"})
with pytest.raises(FrontmatterError):
render_frontmatter({"a": ["x\udfff"]})
def test_render_round_trips_any_text() -> None:
step = 2048
for start in range(0, sys.maxunicode + 1, step):
chars = [chr(c) for c in range(start, min(start + step, sys.maxunicode + 1))]
text = "".join(c for c in chars if unicodedata.category(c) != "Cs")
rendered = render_frontmatter({"k": text, "l": [text, "x"]})
block, body = split_document(rendered + "body")
assert body == "body"
assert parse_frontmatter(block) == {"k": text, "l": [text, "x"]}
for text in ["", " ", "---", "a\r\nb", "\ufeffx", "\ufffe\uffff", "#c", "\t"]:
block, _ = split_document(render_frontmatter({"k": text}))
assert parse_frontmatter(block) == {"k": text}