Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests and enhance PR help message functionality #1378

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ loguru==0.7.2
msrest==0.7.1
openai==1.54.1
pytest==7.4.0
pytest-asyncio==0.21.0
PyGithub==1.59.*
PyYAML==6.0.1
python-gitlab==3.15.0
Expand Down
66 changes: 66 additions & 0 deletions tests/unittest/test_pr_help_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pytest
from unittest.mock import Mock, patch
from pathlib import Path

from pr_agent.tools.pr_help_message import PRHelpMessage, extract_header


def test_extract_header():
# Test with valid header
snippet = "Header 1: Test\n===Snippet content==="
assert extract_header(snippet) == "#test"

# Test with no header
snippet = "Some content\n===Snippet content==="
assert extract_header(snippet) == ""

# Test with multiple headers (should take first header in reversed list)
snippet = "Header 1: First\nHeader 2: Last\n===Snippet content==="
assert extract_header(snippet) == "#first"


@pytest.fixture
def mock_git_provider():
mock = Mock()
mock.pr_url = "https://github.com/test/repo/pull/1"
return mock


@pytest.mark.asyncio
@patch("pr_agent.tools.pr_help_message.get_git_provider_with_context")
@patch("pr_agent.tools.pr_help_message.get_settings")
async def test_pr_help_message_init(mock_get_settings, mock_get_provider, mock_git_provider):
# Setup
mock_get_provider.return_value = mock_git_provider
mock_get_settings.return_value.get.return_value = 5
mock_ai_handler = Mock()

# Test initialization with question
help_msg = PRHelpMessage("test_url", args=["test", "question"], ai_handler=mock_ai_handler)

assert help_msg.question_str == "test question"
assert help_msg.git_provider == mock_git_provider
assert help_msg.num_retrieved_snippets == 5
assert help_msg.vars == {
"question": "test question",
"snippets": ""
}


@pytest.mark.asyncio
@patch("pr_agent.tools.pr_help_message.get_git_provider_with_context")
@patch("pr_agent.tools.pr_help_message.get_settings")
async def test_pr_help_message_run_no_openai_key(mock_get_settings, mock_get_provider, mock_git_provider):
# Setup
mock_get_provider.return_value = mock_git_provider
mock_get_settings.return_value.get.side_effect = [5, None] # num_snippets, openai key
mock_get_settings.return_value.config.publish_output = True

help_msg = PRHelpMessage("test_url", args=["test"], ai_handler=Mock())

# Test run with no OpenAI key
await help_msg.run()

mock_git_provider.publish_comment.assert_called_once_with(
"The `Help` tool chat feature requires an OpenAI API key for calculating embeddings"
)
Loading