askrobots / dbbasic-textbrowser
Public
A text-mode web browser with AI help, inspired by Lynx, plus TextFirst.css for text-first websites.
test_browser.py · 7.6 KB · 218 lines
| 1 | """ |
| 2 | Unit tests for DBBasic TextBrowser |
| 3 | """ |
| 4 | |
| 5 | import unittest |
| 6 | from unittest.mock import Mock, patch, MagicMock |
| 7 | import sys |
| 8 | import os |
| 9 | |
| 10 | # Add parent directory to path to import browser module |
| 11 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 12 | |
| 13 | # Mock curses before importing browser with proper key constants |
| 14 | mock_curses = MagicMock() |
| 15 | mock_curses.KEY_UP = 259 |
| 16 | mock_curses.KEY_DOWN = 258 |
| 17 | mock_curses.KEY_PPAGE = 339 |
| 18 | mock_curses.KEY_NPAGE = 338 |
| 19 | mock_curses.KEY_HOME = 262 |
| 20 | mock_curses.KEY_END = 360 |
| 21 | sys.modules['curses'] = mock_curses |
| 22 | |
| 23 | from browser import Browser |
| 24 | |
| 25 | |
| 26 | class TestURLDetection(unittest.TestCase): |
| 27 | """Test URL detection functionality""" |
| 28 | |
| 29 | def setUp(self): |
| 30 | """Set up test fixtures""" |
| 31 | # Create a mock stdscr for Browser initialization |
| 32 | self.mock_stdscr = Mock() |
| 33 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 34 | |
| 35 | def test_valid_urls(self): |
| 36 | """Test that valid URLs are correctly identified""" |
| 37 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 38 | browser = Browser(self.mock_stdscr) |
| 39 | |
| 40 | # Test various URL formats |
| 41 | self.assertTrue(browser.is_url("example.com")) |
| 42 | self.assertTrue(browser.is_url("https://example.com")) |
| 43 | self.assertTrue(browser.is_url("http://example.com")) |
| 44 | self.assertTrue(browser.is_url("example.com/path")) |
| 45 | self.assertTrue(browser.is_url("subdomain.example.com")) |
| 46 | self.assertTrue(browser.is_url("example.co.uk")) |
| 47 | |
| 48 | def test_invalid_urls(self): |
| 49 | """Test that non-URLs are correctly identified""" |
| 50 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 51 | browser = Browser(self.mock_stdscr) |
| 52 | |
| 53 | # Test non-URL strings |
| 54 | self.assertFalse(browser.is_url("summarize this page")) |
| 55 | self.assertFalse(browser.is_url("what are the main points?")) |
| 56 | self.assertFalse(browser.is_url("hello world")) |
| 57 | self.assertFalse(browser.is_url("123")) |
| 58 | |
| 59 | |
| 60 | class TestColorMapping(unittest.TestCase): |
| 61 | """Test color mapping functionality""" |
| 62 | |
| 63 | def setUp(self): |
| 64 | """Set up test fixtures""" |
| 65 | self.mock_stdscr = Mock() |
| 66 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 67 | |
| 68 | def test_color_map_exists(self): |
| 69 | """Test that color map is properly initialized""" |
| 70 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 71 | browser = Browser(self.mock_stdscr) |
| 72 | |
| 73 | # Check that basic colors are mapped |
| 74 | self.assertIn('red', browser.color_map) |
| 75 | self.assertIn('green', browser.color_map) |
| 76 | self.assertIn('blue', browser.color_map) |
| 77 | self.assertIn('yellow', browser.color_map) |
| 78 | self.assertIn('cyan', browser.color_map) |
| 79 | self.assertIn('magenta', browser.color_map) |
| 80 | self.assertIn('white', browser.color_map) |
| 81 | |
| 82 | def test_extended_colors(self): |
| 83 | """Test that extended colors are mapped to basic colors""" |
| 84 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 85 | browser = Browser(self.mock_stdscr) |
| 86 | |
| 87 | # Check extended color mappings |
| 88 | self.assertIn('orange', browser.color_map) |
| 89 | self.assertIn('purple', browser.color_map) |
| 90 | self.assertIn('gray', browser.color_map) |
| 91 | self.assertIn('grey', browser.color_map) |
| 92 | |
| 93 | |
| 94 | class TestAIIntegration(unittest.TestCase): |
| 95 | """Test AI integration functionality""" |
| 96 | |
| 97 | def setUp(self): |
| 98 | """Set up test fixtures""" |
| 99 | self.mock_stdscr = Mock() |
| 100 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 101 | |
| 102 | def test_ai_disabled_without_key(self): |
| 103 | """Test that AI is disabled when no API key is set""" |
| 104 | with patch.dict(os.environ, {}, clear=True): |
| 105 | if 'OPENAI_API_KEY' in os.environ: |
| 106 | del os.environ['OPENAI_API_KEY'] |
| 107 | browser = Browser(self.mock_stdscr) |
| 108 | self.assertFalse(browser.ai_enabled) |
| 109 | self.assertIsNone(browser.client) |
| 110 | |
| 111 | def test_ai_enabled_with_key(self): |
| 112 | """Test that AI is enabled when API key is set""" |
| 113 | with patch.dict(os.environ, {'OPENAI_API_KEY': 'test-key'}): |
| 114 | browser = Browser(self.mock_stdscr) |
| 115 | self.assertTrue(browser.ai_enabled) |
| 116 | self.assertIsNotNone(browser.client) |
| 117 | |
| 118 | |
| 119 | class TestBrowserInitialization(unittest.TestCase): |
| 120 | """Test browser initialization""" |
| 121 | |
| 122 | def test_browser_initializes(self): |
| 123 | """Test that browser initializes with default values""" |
| 124 | mock_stdscr = Mock() |
| 125 | mock_stdscr.getmaxyx.return_value = (24, 80) |
| 126 | |
| 127 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 128 | browser = Browser(mock_stdscr) |
| 129 | |
| 130 | # Check initial state |
| 131 | self.assertEqual(browser.current_url, "") |
| 132 | self.assertEqual(browser.page_content, []) |
| 133 | self.assertEqual(browser.page_text, "") |
| 134 | self.assertEqual(browser.scroll_offset, 0) |
| 135 | self.assertTrue(browser.running) |
| 136 | self.assertEqual(browser.forms, []) |
| 137 | self.assertEqual(browser.links, []) |
| 138 | |
| 139 | |
| 140 | class TestFormHandling(unittest.TestCase): |
| 141 | """Test form handling functionality""" |
| 142 | |
| 143 | def setUp(self): |
| 144 | """Set up test fixtures""" |
| 145 | self.mock_stdscr = Mock() |
| 146 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 147 | |
| 148 | def test_no_forms_initially(self): |
| 149 | """Test that browser starts with no forms""" |
| 150 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 151 | browser = Browser(self.mock_stdscr) |
| 152 | self.assertEqual(browser.forms, []) |
| 153 | |
| 154 | |
| 155 | class TestLinkHandling(unittest.TestCase): |
| 156 | """Test link handling functionality""" |
| 157 | |
| 158 | def setUp(self): |
| 159 | """Set up test fixtures""" |
| 160 | self.mock_stdscr = Mock() |
| 161 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 162 | |
| 163 | def test_no_links_initially(self): |
| 164 | """Test that browser starts with no links""" |
| 165 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 166 | browser = Browser(self.mock_stdscr) |
| 167 | self.assertEqual(browser.links, []) |
| 168 | |
| 169 | |
| 170 | class TestPageFetching(unittest.TestCase): |
| 171 | """Test page fetching functionality""" |
| 172 | |
| 173 | def setUp(self): |
| 174 | """Set up test fixtures""" |
| 175 | self.mock_stdscr = Mock() |
| 176 | self.mock_stdscr.getmaxyx.return_value = (24, 80) |
| 177 | |
| 178 | @patch('browser.requests.get') |
| 179 | def test_fetch_page_with_protocol(self, mock_get): |
| 180 | """Test fetching a page with https protocol""" |
| 181 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 182 | browser = Browser(self.mock_stdscr) |
| 183 | |
| 184 | # Mock response |
| 185 | mock_response = Mock() |
| 186 | mock_response.text = "<html><body><h1>Test Page</h1></body></html>" |
| 187 | mock_response.raise_for_status = Mock() |
| 188 | mock_get.return_value = mock_response |
| 189 | |
| 190 | # Fetch page |
| 191 | result = browser.fetch_page("https://example.com") |
| 192 | |
| 193 | # Check that request was made |
| 194 | mock_get.assert_called_once() |
| 195 | self.assertTrue(result) |
| 196 | |
| 197 | @patch('browser.requests.get') |
| 198 | def test_fetch_page_adds_protocol(self, mock_get): |
| 199 | """Test that https:// is added to URLs without protocol""" |
| 200 | with patch.dict(os.environ, {'OPENAI_API_KEY': ''}): |
| 201 | browser = Browser(self.mock_stdscr) |
| 202 | |
| 203 | # Mock response |
| 204 | mock_response = Mock() |
| 205 | mock_response.text = "<html><body><h1>Test Page</h1></body></html>" |
| 206 | mock_response.raise_for_status = Mock() |
| 207 | mock_get.return_value = mock_response |
| 208 | |
| 209 | # Fetch page without protocol |
| 210 | browser.fetch_page("example.com") |
| 211 | |
| 212 | # Check that https:// was added |
| 213 | call_args = mock_get.call_args |
| 214 | self.assertTrue(call_args[0][0].startswith('https://')) |
| 215 | |
| 216 | |
| 217 | if __name__ == '__main__': |
| 218 | unittest.main() |