import pytest from download_service import DownloadService, format_download_tasks class FakeResponse: def __init__(self, data): self.data = data class FakeClient: def __init__(self): self.calls = [] async def get(self, path, *, params=None): self.calls.append(("GET", path, params)) if path != "/api/v1/download/": return FakeResponse({"ok": True}) return FakeResponse([{"download_id": "abc", "name": "沙丘", "state": "下载中"}]) async def delete(self, path, *, params=None): self.calls.append(("DELETE", path, params)) return FakeResponse({"ok": True}) @pytest.fixture def service(): service = DownloadService({}) service._client = FakeClient() return service @pytest.mark.asyncio async def test_download_task_operations(service) -> None: assert (await service.list_tasks())[0]["download_id"] == "abc" assert (await service.start("abc"))["ok"] assert (await service.stop("abc"))["ok"] assert (await service.delete("abc"))["ok"] assert service._client.calls == [ ("GET", "/api/v1/download/", None), ("GET", "/api/v1/download/start/abc", None), ("GET", "/api/v1/download/stop/abc", None), ("DELETE", "/api/v1/download/abc", None), ] def test_format_download_tasks() -> None: text = format_download_tasks( [{"download_id": "abc", "name": "沙丘", "state": "下载中"}] ) assert "abc" in text assert "沙丘" in text assert "下载中" in text assert "当前没有下载任务" in format_download_tasks([])