Files
coding-agent-gitea/tests/test_file_tools.py
T
meeks dfdd8c0931 fix: improve prompts, error messages, and workspace concurrency
- Externalize coordinator, notification, and planning prompts to separate files
- Add workspace mutex for concurrent file operations
- Improve error messages across file_tools, issue_tools, and pr_tools
- Add logging to tool modules for better debugging
- Update tests to match new error message strings
2026-08-01 20:01:46 +02:00

160 lines
5.4 KiB
Python

from unittest.mock import MagicMock
from gitea.client import GiteaClient
from gitea.tools.file_tools import FileTools
def _create_mock_client() -> MagicMock:
"""Create a mock GiteaClient with sub-client attributes."""
mock_client: MagicMock = MagicMock(spec=GiteaClient)
mock_client.files = MagicMock()
return mock_client
def test_get_file_content_string_success() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.return_value = "file content here"
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
assert res == "1: file content here"
mock_client.files.get_file_content.assert_called_once_with(
"owner", "repo", "path/to/file"
)
def test_get_file_content_list_success() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.return_value = ["line1", "line2"]
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
assert res == "1: line1\n2: line2"
def test_get_file_content_failure() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.side_effect = Exception("API Error")
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
assert "Could not retrieve file" in res
assert "path/to/file" in res
def test_get_file_content_with_ref_string_success() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.return_value = "file content here"
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content_with_ref(
"owner", "repo", "path/to/file", "main"
)
assert res == "1: file content here"
mock_client.files.get_file_content.assert_called_once_with(
"owner", "repo", "path/to/file", "main"
)
def test_get_file_content_with_ref_list_success() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.return_value = ["line1", "line2"]
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content_with_ref(
"owner", "repo", "path/to/file", "main"
)
assert res == "1: line1\n2: line2"
def test_get_file_content_with_ref_failure() -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.side_effect = Exception("API Error")
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.get_file_content_with_ref(
"owner", "repo", "path/to/file", "main"
)
assert "Could not retrieve file" in res
assert "path/to/file" in res
def test_commit_file_success() -> None:
mock_client = _create_mock_client()
mock_client.files.update_file.return_value = {}
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.commit_file(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
assert "committed successfully" in res
mock_client.files.update_file.assert_called_once_with(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
def test_commit_file_failure() -> None:
mock_client = _create_mock_client()
mock_client.files.update_file.side_effect = Exception("API Error")
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.commit_file(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
assert "Could not commit file" in res
assert "path/to/file" in res
def test_update_file_success() -> None:
mock_client = _create_mock_client()
mock_client.files.update_file.return_value = {}
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.update_file(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
assert "updated in" in res
mock_client.files.update_file.assert_called_once_with(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
def test_update_file_failure() -> None:
mock_client = _create_mock_client()
mock_client.files.update_file.side_effect = Exception("API Error")
file_tools: FileTools = FileTools(mock_client)
res: str = file_tools.update_file(
"owner", "repo", "path/to/file", "msg", "content", "branch"
)
assert "Could not update file" in res
assert "path/to/file" in res
def test_get_file_content_uses_local_file_when_available(tmp_path: str) -> None:
import os
mock_client = _create_mock_client()
repo_dir = tmp_path / "owner" / "repo"
repo_dir.mkdir(parents=True)
file_path = repo_dir / "path" / "to" / "file"
file_path.parent.mkdir(parents=True)
file_path.write_text("local file content")
file_tools = FileTools(mock_client, str(tmp_path))
res = file_tools.get_file_content("owner", "repo", "path/to/file")
assert res == "1: local file content"
mock_client.files.get_file_content.assert_not_called()
def test_get_file_content_falls_back_to_api_when_no_local(tmp_path: str) -> None:
mock_client = _create_mock_client()
mock_client.files.get_file_content.return_value = "api content"
file_tools = FileTools(mock_client, str(tmp_path))
res = file_tools.get_file_content("owner", "repo", "path/to/file")
assert res == "1: api content"
mock_client.files.get_file_content.assert_called_once_with(
"owner", "repo", "path/to/file"
)