|
| 1 | +"""Tests for Bugzilla MCP tools.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from fastmcp.client import Client |
| 7 | +from fastmcp.client.transports import FastMCPTransport |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +async def mcp_client(): |
| 12 | + """Create an MCP client for testing.""" |
| 13 | + from bugbug_mcp.server import mcp |
| 14 | + |
| 15 | + async with Client(mcp) as client: |
| 16 | + yield client |
| 17 | + |
| 18 | + |
| 19 | +def setup_bugzilla_mock(mocker, bugs): |
| 20 | + """Helper to setup Bugzilla mock with given bugs.""" |
| 21 | + mock_bugzilla_class = mocker.patch("libmozdata.bugzilla.Bugzilla") |
| 22 | + mock_instance = MagicMock() |
| 23 | + |
| 24 | + # Store bugs to be returned |
| 25 | + mock_instance._bugs = bugs |
| 26 | + |
| 27 | + # When Bugzilla is initialized, capture the bughandler |
| 28 | + def mock_init(params, include_fields, bughandler): |
| 29 | + mock_instance._bughandler = bughandler |
| 30 | + return mock_instance |
| 31 | + |
| 32 | + # When get_data().wait() is called, invoke the handler with bugs |
| 33 | + def mock_get_data(): |
| 34 | + for bug in mock_instance._bugs: |
| 35 | + mock_instance._bughandler(bug) |
| 36 | + return mock_instance |
| 37 | + |
| 38 | + mock_instance.get_data = mock_get_data |
| 39 | + mock_instance.wait = MagicMock() |
| 40 | + mock_bugzilla_class.side_effect = mock_init |
| 41 | + |
| 42 | + return mock_bugzilla_class |
| 43 | + |
| 44 | + |
| 45 | +class TestBugzillaQuickSearch: |
| 46 | + """Test the bugzilla_quick_search tool.""" |
| 47 | + |
| 48 | + async def test_quick_search_basic( |
| 49 | + self, mocker, mcp_client: Client[FastMCPTransport] |
| 50 | + ): |
| 51 | + """Test basic quick search functionality.""" |
| 52 | + mock_bugs = [ |
| 53 | + { |
| 54 | + "id": 123456, |
| 55 | + "status": "NEW", |
| 56 | + "summary": "Test bug 1", |
| 57 | + "product": "Firefox", |
| 58 | + "component": "General", |
| 59 | + "priority": "P1", |
| 60 | + "severity": "S2", |
| 61 | + }, |
| 62 | + { |
| 63 | + "id": 789012, |
| 64 | + "status": "ASSIGNED", |
| 65 | + "summary": "Test bug 2", |
| 66 | + "product": "Core", |
| 67 | + "component": "DOM", |
| 68 | + "priority": "P2", |
| 69 | + "severity": "S3", |
| 70 | + }, |
| 71 | + ] |
| 72 | + |
| 73 | + mock_bugzilla = setup_bugzilla_mock(mocker, mock_bugs) |
| 74 | + |
| 75 | + result = await mcp_client.call_tool( |
| 76 | + name="bugzilla_quick_search", |
| 77 | + arguments={"search_query": "firefox crash", "limit": 2}, |
| 78 | + ) |
| 79 | + |
| 80 | + # Verify API call |
| 81 | + mock_bugzilla.assert_called_once() |
| 82 | + call_args = mock_bugzilla.call_args[0][0] |
| 83 | + assert call_args["quicksearch"] == "firefox crash" |
| 84 | + assert call_args["limit"] == 2 |
| 85 | + |
| 86 | + # Verify result |
| 87 | + result_text = result.content[0].text |
| 88 | + assert "Found 2 bug(s)" in result_text |
| 89 | + assert "Bug 123456 [NEW]" in result_text |
| 90 | + assert "Bug 789012 [ASSIGNED]" in result_text |
| 91 | + assert "Test bug 1" in result_text |
| 92 | + assert "Firefox::General" in result_text |
| 93 | + assert "Core::DOM" in result_text |
| 94 | + |
| 95 | + async def test_quick_search_no_results( |
| 96 | + self, mocker, mcp_client: Client[FastMCPTransport] |
| 97 | + ): |
| 98 | + """Test quick search with no results.""" |
| 99 | + setup_bugzilla_mock(mocker, []) |
| 100 | + |
| 101 | + result = await mcp_client.call_tool( |
| 102 | + name="bugzilla_quick_search", |
| 103 | + arguments={"search_query": "nonexistent query"}, |
| 104 | + ) |
| 105 | + |
| 106 | + result_text = result.content[0].text |
| 107 | + assert "No bugs found matching: nonexistent query" in result_text |
| 108 | + |
| 109 | + async def test_quick_search_custom_limit( |
| 110 | + self, mocker, mcp_client: Client[FastMCPTransport] |
| 111 | + ): |
| 112 | + """Test quick search with custom limit.""" |
| 113 | + mock_bugzilla = setup_bugzilla_mock(mocker, []) |
| 114 | + |
| 115 | + await mcp_client.call_tool( |
| 116 | + name="bugzilla_quick_search", |
| 117 | + arguments={"search_query": "test", "limit": 50}, |
| 118 | + ) |
| 119 | + |
| 120 | + call_args = mock_bugzilla.call_args[0][0] |
| 121 | + assert call_args["limit"] == 50 |
| 122 | + |
| 123 | + async def test_quick_search_handles_missing_fields( |
| 124 | + self, mocker, mcp_client: Client[FastMCPTransport] |
| 125 | + ): |
| 126 | + """Test that missing fields are handled gracefully.""" |
| 127 | + mock_bugs = [{"id": 123456, "summary": "Test bug"}] |
| 128 | + setup_bugzilla_mock(mocker, mock_bugs) |
| 129 | + |
| 130 | + result = await mcp_client.call_tool( |
| 131 | + name="bugzilla_quick_search", arguments={"search_query": "test"} |
| 132 | + ) |
| 133 | + |
| 134 | + result_text = result.content[0].text |
| 135 | + assert "Bug 123456" in result_text |
| 136 | + assert "Test bug" in result_text |
| 137 | + assert "N/A" in result_text |
0 commit comments