fix: harden memoir image generation flow

This commit is contained in:
Kevin
2026-03-11 11:26:42 +08:00
parent a76cf8da18
commit 00092d34c9
14 changed files with 1162 additions and 69 deletions

View File

@@ -6,6 +6,42 @@ from api.tasks.memoir_tasks import initialize_chapter_images
class MemoirImageBootstrapTest(unittest.TestCase):
def test_initialize_chapter_images_keeps_only_completed_assets_when_disabled(self):
chapter = type(
"ChapterStub",
(),
{
"id": "chapter-1",
"title": "童年的夏天",
"category": "childhood",
"content": "那条路我一直记得。",
"images": [
{
"index": 0,
"placeholder": "{{IMAGE:南方小镇的青石板路}}",
"description": "南方小镇的青石板路",
"status": "completed",
"url": "https://cos.example.com/existing.png",
},
{
"index": 1,
"placeholder": "{{IMAGE:奶奶坐在院子里的藤椅上}}",
"description": "奶奶坐在院子里的藤椅上",
"status": "pending",
"url": None,
},
],
},
)()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "false"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(len(assets), 1)
self.assertEqual(assets[0]["status"], "completed")
self.assertEqual(assets[0]["url"], "https://cos.example.com/existing.png")
self.assertEqual(chapter.images, assets)
def test_initialize_chapter_images_sets_pending_assets_when_enabled(self):
chapter = type(
"ChapterStub",
@@ -86,3 +122,30 @@ class MemoirImageBootstrapTest(unittest.TestCase):
self.assertEqual(len(assets), 1)
self.assertEqual(assets[0]["status"], "pending")
self.assertEqual(assets[0]["placeholder"], "{{IMAGE:1938年初的上海弄堂口冬日萧瑟}}")
def test_initialize_chapter_images_normalizes_invalid_existing_asset_status(self):
chapter = type(
"ChapterStub",
(),
{
"id": "chapter-1",
"title": "童年的夏天",
"category": "childhood",
"content": "开头。\n\n{{IMAGE:南方小镇的青石板路}}\n\n结尾。",
"images": [
{
"index": 0,
"placeholder": "{{IMAGE:南方小镇的青石板路}}",
"description": "南方小镇的青石板路",
"status": "mystery",
}
],
},
)()
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
assets = initialize_chapter_images(chapter)
self.assertEqual(len(assets), 1)
self.assertEqual(assets[0]["status"], "failed")
self.assertEqual(assets[0]["error"], "invalid image status: mystery")