2026-03-10 17:02:50 +08:00
|
|
|
|
import os
|
2026-03-10 16:03:49 +08:00
|
|
|
|
import unittest
|
2026-03-10 17:02:50 +08:00
|
|
|
|
import unittest.mock
|
2026-03-10 16:03:49 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.tasks.memoir_tasks import initialize_chapter_images
|
2026-03-10 16:03:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MemoirImageBootstrapTest(unittest.TestCase):
|
2026-03-11 11:26:42 +08:00
|
|
|
|
def test_initialize_chapter_images_keeps_only_completed_assets_when_disabled(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op,直接返回 []"""
|
2026-03-11 11:26:42 +08:00
|
|
|
|
chapter = type(
|
|
|
|
|
|
"ChapterStub",
|
|
|
|
|
|
(),
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": "chapter-1",
|
|
|
|
|
|
"title": "童年的夏天",
|
|
|
|
|
|
"category": "childhood",
|
|
|
|
|
|
},
|
|
|
|
|
|
)()
|
|
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "false"}, clear=False):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 11:26:42 +08:00
|
|
|
|
|
2026-03-10 17:02:50 +08:00
|
|
|
|
def test_initialize_chapter_images_sets_pending_assets_when_enabled(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
2026-03-10 16:03:49 +08:00
|
|
|
|
chapter = type(
|
|
|
|
|
|
"ChapterStub",
|
|
|
|
|
|
(),
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": "chapter-1",
|
|
|
|
|
|
"title": "童年的夏天",
|
|
|
|
|
|
"category": "childhood",
|
|
|
|
|
|
},
|
|
|
|
|
|
)()
|
|
|
|
|
|
|
2026-03-10 17:02:50 +08:00
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
2026-03-10 16:03:49 +08:00
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-10 17:02:50 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_preserves_completed_assets_and_adds_only_new_placeholders(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-10 17:02:50 +08:00
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 10:06:12 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_accepts_double_brace_placeholders(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-11 10:06:12 +08:00
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 11:26:42 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_normalizes_invalid_existing_asset_status(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-11 11:26:42 +08:00
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_preserves_existing_completed_assets_beyond_effective_max(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
|
|
|
|
|
with unittest.mock.patch.dict(
|
|
|
|
|
|
os.environ,
|
2026-03-13 11:12:10 +08:00
|
|
|
|
{"MEMOIR_IMAGE_ENABLED": "true", "MEMOIR_IMAGE_MAX_PER_CHAPTER": "2"},
|
2026-03-11 14:07:02 +08:00
|
|
|
|
clear=False,
|
|
|
|
|
|
):
|
|
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_increases_limit_for_long_content(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
2026-03-11 14:07:02 +08:00
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
|
|
|
|
|
def test_initialize_chapter_images_caps_dynamic_limit_at_max_images_cap(self):
|
2026-03-13 11:12:10 +08:00
|
|
|
|
"""图片初始化已迁移到 _save_narrative_to_sections;此处为兼容 no-op"""
|
|
|
|
|
|
chapter = type("ChapterStub", (), {"id": "chapter-1", "title": "童年的夏天", "category": "childhood"})()
|
2026-03-11 14:07:02 +08:00
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
2026-03-11 14:07:02 +08:00
|
|
|
|
assets = initialize_chapter_images(chapter)
|
|
|
|
|
|
|
2026-03-13 11:12:10 +08:00
|
|
|
|
self.assertEqual(assets, [])
|