6c9c7fe7f3
CI / integration tests (3.13) (push) Failing after 1s
Commit lint / pull request title (push) Has been skipped
Docs / links (push) Failing after 1s
CI / unit tests (3.13) (push) Failing after 1s
CI / lint (push) Failing after 1s
CI / integration tests (push) Failing after 1s
CI / package build (push) Failing after 1s
Commit lint / commit messages (push) Failing after 1s
CI / unit tests (push) Failing after 1s
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""Helpers for reading knowledge md files (truth layer) in tests.
|
|
|
|
Provides functions to parse index.md and topic md files so tests can
|
|
assert against the truth layer rather than only the derived SQLite/API
|
|
responses.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import yaml
|
|
|
|
|
|
def read_document_md(doc_dir: Path) -> dict[str, Any]:
|
|
"""Read ``index.md`` from *doc_dir*, return frontmatter + body.
|
|
|
|
Returns:
|
|
``{"frontmatter": dict, "body": str}``
|
|
|
|
Raises:
|
|
FileNotFoundError: If ``index.md`` does not exist.
|
|
"""
|
|
index = doc_dir / "index.md"
|
|
text = index.read_text(encoding="utf-8")
|
|
return _parse_md(text)
|
|
|
|
|
|
def read_topic_mds(doc_dir: Path) -> list[dict[str, Any]]:
|
|
"""Read all ``N_*.md`` topic files, return list of frontmatter + body.
|
|
|
|
Sorted by filename (i.e. by topic_index prefix).
|
|
"""
|
|
results = []
|
|
for f in sorted(doc_dir.iterdir()):
|
|
if f.name == "index.md" or f.suffix != ".md":
|
|
continue
|
|
text = f.read_text(encoding="utf-8")
|
|
parsed = _parse_md(text)
|
|
parsed["filename"] = f.name
|
|
results.append(parsed)
|
|
return results
|
|
|
|
|
|
def find_doc_dir(knowledge_dir: Path, doc_id: str) -> Path | None:
|
|
"""Scan *knowledge_dir* tree to find the directory containing *doc_id*'s index.md.
|
|
|
|
Returns the directory Path, or ``None`` if not found.
|
|
"""
|
|
if not knowledge_dir.exists():
|
|
return None
|
|
for index_md in knowledge_dir.rglob("index.md"):
|
|
text = index_md.read_text(encoding="utf-8")
|
|
parsed = _parse_md(text)
|
|
if parsed["frontmatter"].get("doc_id") == doc_id:
|
|
return index_md.parent
|
|
return None
|
|
|
|
|
|
def _parse_md(text: str) -> dict[str, Any]:
|
|
"""Split YAML frontmatter from body."""
|
|
parts = text.split("---", 2)
|
|
if len(parts) < 3:
|
|
return {"frontmatter": {}, "body": text}
|
|
fm: dict[str, Any] = yaml.safe_load(parts[1]) or {}
|
|
body = parts[2].strip()
|
|
return {"frontmatter": fm, "body": body}
|