|
| 1 | +import unittest |
| 2 | +from integuru.agent import IntegrationAgent |
| 3 | +from integuru.models.agent_state import AgentState |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | + |
| 6 | +class TestIntegrationAgent(unittest.TestCase): |
| 7 | + |
| 8 | + def setUp(self): |
| 9 | + self.prompt = "Test prompt" |
| 10 | + self.har_file_path = "test.har" |
| 11 | + self.cookie_path = "test_cookies.json" |
| 12 | + self.agent = IntegrationAgent(self.prompt, self.har_file_path, self.cookie_path) |
| 13 | + self.state = AgentState( |
| 14 | + master_node=None, |
| 15 | + in_process_node=None, |
| 16 | + to_be_processed_nodes=[], |
| 17 | + in_process_node_dynamic_parts=[], |
| 18 | + action_url="", |
| 19 | + input_variables={} |
| 20 | + ) |
| 21 | + |
| 22 | + @patch('integuru.agent.llm.get_instance') |
| 23 | + def test_end_url_identify_agent(self, mock_llm_instance): |
| 24 | + mock_response = MagicMock() |
| 25 | + mock_response.additional_kwargs = { |
| 26 | + 'function_call': { |
| 27 | + 'arguments': '{"url": "http://example.com/action"}' |
| 28 | + } |
| 29 | + } |
| 30 | + mock_llm_instance.return_value.invoke.return_value = mock_response |
| 31 | + |
| 32 | + updated_state = self.agent.end_url_identify_agent(self.state) |
| 33 | + self.assertEqual(updated_state[self.agent.ACTION_URL_KEY], "http://example.com/action") |
| 34 | + |
| 35 | + @patch('integuru.agent.llm.get_instance') |
| 36 | + def test_input_variables_identifying_agent(self, mock_llm_instance): |
| 37 | + self.state[self.agent.IN_PROCESS_NODE_KEY] = "node_1" |
| 38 | + self.state[self.agent.INPUT_VARIABLES_KEY] = {"var1": "value1"} |
| 39 | + self.agent.dag_manager.graph.add_node("node_1", content={"key": MagicMock()}) |
| 40 | + self.agent.dag_manager.graph.nodes["node_1"]["content"]["key"].to_curl_command.return_value = "curl command" |
| 41 | + |
| 42 | + mock_response = MagicMock() |
| 43 | + mock_response.additional_kwargs = { |
| 44 | + 'function_call': { |
| 45 | + 'arguments': '{"identified_variables": [{"variable_name": "var1", "variable_value": "value1"}]}' |
| 46 | + } |
| 47 | + } |
| 48 | + mock_llm_instance.return_value.invoke.return_value = mock_response |
| 49 | + |
| 50 | + updated_state = self.agent.input_variables_identifying_agent(self.state) |
| 51 | + self.assertEqual(updated_state[self.agent.INPUT_VARIABLES_KEY], {"var1": "value1"}) |
| 52 | + |
| 53 | + @patch('integuru.agent.llm.get_instance') |
| 54 | + def test_dynamic_part_identifying_agent(self, mock_llm_instance): |
| 55 | + self.state[self.agent.TO_BE_PROCESSED_NODES_KEY] = ["node_1"] |
| 56 | + self.agent.dag_manager.graph.add_node("node_1", content={"key": MagicMock()}) |
| 57 | + self.agent.dag_manager.graph.nodes["node_1"]["content"]["key"].to_minified_curl_command.return_value = "curl command" |
| 58 | + |
| 59 | + mock_response = MagicMock() |
| 60 | + mock_response.additional_kwargs = { |
| 61 | + 'function_call': { |
| 62 | + 'arguments': '{"dynamic_parts": ["dynamic_part1"]}' |
| 63 | + } |
| 64 | + } |
| 65 | + mock_llm_instance.return_value.invoke.return_value = mock_response |
| 66 | + |
| 67 | + updated_state = self.agent.dynamic_part_identifying_agent(self.state) |
| 68 | + self.assertEqual(updated_state[self.agent.IN_PROCESS_NODE_DYNAMIC_PARTS_KEY], ["dynamic_part1"]) |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + unittest.main() |
0 commit comments