|
| 1 | +from unittest.mock import Mock, patch |
1 | 2 | from llama_index.core.tools.tool_spec.base import BaseToolSpec |
| 3 | +from llama_index.core.schema import Document |
2 | 4 | from llama_index.tools.tavily_research import TavilyToolSpec |
3 | 5 |
|
4 | 6 |
|
5 | 7 | def test_class(): |
6 | 8 | names_of_base_classes = [b.__name__ for b in TavilyToolSpec.__mro__] |
7 | 9 | assert BaseToolSpec.__name__ in names_of_base_classes |
| 10 | + |
| 11 | + |
| 12 | +def test_spec_functions(): |
| 13 | + """Test that spec_functions includes both search and extract methods.""" |
| 14 | + assert "search" in TavilyToolSpec.spec_functions |
| 15 | + assert "extract" in TavilyToolSpec.spec_functions |
| 16 | + |
| 17 | + |
| 18 | +@patch("tavily.TavilyClient") |
| 19 | +def test_init(mock_tavily_client): |
| 20 | + """Test TavilyToolSpec initialization.""" |
| 21 | + api_key = "test_api_key" |
| 22 | + tool = TavilyToolSpec(api_key=api_key) |
| 23 | + |
| 24 | + mock_tavily_client.assert_called_once_with(api_key=api_key) |
| 25 | + assert tool.client == mock_tavily_client.return_value |
| 26 | + |
| 27 | + |
| 28 | +@patch("tavily.TavilyClient") |
| 29 | +def test_search(mock_tavily_client): |
| 30 | + """Test search method returns properly formatted Document objects.""" |
| 31 | + # Setup mock response |
| 32 | + mock_response = { |
| 33 | + "results": [ |
| 34 | + {"content": "Test content 1", "url": "https://example1.com"}, |
| 35 | + {"content": "Test content 2", "url": "https://example2.com"}, |
| 36 | + ] |
| 37 | + } |
| 38 | + |
| 39 | + mock_client_instance = Mock() |
| 40 | + mock_client_instance.search.return_value = mock_response |
| 41 | + mock_tavily_client.return_value = mock_client_instance |
| 42 | + |
| 43 | + # Create tool and call search |
| 44 | + tool = TavilyToolSpec(api_key="test_key") |
| 45 | + results = tool.search("test query", max_results=5) |
| 46 | + |
| 47 | + # Verify client.search was called correctly |
| 48 | + mock_client_instance.search.assert_called_once_with( |
| 49 | + "test query", max_results=5, search_depth="advanced" |
| 50 | + ) |
| 51 | + |
| 52 | + # Verify results |
| 53 | + assert len(results) == 2 |
| 54 | + assert all(isinstance(doc, Document) for doc in results) |
| 55 | + |
| 56 | + assert results[0].text == "Test content 1" |
| 57 | + assert results[0].extra_info["url"] == "https://example1.com" |
| 58 | + |
| 59 | + assert results[1].text == "Test content 2" |
| 60 | + assert results[1].extra_info["url"] == "https://example2.com" |
| 61 | + |
| 62 | + |
| 63 | +@patch("tavily.TavilyClient") |
| 64 | +def test_search_with_default_max_results(mock_tavily_client): |
| 65 | + """Test search method uses default max_results of 6.""" |
| 66 | + mock_response = {"results": []} |
| 67 | + |
| 68 | + mock_client_instance = Mock() |
| 69 | + mock_client_instance.search.return_value = mock_response |
| 70 | + mock_tavily_client.return_value = mock_client_instance |
| 71 | + |
| 72 | + tool = TavilyToolSpec(api_key="test_key") |
| 73 | + tool.search("test query") |
| 74 | + |
| 75 | + mock_client_instance.search.assert_called_once_with( |
| 76 | + "test query", max_results=6, search_depth="advanced" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@patch("tavily.TavilyClient") |
| 81 | +def test_extract(mock_tavily_client): |
| 82 | + """Test extract method returns properly formatted Document objects.""" |
| 83 | + # Setup mock response |
| 84 | + mock_response = { |
| 85 | + "results": [ |
| 86 | + { |
| 87 | + "raw_content": "Extracted content 1", |
| 88 | + "url": "https://example1.com", |
| 89 | + "favicon": "https://example1.com/favicon.ico", |
| 90 | + "images": ["https://example1.com/image1.jpg"], |
| 91 | + }, |
| 92 | + { |
| 93 | + "raw_content": "Extracted content 2", |
| 94 | + "url": "https://example2.com", |
| 95 | + "favicon": "https://example2.com/favicon.ico", |
| 96 | + "images": ["https://example2.com/image2.jpg"], |
| 97 | + }, |
| 98 | + ] |
| 99 | + } |
| 100 | + |
| 101 | + mock_client_instance = Mock() |
| 102 | + mock_client_instance.extract.return_value = mock_response |
| 103 | + mock_tavily_client.return_value = mock_client_instance |
| 104 | + |
| 105 | + # Create tool and call extract |
| 106 | + tool = TavilyToolSpec(api_key="test_key") |
| 107 | + urls = ["https://example1.com", "https://example2.com"] |
| 108 | + results = tool.extract( |
| 109 | + urls=urls, |
| 110 | + include_images=True, |
| 111 | + include_favicon=True, |
| 112 | + extract_depth="advanced", |
| 113 | + format="text", |
| 114 | + ) |
| 115 | + |
| 116 | + # Verify client.extract was called correctly |
| 117 | + mock_client_instance.extract.assert_called_once_with( |
| 118 | + urls, |
| 119 | + include_images=True, |
| 120 | + include_favicon=True, |
| 121 | + extract_depth="advanced", |
| 122 | + format="text", |
| 123 | + ) |
| 124 | + |
| 125 | + # Verify results |
| 126 | + assert len(results) == 2 |
| 127 | + assert all(isinstance(doc, Document) for doc in results) |
| 128 | + |
| 129 | + assert results[0].text == "Extracted content 1" |
| 130 | + assert results[0].extra_info["url"] == "https://example1.com" |
| 131 | + assert results[0].extra_info["favicon"] == "https://example1.com/favicon.ico" |
| 132 | + assert results[0].extra_info["images"] == ["https://example1.com/image1.jpg"] |
| 133 | + |
| 134 | + assert results[1].text == "Extracted content 2" |
| 135 | + assert results[1].extra_info["url"] == "https://example2.com" |
| 136 | + |
| 137 | + |
| 138 | +@patch("tavily.TavilyClient") |
| 139 | +def test_extract_with_defaults(mock_tavily_client): |
| 140 | + """Test extract method uses correct default parameters.""" |
| 141 | + mock_response = {"results": []} |
| 142 | + |
| 143 | + mock_client_instance = Mock() |
| 144 | + mock_client_instance.extract.return_value = mock_response |
| 145 | + mock_tavily_client.return_value = mock_client_instance |
| 146 | + |
| 147 | + tool = TavilyToolSpec(api_key="test_key") |
| 148 | + urls = ["https://example.com"] |
| 149 | + tool.extract(urls) |
| 150 | + |
| 151 | + mock_client_instance.extract.assert_called_once_with( |
| 152 | + urls, |
| 153 | + include_images=False, |
| 154 | + include_favicon=False, |
| 155 | + extract_depth="basic", |
| 156 | + format="markdown", |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +@patch("tavily.TavilyClient") |
| 161 | +def test_extract_empty_results(mock_tavily_client): |
| 162 | + """Test extract method handles empty results gracefully.""" |
| 163 | + mock_response = {"results": []} |
| 164 | + |
| 165 | + mock_client_instance = Mock() |
| 166 | + mock_client_instance.extract.return_value = mock_response |
| 167 | + mock_tavily_client.return_value = mock_client_instance |
| 168 | + |
| 169 | + tool = TavilyToolSpec(api_key="test_key") |
| 170 | + results = tool.extract(urls=["https://example.com"]) |
| 171 | + |
| 172 | + assert results == [] |
| 173 | + |
| 174 | + |
| 175 | +@patch("tavily.TavilyClient") |
| 176 | +def test_extract_missing_fields(mock_tavily_client): |
| 177 | + """Test extract method handles missing fields in response.""" |
| 178 | + # Mock response with missing fields |
| 179 | + mock_response = { |
| 180 | + "results": [ |
| 181 | + { |
| 182 | + "url": "https://example.com" |
| 183 | + # Missing raw_content, favicon, images |
| 184 | + } |
| 185 | + ] |
| 186 | + } |
| 187 | + |
| 188 | + mock_client_instance = Mock() |
| 189 | + mock_client_instance.extract.return_value = mock_response |
| 190 | + mock_tavily_client.return_value = mock_client_instance |
| 191 | + |
| 192 | + tool = TavilyToolSpec(api_key="test_key") |
| 193 | + results = tool.extract(urls=["https://example.com"]) |
| 194 | + |
| 195 | + assert len(results) == 1 |
| 196 | + assert results[0].text == "" # Empty string for missing raw_content |
| 197 | + assert results[0].extra_info["url"] == "https://example.com" |
| 198 | + assert results[0].extra_info["favicon"] is None |
| 199 | + assert results[0].extra_info["images"] is None |
| 200 | + |
| 201 | + |
| 202 | +@patch("tavily.TavilyClient") |
| 203 | +def test_extract_no_results_key(mock_tavily_client): |
| 204 | + """Test extract method handles response without 'results' key.""" |
| 205 | + mock_response = {} # No 'results' key |
| 206 | + |
| 207 | + mock_client_instance = Mock() |
| 208 | + mock_client_instance.extract.return_value = mock_response |
| 209 | + mock_tavily_client.return_value = mock_client_instance |
| 210 | + |
| 211 | + tool = TavilyToolSpec(api_key="test_key") |
| 212 | + results = tool.extract(urls=["https://example.com"]) |
| 213 | + |
| 214 | + assert results == [] |
0 commit comments