import base64 import unittest from core.trae_bot import TraeBot class DummyPage: def __init__(self): self.url = "https://example.com/x" def content(self): return "hi" def screenshot(self, full_page=True, type="png"): return b"\x89PNG\r\n\x1a\nfake" class DummyPageNoScreenshot(DummyPage): def screenshot(self, full_page=True, type="png"): raise RuntimeError("no screenshot") class TestDebugCapture(unittest.TestCase): def test_capture_sets_fields(self): bot = TraeBot("sid") try: raise ValueError("boom") except Exception as e: bot._capture_debug(DummyPage(), e) self.assertIn("ValueError", bot.last_error) self.assertIsInstance(bot.last_traceback, str) self.assertEqual(bot.last_url, "https://example.com/x") self.assertIn("hi", bot.last_html) png = base64.b64decode(bot.last_screenshot_png_b64.encode("ascii")) self.assertTrue(png.startswith(b"\x89PNG")) def test_capture_survives_screenshot_failure(self): bot = TraeBot("sid") try: raise RuntimeError("boom") except Exception as e: bot._capture_debug(DummyPageNoScreenshot(), e) self.assertIn("RuntimeError", bot.last_error) self.assertIsNone(bot.last_screenshot_png_b64)