File size: 9,970 Bytes
3fbbaab | 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | """
test_agent_install.py -- Coverage for agent_install (207 LOC).
Agents install as single files (no references dir). Otherwise structurally
similar to skill_install, so the edge matrix mirrors it.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import pytest
from ctx.adapters.claude_code.install import agent_install
from ctx.adapters.claude_code.install import install_utils
# ── Fixtures ─────────────────────────────────────────────────────────────────
@pytest.fixture()
def wiki_dir(tmp_path: Path) -> Path:
root = tmp_path / "wiki"
(root / "entities" / "agents").mkdir(parents=True)
(root / "converted-agents").mkdir(parents=True)
return root
@pytest.fixture()
def agents_dir(tmp_path: Path) -> Path:
root = tmp_path / "agents"
root.mkdir()
return root
@pytest.fixture()
def isolated_manifest(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
manifest = tmp_path / "skill-manifest.json"
monkeypatch.setattr(install_utils, "MANIFEST_PATH", manifest)
return manifest
def _seed_agent(wiki_dir: Path, slug: str, *, status: str = "cataloged") -> None:
(wiki_dir / "converted-agents" / f"{slug}.md").write_text(
f"agent body for {slug}\n", encoding="utf-8"
)
(wiki_dir / "entities" / "agents" / f"{slug}.md").write_text(
f"---\nname: {slug}\nstatus: {status}\n---\nbody\n",
encoding="utf-8",
)
# ── install_agent ────────────────────────────────────────────────────────────
class TestInstallAgent:
def test_invalid_slug(self, wiki_dir: Path, agents_dir: Path) -> None:
r = agent_install.install_agent(
"../evil", wiki_dir=wiki_dir, agents_dir=agents_dir,
)
assert r.status == "failed"
def test_not_in_wiki(self, wiki_dir: Path, agents_dir: Path) -> None:
r = agent_install.install_agent(
"ghost", wiki_dir=wiki_dir, agents_dir=agents_dir,
)
assert r.status == "not-in-wiki"
assert "ctx-agent-mirror" in r.message
def test_happy_path(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
) -> None:
_seed_agent(wiki_dir, "architect")
r = agent_install.install_agent(
"architect", wiki_dir=wiki_dir, agents_dir=agents_dir,
)
assert r.status == "installed"
assert (agents_dir / "architect.md").read_text(encoding="utf-8").startswith("agent body")
m = install_utils.load_manifest()
assert any(
e["skill"] == "architect" and e["entity_type"] == "agent"
for e in m["load"]
)
entity = wiki_dir / "entities" / "agents" / "architect.md"
assert "status: installed" in entity.read_text(encoding="utf-8")
def test_dry_run(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
) -> None:
_seed_agent(wiki_dir, "a")
r = agent_install.install_agent(
"a", wiki_dir=wiki_dir, agents_dir=agents_dir, dry_run=True,
)
assert r.status == "installed"
assert not (agents_dir / "a.md").exists()
assert install_utils.load_manifest()["load"] == []
def test_skipped_existing_reconciles(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
) -> None:
_seed_agent(wiki_dir, "a")
(agents_dir / "a.md").write_text("old\n", encoding="utf-8")
r = agent_install.install_agent(
"a", wiki_dir=wiki_dir, agents_dir=agents_dir,
)
assert r.status == "skipped-existing"
# Manifest reconciled.
assert any(
e["skill"] == "a" and e["entity_type"] == "agent"
for e in install_utils.load_manifest()["load"]
)
def test_skipped_existing_dry_run_preserves_manifest(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
) -> None:
_seed_agent(wiki_dir, "a")
(agents_dir / "a.md").write_text("old\n", encoding="utf-8")
r = agent_install.install_agent(
"a", wiki_dir=wiki_dir, agents_dir=agents_dir, dry_run=True,
)
assert r.status == "skipped-existing"
assert install_utils.load_manifest()["load"] == []
def test_force_overwrites(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
) -> None:
_seed_agent(wiki_dir, "a")
(agents_dir / "a.md").write_text("old\n", encoding="utf-8")
r = agent_install.install_agent(
"a", wiki_dir=wiki_dir, agents_dir=agents_dir, force=True,
)
assert r.status == "installed"
content = (agents_dir / "a.md").read_text(encoding="utf-8")
assert content.startswith("agent body")
# ── _split_slugs ─────────────────────────────────────────────────────────────
class TestSplitSlugs:
def _ns(self, **kwargs: object) -> argparse.Namespace:
ns = argparse.Namespace()
defaults: dict[str, object] = {
"slug": None,
"slugs": None,
"slugs_positional": [],
}
defaults.update(kwargs)
for k, v in defaults.items():
setattr(ns, k, v)
return ns
def test_all_three_sources(self) -> None:
out = agent_install._split_slugs(
self._ns(slug="a", slugs="b,c", slugs_positional=["d"])
)
assert out == ["a", "b", "c", "d"]
def test_trims_comma_empties(self) -> None:
assert agent_install._split_slugs(
self._ns(slugs="x, ,y ,"),
) == ["x", "y"]
# ── main / CLI ───────────────────────────────────────────────────────────────
class TestMain:
def test_no_args_exit_2(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr("sys.argv", ["ctx-agent-install"])
with pytest.raises(SystemExit) as ei:
agent_install.main()
assert ei.value.code == 2
def test_happy(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
_seed_agent(wiki_dir, "a")
monkeypatch.setattr(
"sys.argv",
["ctx-agent-install", "--slug", "a",
"--wiki-dir", str(wiki_dir),
"--agents-dir", str(agents_dir)],
)
with pytest.raises(SystemExit) as ei:
agent_install.main()
assert ei.value.code == 0
assert "[OK]" in capsys.readouterr().out
def test_json_output(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
_seed_agent(wiki_dir, "a")
monkeypatch.setattr(
"sys.argv",
["ctx-agent-install", "--slug", "a",
"--wiki-dir", str(wiki_dir),
"--agents-dir", str(agents_dir),
"--json"],
)
with pytest.raises(SystemExit):
agent_install.main()
payload = json.loads(capsys.readouterr().out)
assert payload[0]["slug"] == "a"
assert payload[0]["status"] == "installed"
def test_multi_slug_dedup(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
_seed_agent(wiki_dir, "a")
_seed_agent(wiki_dir, "b")
monkeypatch.setattr(
"sys.argv",
["ctx-agent-install", "--slug", "a", "--slugs", "a,b",
"--wiki-dir", str(wiki_dir),
"--agents-dir", str(agents_dir),
"--json"],
)
with pytest.raises(SystemExit):
agent_install.main()
payload = json.loads(capsys.readouterr().out)
assert [r["slug"] for r in payload] == ["a", "b"]
def test_not_in_wiki_exit_1(
self,
wiki_dir: Path,
agents_dir: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
"sys.argv",
["ctx-agent-install", "ghost",
"--wiki-dir", str(wiki_dir),
"--agents-dir", str(agents_dir)],
)
with pytest.raises(SystemExit) as ei:
agent_install.main()
assert ei.value.code == 1
def test_skipped_existing_exit_0(
self,
wiki_dir: Path,
agents_dir: Path,
isolated_manifest: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
_seed_agent(wiki_dir, "a")
(agents_dir / "a.md").write_text("x\n", encoding="utf-8")
monkeypatch.setattr(
"sys.argv",
["ctx-agent-install", "--slug", "a",
"--wiki-dir", str(wiki_dir),
"--agents-dir", str(agents_dir)],
)
with pytest.raises(SystemExit) as ei:
agent_install.main()
assert ei.value.code == 0
|