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
|
|
|
|
|
|
|
|
from api.tasks.memoir_tasks import initialize_chapter_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MemoirImageBootstrapTest(unittest.TestCase):
|
2026-03-10 17:02:50 +08:00
|
|
|
def test_initialize_chapter_images_sets_pending_assets_when_enabled(self):
|
2026-03-10 16:03:49 +08:00
|
|
|
chapter = type(
|
|
|
|
|
"ChapterStub",
|
|
|
|
|
(),
|
|
|
|
|
{
|
|
|
|
|
"id": "chapter-1",
|
|
|
|
|
"title": "童年的夏天",
|
|
|
|
|
"category": "childhood",
|
|
|
|
|
"content": "那条路我一直记得。\n\n{{{{IMAGE:南方小镇的青石板路}}}}",
|
|
|
|
|
"images": [],
|
|
|
|
|
},
|
|
|
|
|
)()
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.assertEqual(len(assets), 1)
|
|
|
|
|
self.assertEqual(assets[0]["status"], "pending")
|
2026-03-10 17:02:50 +08:00
|
|
|
|
|
|
|
|
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"], "奶奶坐在院子里的藤椅上")
|