Files
life-echo/api/tests/test_chapter_markdown_compose.py

67 lines
2.2 KiB
Python
Raw Normal View History

2026-03-20 15:15:35 +08:00
import unittest
from app.features.memoir.chapter_markdown_compose import (
compose_ordered_stories_to_markdown,
compose_ordered_stories_to_pdf_markdown,
2026-03-20 15:15:35 +08:00
materialize_chapter_markdown_from_loaded_chapter,
)
class ChapterMarkdownComposeTest(unittest.TestCase):
def test_joins_bodies_with_hr_only_no_story_headings(self):
2026-03-20 15:15:35 +08:00
md = compose_ordered_stories_to_markdown(
[("第一章", "正文A"), ("第二章", "正文B")]
)
self.assertNotIn("##", md)
2026-03-20 15:15:35 +08:00
self.assertIn("正文A", md)
self.assertIn("正文B", md)
self.assertIn("\n\n---\n\n", md)
self.assertTrue(md.index("正文A") < md.index("---"))
self.assertTrue(md.index("---") < md.index("正文B"))
2026-03-20 15:15:35 +08:00
def test_preserves_asset_refs(self):
body = "![x](asset://abc-123)"
md = compose_ordered_stories_to_markdown([("S", body)])
self.assertIn("asset://abc-123", md)
def test_empty_title_still_composes_body(self):
2026-03-20 15:15:35 +08:00
md = compose_ordered_stories_to_markdown([("", "仅正文")])
self.assertEqual(md, "仅正文")
self.assertNotIn("##", md)
2026-03-20 15:15:35 +08:00
def test_empty_body_skipped(self):
2026-03-20 15:15:35 +08:00
md = compose_ordered_stories_to_markdown([("仅标题", "")])
self.assertEqual(md, "")
def test_pdf_markdown_includes_story_headings(self):
md = compose_ordered_stories_to_pdf_markdown(
[("第一章", "正文A"), ("第二章", "正文B")]
)
self.assertIn("## 第一章", md)
self.assertIn("## 第二章", md)
2026-03-20 15:15:35 +08:00
def test_materialize_respects_order_index(self):
class _S:
def __init__(self, title: str, body: str):
self.title = title
self.canonical_markdown = body
class _L:
def __init__(self, o: int, story: _S):
self.order_index = o
self.story = story
ch = type(
"Ch",
(),
{
"story_links": [
_L(1, _S("B", "")),
_L(0, _S("A", "")),
]
},
)()
md = materialize_chapter_markdown_from_loaded_chapter(ch)
self.assertLess(md.index(""), md.index(""))
self.assertNotIn("##", md)