askrobots / dbbasic-textbrowser
Public
A text-mode web browser with AI help, inspired by Lynx, plus TextFirst.css for text-first websites.
test_integration.py · 18.4 KB · 525 lines
| 1 | """ |
| 2 | Integration tests for DBBasic TextBrowser |
| 3 | Tests real functionality like form submission, link clicking, AI interaction |
| 4 | """ |
| 5 | |
| 6 | import unittest |
| 7 | from unittest.mock import Mock, patch, MagicMock, call |
| 8 | import sys |
| 9 | import os |
| 10 | |
| 11 | # Add parent directory to path |
| 12 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 13 | |
| 14 | # Mock curses before importing with proper key constants |
| 15 | mock_curses = MagicMock() |
| 16 | mock_curses.KEY_UP = 259 |
| 17 | mock_curses.KEY_DOWN = 258 |
| 18 | mock_curses.KEY_PPAGE = 339 |
| 19 | mock_curses.KEY_NPAGE = 338 |
| 20 | mock_curses.KEY_HOME = 262 |
| 21 | mock_curses.KEY_END = 360 |
| 22 | sys.modules['curses'] = mock_curses |
| 23 | |
| 24 | from browser import Browser |
| 25 | from bs4 import BeautifulSoup |
| 26 | |
| 27 | |
| 28 | class TestFormSubmission(unittest.TestCase): |
| 29 | """Test form detection and submission""" |
| 30 | |
| 31 | def setUp(self): |
| 32 | """Set up test fixtures""" |
| 33 | self.mock_stdscr = Mock() |
| 34 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 35 | |
| 36 | @patch('browser.requests.get') |
| 37 | def test_form_detection(self, mock_get): |
| 38 | """Test that forms are detected on pages""" |
| 39 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 40 | browser = Browser(self.mock_stdscr) |
| 41 | |
| 42 | # Mock HTML with a form |
| 43 | html_content = """ |
| 44 | <html> |
| 45 | <body> |
| 46 | <form action="/search" method="get"> |
| 47 | <input type="text" name="q" placeholder="Search..."> |
| 48 | <input type="submit" value="Search"> |
| 49 | </form> |
| 50 | </body> |
| 51 | </html> |
| 52 | """ |
| 53 | |
| 54 | mock_response = Mock() |
| 55 | mock_response.text = html_content |
| 56 | mock_response.raise_for_status = Mock() |
| 57 | mock_get.return_value = mock_response |
| 58 | |
| 59 | # Fetch page |
| 60 | browser.fetch_page("https://example.com") |
| 61 | |
| 62 | # Check that form was detected |
| 63 | self.assertEqual(len(browser.forms), 1) |
| 64 | self.assertEqual(browser.forms[0]['method'], 'GET') |
| 65 | self.assertEqual(browser.forms[0]['action'], '/search') |
| 66 | self.assertEqual(len(browser.forms[0]['fields']), 1) |
| 67 | self.assertEqual(browser.forms[0]['fields'][0]['name'], 'q') |
| 68 | |
| 69 | @patch('browser.requests.get') |
| 70 | def test_form_submission_get(self, mock_get): |
| 71 | """Test that GET form submission works""" |
| 72 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 73 | browser = Browser(self.mock_stdscr) |
| 74 | |
| 75 | # Setup initial page with form |
| 76 | html_content = """ |
| 77 | <html> |
| 78 | <body> |
| 79 | <form action="/search" method="get"> |
| 80 | <input type="text" name="q"> |
| 81 | </form> |
| 82 | </body> |
| 83 | </html> |
| 84 | """ |
| 85 | |
| 86 | mock_response = Mock() |
| 87 | mock_response.text = html_content |
| 88 | mock_response.url = "https://example.com" |
| 89 | mock_response.raise_for_status = Mock() |
| 90 | mock_get.return_value = mock_response |
| 91 | |
| 92 | browser.fetch_page("https://example.com") |
| 93 | |
| 94 | # Prepare form data |
| 95 | form = browser.forms[0] |
| 96 | form_values = {'q': 'test query'} |
| 97 | |
| 98 | # Mock the result page |
| 99 | result_html = "<html><body><h1>Search Results</h1></body></html>" |
| 100 | mock_response.text = result_html |
| 101 | mock_response.url = "https://example.com/search?q=test+query" |
| 102 | |
| 103 | # Submit form |
| 104 | browser.submit_form(form, form_values) |
| 105 | |
| 106 | # Verify GET request was made with parameters |
| 107 | self.assertEqual(mock_get.call_count, 2) |
| 108 | last_call = mock_get.call_args_list[1] |
| 109 | self.assertIn('params', last_call.kwargs) |
| 110 | self.assertEqual(last_call.kwargs['params'], form_values) |
| 111 | |
| 112 | @patch('browser.requests.post') |
| 113 | def test_form_submission_post(self, mock_post): |
| 114 | """Test that POST form submission works""" |
| 115 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 116 | browser = Browser(self.mock_stdscr) |
| 117 | |
| 118 | # Setup form manually |
| 119 | browser.current_url = "https://example.com" |
| 120 | form = { |
| 121 | 'action': '/login', |
| 122 | 'method': 'POST', |
| 123 | 'fields': [ |
| 124 | {'name': 'username', 'type': 'text'}, |
| 125 | {'name': 'password', 'type': 'password'} |
| 126 | ] |
| 127 | } |
| 128 | browser.forms = [form] |
| 129 | |
| 130 | # Mock POST response |
| 131 | mock_response = Mock() |
| 132 | mock_response.text = "<html><body><h1>Welcome</h1></body></html>" |
| 133 | mock_response.url = "https://example.com/dashboard" |
| 134 | mock_response.raise_for_status = Mock() |
| 135 | mock_post.return_value = mock_response |
| 136 | |
| 137 | # Submit form |
| 138 | form_values = {'username': 'testuser', 'password': 'testpass'} |
| 139 | browser.submit_form(form, form_values) |
| 140 | |
| 141 | # Verify POST was called |
| 142 | mock_post.assert_called_once() |
| 143 | call_kwargs = mock_post.call_args.kwargs |
| 144 | self.assertEqual(call_kwargs['data'], form_values) |
| 145 | self.assertIn('https://example.com/login', mock_post.call_args.args) |
| 146 | |
| 147 | |
| 148 | class TestLinkNavigation(unittest.TestCase): |
| 149 | """Test link extraction and navigation""" |
| 150 | |
| 151 | def setUp(self): |
| 152 | """Set up test fixtures""" |
| 153 | self.mock_stdscr = Mock() |
| 154 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 155 | |
| 156 | @patch('browser.requests.get') |
| 157 | def test_link_extraction(self, mock_get): |
| 158 | """Test that links are properly extracted from HTML""" |
| 159 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 160 | browser = Browser(self.mock_stdscr) |
| 161 | |
| 162 | # Mock HTML with links |
| 163 | html_content = """ |
| 164 | <html> |
| 165 | <body> |
| 166 | <a href="https://example.com/page1">Page 1</a> |
| 167 | <a href="/page2">Page 2</a> |
| 168 | <a href="#anchor">Anchor</a> |
| 169 | <a href="javascript:void(0)">JS Link</a> |
| 170 | <a href="mailto:test@example.com">Email</a> |
| 171 | </body> |
| 172 | </html> |
| 173 | """ |
| 174 | |
| 175 | mock_response = Mock() |
| 176 | mock_response.text = html_content |
| 177 | mock_response.raise_for_status = Mock() |
| 178 | mock_get.return_value = mock_response |
| 179 | |
| 180 | # Fetch page |
| 181 | browser.fetch_page("https://example.com") |
| 182 | |
| 183 | # Should extract 2 valid links (absolute and relative) |
| 184 | # Should skip anchor, javascript:, and mailto: |
| 185 | self.assertEqual(len(browser.links), 2) |
| 186 | self.assertEqual(browser.links[0]['url'], 'https://example.com/page1') |
| 187 | self.assertEqual(browser.links[1]['url'], 'https://example.com/page2') |
| 188 | |
| 189 | @patch('browser.requests.get') |
| 190 | def test_link_navigation(self, mock_get): |
| 191 | """Test that clicking a link navigates to that URL""" |
| 192 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 193 | browser = Browser(self.mock_stdscr) |
| 194 | |
| 195 | # Setup initial page |
| 196 | html_content = """ |
| 197 | <html> |
| 198 | <body> |
| 199 | <a href="https://example.com/next">Next Page</a> |
| 200 | </body> |
| 201 | </html> |
| 202 | """ |
| 203 | |
| 204 | mock_response = Mock() |
| 205 | mock_response.text = html_content |
| 206 | mock_response.raise_for_status = Mock() |
| 207 | mock_get.return_value = mock_response |
| 208 | |
| 209 | browser.fetch_page("https://example.com") |
| 210 | |
| 211 | # Navigate to first link |
| 212 | next_page_html = "<html><body><h1>Next Page</h1></body></html>" |
| 213 | mock_response.text = next_page_html |
| 214 | |
| 215 | link_url = browser.links[0]['url'] |
| 216 | browser.fetch_page(link_url) |
| 217 | |
| 218 | # Verify we navigated |
| 219 | self.assertEqual(browser.current_url, link_url) |
| 220 | |
| 221 | @patch('browser.requests.get') |
| 222 | def test_relative_url_handling(self, mock_get): |
| 223 | """Test that relative URLs are converted to absolute""" |
| 224 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 225 | browser = Browser(self.mock_stdscr) |
| 226 | |
| 227 | html_content = """ |
| 228 | <html> |
| 229 | <body> |
| 230 | <a href="/relative/path">Relative Link</a> |
| 231 | <a href="../parent/path">Parent Link</a> |
| 232 | </body> |
| 233 | </html> |
| 234 | """ |
| 235 | |
| 236 | mock_response = Mock() |
| 237 | mock_response.text = html_content |
| 238 | mock_response.raise_for_status = Mock() |
| 239 | mock_get.return_value = mock_response |
| 240 | |
| 241 | browser.fetch_page("https://example.com/current/page") |
| 242 | |
| 243 | # Check that relative URLs were made absolute |
| 244 | self.assertTrue(browser.links[0]['url'].startswith('https://')) |
| 245 | self.assertTrue(browser.links[1]['url'].startswith('https://')) |
| 246 | |
| 247 | |
| 248 | class TestAICommands(unittest.TestCase): |
| 249 | """Test AI command processing""" |
| 250 | |
| 251 | def setUp(self): |
| 252 | """Set up test fixtures""" |
| 253 | self.mock_stdscr = Mock() |
| 254 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 255 | self.mock_stdscr.refresh = Mock() |
| 256 | |
| 257 | def test_ai_command_without_key(self): |
| 258 | """Test that AI commands fail gracefully without API key""" |
| 259 | with patch.dict(os.environ, {}, clear=True): |
| 260 | if 'OPENAI_API_KEY' in os.environ: |
| 261 | del os.environ['OPENAI_API_KEY'] |
| 262 | |
| 263 | browser = Browser(self.mock_stdscr) |
| 264 | browser.page_text = "Some page content" |
| 265 | |
| 266 | # Try to process AI command |
| 267 | browser.process_ai_command("summarize this page") |
| 268 | |
| 269 | # Should show error message |
| 270 | self.assertIn("AI features are not enabled", browser.page_content[0]) |
| 271 | |
| 272 | @patch('browser.OpenAI') |
| 273 | def test_ai_command_with_key(self, mock_openai_class): |
| 274 | """Test that AI commands work with API key""" |
| 275 | with patch.dict(os.environ, {'OPENAI_API_KEY': 'test-key'}): |
| 276 | # Setup mock OpenAI client |
| 277 | mock_client = Mock() |
| 278 | mock_openai_class.return_value = mock_client |
| 279 | |
| 280 | # Mock response |
| 281 | mock_message = Mock() |
| 282 | mock_message.content = "This page is about testing." |
| 283 | mock_message.tool_calls = None |
| 284 | |
| 285 | mock_choice = Mock() |
| 286 | mock_choice.message = mock_message |
| 287 | |
| 288 | mock_response = Mock() |
| 289 | mock_response.choices = [mock_choice] |
| 290 | |
| 291 | mock_client.chat.completions.create.return_value = mock_response |
| 292 | |
| 293 | browser = Browser(self.mock_stdscr) |
| 294 | browser.page_text = "Some page content about testing" |
| 295 | browser.current_url = "https://example.com" |
| 296 | |
| 297 | # Process AI command |
| 298 | browser.process_ai_command("summarize this page") |
| 299 | |
| 300 | # Verify OpenAI was called |
| 301 | mock_client.chat.completions.create.assert_called_once() |
| 302 | |
| 303 | # Verify response was displayed |
| 304 | page_text = '\n'.join(browser.page_content) |
| 305 | self.assertIn("This page is about testing", page_text) |
| 306 | |
| 307 | @patch('browser.OpenAI') |
| 308 | def test_ai_navigation_function_call(self, mock_openai_class): |
| 309 | """Test that AI can navigate using function calling""" |
| 310 | with patch.dict(os.environ, {'OPENAI_API_KEY': 'test-key'}): |
| 311 | # Setup mock OpenAI client |
| 312 | mock_client = Mock() |
| 313 | mock_openai_class.return_value = mock_client |
| 314 | |
| 315 | # Mock function call response |
| 316 | mock_tool_call = Mock() |
| 317 | mock_tool_call.function.name = "navigate_to_url" |
| 318 | mock_tool_call.function.arguments = '{"url": "https://wikipedia.org", "reason": "Looking up WebDAV"}' |
| 319 | |
| 320 | mock_message = Mock() |
| 321 | mock_message.content = None |
| 322 | mock_message.tool_calls = [mock_tool_call] |
| 323 | |
| 324 | mock_choice = Mock() |
| 325 | mock_choice.message = mock_message |
| 326 | |
| 327 | mock_response = Mock() |
| 328 | mock_response.choices = [mock_choice] |
| 329 | |
| 330 | mock_client.chat.completions.create.return_value = mock_response |
| 331 | |
| 332 | browser = Browser(self.mock_stdscr) |
| 333 | |
| 334 | # Mock fetch_page to avoid actual network call |
| 335 | with patch.object(browser, 'fetch_page') as mock_fetch: |
| 336 | browser.process_ai_command("go to wikipedia for WebDAV") |
| 337 | |
| 338 | # Verify navigate_to_url was called |
| 339 | mock_fetch.assert_called_once_with("https://wikipedia.org") |
| 340 | |
| 341 | |
| 342 | class TestPageRendering(unittest.TestCase): |
| 343 | """Test page rendering and display""" |
| 344 | |
| 345 | def setUp(self): |
| 346 | """Set up test fixtures""" |
| 347 | self.mock_stdscr = Mock() |
| 348 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 349 | self.mock_stdscr.addstr = Mock() |
| 350 | self.mock_stdscr.clear = Mock() |
| 351 | self.mock_stdscr.refresh = Mock() |
| 352 | |
| 353 | def test_render_basic_page(self): |
| 354 | """Test that basic pages render without errors""" |
| 355 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 356 | browser = Browser(self.mock_stdscr) |
| 357 | browser.page_content = ["Line 1", "Line 2", "Line 3"] |
| 358 | browser.current_url = "https://example.com" |
| 359 | |
| 360 | # Render should not raise |
| 361 | browser.render() |
| 362 | |
| 363 | # Verify screen was cleared and refreshed |
| 364 | self.mock_stdscr.clear.assert_called() |
| 365 | self.mock_stdscr.refresh.assert_called() |
| 366 | |
| 367 | def test_scroll_handling(self): |
| 368 | """Test that scrolling works correctly""" |
| 369 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 370 | browser = Browser(self.mock_stdscr) |
| 371 | |
| 372 | # Create content longer than screen |
| 373 | browser.page_content = [f"Line {i}" for i in range(100)] |
| 374 | browser.scroll_offset = 0 |
| 375 | |
| 376 | # Test scrolling down |
| 377 | content_height = 24 - 2 # Screen height minus bars |
| 378 | browser.scroll_offset = 10 |
| 379 | |
| 380 | # Should be able to scroll |
| 381 | self.assertEqual(browser.scroll_offset, 10) |
| 382 | |
| 383 | # Test max scroll |
| 384 | max_scroll = len(browser.page_content) - content_height |
| 385 | browser.scroll_offset = max_scroll |
| 386 | self.assertLessEqual(browser.scroll_offset, max_scroll) |
| 387 | |
| 388 | |
| 389 | class TestKeyboardInput(unittest.TestCase): |
| 390 | """Test keyboard input handling""" |
| 391 | |
| 392 | def setUp(self): |
| 393 | """Set up test fixtures""" |
| 394 | self.mock_stdscr = Mock() |
| 395 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 396 | |
| 397 | def test_quit_key(self): |
| 398 | """Test that Q key quits the browser""" |
| 399 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 400 | browser = Browser(self.mock_stdscr) |
| 401 | browser.running = True |
| 402 | |
| 403 | # Press 'q' key |
| 404 | browser.handle_input(ord('q')) |
| 405 | |
| 406 | # Browser should stop running |
| 407 | self.assertFalse(browser.running) |
| 408 | |
| 409 | def test_help_key(self): |
| 410 | """Test that H key shows help""" |
| 411 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 412 | browser = Browser(self.mock_stdscr) |
| 413 | |
| 414 | # Mock the help file with proper content |
| 415 | help_html = """ |
| 416 | <html> |
| 417 | <body> |
| 418 | <h1>Help Page</h1> |
| 419 | <p>This is the help content</p> |
| 420 | </body> |
| 421 | </html> |
| 422 | """ |
| 423 | mock_file = MagicMock() |
| 424 | mock_file.read.return_value = help_html |
| 425 | mock_file.__enter__.return_value = mock_file |
| 426 | |
| 427 | with patch('builtins.open', return_value=mock_file): |
| 428 | with patch('os.path.exists', return_value=True): |
| 429 | browser.handle_input(ord('h')) |
| 430 | |
| 431 | # Should have loaded help content |
| 432 | self.assertIsNotNone(browser.page_content) |
| 433 | self.assertTrue(len(browser.page_content) > 0) |
| 434 | |
| 435 | def test_number_key_navigation(self): |
| 436 | """Test that number keys navigate to links""" |
| 437 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 438 | browser = Browser(self.mock_stdscr) |
| 439 | |
| 440 | # Setup links |
| 441 | browser.links = [ |
| 442 | {'url': 'https://example.com/0', 'text': 'Link 0'}, |
| 443 | {'url': 'https://example.com/1', 'text': 'Link 1'}, |
| 444 | ] |
| 445 | |
| 446 | # Mock fetch_page |
| 447 | with patch.object(browser, 'fetch_page') as mock_fetch: |
| 448 | # Press '0' key |
| 449 | browser.handle_input(ord('0')) |
| 450 | |
| 451 | # Should navigate to first link |
| 452 | mock_fetch.assert_called_once_with('https://example.com/0') |
| 453 | |
| 454 | def test_scroll_keys(self): |
| 455 | """Test that arrow keys scroll the page""" |
| 456 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 457 | browser = Browser(self.mock_stdscr) |
| 458 | browser.page_content = [f"Line {i}" for i in range(100)] |
| 459 | browser.scroll_offset = 10 |
| 460 | |
| 461 | # Press up arrow (KEY_UP = 259) |
| 462 | browser.handle_input(259) |
| 463 | self.assertEqual(browser.scroll_offset, 9) |
| 464 | |
| 465 | # Press down arrow (KEY_DOWN = 258) |
| 466 | browser.handle_input(258) |
| 467 | self.assertEqual(browser.scroll_offset, 10) |
| 468 | |
| 469 | |
| 470 | class TestColorRendering(unittest.TestCase): |
| 471 | """Test HTML color rendering""" |
| 472 | |
| 473 | def setUp(self): |
| 474 | """Set up test fixtures""" |
| 475 | self.mock_stdscr = Mock() |
| 476 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 477 | self.mock_stdscr.addstr = Mock() |
| 478 | |
| 479 | @patch('browser.requests.get') |
| 480 | def test_font_color_parsing(self, mock_get): |
| 481 | """Test that <font color> tags are parsed correctly""" |
| 482 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 483 | browser = Browser(self.mock_stdscr) |
| 484 | |
| 485 | # HTML with font colors |
| 486 | html_content = """ |
| 487 | <html> |
| 488 | <body> |
| 489 | <font color="red">Error</font> |
| 490 | <font color="green">Success</font> |
| 491 | <font color="blue">Info</font> |
| 492 | </body> |
| 493 | </html> |
| 494 | """ |
| 495 | |
| 496 | mock_response = Mock() |
| 497 | mock_response.text = html_content |
| 498 | mock_response.raise_for_status = Mock() |
| 499 | mock_get.return_value = mock_response |
| 500 | |
| 501 | browser.fetch_page("https://example.com") |
| 502 | |
| 503 | # Check that color markers are in content |
| 504 | page_text = ''.join(browser.page_content) |
| 505 | self.assertIn('«red»', page_text) |
| 506 | self.assertIn('«green»', page_text) |
| 507 | self.assertIn('«blue»', page_text) |
| 508 | |
| 509 | def test_color_rendering(self): |
| 510 | """Test that colored text is rendered with color codes""" |
| 511 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 512 | browser = Browser(self.mock_stdscr) |
| 513 | |
| 514 | # Test line with color markers |
| 515 | line = "«red»Error: Something went wrong«/red»" |
| 516 | |
| 517 | # This should not raise an error |
| 518 | browser.render_line_with_formatting(1, line, 80) |
| 519 | |
| 520 | # Verify addstr was called (color rendering happened) |
| 521 | self.assertTrue(self.mock_stdscr.addstr.called) |
| 522 | |
| 523 | |
| 524 | if __name__ == '__main__': |
| 525 | unittest.main() |