A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """CLI integration tests.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | import subprocess |
| 7 | import sys |
| 8 | from pathlib import Path |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | from agent_context.cli import main |
| 13 | |
| 14 | |
| 15 | def test_help(capsys): |
| 16 | with pytest.raises(SystemExit) as exc: |
| 17 | main(["--help"]) |
| 18 | assert exc.value.code == 0 |
| 19 | out = capsys.readouterr().out |
| 20 | assert "agent-context" in out |
| 21 | assert "analyze" in out |
| 22 | assert "pack" in out |
| 23 | |
| 24 | |
| 25 | def test_version(capsys): |
| 26 | with pytest.raises(SystemExit) as exc: |
| 27 | main(["--version"]) |
| 28 | assert exc.value.code == 0 |
| 29 | assert "agent-context" in capsys.readouterr().out |
| 30 | |
| 31 | |
| 32 | def test_analyze_text_and_json(sample_repo: Path, capsys): |
| 33 | assert main(["analyze", str(sample_repo)]) == 0 |
| 34 | out = capsys.readouterr().out |
| 35 | assert "Repository Analysis" in out |
| 36 | |
| 37 | assert main(["analyze", str(sample_repo), "--json"]) == 0 |
| 38 | data = json.loads(capsys.readouterr().out) |
| 39 | assert "estimated_tokens" in data |
| 40 | assert data["files"]["total"] >= 1 |
| 41 | |
| 42 | |
| 43 | def test_pack_writes_default(sample_repo: Path, capsys, monkeypatch): |
| 44 | monkeypatch.chdir(sample_repo) |
| 45 | assert main(["pack", "fix authentication middleware", "--tokens", "5000"]) == 0 |
| 46 | out = capsys.readouterr().out |
| 47 | assert "Top matches" in out |
| 48 | ctx = sample_repo / ".agent-context" / "context.md" |
| 49 | assert ctx.is_file() |
| 50 | text = ctx.read_text(encoding="utf-8") |
| 51 | assert "Repository Context" in text |
| 52 | assert "super-secret-key-12345" not in text |
| 53 | |
| 54 | |
| 55 | def test_pack_json_and_stdout(sample_repo: Path, capsys): |
| 56 | assert main(["pack", "auth", str(sample_repo), "--tokens", "4000", "--json"]) == 0 |
| 57 | data = json.loads(capsys.readouterr().out) |
| 58 | assert "selected_files" in data |
| 59 | |
| 60 | assert main(["pack", "auth", str(sample_repo), "--tokens", "4000", "--stdout"]) == 0 |
| 61 | md = capsys.readouterr().out |
| 62 | assert md.startswith("# Repository Context") |
| 63 | |
| 64 | |
| 65 | def test_pack_verbose(sample_repo: Path, capsys): |
| 66 | assert main(["pack", "authentication", str(sample_repo), "--tokens", "4000", "-v"]) == 0 |
| 67 | out = capsys.readouterr().out |
| 68 | assert "signals:" in out |
| 69 | |
| 70 | |
| 71 | def test_explain(sample_repo: Path, capsys): |
| 72 | assert main(["explain", str(sample_repo)]) == 0 |
| 73 | out = capsys.readouterr().out |
| 74 | assert "Repository Map" in out |
| 75 | assert "Detected technology" in out or "Likely architecture" in out |
| 76 | |
| 77 | assert main(["explain", str(sample_repo), "--json"]) == 0 |
| 78 | data = json.loads(capsys.readouterr().out) |
| 79 | assert "detected_technology" in data |
| 80 | |
| 81 | |
| 82 | def test_tokens_command(sample_repo: Path, capsys): |
| 83 | readme = sample_repo / "README.md" |
| 84 | assert main(["tokens", str(readme)]) == 0 |
| 85 | out = capsys.readouterr().out |
| 86 | assert "Estimated tokens" in out |
| 87 | |
| 88 | assert main(["tokens", str(readme), "--json"]) == 0 |
| 89 | data = json.loads(capsys.readouterr().out) |
| 90 | assert data["estimated_tokens"] >= 1 |
| 91 | |
| 92 | |
| 93 | def test_invalid_path(): |
| 94 | assert main(["analyze", "/nonexistent/path/xyz"]) == 2 |
| 95 | |
| 96 | |
| 97 | def test_invalid_tokens(sample_repo: Path): |
| 98 | assert main(["pack", "auth", str(sample_repo), "--tokens", "0"]) == 2 |
| 99 | |
| 100 | |
| 101 | def test_config_loading(sample_repo: Path, capsys): |
| 102 | (sample_repo / ".agent-context.toml").write_text( |
| 103 | "[context]\nmax_tokens = 3000\n\n[security]\nredact_secrets = true\n", |
| 104 | encoding="utf-8", |
| 105 | ) |
| 106 | assert main(["pack", "auth", str(sample_repo), "--json"]) == 0 |
| 107 | data = json.loads(capsys.readouterr().out) |
| 108 | assert data["budget"] == 3000 |
| 109 | |
| 110 | |
| 111 | def test_bad_config(sample_repo: Path): |
| 112 | (sample_repo / ".agent-context.toml").write_text("not = [valid\n", encoding="utf-8") |
| 113 | assert main(["analyze", str(sample_repo)]) == 2 |
| 114 | |
| 115 | |
| 116 | def test_installed_entry_point(sample_repo: Path): |
| 117 | """Verify console script is importable / runnable via python -m.""" |
| 118 | proc = subprocess.run( |
| 119 | [sys.executable, "-m", "agent_context.cli", "--version"], |
| 120 | capture_output=True, |
| 121 | text=True, |
| 122 | check=False, |
| 123 | ) |
| 124 | assert proc.returncode == 0 |
| 125 | assert "agent-context" in proc.stdout |