from pathlib import Path import pytest from app.bot.handlers import REPOSITORY_REQUEST from app.repository.security import validate_repository_url from app.repository.tools import RepositoryTools def test_repository_url_is_normalized_and_rejects_embedded_credentials() -> None: assert ( validate_repository_url("https://github.com/acme/project/") == "https://github.com/acme/project.git" ) with pytest.raises(ValueError): validate_repository_url("https://token@github.com/acme/project.git") with pytest.raises(ValueError): validate_repository_url("git@github.com:acme/project.git") def test_natural_language_repository_request_is_recognized() -> None: match = REPOSITORY_REQUEST.search("Посмотри этот репозиторий: https://github.com/acme/project") assert match is not None assert match.group(1) == "https://github.com/acme/project" def test_partial_clone_tools_read_only_requested_blob(tmp_path: Path) -> None: repo = tmp_path / "repo" repo.mkdir() import subprocess subprocess.run(["git", "init"], cwd=repo, check=True, capture_output=True) subprocess.run(["git", "config", "user.email", "test@example.test"], cwd=repo, check=True) subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, check=True) (repo / "src").mkdir() (repo / "src" / "app.py").write_text("answer = 42\n", encoding="utf-8") (repo / ".env").write_text("SECRET=nope\n", encoding="utf-8") subprocess.run(["git", "add", "."], cwd=repo, check=True) subprocess.run(["git", "commit", "-m", "initial"], cwd=repo, check=True, capture_output=True) tools = RepositoryTools(repo, max_file_bytes=1_000) assert "src/app.py" in tools.tree() assert ".env" not in tools.tree() assert "1: answer = 42" in tools.read_file("src/app.py") with pytest.raises(ValueError): tools.read_file(".env")