import os import sys import unittest project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) if project_root not in sys.path: sys.path.insert(0, project_root) from toolkit.proxy_manager import ProxyManager from vs_types import VSProxy, BizLogicError class TestProxyManager(unittest.TestCase): def setUp(self): self.pm = ProxyManager.Instance() def test_local_proxy_single(self): # 测试 local 池 (单端口) res = self.pm.get_next_proxy(["local:local"]) self.assertIsInstance(res, dict) self.assertEqual(res["ip"], "127.0.0.1") self.assertEqual(res["port"], 7897) # 验证能够无缝构建 VSProxy proxy_obj = VSProxy(**res) self.assertEqual(proxy_obj.port, 7897) def test_local_proxy_multi_pool(self): # 测试 local_multi_pool (多端口 10001 ~ 10306) res = self.pm.get_next_proxy(["local:local_multi_pool"]) self.assertIsInstance(res, dict) self.assertEqual(res["ip"], "127.0.0.1") self.assertTrue(10001 <= res["port"] <= 10400) proxy_obj = VSProxy(**res) self.assertTrue(10001 <= proxy_obj.port <= 10400) def test_invalid_prefix(self): # 测试未带前缀的非法格式,必须抛出 BizLogicError with self.assertRaises(BizLogicError) as ctx: self.pm.get_next_proxy(["proxy-cheap"]) self.assertIn("Must start with 'local:' or 'cloud:'", str(ctx.exception)) def test_nonexistent_local_pool(self): # 测试不存在的本地池 with self.assertRaises(BizLogicError) as ctx: self.pm.get_next_proxy(["local:non_existent_pool_12345"]) self.assertIn("No proxies found in local pools", str(ctx.exception)) if __name__ == "__main__": unittest.main()