Add Page and SourcePage with lossless reading and source writing
CI / checks (pull_request) Successful in 53s
CI / checks (pull_request) Successful in 53s
Pages keep their original bytes; fields and sections are views. Source pages are read strictly and written in a fixed field order. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
506bcc458a
commit
fb4e5aed1f
4 files changed
+472
No files matched your search
@@ -0,0 +1,175 @@
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from chemenu.domain.errors import FactTimeError, FrontmatterError, InvalidSourceError
|
||||
from chemenu.domain.identity import PageId, PageTitle
|
||||
from chemenu.domain.page import parse_page
|
||||
from chemenu.domain.source import Authority, AuthorityForm, DatingBasis, FactTime
|
||||
from chemenu.domain.source_page import parse_source_page, render_source_page
|
||||
|
||||
ID = "01a238ea-2e6d-7157-ba55-5da7405095ff"
|
||||
TITLE = PageTitle("Note")
|
||||
FACT = FactTime(earliest=date(2026, 11, 28), latest=date(2026, 11, 29), basis=DatingBasis.CAPTURED)
|
||||
|
||||
|
||||
def render(
|
||||
*,
|
||||
aliases: list[str] | None = None,
|
||||
raw_files: list[str] | None = None,
|
||||
fact_time: FactTime = FACT,
|
||||
body: str = "text",
|
||||
) -> bytes:
|
||||
return render_source_page(
|
||||
title=TITLE,
|
||||
id=PageId(ID),
|
||||
aliases=["A"] if aliases is None else aliases,
|
||||
raw_files=["raw/notes/a.md"] if raw_files is None else raw_files,
|
||||
fact_time=fact_time,
|
||||
authority=Authority.ORGANIZATION,
|
||||
authority_form=AuthorityForm.ORAL,
|
||||
body=body,
|
||||
)
|
||||
|
||||
|
||||
def source(**fields: str | None) -> bytes:
|
||||
base: dict[str, str | None] = {
|
||||
"type": '"source"',
|
||||
"raw_files": '["a.md"]',
|
||||
"fact_latest": "2026-11-28",
|
||||
"fact_basis": "captured",
|
||||
"authority": "official",
|
||||
"authority_form": "written",
|
||||
}
|
||||
base.update(fields)
|
||||
lines = [f"{k}: {v}" for k, v in base.items() if v is not None]
|
||||
return "\n".join(["---", f"id: {ID}", *lines, "---", "# Note", ""]).encode()
|
||||
|
||||
|
||||
def test_render_then_parse() -> None:
|
||||
point = datetime(2026, 11, 28, 14, 32, tzinfo=timezone(timedelta(hours=1)))
|
||||
fact = FactTime(earliest=point, latest=point, basis=DatingBasis.CAPTURED)
|
||||
data = render(
|
||||
fact_time=fact,
|
||||
aliases=[],
|
||||
raw_files=["raw/notes/2026-11-28-1432.md"],
|
||||
body="\n \n indented\n\r\nline2\r\n\n\n",
|
||||
)
|
||||
assert data == (
|
||||
b'---\nid: "01a238ea-2e6d-7157-ba55-5da7405095ff"\ntype: "source"\naliases: []\n'
|
||||
b'raw_files: ["raw/notes/2026-11-28-1432.md"]\n'
|
||||
b'fact_earliest: "2026-11-28T14:32:00+01:00"\n'
|
||||
b'fact_latest: "2026-11-28T14:32:00+01:00"\nfact_basis: "captured"\n'
|
||||
b'authority: "organization"\nauthority_form: "oral"\n---\n\n'
|
||||
b"# Note\n\n indented\n\nline2\n"
|
||||
)
|
||||
sp = parse_source_page(TITLE, data)
|
||||
assert sp.fact_time == fact
|
||||
assert sp.page.id == PageId(ID)
|
||||
assert sp.page.title == TITLE
|
||||
assert sp.page.body == "\n# Note\n\n indented\n\nline2\n"
|
||||
assert sp.raw_files == ("raw/notes/2026-11-28-1432.md",)
|
||||
assert sp.authority is Authority.ORGANIZATION
|
||||
assert sp.authority_form is AuthorityForm.ORAL
|
||||
assert parse_page(TITLE, data).raw == sp.page.raw
|
||||
|
||||
assert render(body="").endswith(b"# Note\n")
|
||||
assert render(body=" \n\n").endswith(b"# Note\n")
|
||||
for body in ["# H1", "\n # H1\n", "a\n\n# H1\nb", "a\n```\n# x\n```\n# H1", "#"]:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
render(body=body)
|
||||
for body in ["a\n```\n# x\n```\n", "a\n<!--\n# x\n-->", " # code", "## two", "#hash"]:
|
||||
assert parse_source_page(TITLE, render(body=body)).page.body.endswith("\n")
|
||||
with pytest.raises(InvalidSourceError):
|
||||
render(body="a\ud800")
|
||||
with pytest.raises(FrontmatterError):
|
||||
render(aliases=["\ud800"])
|
||||
|
||||
|
||||
def test_fact_times_are_comparable() -> None:
|
||||
later = FactTime(
|
||||
earliest=date(2026, 12, 1), latest=date(2026, 12, 1), basis=DatingBasis.RECEIVED
|
||||
)
|
||||
first = parse_source_page(TITLE, render())
|
||||
second = parse_source_page(TITLE, render(fact_time=later))
|
||||
assert first.fact_time.precedes(second.fact_time)
|
||||
assert not second.fact_time.precedes(first.fact_time)
|
||||
|
||||
|
||||
def test_comma_survives() -> None:
|
||||
names = ["a,b.md", 'q"uote.md', "x..y/c d.md"]
|
||||
sp = parse_source_page(TITLE, render(raw_files=names, aliases=["a, b", "c"]))
|
||||
assert sp.raw_files == tuple(names)
|
||||
assert sp.page.aliases == ("a, b", "c")
|
||||
|
||||
|
||||
def test_missing_field_raises() -> None:
|
||||
for field in ["type", "raw_files", "fact_latest", "fact_basis", "authority", "authority_form"]:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
parse_source_page(TITLE, source(**{field: None}))
|
||||
assert parse_source_page(TITLE, source(fact_earliest=None)).fact_time.earliest is None
|
||||
for bad in [
|
||||
{"raw_files": "[]"},
|
||||
{"raw_files": "a.md"},
|
||||
{"raw_files": "[[a]]"},
|
||||
{"raw_files": "{a: b}"},
|
||||
{"raw_files": "[a.md, a.md]"},
|
||||
{"fact_latest": "[2026-01-01]"},
|
||||
{"fact_earliest": "{a: b}"},
|
||||
{"fact_basis": "nope"},
|
||||
{"fact_basis": "[captured]"},
|
||||
{"authority": "OFFICIAL"},
|
||||
{"authority": "[a, b]"},
|
||||
{"authority_form": "{a: b}"},
|
||||
{"type": "[source]"},
|
||||
]:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
parse_source_page(TITLE, source(**bad))
|
||||
for bad in [{"fact_latest": "yesterday"}, {"fact_earliest": "2027-01-01"}]:
|
||||
with pytest.raises(FactTimeError):
|
||||
parse_source_page(TITLE, source(**bad))
|
||||
|
||||
|
||||
def test_invalid_raw_file_raises() -> None:
|
||||
bad = [
|
||||
"",
|
||||
"/a",
|
||||
"../a",
|
||||
"a/..",
|
||||
"a/./b",
|
||||
"./a",
|
||||
"a//b",
|
||||
"a/",
|
||||
"a\\b",
|
||||
"a\nb",
|
||||
"a\x00",
|
||||
".",
|
||||
"..",
|
||||
]
|
||||
for name in bad:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
render(raw_files=[name])
|
||||
for names in [[], ["a", "a"]]:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
render(raw_files=names)
|
||||
with pytest.raises(InvalidSourceError):
|
||||
parse_source_page(TITLE, source(raw_files='["a//b"]'))
|
||||
with pytest.raises(InvalidSourceError):
|
||||
parse_source_page(TITLE, source(raw_files='["a\\u0007"]'))
|
||||
assert parse_source_page(TITLE, source(raw_files='["a..b", ".x/..y"]')).raw_files == (
|
||||
"a..b",
|
||||
".x/..y",
|
||||
)
|
||||
|
||||
|
||||
def test_unknown_start_is_omitted() -> None:
|
||||
fact = FactTime(earliest=None, latest=date(2026, 1, 1), basis=DatingBasis.EXIF)
|
||||
data = render(fact_time=fact)
|
||||
assert b"fact_earliest" not in data
|
||||
assert parse_source_page(TITLE, data).fact_time == fact
|
||||
|
||||
|
||||
def test_other_type_raises() -> None:
|
||||
for t in ["note", '""', "Source"]:
|
||||
with pytest.raises(InvalidSourceError):
|
||||
parse_source_page(TITLE, source(type=t))
|
||||
Reference in new issue
Block a user