chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:43:34 +08:00
commit 41fe82d331
768 changed files with 57351 additions and 0 deletions
@@ -0,0 +1,68 @@
import pytest
from unittest.mock import MagicMock, patch
from superagi.helper.github_helper import GithubHelper
from superagi.tools.github.add_file import GithubAddFileTool, GithubAddFileSchema
def test_github_add_file_schema():
schema = GithubAddFileSchema(
repository_name="test_repo",
base_branch="main",
file_name="test_file",
folder_path="test_folder",
commit_message="test_commit",
repository_owner="test_owner"
)
assert schema.repository_name == "test_repo"
assert schema.base_branch == "main"
assert schema.file_name == "test_file"
assert schema.folder_path == "test_folder"
assert schema.commit_message == "test_commit"
assert schema.repository_owner == "test_owner"
@pytest.fixture
def github_add_file_tool():
return GithubAddFileTool()
@patch.object(GithubHelper, "make_fork")
@patch.object(GithubHelper, "create_branch")
@patch.object(GithubHelper, "add_file")
@patch.object(GithubHelper, "create_pull_request")
def test_github_add_file_tool_execute(mock_make_fork, mock_create_branch, mock_add_file, mock_create_pull_request, github_add_file_tool):
github_add_file_tool.toolkit_config.get_tool_config = MagicMock(side_effect=["test_token", "test_username"])
mock_make_fork.return_value = 201
mock_create_branch.return_value = 201
mock_add_file.return_value = 201
mock_create_pull_request.return_value = 201
response = github_add_file_tool._execute(
repository_name="test_repo",
base_branch="main",
commit_message="test_commit",
repository_owner="test_owner",
file_name="test_file",
folder_path="test_folder"
)
assert response == "Pull request to add file/folder has been created"
mock_make_fork.return_value = 422
mock_create_branch.return_value = 422
mock_add_file.return_value = 422
mock_create_pull_request.return_value = 422
response = github_add_file_tool._execute(
repository_name="test_repo",
base_branch="main",
commit_message="test_commit",
repository_owner="test_owner",
file_name="test_file",
folder_path="test_folder"
)
assert response == "Error: Unable to add file/folder to repository "
@@ -0,0 +1,58 @@
import pytest
from unittest.mock import patch, Mock
from pydantic import ValidationError
from superagi.tools.github.fetch_pull_request import GithubFetchPullRequest, GithubFetchPullRequestSchema
@pytest.fixture
def mock_github_helper():
with patch('superagi.tools.github.fetch_pull_request.GithubHelper') as MockGithubHelper:
yield MockGithubHelper
@pytest.fixture
def tool(mock_github_helper):
tool = GithubFetchPullRequest()
tool.toolkit_config = Mock()
tool.toolkit_config.side_effect = ['dummy_token', 'dummy_username']
mock_github_helper_instance = mock_github_helper.return_value
mock_github_helper_instance.get_pull_requests_created_in_last_x_seconds.return_value = ['url1', 'url2']
return tool
def test_execute(tool, mock_github_helper):
mock_github_helper_instance = mock_github_helper.return_value
# Execute the method
result = tool._execute('repo_name', 'repo_owner', 86400)
# Verify results
assert result == "Pull requests: ['url1', 'url2']"
mock_github_helper_instance.get_pull_requests_created_in_last_x_seconds.assert_called_once_with('repo_owner',
'repo_name', 86400)
def test_schema_validation():
# Valid data
valid_data = {'repository_name': 'repo', 'repository_owner': 'owner', 'time_in_seconds': 86400}
GithubFetchPullRequestSchema(**valid_data)
# Invalid data
invalid_data = {'repository_name': 'repo', 'repository_owner': 'owner', 'time_in_seconds': 'string'}
with pytest.raises(ValidationError):
GithubFetchPullRequestSchema(**invalid_data)
def test_execute_error(mock_github_helper):
tool = GithubFetchPullRequest()
tool.toolkit_config = Mock()
tool.toolkit_config.side_effect = ['dummy_token', 'dummy_username']
mock_github_helper_instance = mock_github_helper.return_value
mock_github_helper_instance.get_pull_requests_created_in_last_x_seconds.side_effect = Exception('An error occurred')
# Execute the method
result = tool._execute('repo_name', 'repo_owner', 86400)
# Verify results
assert result == 'Error: Unable to fetch pull requests An error occurred'
@@ -0,0 +1,42 @@
from unittest.mock import MagicMock, patch
from superagi.tools.github.delete_file import GithubDeleteFileTool, GithubDeleteFileSchema
def test_github_delete_file_tool():
# Test case: Successfully delete a file and create a pull request
with patch("superagi.tools.github.delete_file.GithubHelper") as mock_github_helper:
mock_github_helper.return_value.make_fork.return_value = 201
mock_github_helper.return_value.create_branch.return_value = 201
mock_github_helper.return_value.sync_branch.return_value = None
mock_github_helper.return_value.delete_file.return_value = 200
mock_github_helper.return_value.create_pull_request.return_value = 201
tool = GithubDeleteFileTool()
tool.toolkit_config.get_tool_config = MagicMock(side_effect=["GITHUB_ACCESS_TOKEN", "GITHUB_USERNAME"])
args = GithubDeleteFileSchema(
repository_name="test_repo",
base_branch="main",
file_name="test_file.txt",
folder_path="test_folder",
commit_message="Delete test_file.txt",
repository_owner="test_owner"
)
result = tool._execute("test_repo", "main", "test_file.txt", "Delete test_file.txt", "test_owner")
assert result == "Pull request to Delete test_file.txt has been created"
# Test case: Error while deleting file
with patch("superagi.tools.github.delete_file.GithubHelper") as mock_github_helper:
mock_github_helper.return_value.make_fork.return_value = 201
mock_github_helper.return_value.create_branch.return_value = 201
mock_github_helper.return_value.sync_branch.return_value = None
mock_github_helper.return_value.delete_file.return_value = 400
mock_github_helper.return_value.create_pull_request.return_value = 201
tool = GithubDeleteFileTool()
tool.toolkit_config.get_tool_config = MagicMock(side_effect=["GITHUB_ACCESS_TOKEN", "GITHUB_USERNAME"])
result = tool._execute("test_repo", "main", "test_file.txt", "Delete test_file.txt", "test_owner")
assert result == "Error while deleting file"
@@ -0,0 +1,82 @@
import pytest
from unittest.mock import patch, Mock
import pytest_mock
from pydantic import ValidationError
from superagi.tools.github.review_pull_request import GithubReviewPullRequest
class MockLLM:
def get_model(self):
return "some_model"
class MockTokenCounter:
@staticmethod
def count_message_tokens(message, model):
# Mocking the token count based on the length of the content.
# Replace this logic as needed.
return len(message[0]['content'])
def test_split_pull_request_content_into_multiple_parts():
tool = GithubReviewPullRequest()
tool.llm = MockLLM()
# Mocking the pull_request_arr
pull_request_arr = ["part1", "part2", "part3"]
# Calling the method to be tested
result = tool.split_pull_request_content_into_multiple_parts(4000,pull_request_arr)
# Validate the result (this depends on what you expect the output to be)
# For instance, if you expect the result to be a list of all parts concatenated with 'diff --git'
expected = ["diff --gitpart1diff --gitpart2diff --gitpart3"]
assert result == expected
@pytest.mark.parametrize("diff_content, file_path, line_number, expected", [
("file_path_1\n@@ -1,3 +1,4 @@\n+ line1\n+ line2\n+ line3", "file_path_1", 3, 4),
("file_path_2\n@@ -1,3 +1,3 @@\n+ line1\n- line2", "file_path_2", 1, 2),
("file_path_3\n@@ -1,3 +1,4 @@\n+ line1\n+ line2\n- line3", "file_path_3", 2, 3)
])
def test_get_exact_line_number(diff_content, file_path, line_number, expected):
tool = GithubReviewPullRequest()
# Calling the method to be tested
result = tool.get_exact_line_number(diff_content, file_path, line_number)
# Validate the result
assert result == expected
class MockGithubHelper:
def __init__(self, access_token, username):
pass
def get_pull_request_content(self, owner, repo, pr_number):
return 'mock_content'
def get_latest_commit_id_of_pull_request(self, owner, repo, pr_number):
return 'mock_commit_id'
def add_line_comment_to_pull_request(self, *args, **kwargs):
return True
# Your test case
def test_execute():
with patch('superagi.tools.github.review_pull_request.GithubHelper', MockGithubHelper), \
patch('superagi.tools.github.review_pull_request.TokenCounter.count_message_tokens', return_value=3000), \
patch('superagi.tools.github.review_pull_request.Agent.find_org_by_agent_id', return_value=Mock()), \
patch.object(GithubReviewPullRequest, 'get_tool_config', return_value='mock_value'), \
patch.object(GithubReviewPullRequest, 'run_code_review', return_value=None):
# Replace 'your_module' with the actual module name
tool = GithubReviewPullRequest()
tool.llm = Mock()
tool.llm.get_model = Mock(return_value='mock_model')
tool.toolkit_config = Mock()
tool.toolkit_config.session = 'mock_session'
result = tool._execute('mock_repo', 'mock_owner', 42)
assert result == 'Added comments to the pull request:42'