aiagents4pharma 1.28.0__py3-none-any.whl → 1.29.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. aiagents4pharma/talk2scholars/agents/main_agent.py +35 -209
  2. aiagents4pharma/talk2scholars/agents/s2_agent.py +10 -6
  3. aiagents4pharma/talk2scholars/agents/zotero_agent.py +12 -6
  4. aiagents4pharma/talk2scholars/configs/agents/talk2scholars/main_agent/default.yaml +2 -48
  5. aiagents4pharma/talk2scholars/configs/agents/talk2scholars/s2_agent/default.yaml +5 -28
  6. aiagents4pharma/talk2scholars/configs/agents/talk2scholars/zotero_agent/default.yaml +5 -21
  7. aiagents4pharma/talk2scholars/configs/config.yaml +1 -0
  8. aiagents4pharma/talk2scholars/configs/tools/__init__.py +1 -0
  9. aiagents4pharma/talk2scholars/configs/tools/multi_paper_recommendation/default.yaml +1 -1
  10. aiagents4pharma/talk2scholars/configs/tools/search/default.yaml +1 -1
  11. aiagents4pharma/talk2scholars/configs/tools/single_paper_recommendation/default.yaml +1 -1
  12. aiagents4pharma/talk2scholars/configs/tools/zotero_read/default.yaml +42 -1
  13. aiagents4pharma/talk2scholars/configs/tools/zotero_write/__inti__.py +3 -0
  14. aiagents4pharma/talk2scholars/tests/test_main_agent.py +186 -111
  15. aiagents4pharma/talk2scholars/tests/test_s2_display.py +74 -0
  16. aiagents4pharma/talk2scholars/tests/test_s2_multi.py +282 -0
  17. aiagents4pharma/talk2scholars/tests/test_s2_query.py +78 -0
  18. aiagents4pharma/talk2scholars/tests/test_s2_retrieve.py +65 -0
  19. aiagents4pharma/talk2scholars/tests/test_s2_search.py +266 -0
  20. aiagents4pharma/talk2scholars/tests/test_s2_single.py +274 -0
  21. aiagents4pharma/talk2scholars/tests/test_zotero_path.py +57 -0
  22. aiagents4pharma/talk2scholars/tests/test_zotero_read.py +412 -0
  23. aiagents4pharma/talk2scholars/tests/test_zotero_write.py +626 -0
  24. aiagents4pharma/talk2scholars/tools/s2/multi_paper_rec.py +50 -34
  25. aiagents4pharma/talk2scholars/tools/s2/retrieve_semantic_scholar_paper_id.py +8 -8
  26. aiagents4pharma/talk2scholars/tools/s2/search.py +36 -23
  27. aiagents4pharma/talk2scholars/tools/s2/single_paper_rec.py +44 -38
  28. aiagents4pharma/talk2scholars/tools/zotero/__init__.py +2 -0
  29. aiagents4pharma/talk2scholars/tools/zotero/utils/__init__.py +5 -0
  30. aiagents4pharma/talk2scholars/tools/zotero/utils/zotero_path.py +63 -0
  31. aiagents4pharma/talk2scholars/tools/zotero/zotero_read.py +64 -19
  32. aiagents4pharma/talk2scholars/tools/zotero/zotero_write.py +247 -0
  33. {aiagents4pharma-1.28.0.dist-info → aiagents4pharma-1.29.0.dist-info}/METADATA +6 -5
  34. {aiagents4pharma-1.28.0.dist-info → aiagents4pharma-1.29.0.dist-info}/RECORD +37 -28
  35. aiagents4pharma/talk2scholars/tests/test_call_s2.py +0 -100
  36. aiagents4pharma/talk2scholars/tests/test_call_zotero.py +0 -94
  37. aiagents4pharma/talk2scholars/tests/test_s2_tools.py +0 -355
  38. aiagents4pharma/talk2scholars/tests/test_zotero_tool.py +0 -171
  39. {aiagents4pharma-1.28.0.dist-info → aiagents4pharma-1.29.0.dist-info}/LICENSE +0 -0
  40. {aiagents4pharma-1.28.0.dist-info → aiagents4pharma-1.29.0.dist-info}/WHEEL +0 -0
  41. {aiagents4pharma-1.28.0.dist-info → aiagents4pharma-1.29.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,78 @@
1
+ """
2
+ Unit tests for S2 tools functionality.
3
+ """
4
+
5
+ # pylint: disable=redefined-outer-name
6
+ from unittest.mock import patch
7
+ from unittest.mock import MagicMock
8
+ import pytest
9
+ from ..tools.s2.query_results import query_results, NoPapersFoundError
10
+
11
+
12
+ @pytest.fixture
13
+ def initial_state():
14
+ """Provides an empty initial state for tests."""
15
+ return {"papers": {}, "multi_papers": {}}
16
+
17
+
18
+ # Fixed test data for deterministic results
19
+ MOCK_SEARCH_RESPONSE = {
20
+ "data": [
21
+ {
22
+ "paperId": "123",
23
+ "title": "Machine Learning Basics",
24
+ "abstract": "An introduction to ML",
25
+ "year": 2023,
26
+ "citationCount": 100,
27
+ "url": "https://example.com/paper1",
28
+ "authors": [{"name": "Test Author"}],
29
+ }
30
+ ]
31
+ }
32
+
33
+ MOCK_STATE_PAPER = {
34
+ "123": {
35
+ "Title": "Machine Learning Basics",
36
+ "Abstract": "An introduction to ML",
37
+ "Year": 2023,
38
+ "Citation Count": 100,
39
+ "URL": "https://example.com/paper1",
40
+ }
41
+ }
42
+
43
+
44
+ class TestS2Tools:
45
+ """Unit tests for individual S2 tools"""
46
+
47
+ def test_query_results_empty_state(self, initial_state):
48
+ """Tests query_results tool behavior when no papers are found."""
49
+ with pytest.raises(
50
+ NoPapersFoundError,
51
+ match="No papers found. A search needs to be performed first.",
52
+ ):
53
+ query_results.invoke(
54
+ {"question": "List all papers", "state": initial_state}
55
+ )
56
+
57
+ @patch(
58
+ "aiagents4pharma.talk2scholars.tools.s2.query_results.create_pandas_dataframe_agent"
59
+ )
60
+ def test_query_results_with_papers(self, mock_create_agent, initial_state):
61
+ """Tests querying papers when data is available."""
62
+ state = initial_state.copy()
63
+ state["last_displayed_papers"] = "papers"
64
+ state["papers"] = MOCK_STATE_PAPER
65
+
66
+ # Mock the dataframe agent instead of the LLM
67
+ mock_agent = MagicMock()
68
+ mock_agent.invoke.return_value = {"output": "Mocked response"}
69
+
70
+ mock_create_agent.return_value = (
71
+ mock_agent # Mock the function returning the agent
72
+ )
73
+
74
+ # Ensure that the output of query_results is correctly structured
75
+ result = query_results.invoke({"question": "List all papers", "state": state})
76
+
77
+ assert isinstance(result, str) # Ensure output is a string
78
+ assert result == "Mocked response" # Validate the expected response
@@ -0,0 +1,65 @@
1
+ """
2
+ Unit tests for S2 tools functionality.
3
+ """
4
+
5
+ # pylint: disable=redefined-outer-name
6
+ from unittest.mock import patch
7
+ import pytest
8
+ from langgraph.types import Command
9
+ from ..tools.s2.retrieve_semantic_scholar_paper_id import (
10
+ retrieve_semantic_scholar_paper_id,
11
+ )
12
+
13
+
14
+ # Fixed test data for deterministic results
15
+ MOCK_SEARCH_RESPONSE = {
16
+ "data": [
17
+ {
18
+ "paperId": "123",
19
+ "title": "Machine Learning Basics",
20
+ "abstract": "An introduction to ML",
21
+ "year": 2023,
22
+ "citationCount": 100,
23
+ "url": "https://example.com/paper1",
24
+ "authors": [{"name": "Test Author"}],
25
+ }
26
+ ]
27
+ }
28
+
29
+ MOCK_STATE_PAPER = {
30
+ "123": {
31
+ "Title": "Machine Learning Basics",
32
+ "Abstract": "An introduction to ML",
33
+ "Year": 2023,
34
+ "Citation Count": 100,
35
+ "URL": "https://example.com/paper1",
36
+ }
37
+ }
38
+
39
+
40
+ class TestS2Tools:
41
+ """Unit tests for individual S2 tools"""
42
+
43
+ @patch("requests.get")
44
+ def test_retrieve_semantic_scholar_paper_id(self, mock_get):
45
+ """Tests retrieving a paper ID from Semantic Scholar."""
46
+ mock_get.return_value.json.return_value = MOCK_SEARCH_RESPONSE
47
+ mock_get.return_value.status_code = 200
48
+
49
+ result = retrieve_semantic_scholar_paper_id.invoke(
50
+ input={"paper_title": "Machine Learning Basics", "tool_call_id": "test123"}
51
+ )
52
+
53
+ assert isinstance(result, Command)
54
+ assert "messages" in result.update
55
+ assert (
56
+ "Paper ID for 'Machine Learning Basics' is: 123"
57
+ in result.update["messages"][0].content
58
+ )
59
+
60
+ def test_retrieve_semantic_scholar_paper_id_no_results(self):
61
+ """Test retrieving a paper ID when no results are found."""
62
+ with pytest.raises(ValueError, match="No papers found for query: UnknownPaper"):
63
+ retrieve_semantic_scholar_paper_id.invoke(
64
+ input={"paper_title": "UnknownPaper", "tool_call_id": "test123"}
65
+ )
@@ -0,0 +1,266 @@
1
+ """
2
+ Unit tests for S2 tools functionality.
3
+ """
4
+
5
+ from types import SimpleNamespace
6
+ import pytest
7
+ import hydra
8
+ import requests
9
+ from langgraph.types import Command
10
+ from langchain_core.messages import ToolMessage
11
+ from aiagents4pharma.talk2scholars.tools.s2.search import search_tool
12
+
13
+ # --- Dummy Hydra Config Setup ---
14
+
15
+
16
+ class DummyHydraContext:
17
+ """hydra.initialize context manager that does nothing."""
18
+
19
+ def __enter__(self):
20
+ return None
21
+
22
+ def __exit__(self, exc_type, exc_val, traceback):
23
+ return None
24
+
25
+
26
+ # Create a dummy configuration that mimics the expected hydra config
27
+ dummy_config = SimpleNamespace(
28
+ tools=SimpleNamespace(
29
+ search=SimpleNamespace(
30
+ api_endpoint="http://dummy.endpoint",
31
+ api_fields=["paperId", "title", "authors"],
32
+ )
33
+ )
34
+ )
35
+
36
+ # --- Dummy Response Classes and Functions for requests.get ---
37
+
38
+
39
+ class DummyResponse:
40
+ """A dummy response class for mocking HTTP responses."""
41
+
42
+ def __init__(self, json_data, status_code=200):
43
+ """initialize a DummyResponse with the given JSON data and status code."""
44
+ self._json_data = json_data
45
+ self.status_code = status_code
46
+
47
+ def json(self):
48
+ """access the JSON data from the response."""
49
+ return self._json_data
50
+
51
+ def raise_for_status(self):
52
+ """Raise an HTTP error for status codes >= 400."""
53
+ if self.status_code >= 400:
54
+ raise requests.HTTPError("HTTP Error")
55
+
56
+
57
+ def test_dummy_response_no_error():
58
+ """Test that raise_for_status does not raise an exception for a successful response."""
59
+ # Create a DummyResponse with a successful status code.
60
+ response = DummyResponse({"data": "success"}, status_code=200)
61
+ # Calling raise_for_status should not raise an exception and should return None.
62
+ assert response.raise_for_status() is None
63
+
64
+
65
+ def test_dummy_response_raise_error():
66
+ """Test that raise_for_status raises an exception for a failing response."""
67
+ # Create a DummyResponse with a failing status code.
68
+ response = DummyResponse({"error": "fail"}, status_code=400)
69
+ # Calling raise_for_status should raise an HTTPError.
70
+ with pytest.raises(requests.HTTPError):
71
+ response.raise_for_status()
72
+
73
+
74
+ def dummy_requests_get_success(url, params, timeout):
75
+ """A dummy requests.get function that returns a successful response."""
76
+ # Record call parameters for assertions
77
+ dummy_requests_get_success.called_url = url
78
+ dummy_requests_get_success.called_params = params
79
+ dummy_requests_get_success.called_timeout = timeout
80
+
81
+ # Simulate a valid API response with three papers;
82
+ # one paper missing authors should be filtered out.
83
+ dummy_data = {
84
+ "data": [
85
+ {
86
+ "paperId": "1",
87
+ "title": "Paper 1",
88
+ "authors": ["Author A"],
89
+ "year": 2020,
90
+ "citationCount": 10,
91
+ "url": "http://paper1",
92
+ "externalIds": {"ArXiv": "arxiv1"},
93
+ },
94
+ {
95
+ "paperId": "2",
96
+ "title": "Paper 2",
97
+ "authors": ["Author B"],
98
+ "year": 2021,
99
+ "citationCount": 20,
100
+ "url": "http://paper2",
101
+ "externalIds": {},
102
+ },
103
+ {
104
+ "paperId": "3",
105
+ "title": "Paper 3",
106
+ "authors": None, # This paper should be filtered out.
107
+ "year": 2022,
108
+ "citationCount": 30,
109
+ "url": "http://paper3",
110
+ "externalIds": {"ArXiv": "arxiv3"},
111
+ },
112
+ ]
113
+ }
114
+ return DummyResponse(dummy_data)
115
+
116
+
117
+ def dummy_requests_get_no_data(url, params, timeout):
118
+ """A dummy requests.get function that returns a response without the expected 'data' key."""
119
+ dummy_requests_get_no_data.called_url = url
120
+ dummy_requests_get_no_data.called_params = params
121
+ dummy_requests_get_no_data.called_timeout = timeout
122
+ # Simulate a response with an unexpected format (missing "data" key)
123
+ return DummyResponse({"error": "Invalid format"})
124
+
125
+
126
+ def dummy_requests_get_no_papers(url, params, timeout):
127
+ """A dummy requests.get function that returns a response with an empty papers list."""
128
+ dummy_requests_get_no_papers.called_url = url
129
+ dummy_requests_get_no_papers.called_params = params
130
+ dummy_requests_get_no_papers.called_timeout = timeout
131
+ # Simulate a response with an empty papers list.
132
+ return DummyResponse({"data": []})
133
+
134
+
135
+ def dummy_requests_get_exception(url, params, timeout):
136
+ """A dummy requests.get function that raises an exception."""
137
+ dummy_requests_get_exception.called_url = url
138
+ dummy_requests_get_exception.called_params = params
139
+ dummy_requests_get_exception.called_timeout = timeout
140
+ # Simulate a network/connection exception.
141
+ raise requests.exceptions.RequestException("Connection error")
142
+
143
+
144
+ # --- Pytest Fixture to Patch Hydra ---
145
+ @pytest.fixture(autouse=True)
146
+ def patch_hydra(monkeypatch):
147
+ """hydra patch to mock initialize and compose functions."""
148
+ # Patch hydra.initialize to return our dummy context manager.
149
+ monkeypatch.setattr(
150
+ hydra, "initialize", lambda version_base, config_path: DummyHydraContext()
151
+ )
152
+ # Patch hydra.compose to return our dummy config.
153
+ monkeypatch.setattr(hydra, "compose", lambda config_name, overrides: dummy_config)
154
+
155
+
156
+ # --- Test Cases ---
157
+
158
+
159
+ def test_search_tool_success(monkeypatch):
160
+ """
161
+ Test that search_tool returns a valid Command object when the API response is successful.
162
+ Also checks that papers without required fields are filtered out.
163
+ """
164
+ monkeypatch.setattr(requests, "get", dummy_requests_get_success)
165
+
166
+ tool_call_id = "test_tool_call_id"
167
+ # Invoke using .run() with a dictionary input.
168
+ result = search_tool.run(
169
+ {
170
+ "query": "machine learning",
171
+ "tool_call_id": tool_call_id,
172
+ "limit": 3,
173
+ "year": "2020",
174
+ }
175
+ )
176
+
177
+ # Check that a Command is returned with the expected update structure.
178
+ assert isinstance(result, Command)
179
+ update = result.update
180
+ assert "papers" in update
181
+
182
+ papers = update["papers"]
183
+ # Papers with valid 'title' and 'authors' should be included.
184
+ assert "1" in papers
185
+ assert "2" in papers
186
+ # Paper "3" is missing authors and should be filtered out.
187
+ assert "3" not in papers
188
+
189
+ # Check that a ToolMessage is included in the messages.
190
+ messages = update.get("messages", [])
191
+ assert len(messages) == 1
192
+ msg = messages[0]
193
+ assert isinstance(msg, ToolMessage)
194
+ assert "Number of papers found:" in msg.content
195
+
196
+ # Verify that the correct parameters were sent to requests.get.
197
+ called_params = dummy_requests_get_success.called_params
198
+ assert called_params["query"] == "machine learning"
199
+ # The "year" parameter should have been added.
200
+ assert called_params["year"] == "2020"
201
+ # The limit is set to min(limit, 100) so it should be 3.
202
+ assert called_params["limit"] == 3
203
+ # The fields should be a comma-separated string from the dummy config.
204
+ assert called_params["fields"] == "paperId,title,authors"
205
+
206
+
207
+ def test_search_tool_unexpected_format(monkeypatch):
208
+ """
209
+ Test that search_tool raises a RuntimeError when the API response
210
+ does not include the expected 'data' key.
211
+ """
212
+ monkeypatch.setattr(requests, "get", dummy_requests_get_no_data)
213
+ tool_call_id = "test_tool_call_id"
214
+ with pytest.raises(
215
+ RuntimeError,
216
+ match=(
217
+ "Unexpected response from Semantic Scholar API. The results could not be "
218
+ "retrieved due to an unexpected format. "
219
+ "Please modify your search query and try again."
220
+ ),
221
+ ):
222
+ search_tool.run(
223
+ {
224
+ "query": "test",
225
+ "tool_call_id": tool_call_id,
226
+ }
227
+ )
228
+
229
+
230
+ def test_search_tool_no_papers(monkeypatch):
231
+ """
232
+ Test that search_tool raises a RuntimeError when the API returns no papers.
233
+ """
234
+ monkeypatch.setattr(requests, "get", dummy_requests_get_no_papers)
235
+ tool_call_id = "test_tool_call_id"
236
+ with pytest.raises(
237
+ RuntimeError,
238
+ match=(
239
+ "No papers were found for your query. Consider refining your search "
240
+ "by using more specific keywords or different terms."
241
+ ),
242
+ ):
243
+ search_tool.run(
244
+ {
245
+ "query": "test",
246
+ "tool_call_id": tool_call_id,
247
+ }
248
+ )
249
+
250
+
251
+ def test_search_tool_requests_exception(monkeypatch):
252
+ """
253
+ Test that search_tool raises a RuntimeError when requests.get throws an exception.
254
+ """
255
+ monkeypatch.setattr(requests, "get", dummy_requests_get_exception)
256
+ tool_call_id = "test_tool_call_id"
257
+ with pytest.raises(
258
+ RuntimeError,
259
+ match="Failed to connect to Semantic Scholar API. Please retry the same query.",
260
+ ):
261
+ search_tool.run(
262
+ {
263
+ "query": "test",
264
+ "tool_call_id": tool_call_id,
265
+ }
266
+ )
@@ -0,0 +1,274 @@
1
+ """
2
+ Unit tests for S2 tools functionality.
3
+ """
4
+
5
+ from types import SimpleNamespace
6
+ import pytest
7
+ import requests
8
+ import hydra
9
+ from langgraph.types import Command
10
+ from langchain_core.messages import ToolMessage
11
+ from aiagents4pharma.talk2scholars.tools.s2.single_paper_rec import (
12
+ get_single_paper_recommendations,
13
+ )
14
+
15
+ # --- Dummy Hydra Config Setup ---
16
+
17
+
18
+ class DummyHydraContext:
19
+ """
20
+ A dummy context manager for mocking Hydra's initialize and compose functions.
21
+ """
22
+
23
+ def __enter__(self):
24
+ return None
25
+
26
+ def __exit__(self, exc_val, exc_type, traceback):
27
+ pass
28
+
29
+
30
+ # Create a dummy configuration that mimics the expected Hydra config
31
+ dummy_config = SimpleNamespace(
32
+ tools=SimpleNamespace(
33
+ single_paper_recommendation=SimpleNamespace(
34
+ api_endpoint="http://dummy.endpoint",
35
+ api_fields=["paperId", "title", "authors"],
36
+ recommendation_params=SimpleNamespace(from_pool="default_pool"),
37
+ request_timeout=10,
38
+ )
39
+ )
40
+ )
41
+
42
+ # --- Dummy Response Classes and Functions for requests.get ---
43
+
44
+
45
+ class DummyResponse:
46
+ """
47
+ A dummy response class for mocking HTTP responses.
48
+ """
49
+
50
+ def __init__(self, json_data, status_code=200):
51
+ """Initialize a DummyResponse with the given JSON data and status code."""
52
+ self._json_data = json_data
53
+ self.status_code = status_code
54
+
55
+ def json(self):
56
+ """Return the JSON data from the response."""
57
+ return self._json_data
58
+
59
+ def raise_for_status(self):
60
+ """Raise an HTTP error for status codes >= 400."""
61
+ if self.status_code >= 400:
62
+ raise requests.HTTPError("HTTP Error")
63
+
64
+
65
+ def test_dummy_response_no_error():
66
+ """Test DummyResponse does not raise an error for 200 status code."""
67
+ response = DummyResponse({"data": "success"}, status_code=200)
68
+ # Calling raise_for_status should not raise an exception and should return None.
69
+ assert response.raise_for_status() is None
70
+
71
+
72
+ def test_dummy_response_raise_error():
73
+ """Test DummyResponse raises an HTTPError for status codes >= 400."""
74
+ response = DummyResponse({"error": "fail"}, status_code=400)
75
+ # Calling raise_for_status should raise an HTTPError.
76
+ with pytest.raises(requests.HTTPError):
77
+ response.raise_for_status()
78
+
79
+
80
+ def dummy_requests_get_success(url, params, timeout):
81
+ """
82
+ Dummy function to simulate a successful API request returning recommended papers.
83
+ """
84
+ dummy_requests_get_success.called_url = url
85
+ dummy_requests_get_success.called_params = params
86
+ dummy_requests_get_success.called_timeout = timeout
87
+
88
+ # Simulate a valid API response with three recommended papers;
89
+ # one paper missing authors should be filtered out.
90
+ dummy_data = {
91
+ "recommendedPapers": [
92
+ {
93
+ "paperId": "paper1",
94
+ "title": "Recommended Paper 1",
95
+ "authors": ["Author A"],
96
+ "year": 2020,
97
+ "citationCount": 15,
98
+ "url": "http://paper1",
99
+ "externalIds": {"ArXiv": "arxiv1"},
100
+ },
101
+ {
102
+ "paperId": "paper2",
103
+ "title": "Recommended Paper 2",
104
+ "authors": ["Author B"],
105
+ "year": 2021,
106
+ "citationCount": 25,
107
+ "url": "http://paper2",
108
+ "externalIds": {},
109
+ },
110
+ {
111
+ "paperId": "paper3",
112
+ "title": "Recommended Paper 3",
113
+ "authors": None, # This paper should be filtered out.
114
+ "year": 2022,
115
+ "citationCount": 35,
116
+ "url": "http://paper3",
117
+ "externalIds": {"ArXiv": "arxiv3"},
118
+ },
119
+ ]
120
+ }
121
+ return DummyResponse(dummy_data)
122
+
123
+
124
+ def dummy_requests_get_unexpected(url, params, timeout):
125
+ """
126
+ Dummy function to simulate an API response with an unexpected format.
127
+ """
128
+ dummy_requests_get_unexpected.called_url = url
129
+ dummy_requests_get_unexpected.called_params = params
130
+ dummy_requests_get_unexpected.called_timeout = timeout
131
+ return DummyResponse({"error": "Invalid format"})
132
+
133
+
134
+ def dummy_requests_get_no_recs(url, params, timeout):
135
+ """
136
+ Dummy function to simulate an API response returning no recommendations.
137
+ """
138
+ dummy_requests_get_no_recs.called_url = url
139
+ dummy_requests_get_no_recs.called_params = params
140
+ dummy_requests_get_no_recs.called_timeout = timeout
141
+ return DummyResponse({"recommendedPapers": []})
142
+
143
+
144
+ def dummy_requests_get_exception(url, params, timeout):
145
+ """
146
+ Dummy function to simulate a request exception (e.g., network failure).
147
+ """
148
+ dummy_requests_get_exception.called_url = url
149
+ dummy_requests_get_exception.called_params = params
150
+ dummy_requests_get_exception.called_timeout = timeout
151
+ raise requests.exceptions.RequestException("Connection error")
152
+
153
+
154
+ # --- Pytest Fixture to Patch Hydra ---
155
+ @pytest.fixture(autouse=True)
156
+ def patch_hydra(monkeypatch):
157
+ """Patch Hydra's initialize and compose functions with dummy implementations."""
158
+ monkeypatch.setattr(
159
+ hydra, "initialize", lambda version_base, config_path: DummyHydraContext()
160
+ )
161
+ # Patch hydra.compose to return our dummy config.
162
+ monkeypatch.setattr(hydra, "compose", lambda config_name, overrides: dummy_config)
163
+
164
+
165
+ # --- Test Cases ---
166
+
167
+
168
+ def test_single_paper_rec_success(monkeypatch):
169
+ """
170
+ Test that get_single_paper_recommendations returns a valid Command object
171
+ when the API response is successful. Also, ensure that recommendations missing
172
+ required fields (like authors) are filtered out.
173
+ """
174
+ monkeypatch.setattr(requests, "get", dummy_requests_get_success)
175
+
176
+ tool_call_id = "test_tool_call_id"
177
+ input_data = {
178
+ "paper_id": "12345",
179
+ "tool_call_id": tool_call_id,
180
+ "limit": 3,
181
+ "year": "2020",
182
+ }
183
+ # Invoke the tool using .run() with a single dictionary as input.
184
+ result = get_single_paper_recommendations.run(input_data)
185
+
186
+ # Validate that the result is a Command with the expected structure.
187
+ assert isinstance(result, Command)
188
+ update = result.update
189
+ assert "papers" in update
190
+
191
+ papers = update["papers"]
192
+ # Papers with valid 'title' and 'authors' should be included.
193
+ assert "paper1" in papers
194
+ assert "paper2" in papers
195
+ # Paper "paper3" is missing authors and should be filtered out.
196
+ assert "paper3" not in papers
197
+
198
+ # Check that a ToolMessage is included in the messages.
199
+ messages = update.get("messages", [])
200
+ assert len(messages) == 1
201
+ msg = messages[0]
202
+ assert isinstance(msg, ToolMessage)
203
+ assert "Recommendations based on the single paper were successful" in msg.content
204
+
205
+ # Verify that the correct parameters were sent to requests.get.
206
+ called_params = dummy_requests_get_success.called_params
207
+ assert called_params["limit"] == 3 # limited to min(limit, 500)
208
+ # "fields" should be a comma-separated string from the dummy config.
209
+ assert called_params["fields"] == "paperId,title,authors"
210
+ # Check that the "from" parameter is set from our dummy config.
211
+ assert called_params["from"] == "default_pool"
212
+ # The year parameter should be present.
213
+ assert called_params["year"] == "2020"
214
+
215
+
216
+ def test_single_paper_rec_unexpected_format(monkeypatch):
217
+ """
218
+ Test that get_single_paper_recommendations raises a RuntimeError when the API
219
+ response does not include the expected 'recommendedPapers' key.
220
+ """
221
+ monkeypatch.setattr(requests, "get", dummy_requests_get_unexpected)
222
+ tool_call_id = "test_tool_call_id"
223
+ input_data = {
224
+ "paper_id": "12345",
225
+ "tool_call_id": tool_call_id,
226
+ }
227
+ with pytest.raises(
228
+ RuntimeError,
229
+ match=(
230
+ "Unexpected response from Semantic Scholar API. The results could not be "
231
+ "retrieved due to an unexpected format. "
232
+ "Please modify your search query and try again."
233
+ ),
234
+ ):
235
+ get_single_paper_recommendations.run(input_data)
236
+
237
+
238
+ def test_single_paper_rec_no_recommendations(monkeypatch):
239
+ """
240
+ Test that get_single_paper_recommendations raises a RuntimeError when the API
241
+ returns no recommendations.
242
+ """
243
+ monkeypatch.setattr(requests, "get", dummy_requests_get_no_recs)
244
+ tool_call_id = "test_tool_call_id"
245
+ input_data = {
246
+ "paper_id": "12345",
247
+ "tool_call_id": tool_call_id,
248
+ }
249
+ with pytest.raises(
250
+ RuntimeError,
251
+ match=(
252
+ "No recommendations were found for your query. Consider refining your search "
253
+ "by using more specific keywords or different terms."
254
+ ),
255
+ ):
256
+ get_single_paper_recommendations.run(input_data)
257
+
258
+
259
+ def test_single_paper_rec_requests_exception(monkeypatch):
260
+ """
261
+ Test that get_single_paper_recommendations raises a RuntimeError when requests.get
262
+ throws an exception.
263
+ """
264
+ monkeypatch.setattr(requests, "get", dummy_requests_get_exception)
265
+ tool_call_id = "test_tool_call_id"
266
+ input_data = {
267
+ "paper_id": "12345",
268
+ "tool_call_id": tool_call_id,
269
+ }
270
+ with pytest.raises(
271
+ RuntimeError,
272
+ match="Failed to connect to Semantic Scholar API. Please retry the same query.",
273
+ ):
274
+ get_single_paper_recommendations.run(input_data)