Files
document-haness/scripts/tests/test_project_layout_context.py
T

146 lines
4.6 KiB
Python

from __future__ import annotations
import importlib.util
import json
import os
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "verify-project-layout.py"
spec = importlib.util.spec_from_file_location("verify_project_layout", SCRIPT)
layout = importlib.util.module_from_spec(spec)
spec.loader.exec_module(layout)
def _section(line: int, level: int, title: str, start: int, end: int, text: str) -> dict:
return {
"heading": {"line": line, "level": level, "text": title},
"start_line": start,
"end_line": end,
"text": text,
}
class TechVizContextFallbackTest(unittest.TestCase):
def _check(self, document: str, context: dict) -> bool | None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
doc = root / "document.md"
ctx = root / "context.json"
doc.write_text(document, encoding="utf-8")
ctx.write_text(json.dumps(context, ensure_ascii=False), encoding="utf-8")
return layout._context_snapshot_matches(str(doc), str(ctx))
def test_parent_predecessor_is_only_the_preamble_before_current_child(self):
document = "\n".join([
"# Root",
"root",
"## Parent",
"preamble",
"### Current",
"current",
"### Next",
"next",
"## Tail",
"tail",
])
context = {
"anchor": {"kind": "heading", "value": "Current", "line": 5},
"previous_section": _section(
3, 2, "Parent", 3, 4, "## Parent\npreamble"
),
"current_section": _section(
5, 3, "Current", 5, 6, "### Current\ncurrent"
),
"next_section": _section(
7, 3, "Next", 7, 8, "### Next\nnext"
),
}
self.assertTrue(self._check(document, context))
changed = document.replace("preamble", "changed preamble")
self.assertFalse(self._check(changed, context))
def test_fenced_hash_lines_do_not_end_neighbor_section(self):
document = "\n".join([
"# Root",
"## Current",
"current",
"## Data",
"intro",
"~~~text",
"# not a heading",
"## also not a heading",
"~~~",
"tail",
"## End",
"end",
])
context = {
"anchor": {"kind": "heading", "value": "Current", "line": 2},
"previous_section": _section(
1, 1, "Root", 1, 1, "# Root"
),
"current_section": _section(
2, 2, "Current", 2, 3, "## Current\ncurrent"
),
"next_section": _section(
4, 2, "Data", 4, 10,
"## Data\nintro\n~~~text\n# not a heading\n"
"## also not a heading\n~~~\ntail",
),
}
self.assertTrue(self._check(document, context))
def test_legacy_context_without_anchor_uses_current_heading(self):
document = "\n".join([
"# Root",
"## Previous",
"previous",
"## Current",
"current",
"## Next",
"next",
])
context = {
"previous_section": {
"heading": {"text": "Previous"},
"text": "## Previous\nprevious",
},
"current_section": {
"heading": {"text": "Current"},
"text": "## Current\ncurrent",
},
"next_section": {
"heading": {"text": "Next"},
"text": "## Next\nnext",
},
}
self.assertTrue(self._check(document, context))
def test_keycloak_session_store_seven_fresh_contexts_match_without_techviz(self):
ids = [
"a1-transport-vs-discovery",
"d2-upgrade-direction",
"lab-topology",
"measurement-control",
"observation-points",
"open-questions-answered",
"wrong-predictions",
]
document = ROOT / "docs" / "keycloak-session-store" / "final" / "document.md"
base = ROOT / "docs" / "keycloak-session-store" / "final" / ".techviz"
for diagram_id in ids:
with self.subTest(diagram_id=diagram_id):
context = base / diagram_id / "context.json"
self.assertTrue(
layout._context_snapshot_matches(str(document), str(context))
)
if __name__ == "__main__":
unittest.main()