89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
import os
|
||
import unittest
|
||
import unittest.mock
|
||
|
||
from api.tasks.memoir_tasks import initialize_chapter_images
|
||
|
||
|
||
class MemoirImageBootstrapTest(unittest.TestCase):
|
||
def test_initialize_chapter_images_sets_pending_assets_when_enabled(self):
|
||
chapter = type(
|
||
"ChapterStub",
|
||
(),
|
||
{
|
||
"id": "chapter-1",
|
||
"title": "童年的夏天",
|
||
"category": "childhood",
|
||
"content": "那条路我一直记得。\n\n{{{{IMAGE:南方小镇的青石板路}}}}",
|
||
"images": [],
|
||
},
|
||
)()
|
||
|
||
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"], "pending")
|
||
|
||
def test_initialize_chapter_images_preserves_completed_assets_and_adds_only_new_placeholders(self):
|
||
chapter = type(
|
||
"ChapterStub",
|
||
(),
|
||
{
|
||
"id": "chapter-1",
|
||
"title": "童年的夏天",
|
||
"category": "childhood",
|
||
"content": (
|
||
"那条路我一直记得。\n\n"
|
||
"{{{{IMAGE:南方小镇的青石板路}}}}\n\n"
|
||
"奶奶总坐在门口。\n\n"
|
||
"{{{{IMAGE:奶奶坐在院子里的藤椅上}}}}"
|
||
),
|
||
"images": [
|
||
{
|
||
"index": 0,
|
||
"placeholder": "{{{{IMAGE:南方小镇的青石板路}}}}",
|
||
"description": "南方小镇的青石板路",
|
||
"prompt": "A serene southern China town",
|
||
"url": "https://cos.example.com/existing.png",
|
||
"status": "completed",
|
||
"provider": "liblib",
|
||
"style": "watercolor",
|
||
"size": "1024x1024",
|
||
"error": None,
|
||
"created_at": "2026-03-10T10:00:00Z",
|
||
"updated_at": "2026-03-10T10:00:00Z",
|
||
}
|
||
],
|
||
},
|
||
)()
|
||
|
||
with unittest.mock.patch.dict(os.environ, {"MEMOIR_IMAGE_ENABLED": "true"}, clear=False):
|
||
assets = initialize_chapter_images(chapter)
|
||
|
||
self.assertEqual(len(assets), 2)
|
||
self.assertEqual(assets[0]["status"], "completed")
|
||
self.assertEqual(assets[0]["url"], "https://cos.example.com/existing.png")
|
||
self.assertEqual(assets[1]["status"], "pending")
|
||
self.assertEqual(assets[1]["description"], "奶奶坐在院子里的藤椅上")
|
||
|
||
def test_initialize_chapter_images_accepts_double_brace_placeholders(self):
|
||
chapter = type(
|
||
"ChapterStub",
|
||
(),
|
||
{
|
||
"id": "chapter-1",
|
||
"title": "童年的夏天",
|
||
"category": "childhood",
|
||
"content": "开头。\n\n{{IMAGE:1938年初的上海弄堂口,冬日萧瑟}}\n\n结尾。",
|
||
"images": [],
|
||
},
|
||
)()
|
||
|
||
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"], "pending")
|
||
self.assertEqual(assets[0]["placeholder"], "{{IMAGE:1938年初的上海弄堂口,冬日萧瑟}}")
|