test_import_side_effects.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from __future__ import annotations
  2. import json
  3. import subprocess
  4. import sys
  5. from pathlib import Path
  6. PROJECT_ROOT = Path(__file__).parents[1]
  7. def _run_isolated(code: str) -> dict:
  8. result = subprocess.run(
  9. [sys.executable, "-c", code],
  10. cwd=PROJECT_ROOT,
  11. check=True,
  12. capture_output=True,
  13. text=True,
  14. )
  15. return json.loads(result.stdout)
  16. def test_import_agent_does_not_configure_logging_or_mutate_sys_path() -> None:
  17. result = _run_isolated(
  18. """
  19. import json
  20. import logging
  21. import sys
  22. handlers_before = tuple(logging.getLogger().handlers)
  23. path_before = tuple(sys.path)
  24. import agent
  25. print(json.dumps({
  26. "handlers_unchanged": tuple(logging.getLogger().handlers) == handlers_before,
  27. "path_unchanged": tuple(sys.path) == path_before,
  28. }))
  29. """
  30. )
  31. assert result == {"handlers_unchanged": True, "path_unchanged": True}
  32. def test_packaged_im_client_is_the_builtin_runtime() -> None:
  33. result = _run_isolated(
  34. """
  35. import json
  36. from agent.im_client import IMClient
  37. from agent.tools.builtin.im.chat import IMClient as BuiltinIMClient
  38. print(json.dumps({"same_client": IMClient is BuiltinIMClient}))
  39. """
  40. )
  41. assert result == {"same_client": True}