58 行
1.9 KiB
Python
58 行
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from tests.helpers import install_runtime_stubs
|
|
|
|
install_runtime_stubs()
|
|
|
|
from services.archive import ArchiveService # noqa: E402
|
|
from services.download import DownloadedComic # noqa: E402
|
|
from services.settings import PluginSettings # noqa: E402
|
|
|
|
|
|
class ArchiveServiceTest(unittest.TestCase):
|
|
def test_cbz_contains_comic_info_and_ordered_pages(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
root = Path(temp_dir)
|
|
image_dir = root / "downloaded"
|
|
image_dir.mkdir()
|
|
image_files = []
|
|
for index in range(1, 3):
|
|
path = image_dir / f"{index:05d}.jpg"
|
|
Image.new("RGB", (8, 8), (index * 60, 0, 0)).save(path)
|
|
image_files.append(path)
|
|
|
|
settings = PluginSettings.from_config(
|
|
{"archive_root": str(root / "library"), "archive_format": "cbz"},
|
|
root / "data",
|
|
)
|
|
settings.ensure_dirs()
|
|
comic = DownloadedComic(
|
|
comic_id="123456",
|
|
title="测试标题",
|
|
work_dir=root / "work",
|
|
download_dir=image_dir,
|
|
image_files=image_files,
|
|
)
|
|
|
|
result = asyncio.run(ArchiveService(settings).archive(comic))
|
|
|
|
with zipfile.ZipFile(result.path) as archive:
|
|
names = archive.namelist()
|
|
self.assertIn("ComicInfo.xml", names)
|
|
self.assertIn("0001.jpg", names)
|
|
self.assertIn("0002.jpg", names)
|
|
comic_info = archive.read("ComicInfo.xml").decode("utf-8")
|
|
self.assertIn("<Series>测试标题</Series>", comic_info)
|
|
self.assertIn("<Number>123456</Number>", comic_info)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|