from __future__ import annotations import asyncio import subprocess import sys from pathlib import Path import main from backend.services import workflow_round_worker_runtime def test_plugin_import_coexists_with_an_existing_host_core_package(tmp_path: Path) -> None: """Regression for AstrBot loading its own top-level ``core`` first.""" foreign_core = tmp_path / "core" foreign_core.mkdir() (foreign_core / "__init__.py").write_text("host_marker = 'astrbot'\n", encoding="utf-8") plugin_root = Path(__file__).resolve().parents[1] script = "\n".join( ( "import sys", f"sys.path.insert(0, {str(tmp_path)!r})", "import core", "assert core.host_marker == 'astrbot'", f"sys.path.insert(0, {str(plugin_root)!r})", "import main", "assert main.PLUGIN_NAME == 'astrbot_plugin_imagestudio'", "assert core.host_marker == 'astrbot'", f"assert core.__path__[0] == {str(plugin_root / 'core')!r}", ) ) result = subprocess.run( [sys.executable, "-c", script], cwd=plugin_root, capture_output=True, text=True, timeout=30, check=False, ) assert result.returncode == 0, result.stderr def test_initialize_migrates_before_worker_start_and_route_registration(monkeypatch) -> None: events: list[str] = [] class _WorkerRuntime: def start(self): events.append("worker_start") return {"running": True} def stop(self): events.append("worker_stop") return {"running": False} worker_runtime = _WorkerRuntime() monkeypatch.setattr( main, "configure_astrbot_provider_bridge", lambda context, loop: events.append("bridge_on" if context is not None or loop is not None else "bridge_off"), ) monkeypatch.setattr(main, "apply_astrbot_config_overrides", lambda config: events.append("config") or {}) monkeypatch.setattr(main, "initialize_runtime_database", lambda: events.append("database") or {"initialized": True}) monkeypatch.setattr(main, "register_plugin", lambda context: events.append("register") or {"registered": True}) monkeypatch.setattr( workflow_round_worker_runtime, "create_workflow_round_worker_runtime", lambda **kwargs: events.append("worker_create") or worker_runtime, ) plugin = main.Main(context=object()) async def replay() -> None: events.append("notification_replay") monkeypatch.setattr(plugin, "_replay_round_terminal_notifications", replay) async def exercise() -> None: await plugin.initialize() await asyncio.sleep(0) await plugin.terminate() asyncio.run(exercise()) assert events.index("config") < events.index("database") assert events.index("database") < events.index("worker_create") assert events.index("worker_start") < events.index("register") assert "notification_replay" in events assert events.index("worker_stop") < events.index("bridge_off") def test_worker_start_failure_keeps_plugin_available_for_recovery(monkeypatch) -> None: class _BrokenWorkerRuntime: def start(self): raise RuntimeError("worker startup probe") monkeypatch.setattr(main, "configure_astrbot_provider_bridge", lambda context, loop: None) monkeypatch.setattr(main, "apply_astrbot_config_overrides", lambda config: {}) monkeypatch.setattr(main, "initialize_runtime_database", lambda: {"initialized": True}) monkeypatch.setattr( workflow_round_worker_runtime, "create_workflow_round_worker_runtime", lambda **kwargs: _BrokenWorkerRuntime(), ) plugin = main.Main(context=None) asyncio.run(plugin.initialize()) assert plugin.database_result == {"initialized": True} assert plugin.round_worker_runtime is None assert plugin.round_worker_result == { "running": False, "error_type": "RuntimeError", "message": "worker startup probe", }