test_main.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from __future__ import annotations
  2. import argparse
  3. import runpy
  4. import sys
  5. from types import SimpleNamespace
  6. import pytest
  7. from agency_swarm.cli import main as cli_main
  8. def test_main_dispatches_migrate_agent(monkeypatch: pytest.MonkeyPatch) -> None:
  9. calls: list[tuple[str, str]] = []
  10. def fake_migrate(settings_file: str, output_dir: str) -> int:
  11. calls.append((settings_file, output_dir))
  12. return 7
  13. monkeypatch.setattr(cli_main, "migrate_agent_command", fake_migrate)
  14. monkeypatch.setattr(
  15. sys,
  16. "argv",
  17. ["agency-swarm", "migrate-agent", "settings.json", "--output-dir", "out"],
  18. )
  19. with pytest.raises(SystemExit) as exc_info:
  20. cli_main.main()
  21. assert exc_info.value.code == 7
  22. assert calls == [("settings.json", "out")]
  23. def test_main_dispatches_import_tool(monkeypatch: pytest.MonkeyPatch) -> None:
  24. calls: list[tuple[str | None, str, bool]] = []
  25. def fake_import(tool_name: str | None, directory: str, list_tools: bool) -> int:
  26. calls.append((tool_name, directory, list_tools))
  27. return 3
  28. monkeypatch.setattr(cli_main, "import_tool_command", fake_import)
  29. monkeypatch.setattr(
  30. sys,
  31. "argv",
  32. ["agency-swarm", "import-tool", "IPythonInterpreter", "--directory", "./dest", "--list"],
  33. )
  34. with pytest.raises(SystemExit) as exc_info:
  35. cli_main.main()
  36. assert exc_info.value.code == 3
  37. assert calls == [("IPythonInterpreter", "./dest", True)]
  38. def test_main_create_agent_template_success(monkeypatch: pytest.MonkeyPatch) -> None:
  39. captured_kwargs: dict[str, object] = {}
  40. def fake_create_agent_template(**kwargs: object) -> bool:
  41. captured_kwargs.update(kwargs)
  42. return True
  43. monkeypatch.setattr(cli_main, "create_agent_template", fake_create_agent_template)
  44. monkeypatch.setattr(
  45. sys,
  46. "argv",
  47. [
  48. "agency-swarm",
  49. "create-agent-template",
  50. "Data Analyst",
  51. "--description",
  52. "Analyzes data",
  53. "--model",
  54. "gpt-5.4-mini",
  55. "--reasoning",
  56. "high",
  57. "--max-tokens",
  58. "100",
  59. "--temperature",
  60. "0.2",
  61. "--instructions",
  62. "Be concise",
  63. "--use-txt",
  64. "--path",
  65. "./agents",
  66. ],
  67. )
  68. cli_main.main()
  69. assert captured_kwargs == {
  70. "agent_name": "Data Analyst",
  71. "agent_description": "Analyzes data",
  72. "model": "gpt-5.4-mini",
  73. "reasoning": "high",
  74. "max_tokens": 100,
  75. "temperature": 0.2,
  76. "instructions": "Be concise",
  77. "use_txt": True,
  78. "path": "./agents",
  79. }
  80. def test_main_create_agent_template_failure_exits_one(monkeypatch: pytest.MonkeyPatch) -> None:
  81. monkeypatch.setattr(cli_main, "create_agent_template", lambda **_kwargs: False)
  82. monkeypatch.setattr(sys, "argv", ["agency-swarm", "create-agent-template", "Writer"])
  83. with pytest.raises(SystemExit) as exc_info:
  84. cli_main.main()
  85. assert exc_info.value.code == 1
  86. def test_main_create_agent_template_exception_prints_error(
  87. monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
  88. ) -> None:
  89. def explode(**_kwargs: object) -> bool:
  90. raise RuntimeError("boom")
  91. monkeypatch.setattr(cli_main, "create_agent_template", explode)
  92. monkeypatch.setattr(sys, "argv", ["agency-swarm", "create-agent-template", "Writer"])
  93. with pytest.raises(SystemExit) as exc_info:
  94. cli_main.main()
  95. assert exc_info.value.code == 1
  96. assert "ERROR: boom" in capsys.readouterr().err
  97. def test_main_without_command_prints_help(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
  98. monkeypatch.setattr(sys, "argv", ["agency-swarm"])
  99. cli_main.main()
  100. output = capsys.readouterr().out
  101. assert "Agency Swarm CLI tools" in output
  102. assert "create-agent-template" in output
  103. def test_main_unknown_command_falls_back_to_message(
  104. monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
  105. ) -> None:
  106. monkeypatch.setattr(
  107. argparse.ArgumentParser,
  108. "parse_args",
  109. lambda _self: SimpleNamespace(command="custom-command"),
  110. )
  111. cli_main.main()
  112. assert "Unknown command: custom-command" in capsys.readouterr().out
  113. def test_module_entrypoint_executes_main(monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
  114. monkeypatch.setattr(sys, "argv", ["agency-swarm"])
  115. monkeypatch.delitem(sys.modules, "agency_swarm.cli.main", raising=False)
  116. runpy.run_module("agency_swarm.cli.main", run_name="__main__")
  117. assert "Agency Swarm CLI tools" in capsys.readouterr().out