|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest.mock import MagicMock, patch |
| 17 | +from google.genai import types |
| 18 | +from agent import BrowserAgent, multiply_numbers |
| 19 | +from computers import EnvState |
| 20 | + |
| 21 | +class TestBrowserAgent(unittest.TestCase): |
| 22 | + def setUp(self): |
| 23 | + self.mock_browser_computer = MagicMock() |
| 24 | + self.mock_browser_computer.screen_size.return_value = (1000, 1000) |
| 25 | + self.agent = BrowserAgent( |
| 26 | + browser_computer=self.mock_browser_computer, |
| 27 | + query="test query", |
| 28 | + model_name="test_model" |
| 29 | + ) |
| 30 | + # Mock the genai client |
| 31 | + self.agent._client = MagicMock() |
| 32 | + |
| 33 | + def test_multiply_numbers(self): |
| 34 | + self.assertEqual(multiply_numbers(2, 3), {"result": 5}) |
| 35 | + |
| 36 | + def test_handle_action_open_web_browser(self): |
| 37 | + action = types.FunctionCall(name="open_web_browser", args={}) |
| 38 | + self.agent.handle_action(action) |
| 39 | + self.mock_browser_computer.open_web_browser.assert_called_once() |
| 40 | + |
| 41 | + def test_handle_action_click_at(self): |
| 42 | + action = types.FunctionCall(name="click_at", args={"x": 100, "y": 200}) |
| 43 | + self.agent.handle_action(action) |
| 44 | + self.mock_browser_computer.click_at.assert_called_once_with(x=100, y=200) |
| 45 | + |
| 46 | + def test_handle_action_type_text_at(self): |
| 47 | + action = types.FunctionCall(name="type_text_at", args={"x": 100, "y": 200, "text": "hello"}) |
| 48 | + self.agent.handle_action(action) |
| 49 | + self.mock_browser_computer.type_text_at.assert_called_once_with( |
| 50 | + x=100, y=200, text="hello", press_enter=False, clear_before_typing=True |
| 51 | + ) |
| 52 | + |
| 53 | + def test_handle_action_scroll_document(self): |
| 54 | + action = types.FunctionCall(name="scroll_document", args={"direction": "down"}) |
| 55 | + self.agent.handle_action(action) |
| 56 | + self.mock_browser_computer.scroll_document.assert_called_once_with("down") |
| 57 | + |
| 58 | + def test_handle_action_navigate(self): |
| 59 | + action = types.FunctionCall(name="navigate", args={"url": "https://example.com"}) |
| 60 | + self.agent.handle_action(action) |
| 61 | + self.mock_browser_computer.navigate.assert_called_once_with("https://example.com") |
| 62 | + |
| 63 | + def test_handle_action_unknown_function(self): |
| 64 | + action = types.FunctionCall(name="unknown_function", args={}) |
| 65 | + with self.assertRaises(ValueError): |
| 66 | + self.agent.handle_action(action) |
| 67 | + |
| 68 | + def test_normalize_x(self): |
| 69 | + self.assertEqual(self.agent.normalize_x(500), 500) |
| 70 | + |
| 71 | + def test_normalize_y(self): |
| 72 | + self.assertEqual(self.agent.normalize_y(500), 500) |
| 73 | + |
| 74 | + @patch('agent.BrowserAgent.get_model_response') |
| 75 | + def test_run_one_iteration_no_function_calls(self, mock_get_model_response): |
| 76 | + mock_response = MagicMock() |
| 77 | + mock_candidate = MagicMock() |
| 78 | + mock_candidate.content.parts = [types.Part(text="some reasoning")] |
| 79 | + mock_response.candidates = [mock_candidate] |
| 80 | + mock_get_model_response.return_value = mock_response |
| 81 | + |
| 82 | + result = self.agent.run_one_iteration() |
| 83 | + |
| 84 | + self.assertEqual(result, "COMPLETE") |
| 85 | + self.assertEqual(len(self.agent._contents), 2) |
| 86 | + self.assertEqual(self.agent._contents[1], mock_candidate.content) |
| 87 | + |
| 88 | + @patch('agent.BrowserAgent.get_model_response') |
| 89 | + @patch('agent.BrowserAgent._execute_function_call') |
| 90 | + def test_run_one_iteration_with_function_call(self, mock_execute_function_call, mock_get_model_response): |
| 91 | + mock_response = MagicMock() |
| 92 | + mock_candidate = MagicMock() |
| 93 | + function_call = types.FunctionCall(name="navigate", args={"url": "https://example.com"}) |
| 94 | + mock_candidate.content.parts = [types.Part(function_call=function_call)] |
| 95 | + mock_response.candidates = [mock_candidate] |
| 96 | + mock_get_model_response.return_value = mock_response |
| 97 | + |
| 98 | + mock_env_state = EnvState(screenshot=b"screenshot", url="https://example.com") |
| 99 | + mock_execute_function_call.return_value = mock_env_state |
| 100 | + |
| 101 | + result = self.agent.run_one_iteration() |
| 102 | + |
| 103 | + self.assertEqual(result, "CONTINUE") |
| 104 | + mock_execute_function_call.assert_called_once_with(function_call) |
| 105 | + self.assertEqual(len(self.agent._contents), 3) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + unittest.main() |
0 commit comments