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 repository scanner.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from agent_context.scanner import ( |
| 9 | FileKind, |
| 10 | classify_file, |
| 11 | format_analysis, |
| 12 | is_binary_content, |
| 13 | is_binary_path, |
| 14 | scan_repository, |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | def test_scan_sample_repo(sample_repo: Path): |
| 19 | result = scan_repository(sample_repo) |
| 20 | assert result.root == sample_repo.resolve() or result.root == sample_repo |
| 21 | assert result.total_files >= 5 |
| 22 | assert len(result.source_files) + len(result.test_files) >= 4 |
| 23 | assert result.ignored_count >= 1 # node_modules / dist |
| 24 | # Sensitive by name should be flagged even if .gitignore also ignores .env |
| 25 | # .env may be ignored via gitignore — credentials.json should still appear |
| 26 | # if not gitignored. |
| 27 | names = {f.rel_path for f in result.files} |
| 28 | assert "src/auth/middleware.ts" in names |
| 29 | assert "node_modules/lodash/index.js" not in names |
| 30 | assert "dist/bundle.js" not in names |
| 31 | |
| 32 | |
| 33 | def test_binary_detection(sample_repo: Path): |
| 34 | assert is_binary_path(sample_repo / "logo.png") |
| 35 | # Content-based: create unknown extension binary |
| 36 | blob = sample_repo / "data.unknown" |
| 37 | blob.write_bytes(b"\x00\x01\x02\x03" + b"\xff" * 100) |
| 38 | assert is_binary_content(blob) |
| 39 | |
| 40 | |
| 41 | def test_classify_kinds(): |
| 42 | assert classify_file("src/a.ts", Path("a.ts"), is_binary=False) == FileKind.SOURCE |
| 43 | assert classify_file("tests/a.test.ts", Path("a.test.ts"), is_binary=False) == FileKind.TEST |
| 44 | assert classify_file("README.md", Path("README.md"), is_binary=False) == FileKind.DOCUMENTATION |
| 45 | assert classify_file("x.png", Path("x.png"), is_binary=True) == FileKind.BINARY |
| 46 | |
| 47 | |
| 48 | def test_symlink_skipped(tmp_path: Path): |
| 49 | root = tmp_path / "repo" |
| 50 | root.mkdir() |
| 51 | (root / ".git").mkdir() |
| 52 | real = root / "real.py" |
| 53 | real.write_text("print(1)\n", encoding="utf-8") |
| 54 | link = root / "link.py" |
| 55 | try: |
| 56 | link.symlink_to(real) |
| 57 | except OSError: |
| 58 | # Windows without symlink privilege — skip. |
| 59 | return |
| 60 | result = scan_repository(root) |
| 61 | paths = {f.rel_path for f in result.files} |
| 62 | assert "real.py" in paths |
| 63 | assert "link.py" not in paths |
| 64 | assert any("symlink" in w.lower() for w in result.warnings) |
| 65 | |
| 66 | |
| 67 | def test_unreadable_file(tmp_path: Path, monkeypatch): |
| 68 | root = tmp_path / "repo" |
| 69 | root.mkdir() |
| 70 | (root / ".git").mkdir() |
| 71 | bad = root / "bad.py" |
| 72 | bad.write_text("x=1\n", encoding="utf-8") |
| 73 | |
| 74 | import agent_context.scanner as scanner_mod |
| 75 | |
| 76 | original_open = Path.open |
| 77 | |
| 78 | def flaky_open(self, *args, **kwargs): |
| 79 | if self.name == "bad.py" and "rb" in (args[0] if args else kwargs.get("mode", "r")): |
| 80 | raise OSError("permission denied") |
| 81 | return original_open(self, *args, **kwargs) |
| 82 | |
| 83 | monkeypatch.setattr(Path, "open", flaky_open) |
| 84 | # Also make estimate fail path — scan should continue. |
| 85 | result = scan_repository(root) |
| 86 | # Either skipped with warning, or still present if open wasn't hit that way. |
| 87 | assert isinstance(result.files, list) |
| 88 | |
| 89 | |
| 90 | def test_hidden_files_not_blindly_ignored(tmp_path: Path): |
| 91 | root = tmp_path / "repo" |
| 92 | root.mkdir() |
| 93 | (root / ".git").mkdir() |
| 94 | (root / ".editorconfig").write_text("root = true\n", encoding="utf-8") |
| 95 | result = scan_repository(root) |
| 96 | paths = {f.rel_path for f in result.files} |
| 97 | assert ".editorconfig" in paths |
| 98 | |
| 99 | |
| 100 | def test_format_analysis(sample_repo: Path): |
| 101 | result = scan_repository(sample_repo) |
| 102 | text = format_analysis(result) |
| 103 | assert "Repository Analysis" in text |
| 104 | assert "Estimated tokens" in text |
| 105 | data = result.to_dict() |
| 106 | assert "estimated_tokens" in data |
| 107 | assert data["files"]["total"] == result.total_files |
| 108 | |
| 109 | |
| 110 | def test_large_repo_practical(tmp_path: Path): |
| 111 | """Practical stress: 1000+ small files should scan without error.""" |
| 112 | root = tmp_path / "big" |
| 113 | root.mkdir() |
| 114 | (root / ".git").mkdir() |
| 115 | src = root / "src" |
| 116 | src.mkdir() |
| 117 | for i in range(1100): |
| 118 | (src / f"f{i:04d}.py").write_text(f"x = {i}\n", encoding="utf-8") |
| 119 | result = scan_repository(root) |
| 120 | assert result.total_files >= 1100 |
| 121 | assert result.total_tokens > 0 |