#!/usr/bin/env python3
"""
Simple test to verify logpoint functionality
"""
import asyncio
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from src.chrome_devtools_mcp import (
launch_chrome, navigate_to, search_in_scripts, set_breakpoint,
execute_javascript, get_console_logs, close_chrome
)
async def main():
"""Simple logpoint test"""
print("=" * 60)
print("Simple Logpoint Test - AI Workflow")
print("=" * 60)
try:
# Launch Chrome
print("\n1. Launching Chrome...")
result = await launch_chrome(port=9222)
if not result['success']:
print(f" Failed: {result['error']}")
return
print(" ✅ Chrome launched")
# Create simple test page
print("\n2. Creating test page...")
test_page = """
<!DOCTYPE html>
<html>
<body>
<h1>Simple Logpoint Test</h1>
<button id="btn1" onclick="doTask('Task 1')">Task 1</button>
<button id="btn2" onclick="doTask('Task 2')">Task 2</button>
<div id="output"></div>
<script>
function doTask(taskName) {
console.log('Starting task:', taskName);
// Simulate some processing
const result = processTask(taskName);
// Update UI
document.getElementById('output').textContent = 'Completed: ' + result;
return result;
}
function processTask(task) {
console.log('Processing:', task);
// Simulate work
const processed = task.toUpperCase() + ' - DONE';
console.log('Result:', processed);
return processed;
}
</script>
</body>
</html>
"""
await navigate_to(f"data:text/html,{test_page}")
await asyncio.sleep(1)
print(" ✅ Page loaded")
# AI Workflow
print("\n3. AI searching for functions to debug...")
# Search for the main function
search_result = await search_in_scripts('doTask', 'function')
if search_result['success'] and search_result['data']['matches']:
print(f" ✅ Found doTask at line {search_result['data']['matches'][0]['lineNumber']}")
# Search for helper function
search_result2 = await search_in_scripts('processTask', 'function')
if search_result2['success'] and search_result2['data']['matches']:
print(f" ✅ Found processTask at line {search_result2['data']['matches'][0]['lineNumber']}")
# Set logpoints
print("\n4. Setting logpoints (non-blocking)...")
# Method 1: Function logpoint
bp1 = await set_breakpoint('function', 'doTask', {
'logMessage': 'doTask called',
'pause': False
})
print(f" {'✅' if bp1['success'] else '❌'} Logpoint on doTask")
# Method 2: Enhanced logging via JS injection
await execute_javascript("""
// Wrap processTask to add detailed logging
const originalProcessTask = processTask;
processTask = function(task) {
console.log('🔍 LOGPOINT [processTask]: Input =', task);
const startTime = performance.now();
const result = originalProcessTask.apply(this, arguments);
const duration = performance.now() - startTime;
console.log('🔍 LOGPOINT [processTask]: Output =', result);
console.log('🔍 LOGPOINT [processTask]: Duration =', duration.toFixed(2), 'ms');
return result;
};
console.log('✅ Logpoints installed');
""")
print(" ✅ Enhanced logging installed")
# Test the functions
print("\n5. Testing with logpoints active...")
print(" - Clicking Task 1 button")
await execute_javascript("document.getElementById('btn1').click()")
await asyncio.sleep(0.5)
print(" - Clicking Task 2 button")
await execute_javascript("document.getElementById('btn2').click()")
await asyncio.sleep(0.5)
# Also call directly
print(" - Direct function call")
await execute_javascript("doTask('Direct Call')")
await asyncio.sleep(0.5)
# Get logs
print("\n6. Analyzing captured logs...")
logs = await get_console_logs()
print("\n Execution trace:")
print(" " + "-"*40)
log_count = 0
for log in logs['data']['logs'][-30:]:
if log['text']:
if '🔍 LOGPOINT' in log['text']:
print(f" 📍 {log['text']}")
log_count += 1
elif 'Starting task' in log['text'] or 'Processing' in log['text'] or 'Result:' in log['text']:
print(f" 📝 {log['text']}")
elif '✅' in log['text']:
print(f" ✅ {log['text']}")
print("\n7. Summary:")
print(f" ✅ Captured {log_count} logpoint entries")
print(" ✅ Non-blocking execution verified")
print(" ✅ Detailed execution data collected")
print("\n" + "="*60)
print("✅ AI successfully used tools to set logpoints!")
print("="*60)
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
finally:
await asyncio.sleep(1)
await close_chrome()
if __name__ == "__main__":
asyncio.run(main())