diff --git a/tests/helpers.py b/tests/helpers.py index a0f88c8..d20e050 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -24,3 +24,32 @@ def install_runtime_stubs() -> None: yaml = types.ModuleType("yaml") yaml.safe_dump = lambda *args, **kwargs: "" sys.modules["yaml"] = yaml + + +def install_httpx_stub(status_code: int = 202, text: str = "") -> types.ModuleType: + httpx = types.ModuleType("httpx") + httpx.calls = [] + + class Response: + def __init__(self) -> None: + self.status_code = status_code + self.text = text + + class AsyncClient: + def __init__(self, *args, **kwargs) -> None: + self.args = args + self.kwargs = kwargs + + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb) -> None: + return None + + async def post(self, url, headers=None, auth=None): + httpx.calls.append({"url": url, "headers": headers or {}, "auth": auth}) + return Response() + + httpx.AsyncClient = AsyncClient + sys.modules["httpx"] = httpx + return httpx diff --git a/tests/test_integration_archive_komga.py b/tests/test_integration_archive_komga.py new file mode 100644 index 0000000..320471a --- /dev/null +++ b/tests/test_integration_archive_komga.py @@ -0,0 +1,80 @@ +from __future__ import annotations + +import asyncio +import importlib +import sys +import tempfile +import unittest +import zipfile +from pathlib import Path + +from PIL import Image + +from tests.helpers import install_httpx_stub, install_runtime_stubs + +install_runtime_stubs() +httpx_stub = install_httpx_stub() +sys.modules.pop("services.komga", None) + +from services.archive import ArchiveService # noqa: E402 +from services.download import DownloadedComic # noqa: E402 +from services.settings import PluginSettings # noqa: E402 + +komga_module = importlib.import_module("services.komga") +KomgaClient = komga_module.KomgaClient + + +class ArchiveKomgaIntegrationTest(unittest.TestCase): + def test_archive_then_trigger_komga_scan(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, 4): + path = image_dir / f"{index:05d}.jpg" + Image.new("RGB", (8, 8), (index * 40, 0, 0)).save(path) + image_files.append(path) + + settings = PluginSettings.from_config( + { + "archive_root": str(root / "library"), + "archive_format": "cbz", + "komga": { + "enabled": True, + "base_url": "http://komga.local", + "library_id": "library/中文", + "api_key": "secret", + }, + }, + root / "data", + ) + settings.ensure_dirs() + comic = DownloadedComic( + comic_id="654321", + title="集成测试", + work_dir=root / "work", + download_dir=image_dir, + image_files=image_files, + ) + + archive_result = asyncio.run(ArchiveService(settings).archive(comic)) + scan_result = asyncio.run(KomgaClient(settings.komga).scan_library()) + + self.assertTrue(archive_result.path.exists()) + with zipfile.ZipFile(archive_result.path) as archive: + self.assertIn("ComicInfo.xml", archive.namelist()) + self.assertIn("0003.jpg", archive.namelist()) + + self.assertTrue(scan_result.success) + self.assertEqual(len(httpx_stub.calls), 1) + self.assertEqual( + httpx_stub.calls[0]["url"], + "http://komga.local/api/v1/libraries/library%2F%E4%B8%AD%E6%96%87/scan", + ) + self.assertEqual(httpx_stub.calls[0]["headers"], {"X-API-Key": "secret"}) + self.assertIsNone(httpx_stub.calls[0]["auth"]) + + +if __name__ == "__main__": + unittest.main()