Automated salvage commit — agent session ended (exit 124). Work in progress, may need continuation.
137 lines
4.4 KiB
Python
137 lines
4.4 KiB
Python
"""Tests for lazarus.cli — CLI commands."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from lazarus.cli import main
|
|
from lazarus.roster import MissionRoster
|
|
|
|
|
|
def _run(argv: list[str], tmp_path: Path, capsys: pytest.CaptureFixture) -> tuple:
|
|
"""Helper: run CLI with a tmp registry and capture stdout as JSON.
|
|
|
|
Prepends --registry before the subcommand so argparse sees it as a global arg.
|
|
"""
|
|
registry = str(tmp_path / "roster.json")
|
|
rc = main(["--registry", registry, *argv])
|
|
out = capsys.readouterr().out
|
|
return json.loads(out), rc
|
|
|
|
|
|
# Patch setup_level1_isolation globally for all CLI tests to avoid permission issues
|
|
@pytest.fixture(autouse=True)
|
|
def no_isolation():
|
|
with patch("lazarus.cli.setup_level1_isolation"):
|
|
yield
|
|
|
|
|
|
def test_invite_creates_mission_cell(tmp_path: Path, capsys: pytest.CaptureFixture) -> None:
|
|
"""invite creates a mission cell directory with cell.json."""
|
|
base = str(tmp_path / "missions")
|
|
result, rc = _run(
|
|
[
|
|
"invite", "agentAlpha",
|
|
"--repo", "https://github.com/test/repo",
|
|
"--base-path", base,
|
|
],
|
|
tmp_path,
|
|
capsys,
|
|
)
|
|
assert rc == 0
|
|
mission_id = result["mission_id"]
|
|
cell_json = tmp_path / "missions" / mission_id / "cell.json"
|
|
assert cell_json.exists()
|
|
|
|
|
|
def test_invite_adds_agent_to_roster(tmp_path: Path, capsys: pytest.CaptureFixture) -> None:
|
|
"""After invite, the agent appears in the roster with status 'invited'."""
|
|
base = str(tmp_path / "missions")
|
|
registry = str(tmp_path / "roster.json")
|
|
|
|
rc = main([
|
|
"--registry", registry,
|
|
"invite", "agentBeta",
|
|
"--repo", "https://github.com/test/repo",
|
|
"--base-path", base,
|
|
])
|
|
out = capsys.readouterr().out
|
|
result = json.loads(out)
|
|
assert rc == 0
|
|
|
|
roster = MissionRoster(Path(registry))
|
|
agents = roster.list_agents(result["mission_id"])
|
|
agent_names = [a.agent_name for a in agents]
|
|
assert "agentBeta" in agent_names
|
|
|
|
|
|
def test_list_shows_missions(tmp_path: Path, capsys: pytest.CaptureFixture) -> None:
|
|
"""list command shows missions that were previously created via invite."""
|
|
base = str(tmp_path / "missions")
|
|
registry = str(tmp_path / "roster.json")
|
|
|
|
# Create a mission first
|
|
main(["--registry", registry, "invite", "agentGamma",
|
|
"--repo", "https://github.com/test/repo", "--base-path", base])
|
|
capsys.readouterr() # discard invite output
|
|
|
|
rc = main(["--registry", registry, "list"])
|
|
out = capsys.readouterr().out
|
|
missions = json.loads(out)
|
|
|
|
assert rc == 0
|
|
assert isinstance(missions, list)
|
|
assert len(missions) >= 1
|
|
|
|
|
|
def test_status_shows_agents(tmp_path: Path, capsys: pytest.CaptureFixture) -> None:
|
|
"""status command shows agent count for a mission."""
|
|
base = str(tmp_path / "missions")
|
|
registry = str(tmp_path / "roster.json")
|
|
|
|
main(["--registry", registry, "invite", "agentDelta",
|
|
"--repo", "https://github.com/test/repo", "--base-path", base])
|
|
invite_out = json.loads(capsys.readouterr().out)
|
|
mission_id = invite_out["mission_id"]
|
|
|
|
rc = main(["--registry", registry, "status", mission_id])
|
|
out = capsys.readouterr().out
|
|
status = json.loads(out)
|
|
|
|
assert rc == 0
|
|
assert status["agent_count"] >= 1
|
|
agent_names = [a["agent_name"] for a in status["agents"]]
|
|
assert "agentDelta" in agent_names
|
|
|
|
|
|
def test_depart_updates_agent_status(tmp_path: Path, capsys: pytest.CaptureFixture) -> None:
|
|
"""depart command marks an agent's status as 'departed'."""
|
|
base = str(tmp_path / "missions")
|
|
registry = str(tmp_path / "roster.json")
|
|
|
|
main(["--registry", registry, "invite", "agentEpsilon",
|
|
"--repo", "https://github.com/test/repo", "--base-path", base])
|
|
invite_out = json.loads(capsys.readouterr().out)
|
|
mission_id = invite_out["mission_id"]
|
|
|
|
rc = main(["--registry", registry, "depart", "agentEpsilon",
|
|
"--mission", mission_id])
|
|
out = capsys.readouterr().out
|
|
result = json.loads(out)
|
|
|
|
assert rc == 0
|
|
assert result["status"] == "departed"
|
|
|
|
# Verify persisted
|
|
roster = MissionRoster(Path(registry))
|
|
agents = roster.list_agents(mission_id)
|
|
epsilon = next(a for a in agents if a.agent_name == "agentEpsilon")
|
|
assert epsilon.status == "departed"
|