import os import sys if hasattr(sys.stdout, 'reconfigure'): sys.stdout.reconfigure(encoding='utf-8', errors='replace') sys.stderr.reconfigure(encoding='utf-8', errors='replace') import json import yaml import time import argparse from typing import Optional, Dict, Any, List, Callable from dataclasses import dataclass, field from datetime import datetime 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 plugin import IPlugin from plugin_factory import PluginFactory from toolkit.proxy_manager import ProxyManager from core_types import ( PluginConfig, Account, Proxy, AppointmentType, AvailabilityStatus, QueryResult, BookResult, PluginStatus ) # 默认插件注册映射表 DEFAULT_PLUGIN_MAP = { "tls_plugin": ("plugins/tls_plugin.py", "TlsPlugin"), "vfs_plugin": ("plugins/vfs_plugin.py", "VfsPlugin"), "usa_plugin": ("plugins/usa_plugin.py", "UsaPlugin"), "pol_plugin": ("plugins/pol_plugin.py", "PolPlugin"), "ita_plugin": ("plugins/ita_plugin.py", "ItaPlugin"), "bel_plugin": ("plugins/bel_plugin.py", "BelPlugin"), "de_plugin": ("plugins/de_plugin.py", "DePlugin"), "bls_plugin": ("plugins/bls_plugin.py", "BlsPlugin"), "grc_plugin": ("plugins/grc_plugin.py", "GrcPlugin"), } @dataclass class StepResult: name: str success: bool duration_sec: float message: str = "" data: Any = None error: Optional[Exception] = None class PluginTester: """ 通用签证插件测试类 """ def __init__( self, plugin_name: str, group_id: str = "test.group", config: Optional[PluginConfig] = None, user_inputs: Optional[Dict[str, Any]] = None, appointment_types: Optional[List[AppointmentType]] = None, custom_module_path: Optional[str] = None, custom_class_name: Optional[str] = None, logger: Optional[Callable[[str], None]] = None ): self.plugin_name = plugin_name self.group_id = group_id self.config = config or self.create_default_config() self.user_inputs = user_inputs or {} self.appointment_types = appointment_types or [] self.custom_module_path = custom_module_path self.custom_class_name = custom_class_name self.custom_logger = logger self.factory = PluginFactory() self._register_plugins() self.plugin: Optional[IPlugin] = None self.results: List[StepResult] = [] def _log(self, message: str, level: str = "INFO"): tag = f"[PluginTester][{self.plugin_name}][{level}]" formatted_msg = f"{tag} {message}" if self.custom_logger: self.custom_logger(formatted_msg) else: print(formatted_msg) def _register_plugins(self): for name, (mod_path, cls_name) in DEFAULT_PLUGIN_MAP.items(): full_path = os.path.join(project_root, mod_path) if os.path.exists(full_path): self.factory.register_plugin(name, full_path, cls_name) if self.custom_module_path and self.custom_class_name: self.factory.register_plugin(self.plugin_name, self.custom_module_path, self.custom_class_name) @staticmethod def parse_proxy_url(proxy_url: str) -> Proxy: """从字符串格式代理地址解析 Proxy 对象""" from urllib.parse import urlparse parsed = urlparse(proxy_url if "://" in proxy_url else f"http://{proxy_url}") return Proxy( id=1, proto=parsed.scheme or "http", ip=parsed.hostname or "127.0.0.1", port=parsed.port or 8080, username=parsed.username or "", password=parsed.password or "" ) @staticmethod def create_default_config( username: str = "test_user@example.com", password: str = "TestPassword123!", proxy_url: Optional[str] = None, free_config: Optional[Dict[str, Any]] = None, debug: bool = True ) -> PluginConfig: """快速构建 PluginConfig 配置对象""" account = Account( id=1, username=username, password=password ) proxy = Proxy() if proxy_url: proxy = PluginTester.parse_proxy_url(proxy_url) return PluginConfig( account=account, proxy=proxy, free_config=free_config or {}, session_max_life=3600, debug=debug ) @classmethod def from_json_config( cls, plugin_name: str, config_path: str, group_id: Optional[str] = None, user_override: Optional[str] = None, password_override: Optional[str] = None, proxy_override: Optional[str] = None ) -> 'PluginTester': """ 从 YAML 配置文件加载并初始化测试器 支持单个插件配置或复合 test_conf.yaml (带 "default" 与 "plugins" 结构) """ with open(config_path, "r", encoding="utf-8") as f: root_data = yaml.safe_load(f) or {} default_data = root_data.get("default", {}) plugin_data = {} if "plugins" in root_data and isinstance(root_data["plugins"], dict): plugin_data = root_data["plugins"].get(plugin_name, {}) elif plugin_name in root_data and isinstance(root_data[plugin_name], dict): plugin_data = root_data[plugin_name] else: plugin_data = root_data # 1. 账号解析 (优先使用 plugin 独立配置,回退到 default,再应用 CLI override) account_data = plugin_data.get("account") or default_data.get("account") or {} username = user_override or account_data.get("username", "") password = password_override or account_data.get("password", "") account = Account( id=account_data.get("id", 1), username=username, password=password ) # 2. 代理解析 (CLI override > plugin/default 的 proxy_pool/proxy-pool > plugin/default 的 proxy 对象) if proxy_override: if proxy_override.startswith("local:") or proxy_override.startswith("cloud:"): raw_proxy = ProxyManager.Instance().get_next_proxy([proxy_override]) proxy = Proxy(**raw_proxy) else: proxy = cls.parse_proxy_url(proxy_override) else: pools = plugin_data.get("proxy_pool") or plugin_data.get("proxy-pool") or default_data.get("proxy_pool") or default_data.get("proxy-pool") if pools: if isinstance(pools, str): pools = [pools] raw_proxy = ProxyManager.Instance().get_next_proxy(pools) proxy = Proxy(**raw_proxy) else: proxy_data = plugin_data.get("proxy") or default_data.get("proxy") proxy = Proxy() if proxy_data: proxy = Proxy( id=proxy_data.get("id", 1), proto=proxy_data.get("proto", "http"), ip=proxy_data.get("ip", ""), port=int(proxy_data.get("port", 8080)), username=proxy_data.get("username", ""), password=proxy_data.get("password", "") ) free_config = plugin_data.get("free_config", {}) session_max_life = plugin_data.get("session_max_life", default_data.get("session_max_life", 3600)) debug = plugin_data.get("debug", default_data.get("debug", True)) config = PluginConfig( account=account, proxy=proxy, free_config=free_config, session_max_life=session_max_life, debug=debug ) resolved_group_id = group_id or plugin_data.get("group_id") or "test.group" user_inputs = plugin_data.get("user_inputs", {}) # 3. 预约类型列表解析 raw_apts = plugin_data.get("appointment_types", []) appointment_types = [] for apt in raw_apts: if isinstance(apt, dict): appointment_types.append(AppointmentType( routing_key=apt.get("routing_key", ""), city=apt.get("city", ""), country=apt.get("country", ""), visa_type=apt.get("visa_type", ""), weight=apt.get("weight", 10) )) return cls( plugin_name=plugin_name, group_id=resolved_group_id, config=config, user_inputs=user_inputs, appointment_types=appointment_types ) @classmethod def load_all_from_json(cls, config_path: str) -> List['PluginTester']: """从复合配置文件加载全部已定义的插件测试实例""" with open(config_path, "r", encoding="utf-8") as f: root_data = json.load(f) testers = [] plugins_dict = root_data.get("plugins") if not plugins_dict or not isinstance(plugins_dict, dict): # 尝试直接检查顶层键 plugins_dict = {k: v for k, v in root_data.items() if k != "default" and isinstance(v, dict)} for plg_name in plugins_dict.keys(): tester = cls.from_json_config(plugin_name=plg_name, config_path=config_path) testers.append(tester) return testers # ========================================================================= # 单步测试阶段 (Step Runners) # ========================================================================= def test_init(self) -> StepResult: """测试 1: 实例化插件并设置配置与日志""" t0 = time.time() self._log("▶ [Step 1/7] Initializing plugin instance and configuration...") try: self.plugin = self.factory.create(self.group_id, self.plugin_name) if not self.plugin: raise RuntimeError(f"Factory failed to create instance for plugin '{self.plugin_name}'") self.plugin.set_config(self.config) self.plugin.set_log(self._log) info = self.plugin.get_plugin_info() self._log(f"✅ Plugin successfully initialized: {info}") res = StepResult(name="test_init", success=True, duration_sec=time.time() - t0, data=info) except Exception as e: self._log(f"❌ test_init failed: {e}", level="ERROR") res = StepResult(name="test_init", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_create_session(self) -> StepResult: """测试 2: 建立会话并执行登录/绕过反爬""" t0 = time.time() self._log("▶ [Step 2/7] Testing create_session()...") if not self.plugin: res = StepResult(name="test_create_session", success=False, duration_sec=0, message="Plugin not initialized") self.results.append(res) return res try: self.plugin.create_session() self._log("✅ create_session() executed successfully.") res = StepResult(name="test_create_session", success=True, duration_sec=time.time() - t0) except Exception as e: self._log(f"❌ create_session() failed: {e}", level="ERROR") res = StepResult(name="test_create_session", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_health_check(self) -> StepResult: """测试 3: 检查会话健康状态""" t0 = time.time() self._log("▶ [Step 3/7] Testing health_check()...") if not self.plugin: res = StepResult(name="test_health_check", success=False, duration_sec=0, message="Plugin not initialized") self.results.append(res) return res try: is_healthy = self.plugin.health_check() self._log(f"✅ health_check() returned: {is_healthy}") res = StepResult(name="test_health_check", success=is_healthy, duration_sec=time.time() - t0, data=is_healthy) except Exception as e: self._log(f"❌ health_check() failed: {e}", level="ERROR") res = StepResult(name="test_health_check", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_query(self, routing_key: Optional[str] = None) -> StepResult: """测试 4: 查询预约号源""" t0 = time.time() self._log("▶ [Step 4/7] Testing query()...") if not self.plugin: res = StepResult(name="test_query", success=False, duration_sec=0, message="Plugin not initialized") self.results.append(res) return res try: # 根据 appointment_types 或 free_config 智能构建 AppointmentType if routing_key: apt_type = AppointmentType( routing_key=routing_key, country=self.config.free_config.get("country_code", "CHN"), city=self.config.free_config.get("center_code", "SHA"), visa_type="Tourist" ) elif self.appointment_types: apt_type = self.appointment_types[0] else: apt_configs = self.config.free_config.get("apt_configs", {}) first_key = list(apt_configs.keys())[0] if apt_configs else f"{self.group_id}.default" apt_type = AppointmentType( routing_key=first_key, country=self.config.free_config.get("country_code", "CHN"), city=self.config.free_config.get("center_code", "SHA"), visa_type="Tourist" ) query_result: QueryResult = self.plugin.query(apt_type) self._log(f"✅ query() returned success={query_result.success}, status={query_result.availability_status}") if query_result.earliest_date: self._log(f"📅 Earliest Available Date: {query_result.earliest_date.strftime('%Y-%m-%d')}") res = StepResult(name="test_query", success=True, duration_sec=time.time() - t0, data=query_result) except Exception as e: self._log(f"❌ query() failed: {e}", level="ERROR") res = StepResult(name="test_query", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_book(self, slot_info: Optional[QueryResult] = None, user_inputs: Optional[Dict] = None) -> StepResult: """测试 5: 执行预约锁定""" t0 = time.time() self._log("▶ [Step 5/7] Testing book()...") if not self.plugin: res = StepResult(name="test_book", success=False, duration_sec=0, message="Plugin not initialized") self.results.append(res) return res if not slot_info: target_apt = self.appointment_types[0] if self.appointment_types else AppointmentType(routing_key=f"{self.group_id}.default") slot_info = QueryResult( success=True, availability_status=AvailabilityStatus.Available, earliest_date=datetime.now(), apt_type=target_apt ) inputs = user_inputs or self.user_inputs or { "first_name": "San", "last_name": "Zhang", "passport_no": "E12345678", "email": self.config.account.username if self.config and self.config.account else "test@example.com" } try: book_res: BookResult = self.plugin.book(slot_info=slot_info, user_inputs=inputs) self._log(f"✅ book() returned: success={book_res.success}, urn={book_res.urn}, date={book_res.book_date} {book_res.book_time}") res = StepResult(name="test_book", success=book_res.success, duration_sec=time.time() - t0, data=book_res) except Exception as e: self._log(f"❌ book() failed: {e}", level="ERROR") res = StepResult(name="test_book", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_status(self) -> StepResult: """测试 6: 获取插件当前状态与指标""" t0 = time.time() self._log("▶ [Step 6/7] Testing get_plugin_status()...") if not self.plugin: res = StepResult(name="test_status", success=False, duration_sec=0, message="Plugin not initialized") self.results.append(res) return res try: status: PluginStatus = self.plugin.get_plugin_status() self._log(f"✅ get_plugin_status(): is_healthy={status.is_healthy}, account={status.account}, age={status.session_age_sec}s") res = StepResult(name="test_status", success=True, duration_sec=time.time() - t0, data=status) except Exception as e: self._log(f"❌ get_plugin_status() failed: {e}", level="ERROR") res = StepResult(name="test_status", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res def test_cleanup(self) -> StepResult: """测试 7: 销毁浏览器与释放临时文件资源""" t0 = time.time() self._log("▶ [Step 7/7] Testing cleanup()...") if not self.plugin: res = StepResult(name="test_cleanup", success=True, duration_sec=0, message="Plugin was not initialized") self.results.append(res) return res try: self.plugin.cleanup() self._log("✅ cleanup() executed successfully.") res = StepResult(name="test_cleanup", success=True, duration_sec=time.time() - t0) except Exception as e: self._log(f"❌ cleanup() failed: {e}", level="ERROR") res = StepResult(name="test_cleanup", success=False, duration_sec=time.time() - t0, error=e, message=str(e)) self.results.append(res) return res # ========================================================================= # Pipeline 流水线全自动执行 # ========================================================================= def run_pipeline( self, test_book: bool = False, user_inputs: Optional[Dict] = None, auto_cleanup: bool = True, keep_on_error: bool = False ) -> bool: """ 顺序执行完整流水线测试并打印汇总诊断报告 """ self.results.clear() self._log("=" * 60) self._log(f"🚀 Starting Full Test Pipeline for [{self.plugin_name}]") self._log("=" * 60) all_passed = True slot_info = None try: # 1. Init r = self.test_init() if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True return False # 2. Create Session r = self.test_create_session() if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True return False # 3. Health Check r = self.test_health_check() if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True # 4. Status Check r = self.test_status() if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True # 5. Query r = self.test_query() if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True else: slot_info = r.data # 6. Book (Optional) if test_book: inputs = user_inputs or self.user_inputs r = self.test_book(slot_info=slot_info, user_inputs=inputs) if not r.success: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True # 7. Final Status self.test_status() except Exception as e: all_passed = False if keep_on_error and getattr(self, "plugin", None): self.plugin.keep_browser_on_error = True self._log(f"💥 Pipeline 发生未捕获异常: {e}", level="ERROR") finally: if not all_passed and keep_on_error: self._log("[PluginTester] ⚠️ 测试发生失败/异常且指定了 --keep-on-error,保留浏览器窗口以便排查分析(跳过 cleanup)。", level="WARN") elif auto_cleanup: self.test_cleanup() self._print_report() return all_passed def _print_report(self): """打印格式化测试结果报告""" print("\n" + "=" * 65) print(f"📊 [Test Report Summary] Plugin: {self.plugin_name} (Group: {self.group_id})") print("=" * 65) print(f"{'Step Name':<20} | {'Status':<10} | {'Duration (s)':<12} | {'Details'}") print("-" * 65) total_duration = 0.0 all_ok = True for res in self.results: total_duration += res.duration_sec status_str = "✅ PASS" if res.success else "❌ FAIL" if not res.success: all_ok = False details = res.message or "" if res.data and not details: details = str(res.data)[:40] print(f"{res.name:<20} | {status_str:<10} | {res.duration_sec:<12.2f} | {details}") print("-" * 65) overall_status = "🎉 ALL TESTS PASSED" if all_ok else "💥 SOME TESTS FAILED" print(f"Overall Result: {overall_status} | Total Time: {total_duration:.2f}s") print("=" * 65 + "\n") # ============================================================================= # CLI 命令行入口 # ============================================================================= def resolve_config_path(config_arg: Optional[str]) -> Optional[str]: """ 智能解析配置文件路径: 1. 传入路径存在则直接使用 2. 检查相对于 project_root 的路径 3. 检查 project_root/config/ 下同名文件 4. 回退到 project_root/config/test_conf.yaml 或 test_conf.yaml """ if config_arg: candidates = [ config_arg, os.path.join(project_root, config_arg), os.path.join(project_root, "config", config_arg), os.path.join(project_root, "config", os.path.basename(config_arg)), ] for c in candidates: if c and os.path.exists(c) and os.path.isfile(c): return os.path.abspath(c) # 默认搜索位置 (仅使用 .yaml / .yml) defaults = [ os.path.join(project_root, "config", "test_conf.yaml"), os.path.join(project_root, "config", "test_conf.yml"), os.path.join(project_root, "test_conf.yaml"), os.path.join(project_root, "test_conf.yml"), os.path.join(os.getcwd(), "config", "test_conf.yaml"), os.path.join(os.getcwd(), "config", "test_conf.yml"), os.path.join(os.getcwd(), "test_conf.yaml"), os.path.join(os.getcwd(), "test_conf.yml") ] for d in defaults: if os.path.exists(d) and os.path.isfile(d): return os.path.abspath(d) return None def main(): parser = argparse.ArgumentParser(description="Universal Plugin Test Runner (PluginTester)") parser.add_argument("plugin_pos", nargs="?", default=None, help="Plugin identifier (positional, e.g. vfs_plugin, tls_plugin)") parser.add_argument("--plugin", "-p", help="Plugin identifier (e.g., vfs_plugin, tls_plugin, or 'all')") parser.add_argument("--group", "-g", help="Group identifier override") parser.add_argument("--config", "-c", default=None, help="Path to YAML configuration file (default: config/test_conf.yaml)") parser.add_argument("--user", "-u", help="Username / Email credential override") parser.add_argument("--password", "-pwd", help="Password credential override") parser.add_argument("--proxy", help="Proxy URL override (e.g. http://user:pass@ip:port)") parser.add_argument("--book", action="store_true", help="Run booking test step") parser.add_argument("--no-cleanup", action="store_true", help="Do not automatically cleanup browser on finish") parser.add_argument("--keep-on-error", action="store_true", default=False, help="Do not close browser if any error/exception occurs during test (default: False)") parser.add_argument("--only-init", action="store_true", help="Only test initialization and exit") args = parser.parse_args() target_plugin = args.plugin or args.plugin_pos config_path = resolve_config_path(args.config) if config_path: print(f"[PluginTester] Using configuration file: {config_path}") elif args.config: print(f"[PluginTester] Error: Specified config file '{args.config}' not found.") sys.exit(1) # 如果指定了单个 plugin if target_plugin and target_plugin.lower() != "all": if config_path: tester = PluginTester.from_json_config( plugin_name=target_plugin, config_path=config_path, group_id=args.group, user_override=args.user, password_override=args.password, proxy_override=args.proxy ) else: print("[PluginTester] Warning: No test_conf.yaml found, using basic default config.") cfg = PluginTester.create_default_config( username=args.user or "test_user@example.com", password=args.password or "TestPassword123!", proxy_url=args.proxy ) tester = PluginTester(plugin_name=target_plugin, group_id=args.group or "test.group", config=cfg) if args.only_init: res = tester.test_init() sys.exit(0 if res.success else 1) passed = tester.run_pipeline( test_book=args.book, auto_cleanup=not args.no_cleanup, keep_on_error=args.keep_on_error ) sys.exit(0 if passed else 1) # 如果未指定 plugin 或 target_plugin == all,且存在配置文件,则遍历执行所有插件 elif config_path: testers = PluginTester.load_all_from_json(config_path) if not testers: print(f"[PluginTester] No plugins found in {config_path}") sys.exit(1) overall_passed = True for tester in testers: if args.group: tester.group_id = args.group if args.user: tester.config.account.username = args.user if args.password: tester.config.account.password = args.password if args.proxy: tester.config.proxy = PluginTester.parse_proxy_url(args.proxy) if args.only_init: res = tester.test_init() if not res.success: overall_passed = False else: passed = tester.run_pipeline( test_book=args.book, auto_cleanup=not args.no_cleanup, keep_on_error=args.keep_on_error ) if not passed: overall_passed = False sys.exit(0 if overall_passed else 1) else: print("[PluginTester] Error: Please specify --plugin or provide a valid --config JSON file.") parser.print_help() sys.exit(1) if __name__ == "__main__": main()