sh2/d2ypp2/tests/test_network_preflight.py
2026-06-03 10:53:02 -05:00

119 lines
5.9 KiB
Python

from __future__ import annotations
import os
import unittest
from unittest.mock import patch
from src import network_preflight
class NetworkPreflightTests(unittest.TestCase):
def test_direct_preflight_clears_proxy_environment(self) -> None:
def fake_probe(url: str, **kwargs):
target = "douyin" if "douyin" in url else ("youtube" if "youtube" in url else "google")
return {"ok": True, "url": url, "target": target, "elapsed_ms": 5, "status": 204}
env = {
"HTTP_PROXY": "http://127.0.0.1:7890",
"HTTPS_PROXY": "http://127.0.0.1:7890",
"ALL_PROXY": "http://127.0.0.1:7890",
"LIVE_YOUTUBE_NETWORK_MODE": "shellcrash",
}
with patch.dict(os.environ, env, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": False, "mixed_port": "7890"}):
result = network_preflight.youtube_network_preflight()
self.assertTrue(result["ok"])
self.assertEqual(result["mode"], "direct")
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "direct")
self.assertNotIn("HTTP_PROXY", os.environ)
self.assertNotIn("HTTPS_PROXY", os.environ)
self.assertNotIn("ALL_PROXY", os.environ)
def test_shellcrash_is_used_only_when_already_running(self) -> None:
def fake_probe(url: str, **kwargs):
proxy = kwargs.get("proxy")
return {"ok": bool(proxy), "url": url, "proxy": proxy or "", "elapsed_ms": 5, "status": 204 if proxy else 0}
with patch.dict(os.environ, {}, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": True, "mixed_port": "7890"}):
result = network_preflight.youtube_network_preflight()
self.assertTrue(result["ok"])
self.assertEqual(result["mode"], "shellcrash")
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "shellcrash")
self.assertIn(".douyin.com", os.environ.get("NO_PROXY", ""))
def test_failed_preflight_keeps_shellcrash_manual(self) -> None:
def fake_probe(url: str, **kwargs):
return {"ok": False, "url": url, "elapsed_ms": 5, "error": "blocked"}
with patch.dict(os.environ, {"HTTP_PROXY": "http://127.0.0.1:7890"}, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": False, "mixed_port": "7890"}):
with patch("src.network_preflight.shellcrash_config_status", return_value={"available": False, "path": "", "bytes": 0}):
result = network_preflight.youtube_network_preflight()
self.assertFalse(result["ok"])
self.assertEqual(result["mode"], "failed")
self.assertIn("ShellCrash is not running", result["message"])
self.assertEqual(os.environ.get("LIVE_YOUTUBE_NETWORK_MODE"), "failed")
self.assertNotIn("HTTP_PROXY", os.environ)
def test_direct_failure_autostarts_shellcrash_when_config_exists(self) -> None:
runtime_states = [
{"running": False, "mixed_port": "7890"},
{"running": True, "mixed_port": "7890"},
{"running": True, "mixed_port": "7890"},
]
def fake_runtime():
return runtime_states.pop(0) if runtime_states else {"running": True, "mixed_port": "7890"}
def fake_probe(url: str, **kwargs):
proxy = kwargs.get("proxy")
return {"ok": bool(proxy), "url": url, "proxy": proxy or "", "elapsed_ms": 5, "status": 204 if proxy else 0}
commands: list[list[str]] = []
def fake_run(args: list[str], **kwargs):
commands.append(args)
if args[:2] == ["systemctl", "list-unit-files"]:
return 0, "shellcrash.service enabled"
return 0, "ok"
with patch.dict(os.environ, {}, clear=False):
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", side_effect=fake_runtime):
with patch("src.network_preflight.shellcrash_config_status", return_value={"available": True, "path": "/etc/ShellCrash/yamls/config.yaml", "bytes": 12}):
with patch("src.network_preflight._stage_shellcrash_runtime_config"):
with patch("src.network_preflight._run", side_effect=fake_run):
with patch("src.network_preflight.time.sleep"):
result = network_preflight.youtube_network_preflight()
self.assertTrue(result["ok"])
self.assertEqual(result["mode"], "shellcrash")
self.assertTrue(result["shellcrash_autostart"]["attempted"])
self.assertIn(["systemctl", "enable", "--now", "shellcrash.service"], commands)
def test_system_probe_reports_google_and_douyin(self) -> None:
def fake_probe(url: str, **kwargs):
return {"ok": True, "url": url, "elapsed_ms": 7, "status": 200}
with patch("src.network_preflight._probe_url", side_effect=fake_probe):
with patch("src.network_preflight.shellcrash_runtime", return_value={"running": False, "mixed_port": "7890"}):
result = network_preflight.system_network_probe()
targets = {item["target"] for item in result["direct"]}
self.assertIn("google", targets)
self.assertIn("youtube", targets)
self.assertIn("douyin", targets)
self.assertEqual(result["mode"], "direct")
self.assertEqual(result["shellcrash_policy"]["recommendation"], "keep_shellcrash_stopped")
if __name__ == "__main__":
unittest.main()