A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Tests for context packing.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from agent_context.packer import ( |
| 8 | format_pack_summary, |
| 9 | pack_context, |
| 10 | render_context_markdown, |
| 11 | write_context, |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | def test_pack_selects_auth_files(sample_repo: Path): |
| 16 | result = pack_context(sample_repo, "fix authentication middleware", token_budget=8000) |
| 17 | paths = [p.rel_path for p in result.selected] |
| 18 | assert any("auth" in p for p in paths) |
| 19 | assert result.markdown.startswith("# Repository Context") |
| 20 | assert "Task" in result.markdown |
| 21 | assert result.total_included_tokens > 0 |
| 22 | |
| 23 | |
| 24 | def test_token_budget_respected(sample_repo: Path): |
| 25 | small = pack_context(sample_repo, "authentication", token_budget=500) |
| 26 | large = pack_context(sample_repo, "authentication", token_budget=20000) |
| 27 | assert small.total_included_tokens <= large.total_included_tokens + 500 |
| 28 | # Small budget should select fewer or equal files |
| 29 | assert len(small.selected) <= len(large.selected) |
| 30 | |
| 31 | |
| 32 | def test_deterministic_pack(sample_repo: Path): |
| 33 | a = pack_context(sample_repo, "authentication middleware", token_budget=6000) |
| 34 | b = pack_context(sample_repo, "authentication middleware", token_budget=6000) |
| 35 | assert [p.rel_path for p in a.selected] == [p.rel_path for p in b.selected] |
| 36 | assert a.markdown == b.markdown |
| 37 | |
| 38 | |
| 39 | def test_truncation_of_huge_file(tmp_path: Path): |
| 40 | root = tmp_path / "repo" |
| 41 | (root / "src").mkdir(parents=True) |
| 42 | (root / ".git").mkdir() |
| 43 | huge = root / "src" / "auth_middleware.py" |
| 44 | # ~40k tokens worth of content with a keyword near the middle |
| 45 | parts = ["# filler\n"] * 20000 |
| 46 | parts[10000] = "def authentication_middleware():\n return True\n" |
| 47 | huge.write_text("".join(parts), encoding="utf-8") |
| 48 | result = pack_context(root, "authentication middleware", token_budget=2000) |
| 49 | assert result.selected |
| 50 | packed = result.selected[0] |
| 51 | assert packed.truncated |
| 52 | assert "truncated" in packed.content.lower() or "Truncated" in result.markdown |
| 53 | assert packed.included_tokens < packed.original_tokens |
| 54 | |
| 55 | |
| 56 | def test_write_context_atomic(sample_repo: Path, tmp_path: Path): |
| 57 | result = pack_context(sample_repo, "auth", token_budget=4000) |
| 58 | out = tmp_path / "out" / "context.md" |
| 59 | written = write_context(result, output=out) |
| 60 | assert written == out.resolve() |
| 61 | assert out.is_file() |
| 62 | assert "Repository Context" in out.read_text(encoding="utf-8") |
| 63 | # Replace safely a second time |
| 64 | write_context(result, output=out) |
| 65 | assert out.is_file() |
| 66 | assert not list(out.parent.glob("*.tmp")) |
| 67 | |
| 68 | |
| 69 | def test_summary_and_json_dict(sample_repo: Path): |
| 70 | result = pack_context(sample_repo, "auth", token_budget=5000) |
| 71 | summary = format_pack_summary(result, verbose=True) |
| 72 | assert "Top matches" in summary |
| 73 | assert "signals:" in summary |
| 74 | data = result.to_dict() |
| 75 | assert data["task"] == "auth" |
| 76 | assert "selected_files" in data |
| 77 | md = render_context_markdown(result) |
| 78 | assert md == result.markdown |
| 79 | |
| 80 | |
| 81 | def test_ordering_by_score(sample_repo: Path): |
| 82 | result = pack_context(sample_repo, "authentication middleware", token_budget=10000) |
| 83 | scores = [p.score for p in result.selected] |
| 84 | # Selected list follows ranked order (non-increasing scores among main selection) |
| 85 | ranked_scores = [s.score for s in result.ranked[: len(result.selected)]] |
| 86 | assert ranked_scores == sorted(ranked_scores, reverse=True) |