File size: 5,083 Bytes
21ff762 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | """Tests for agent_add existing-update review behavior."""
from __future__ import annotations
import sys
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock
SRC_DIR = Path(__file__).resolve().parents[1]
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
import agent_add # noqa: E402
class _Decision:
allow = True
warnings: tuple[Any, ...] = ()
def _agent_text(
*,
description: str = "Agent that reviews code changes with clear findings.",
model: str = "inherit",
body: str | None = None,
) -> str:
body = body or (
"This agent reviews code and reports concrete risks.\n\n"
"## Review Process\n\n"
"Read the diff, identify regressions, and return prioritized findings."
)
return "\n".join(
[
"---",
"name: reviewer-agent",
f"description: {description}",
f"model: {model}",
"---",
"# reviewer-agent",
"",
body,
]
)
def _setup_paths(tmp_path: Path) -> tuple[Path, Path, Path]:
wiki = tmp_path / "wiki"
agents_dir = tmp_path / "agents"
source = tmp_path / "reviewer-agent.md"
(wiki / "entities" / "agents").mkdir(parents=True)
agents_dir.mkdir(parents=True)
return wiki, agents_dir, source
def _patch_side_effects(monkeypatch: Any) -> MagicMock:
check = MagicMock(return_value=_Decision())
monkeypatch.setattr(agent_add, "check_intake", check)
monkeypatch.setattr(agent_add, "record_embedding", MagicMock())
monkeypatch.setattr(agent_add, "update_index", MagicMock())
monkeypatch.setattr(agent_add, "append_log", MagicMock())
monkeypatch.setattr(agent_add, "ensure_wiki", MagicMock())
return check
def test_existing_agent_review_skips_without_mutating_files(
tmp_path: Path,
monkeypatch: Any,
) -> None:
wiki, agents_dir, source = _setup_paths(tmp_path)
installed = agents_dir / "reviewer-agent.md"
existing_text = _agent_text(
description="Detailed agent with a conservative review process.",
model="sonnet",
)
installed.write_text(existing_text, encoding="utf-8")
entity = wiki / "entities" / "agents" / "reviewer-agent.md"
entity.write_text("# existing entity\n", encoding="utf-8")
entity_text = entity.read_text(encoding="utf-8")
source.write_text(
_agent_text(description="Short agent.", model="haiku"),
encoding="utf-8",
)
check = _patch_side_effects(monkeypatch)
result = agent_add.add_agent(
source_path=source,
name="reviewer-agent",
wiki_path=wiki,
agents_dir=agents_dir,
review_existing=True,
)
assert result["skipped"] is True
assert result["update_required"] is True
assert "Existing agent already exists: reviewer-agent" in result["update_review"]
assert installed.read_text(encoding="utf-8") == existing_text
assert entity.read_text(encoding="utf-8") == entity_text
check.assert_not_called()
def test_existing_agent_update_existing_applies_change(
tmp_path: Path,
monkeypatch: Any,
) -> None:
wiki, agents_dir, source = _setup_paths(tmp_path)
installed = agents_dir / "reviewer-agent.md"
installed.write_text(_agent_text(), encoding="utf-8")
entity = wiki / "entities" / "agents" / "reviewer-agent.md"
entity.write_text("# existing entity\n", encoding="utf-8")
updated_text = _agent_text(
description="Updated agent with stronger review coverage.",
model="opus",
)
source.write_text(updated_text, encoding="utf-8")
_patch_side_effects(monkeypatch)
result = agent_add.add_agent(
source_path=source,
name="reviewer-agent",
wiki_path=wiki,
agents_dir=agents_dir,
review_existing=True,
update_existing=True,
)
assert result["skipped"] is False
assert result["update_required"] is False
assert result["is_new_page"] is False
assert installed.read_text(encoding="utf-8") == updated_text
assert "Updated agent with stronger review coverage" in entity.read_text(
encoding="utf-8"
)
def test_main_existing_agent_prints_update_review(
tmp_path: Path,
monkeypatch: Any,
capsys: Any,
) -> None:
wiki, agents_dir, source = _setup_paths(tmp_path)
installed = agents_dir / "reviewer-agent.md"
installed.write_text(_agent_text(), encoding="utf-8")
source.write_text(
_agent_text(description="Replacement agent."),
encoding="utf-8",
)
_patch_side_effects(monkeypatch)
monkeypatch.setattr(sys, "argv", [
"agent_add.py",
"--agent-path", str(source),
"--name", "reviewer-agent",
"--wiki", str(wiki),
"--agents-dir", str(agents_dir),
])
agent_add.main()
out = capsys.readouterr().out
assert "Existing agent already exists: reviewer-agent" in out
assert "Use the explicit update flag" in out
assert "Replacement agent." not in installed.read_text(encoding="utf-8")
|