"""章节封面与正文插图数量闸门。""" import unittest from app.features.memoir.cover_eligibility import ( MIN_INLINE_BODY_IMAGES_FOR_CHAPTER_COVER, chapter_eligible_for_cover_by_inline_body_image_count, chapter_needs_cover_enqueue, count_chapter_inline_body_images, ) def _md_with_n_inline_images(n: int) -> str: lines = [f"![c](asset://a{i})" for i in range(n)] return "\n\n".join(lines) + "\n\n正文" class CoverEligibilityTest(unittest.TestCase): def test_count_inline_images(self): class Ch: canonical_markdown = _md_with_n_inline_images(4) self.assertEqual(count_chapter_inline_body_images(Ch()), 4) def test_eligible_only_when_more_than_threshold(self): class Ch: pass ch = Ch() ch.canonical_markdown = _md_with_n_inline_images( MIN_INLINE_BODY_IMAGES_FOR_CHAPTER_COVER ) self.assertFalse(chapter_eligible_for_cover_by_inline_body_image_count(ch)) ch.canonical_markdown = _md_with_n_inline_images( MIN_INLINE_BODY_IMAGES_FOR_CHAPTER_COVER + 1 ) self.assertTrue(chapter_eligible_for_cover_by_inline_body_image_count(ch)) def test_chapter_needs_cover_enqueue_requires_count(self): class Ch: cover_asset_id = None ch = Ch() ch.canonical_markdown = _md_with_n_inline_images(2) self.assertFalse(chapter_needs_cover_enqueue(ch)) ch.canonical_markdown = _md_with_n_inline_images(4) self.assertTrue(chapter_needs_cover_enqueue(ch))