aider/tests/scrape/test_scrape.py

66 lines
2.1 KiB
Python

import unittest
from unittest.mock import MagicMock
import re
from aider.commands import Commands
from aider.io import InputOutput
from aider.scrape import Scraper
class TestScrape(unittest.TestCase):
def setUp(self):
self.io = InputOutput(yes=True)
self.commands = Commands(self.io, None)
def test_cmd_web_imports_playwright(self):
# Run the cmd_web command
result = self.commands.cmd_web("https://example.com")
# Assert that the result contains some content
self.assertIsNotNone(result)
self.assertNotEqual(result, "")
# Try to import playwright
try:
import playwright # noqa: F401
playwright_imported = True
except ImportError:
playwright_imported = False
# Assert that playwright was successfully imported
self.assertTrue(
playwright_imported, "Playwright should be importable after running cmd_web"
)
def test_scrape_actual_url_with_playwright(self):
# Create a Scraper instance with a mock print_error function
mock_print_error = MagicMock()
scraper = Scraper(print_error=mock_print_error, playwright_available=True)
# Scrape a real URL
result = scraper.scrape("https://example.com")
# Assert that the result contains expected content
self.assertIsNotNone(result)
self.assertIn("Example Domain", result)
# Assert that print_error was never called
mock_print_error.assert_not_called()
def test_scraper_print_error_not_called(self):
# Create a Scraper instance with a mock print_error function
mock_print_error = MagicMock()
scraper = Scraper(print_error=mock_print_error)
# Test various methods of the Scraper class
scraper.scrape_with_httpx("https://example.com")
scraper.try_pandoc()
scraper.html_to_markdown("<html><body><h1>Test</h1></body></html>")
# Assert that print_error was never called
mock_print_error.assert_not_called()
if __name__ == "__main__":
unittest.main()