from __future__ import annotations import json import os from pathlib import Path import pytest from tests.e2e._helpers import e2e_enabled from tests.e2e._helpers import log from tests.e2e._helpers import run_cli_live pytestmark = [pytest.mark.e2e] @pytest.fixture def tr(request): return request.config.pluginmanager.getplugin("terminalreporter") @pytest.mark.skipif(not e2e_enabled(), reason="Set WSCRAPER_E2E=1 to run live tests") def test_get_bookmarks_live(tmp_path: Path, tr) -> None: cookie_file = Path(os.getenv("WSCRAPER_COOKIE_FILE", "cookies.txt")) if not cookie_file.exists(): pytest.skip(f"Cookie file not found: {cookie_file}") output_file = tmp_path / "bookmarks.json" log(tr, f"Output file: {output_file}") return_code, output_text = run_cli_live( [ "happyfappy", "--action", "get-bookmarks", "-c", str(cookie_file), "-o", str(output_file), ], tr, ) assert return_code == 0, f"CLI failed:\n{output_text}" assert output_file.exists(), "bookmarks.json was not created" data = json.loads(output_file.read_text(encoding="utf-8")) assert isinstance(data, list), "bookmarks output must be a JSON list" assert len(data) >= 1, "expected at least one bookmark record" log(tr, f"Extracted records: {len(data)}", kind="ok") first = data[0] assert isinstance(first, dict), "bookmark entry must be an object" for required_key in ("pageURL", "isVR", "title", "backgroundImage"): assert required_key in first, f"missing key: {required_key}" assert isinstance(first["pageURL"], str) and first["pageURL"].startswith("http") assert isinstance(first["isVR"], bool) assert isinstance(first["title"], str) and first["title"].strip() != "" @pytest.mark.skipif(not e2e_enabled(), reason="Set WSCRAPER_E2E=1 to run live tests") def test_download_torrent_file_live(tmp_path: Path, tr) -> None: cookie_file = Path(os.getenv("WSCRAPER_COOKIE_FILE", "cookies.txt")) if not cookie_file.exists(): pytest.skip(f"Cookie file not found: {cookie_file}") test_url = os.getenv( "WSCRAPER_TEST_TORRENT_URL", "https://www.happyfappy.net/torrents.php?id=110178", ) output_dir = tmp_path / "torrent" log(tr, f"Output dir: {output_dir}") return_code, output_text = run_cli_live( [ "happyfappy", "--action", "download-torrent-files", "-u", test_url, "-c", str(cookie_file), "-o", str(output_dir), ], tr, ) assert return_code == 0, f"CLI failed:\n{output_text}" assert output_dir.exists(), "torrent output directory was not created" torrent_files = list(output_dir.glob("*.torrent")) assert len(torrent_files) >= 1, "expected at least one .torrent file" log(tr, f"Downloaded .torrent files: {len(torrent_files)}", kind="ok") content = torrent_files[0].read_bytes() assert content.startswith(b"d"), "torrent file should start with bencode dictionary token 'd'" assert b"4:info" in content[:4096], "torrent file should include 'info' dictionary marker"