130 lines
4.5 KiB
Python
130 lines
4.5 KiB
Python
|
|
import pytest
|
||
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||
|
|
|
||
|
|
|
||
|
|
def test_pr_creation_after_push():
|
||
|
|
"""push 성공 후 GiteaClient로 PR을 생성한다."""
|
||
|
|
mock_gitea = MagicMock()
|
||
|
|
mock_gitea.create_pull_request = AsyncMock(
|
||
|
|
return_value={
|
||
|
|
"number": 1,
|
||
|
|
"html_url": "http://gitea:3000/quant/galaxis-po/pulls/1",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
mock_sandbox = MagicMock()
|
||
|
|
mock_result = MagicMock(exit_code=0, output="")
|
||
|
|
|
||
|
|
with patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_gitea_client", return_value=mock_gitea
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_sandbox_backend_sync",
|
||
|
|
return_value=mock_sandbox,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_config",
|
||
|
|
return_value={
|
||
|
|
"configurable": {
|
||
|
|
"thread_id": "test-thread",
|
||
|
|
"repo": {"owner": "quant", "name": "galaxis-po"},
|
||
|
|
}
|
||
|
|
},
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.resolve_repo_dir",
|
||
|
|
return_value="/workspace/galaxis-po",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_has_uncommitted_changes",
|
||
|
|
return_value=True,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_fetch_origin",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_has_unpushed_commits",
|
||
|
|
return_value=False,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_current_branch",
|
||
|
|
return_value="galaxis-agent/test-thread",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_checkout_branch",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_config_user",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_add_all",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_commit",
|
||
|
|
return_value=mock_result,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_push",
|
||
|
|
return_value=mock_result,
|
||
|
|
), patch.dict(
|
||
|
|
"os.environ", {"GITEA_TOKEN": "test-token"},
|
||
|
|
):
|
||
|
|
from agent.tools.commit_and_open_pr import commit_and_open_pr
|
||
|
|
result = commit_and_open_pr(title="feat: add feature", body="PR description")
|
||
|
|
assert result["success"] is True
|
||
|
|
assert "pulls/1" in result["pr_url"]
|
||
|
|
mock_gitea.create_pull_request.assert_called_once()
|
||
|
|
|
||
|
|
|
||
|
|
def test_pr_creation_converts_internal_to_external_url():
|
||
|
|
"""PR URL이 내부 URL에서 외부 URL로 변환된다."""
|
||
|
|
mock_gitea = MagicMock()
|
||
|
|
mock_gitea.create_pull_request = AsyncMock(
|
||
|
|
return_value={
|
||
|
|
"number": 5,
|
||
|
|
"html_url": "http://gitea:3000/quant/galaxis-po/pulls/5",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
mock_sandbox = MagicMock()
|
||
|
|
mock_result = MagicMock(exit_code=0, output="")
|
||
|
|
|
||
|
|
with patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_gitea_client", return_value=mock_gitea
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_sandbox_backend_sync",
|
||
|
|
return_value=mock_sandbox,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.get_config",
|
||
|
|
return_value={
|
||
|
|
"configurable": {
|
||
|
|
"thread_id": "test-thread",
|
||
|
|
"repo": {"owner": "quant", "name": "galaxis-po"},
|
||
|
|
}
|
||
|
|
},
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.resolve_repo_dir",
|
||
|
|
return_value="/workspace/galaxis-po",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_has_uncommitted_changes",
|
||
|
|
return_value=True,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_fetch_origin",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_has_unpushed_commits",
|
||
|
|
return_value=False,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_current_branch",
|
||
|
|
return_value="galaxis-agent/test-thread",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_checkout_branch",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_config_user",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_add_all",
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_commit",
|
||
|
|
return_value=mock_result,
|
||
|
|
), patch(
|
||
|
|
"agent.tools.commit_and_open_pr.git_push",
|
||
|
|
return_value=mock_result,
|
||
|
|
), patch.dict(
|
||
|
|
"os.environ",
|
||
|
|
{
|
||
|
|
"GITEA_TOKEN": "test-token",
|
||
|
|
"GITEA_EXTERNAL_URL": "https://ayuriel.duckdns.org",
|
||
|
|
"GITEA_URL": "http://gitea:3000",
|
||
|
|
},
|
||
|
|
):
|
||
|
|
from agent.tools.commit_and_open_pr import commit_and_open_pr
|
||
|
|
result = commit_and_open_pr(title="feat: test", body="body")
|
||
|
|
assert result["success"] is True
|
||
|
|
assert "ayuriel.duckdns.org" in result["pr_url"]
|
||
|
|
assert "gitea:3000" not in result["pr_url"]
|