106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from techviz.document import build_context, canonicalize_document
|
|
|
|
|
|
class DocumentContextTests(unittest.TestCase):
|
|
def test_extracts_current_and_neighbor_sections(self) -> None:
|
|
text = """# Title
|
|
|
|
## Before
|
|
|
|
before fact
|
|
|
|
## Target
|
|
|
|
target fact
|
|
|
|
<!-- techviz:generate id=target -->
|
|
|
|
## After
|
|
|
|
after fact
|
|
"""
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "doc.md"
|
|
path.write_text(text, encoding="utf-8")
|
|
context = build_context(path, marker_id="target")
|
|
|
|
self.assertEqual(context["current_section"]["heading"]["text"], "Target")
|
|
self.assertEqual(context["previous_section"]["heading"]["text"], "Before")
|
|
self.assertEqual(context["next_section"]["heading"]["text"], "After")
|
|
self.assertIn("target fact", context["numbered_context"])
|
|
self.assertEqual(context["line_number_space"], "canonical-source-with-managed-blocks-collapsed")
|
|
|
|
def test_generated_block_canonicalizes_to_original_marker(self) -> None:
|
|
original = """# Title
|
|
|
|
## Target
|
|
|
|
fact
|
|
|
|
<!-- techviz:generate id=target -->
|
|
|
|
## After
|
|
|
|
more
|
|
"""
|
|
rendered = """# Title
|
|
|
|
## Target
|
|
|
|
fact
|
|
|
|
<!-- techviz:begin id=target context-sha256=abc -->
|
|
<!-- techviz:generate id=target -->
|
|

|
|
|
|
<details><summary>Description</summary>Long description</details>
|
|
<!-- techviz:end id=target -->
|
|
|
|
## After
|
|
|
|
more
|
|
"""
|
|
self.assertEqual(canonicalize_document(rendered), original)
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
original_path = Path(tmp) / "original.md"
|
|
rendered_path = Path(tmp) / "rendered.md"
|
|
original_path.write_text(original, encoding="utf-8")
|
|
rendered_path.write_text(rendered, encoding="utf-8")
|
|
original_context = build_context(original_path, marker_id="target")
|
|
rendered_context = build_context(rendered_path, marker_id="target")
|
|
self.assertEqual(original_context["document_sha256"], rendered_context["document_sha256"])
|
|
self.assertEqual(original_context["numbered_context"], rendered_context["numbered_context"])
|
|
|
|
def test_parent_preamble_is_trimmed_before_nested_target(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
document = Path(tmp) / "nested.md"
|
|
document.write_text(
|
|
"# System\n\n"
|
|
"## Parent\n\n"
|
|
"Parent-level context.\n\n"
|
|
"### Target\n\n"
|
|
"Target details.\n\n"
|
|
"<!-- techviz:generate id=nested -->\n\n"
|
|
"### Following\n\n"
|
|
"Following details.\n",
|
|
encoding="utf-8",
|
|
)
|
|
context = build_context(document, marker_id="nested")
|
|
previous = context["previous_section"]
|
|
self.assertEqual(previous["heading"]["text"], "Parent")
|
|
self.assertLess(previous["end_line"], context["current_section"]["start_line"])
|
|
self.assertIn("Parent-level context.", previous["text"])
|
|
self.assertNotIn("Target details.", previous["text"])
|
|
self.assertEqual(context["next_section"]["heading"]["text"], "Following")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|