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 relevance scoring.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from agent_context.scanner import FileInfo, FileKind, scan_repository |
| 8 | from agent_context.scoring import rank_files, score_file, tokenize_task |
| 9 | |
| 10 | |
| 11 | def _info(rel: str, kind: FileKind = FileKind.SOURCE, **kwargs) -> FileInfo: |
| 12 | return FileInfo( |
| 13 | path=Path(rel), |
| 14 | rel_path=rel, |
| 15 | kind=kind, |
| 16 | size_bytes=kwargs.get("size_bytes", 100), |
| 17 | estimated_tokens=kwargs.get("estimated_tokens", 25), |
| 18 | is_generated=kwargs.get("is_generated", False), |
| 19 | is_large=kwargs.get("is_large", False), |
| 20 | extension=Path(rel).suffix, |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | def test_tokenize_filters_stopwords(): |
| 25 | tokens = tokenize_task("fix the authentication middleware bug") |
| 26 | assert "authentication" in tokens |
| 27 | assert "middleware" in tokens |
| 28 | assert "the" not in tokens |
| 29 | assert "fix" not in tokens |
| 30 | |
| 31 | |
| 32 | def test_filename_and_path_relevance(sample_repo: Path): |
| 33 | scan = scan_repository(sample_repo) |
| 34 | ranked = rank_files(scan.context_candidates, "authentication middleware") |
| 35 | paths = [s.rel_path for s in ranked] |
| 36 | assert paths[0].startswith("src/auth/") |
| 37 | auth_paths = [p for p in paths if "auth" in p] |
| 38 | payment = [p for p in paths if "payment" in p] |
| 39 | assert auth_paths |
| 40 | if payment: |
| 41 | assert ranked[0].score > next(s.score for s in ranked if "payment" in s.rel_path) |
| 42 | |
| 43 | |
| 44 | def test_content_and_symbol_match(tmp_path: Path): |
| 45 | path = tmp_path / "auth_service.py" |
| 46 | path.write_text( |
| 47 | "class AuthService:\n" |
| 48 | " def authenticate(self, user):\n" |
| 49 | " return True\n", |
| 50 | encoding="utf-8", |
| 51 | ) |
| 52 | info = FileInfo( |
| 53 | path=path, |
| 54 | rel_path="auth_service.py", |
| 55 | kind=FileKind.SOURCE, |
| 56 | size_bytes=100, |
| 57 | estimated_tokens=30, |
| 58 | ) |
| 59 | scored = score_file(info, ["authenticate", "auth"]) |
| 60 | names = {s.name for s in scored.signals} |
| 61 | assert "filename match" in names or "content match" in names or "symbol match" in names |
| 62 | assert scored.score > 0 |
| 63 | |
| 64 | |
| 65 | def test_generated_penalty(): |
| 66 | info = _info("src/generated/api.ts", is_generated=True) |
| 67 | scored = score_file(info, ["api"]) |
| 68 | assert any(s.name == "generated penalty" for s in scored.signals) |
| 69 | assert scored.score < score_file(_info("src/api/handler.ts"), ["api"]).score |
| 70 | |
| 71 | |
| 72 | def test_large_file_penalty(): |
| 73 | info = _info("src/auth/big.ts", is_large=True, estimated_tokens=20000) |
| 74 | scored = score_file(info, ["auth"]) |
| 75 | assert any(s.name == "large file penalty" for s in scored.signals) |
| 76 | |
| 77 | |
| 78 | def test_test_relevance(sample_repo: Path): |
| 79 | scan = scan_repository(sample_repo) |
| 80 | ranked = rank_files(scan.context_candidates, "authentication") |
| 81 | test = next(s for s in ranked if s.file.kind == FileKind.TEST) |
| 82 | assert test.score > 0 |
| 83 | assert any(s.name == "test match" for s in test.signals) |
| 84 | |
| 85 | |
| 86 | def test_deterministic_ordering(sample_repo: Path): |
| 87 | scan = scan_repository(sample_repo) |
| 88 | a = [s.rel_path for s in rank_files(scan.context_candidates, "auth session")] |
| 89 | b = [s.rel_path for s in rank_files(scan.context_candidates, "auth session")] |
| 90 | assert a == b |
| 91 | |
| 92 | |
| 93 | def test_source_extension_bonus(): |
| 94 | scored = score_file(_info("src/x.ts"), ["nomatchzzz"]) |
| 95 | # No keyword hits but source bonus may still apply with score 5, |
| 96 | # and min_score filtering happens in rank_files. |
| 97 | assert any(s.name == "source file" for s in scored.signals) |