Files
timmy-home/tests/test_big_brain_repo_audit.py
Alexander Whitestone 303ae44411
Some checks failed
Smoke Test / smoke (push) Failing after 11s
feat: add big-brain nexus audit artifact (#656)
Merge PR #656
2026-04-14 22:18:20 +00:00

91 lines
2.8 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import patch
from scripts.big_brain_repo_audit import (
build_audit_prompt,
call_ollama_chat,
collect_repo_files,
render_context_bundle,
)
def test_collect_repo_files_skips_ignored_directories(tmp_path: Path) -> None:
repo = tmp_path / "repo"
repo.mkdir()
(repo / "README.md").write_text("# Repo\n")
(repo / "app.js").write_text("console.log('ok');\n")
ignored = repo / ".git"
ignored.mkdir()
(ignored / "config").write_text("secret")
node_modules = repo / "node_modules"
node_modules.mkdir()
(node_modules / "pkg.js").write_text("ignored")
files = collect_repo_files(repo)
rel_paths = [item["path"] for item in files]
assert rel_paths == ["README.md", "app.js"]
def test_render_context_bundle_prioritizes_key_files_and_numbers_lines(tmp_path: Path) -> None:
repo = tmp_path / "repo"
repo.mkdir()
(repo / "README.md").write_text("# Repo\ntruth\n")
(repo / "CLAUDE.md").write_text("rules\n")
(repo / "app.js").write_text("line one\nline two\n")
(repo / "server.py").write_text("print('hi')\n")
bundle = render_context_bundle(repo, repo_name="org/repo", max_chars_per_file=200, max_total_chars=2000)
assert "# Audit Context Bundle — org/repo" in bundle
assert "## File manifest" in bundle
assert "README.md" in bundle
assert "### app.js" in bundle
assert "1|line one" in bundle
assert "2|line two" in bundle
def test_build_audit_prompt_requires_file_line_references() -> None:
prompt = build_audit_prompt("Timmy_Foundation/the-nexus", "context bundle")
assert "Architecture summary" in prompt
assert "Top 5 structural issues" in prompt
assert "Top 3 recommended refactors" in prompt
assert "Security concerns" in prompt
assert "file:line" in prompt
assert "Timmy_Foundation/the-nexus" in prompt
class _FakeResponse:
def __init__(self, payload: dict):
self.payload = json.dumps(payload).encode()
def read(self) -> bytes:
return self.payload
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def test_call_ollama_chat_parses_response() -> None:
with patch(
"scripts.big_brain_repo_audit.urllib.request.urlopen",
return_value=_FakeResponse({"message": {"content": "audit output"}}),
) as mocked:
result = call_ollama_chat("prompt text", model="gemma4:latest", ollama_url="http://localhost:11434", num_ctx=65536)
assert result == "audit output"
request = mocked.call_args.args[0]
payload = json.loads(request.data.decode())
assert payload["model"] == "gemma4:latest"
assert payload["options"]["num_ctx"] == 65536
assert payload["messages"][0]["role"] == "user"