test_package_init_lazy_openclaw.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from __future__ import annotations
  2. import os
  3. import subprocess
  4. import sys
  5. from pathlib import Path
  6. def _run_inline(code: str) -> str:
  7. repo_src = Path(__file__).resolve().parents[2] / "src"
  8. env = os.environ.copy()
  9. existing_pythonpath = env.get("PYTHONPATH")
  10. env["PYTHONPATH"] = f"{repo_src}{os.pathsep}{existing_pythonpath}" if existing_pythonpath else str(repo_src)
  11. result = subprocess.run(
  12. [sys.executable, "-c", code],
  13. check=True,
  14. capture_output=True,
  15. text=True,
  16. env=env,
  17. )
  18. return result.stdout.strip()
  19. def test_import_agency_swarm_does_not_eager_import_openclaw_module() -> None:
  20. output = _run_inline("import agency_swarm, sys; print('agency_swarm.integrations.openclaw' in sys.modules)")
  21. assert output == "False"
  22. def test_import_agency_swarm_agents_does_not_eager_import_openclaw_agent_module() -> None:
  23. output = _run_inline("import agency_swarm.agents, sys; print('agency_swarm.agents.openclaw' in sys.modules)")
  24. assert output == "False"
  25. def test_openclaw_exports_load_module_lazily() -> None:
  26. output = _run_inline(
  27. "import importlib.util, agency_swarm, sys; "
  28. "has_deps = importlib.util.find_spec('fastapi') is not None and importlib.util.find_spec('httpx') is not None; "
  29. "print('skip' if not has_deps else ('agency_swarm.integrations.openclaw' in sys.modules)); "
  30. "_ = agency_swarm.attach_openclaw_to_fastapi if has_deps else None; "
  31. "print('skip' if not has_deps else ('agency_swarm.integrations.openclaw' in sys.modules))"
  32. )
  33. lines = output.splitlines()
  34. assert len(lines) == 2
  35. if lines[0] == "skip":
  36. assert lines[1] == "skip"
  37. else:
  38. assert lines == ["False", "True"]
  39. def test_openclaw_agent_export_loads_module_lazily() -> None:
  40. output = _run_inline(
  41. "import importlib.util, agency_swarm, sys; "
  42. "has_deps = importlib.util.find_spec('httpx') is not None; "
  43. "print('skip' if not has_deps else ('agency_swarm.agents.openclaw' in sys.modules)); "
  44. "_ = agency_swarm.OpenClawAgent if has_deps else None; "
  45. "print('skip' if not has_deps else ('agency_swarm.agents.openclaw' in sys.modules))"
  46. )
  47. lines = output.splitlines()
  48. assert len(lines) == 2
  49. if lines[0] == "skip":
  50. assert lines[1] == "skip"
  51. else:
  52. assert lines == ["False", "True"]
  53. def test_wildcard_import_agency_swarm_skips_optional_openclaw_exports_without_deps() -> None:
  54. output = _run_inline(
  55. """
  56. import importlib.util
  57. real_find_spec = importlib.util.find_spec
  58. def fake_find_spec(name, package=None):
  59. if name in {"fastapi", "httpx"}:
  60. return None
  61. return real_find_spec(name, package)
  62. importlib.util.find_spec = fake_find_spec
  63. from agency_swarm import * # noqa: F403
  64. print("ok")
  65. """
  66. )
  67. assert output == "ok"
  68. def test_wildcard_import_agency_swarm_agents_skips_openclaw_agent_without_deps() -> None:
  69. output = _run_inline(
  70. """
  71. import importlib.util
  72. real_find_spec = importlib.util.find_spec
  73. def fake_find_spec(name, package=None):
  74. if name == "httpx":
  75. return None
  76. return real_find_spec(name, package)
  77. importlib.util.find_spec = fake_find_spec
  78. from agency_swarm.agents import * # noqa: F403
  79. print("ok")
  80. """
  81. )
  82. assert output == "ok"