A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Extra coverage for edge paths.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import io |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | |
| 9 | from agent_context.cli import main |
| 10 | from agent_context.config import ConfigError, load_config |
| 11 | from agent_context.ignore import IgnoreMatcher, _parse_pattern |
| 12 | from agent_context.mcp import McpServer |
| 13 | from agent_context.packer import pack_context |
| 14 | from agent_context.scanner import FileKind, classify_file, scan_repository |
| 15 | from agent_context.security import contains_likely_secrets, redact_secrets |
| 16 | from agent_context.tokens import estimate_tokens_from_bytes |
| 17 | |
| 18 | |
| 19 | def test_security_py_is_source_not_docs(): |
| 20 | kind = classify_file("src/agent_context/security.py", Path("security.py"), is_binary=False) |
| 21 | assert kind == FileKind.SOURCE |
| 22 | |
| 23 | |
| 24 | def test_readme_still_docs(): |
| 25 | assert classify_file("README.md", Path("README.md"), is_binary=False) == FileKind.DOCUMENTATION |
| 26 | assert classify_file("LICENSE", Path("LICENSE"), is_binary=False) == FileKind.DOCUMENTATION |
| 27 | |
| 28 | |
| 29 | def test_tokens_stdin(monkeypatch, capsys): |
| 30 | monkeypatch.setattr("sys.stdin", io.StringIO("abcd")) |
| 31 | assert main(["tokens"]) == 0 |
| 32 | out = capsys.readouterr().out |
| 33 | assert "Estimated tokens" in out |
| 34 | |
| 35 | |
| 36 | def test_pack_custom_output(sample_repo: Path, tmp_path: Path): |
| 37 | out = tmp_path / "custom.md" |
| 38 | assert main(["pack", "auth", str(sample_repo), "--tokens", "3000", "-o", str(out)]) == 0 |
| 39 | assert out.is_file() |
| 40 | |
| 41 | |
| 42 | def test_mcp_write_context(sample_repo: Path): |
| 43 | server = McpServer(cwd=sample_repo) |
| 44 | stdin = io.StringIO( |
| 45 | json.dumps( |
| 46 | { |
| 47 | "jsonrpc": "2.0", |
| 48 | "id": 1, |
| 49 | "method": "tools/call", |
| 50 | "params": { |
| 51 | "name": "build_context", |
| 52 | "arguments": { |
| 53 | "task": "auth", |
| 54 | "path": str(sample_repo), |
| 55 | "tokens": 3000, |
| 56 | "write": True, |
| 57 | }, |
| 58 | }, |
| 59 | } |
| 60 | ) |
| 61 | + "\n" |
| 62 | ) |
| 63 | stdout = io.StringIO() |
| 64 | server.stdin = stdin |
| 65 | server.stdout = stdout |
| 66 | server.run() |
| 67 | resp = json.loads(stdout.getvalue().strip()) |
| 68 | payload = json.loads(resp["result"]["content"][0]["text"]) |
| 69 | assert payload.get("output") |
| 70 | assert Path(payload["output"]).is_file() |
| 71 | |
| 72 | |
| 73 | def test_gitignore_negation(tmp_path: Path): |
| 74 | (tmp_path / ".gitignore").write_text("*.log\n!keep.log\n", encoding="utf-8") |
| 75 | matcher = IgnoreMatcher.create(tmp_path, use_builtins=False) |
| 76 | assert matcher.is_ignored("app.log") |
| 77 | # Negation support: last matching rule wins |
| 78 | assert not matcher.is_ignored("keep.log") |
| 79 | |
| 80 | |
| 81 | def test_anchored_gitignore(tmp_path: Path): |
| 82 | (tmp_path / ".gitignore").write_text("/build\n", encoding="utf-8") |
| 83 | matcher = IgnoreMatcher.create(tmp_path, use_builtins=False) |
| 84 | assert matcher.is_ignored("build", is_dir=True) or matcher.should_skip_dir("build") |
| 85 | |
| 86 | |
| 87 | def test_config_bad_section_types(tmp_path: Path): |
| 88 | (tmp_path / ".agent-context.toml").write_text( |
| 89 | '[context]\nmax_tokens = "nope"\n', encoding="utf-8" |
| 90 | ) |
| 91 | try: |
| 92 | load_config(tmp_path) |
| 93 | assert False, "expected ConfigError" |
| 94 | except ConfigError: |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | def test_config_bad_redact_type(tmp_path: Path): |
| 99 | (tmp_path / ".agent-context.toml").write_text( |
| 100 | "[security]\nredact_secrets = 1\n", encoding="utf-8" |
| 101 | ) |
| 102 | try: |
| 103 | load_config(tmp_path) |
| 104 | assert False |
| 105 | except ConfigError: |
| 106 | pass |
| 107 | |
| 108 | |
| 109 | def test_empty_bytes_tokens(): |
| 110 | assert estimate_tokens_from_bytes(b"") == 0 |
| 111 | |
| 112 | |
| 113 | def test_parse_pattern_negation(): |
| 114 | rule = _parse_pattern("!important.txt") |
| 115 | assert rule.negated is True |
| 116 | assert rule.pattern == "important.txt" |
| 117 | |
| 118 | |
| 119 | def test_pack_without_redact(sample_repo: Path): |
| 120 | from agent_context.config import Config |
| 121 | |
| 122 | cfg = Config(redact_secrets=False, max_tokens=5000) |
| 123 | # Still excludes .env by filename; inline secrets in settings may appear |
| 124 | result = pack_context(sample_repo, "settings config", config=cfg, token_budget=5000) |
| 125 | assert result.selected |
| 126 | |
| 127 | |
| 128 | def test_mcp_parse_error(sample_repo: Path): |
| 129 | server = McpServer(cwd=sample_repo) |
| 130 | stdin = io.StringIO("{not-json\n") |
| 131 | stdout = io.StringIO() |
| 132 | server.stdin = stdin |
| 133 | server.stdout = stdout |
| 134 | server.run() |
| 135 | resp = json.loads(stdout.getvalue().strip()) |
| 136 | assert resp["error"]["code"] == -32700 |
| 137 | |
| 138 | |
| 139 | def test_structure_no_file_as_dir(sample_repo: Path): |
| 140 | result = pack_context(sample_repo, "auth", token_budget=4000) |
| 141 | assert "conftest.py/" not in result.structure |
| 142 | assert "test_cli.py/" not in result.markdown |