# SmartPi Test Suite This directory contains automated test scripts to verify all components of the SmartPi Assistant system. ## Quick Start: Run All Tests **super_test.sh** - Comprehensive end-to-end test suite that runs all tests automatically: ```bash # Navigate to tests directory cd tests # Run all tests ./super_test.sh # Or with ngrok URL specified NGROK_URL=https://your-id.ngrok-free.app ./super_test.sh ``` This master test script will: - ✅ Check all prerequisites (bridge, device, ngrok) - ✅ Activate virtual environment automatically - ✅ Run all 4 test suites in order - ✅ Provide color-coded output and final summary - ✅ Save results to timestamped log file **Duration**: ~2-3 minutes for complete test suite --- ## Individual Test Scripts ### test_bridge.py Tests the MCP Bridge Server functionality. **What it tests**: - Bridge server health and availability - MCP manifest endpoint - MCP tool calls (push_messages, get_status, clear_queue) - Direct push endpoint - Error handling **Requirements**: - Bridge server must be running - Python 3.8+ with `requests` library **Usage**: ```bash # Install requirements pip install requests # Run tests (default: http://localhost:5055) python test_bridge.py # Test bridge at different URL (if needed) python test_bridge.py http://localhost:5055 ``` **Expected output**: ``` Testing: Bridge Health Check ✓ Bridge is healthy Bridge: online Device: ... Testing: MCP Tool: push_messages ✓ Messages pushed successfully Message count: 2 ... Total: 9/9 tests passed All tests passed! ✓ ``` --- ### test_pico_endpoint.py Tests the Raspberry Pi Pico W device endpoints directly. **What it tests**: - Device network reachability - Status endpoint - Message posting (single and batch) - Theme variations - Priority sorting - Message wrapping - Queue clearing - Error handling **Requirements**: - SmartPi device must be powered on and connected to WiFi - Python 3.8+ with `requests` library - Update `PICO_IP` in script or pass as argument **Usage**: ```bash # Update IP in script first, or pass as argument python test_pico_endpoint.py # Test specific device (if IP differs) python test_pico_endpoint.py 10.0.0.230 ``` **Expected output**: ``` SmartPi Pico W Endpoint Test Suite Device URL: http://10.0.0.230:5000 Note: Watch the SmartPi display during tests Testing: Device Reachability ✓ Device is reachable at http://10.0.0.230:5000 Testing: Single Message ✓ Single message posted successfully ⚠ Check SmartPi display to verify message appears ... Total: 9/9 tests passed All tests passed! ✓ ``` --- ### super_test.sh - Master Test Suite Runs all test scripts automatically with comprehensive reporting. **What it tests**: - All bridge server functionality - All Pico device endpoints - Complete message flow - ngrok tunnel (if available) - Prerequisites validation - System health **Features**: - Auto-activates virtual environment - Color-coded output - Progress tracking - Final summary report - Automatic log file creation - Graceful error handling - Component health checks **Usage**: ```bash # Run all tests ./super_test.sh # Specify ngrok URL NGROK_URL=https://your-id.ngrok-free.app ./super_test.sh ``` **Expected output**: ``` ╔════════════════════════════════════════════════════════════════╗ ║ SmartPi Assistant - Super Test Suite ║ ╚════════════════════════════════════════════════════════════════╝ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Checking Prerequisites ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ▶ Checking virtual environment... ✓ Virtual environment found ▶ Checking Python... ✓ Python found: Python 3.11.5 ▶ Checking required tools... ✓ curl found ✓ jq found ▶ Checking bridge server... ✓ Bridge server is running at http://localhost:5055 ▶ Checking SmartPi device... ✓ SmartPi device is online at http://10.0.0.230:5000 ▶ Checking ngrok tunnel... ✓ ngrok tunnel is active: https://abc123.ngrok-free.app ✓ All prerequisites met! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Test Suite 1: Bridge Server (MCP) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ [Bridge tests run here...] ✓ Bridge server tests PASSED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Test Suite 2: SmartPi Device (Pico W) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ [Pico tests run here...] ✓ Pico device tests PASSED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Test Suite 3: End-to-End Message Flow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ [E2E tests run here...] ✓ End-to-end message flow tests PASSED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Test Suite 4: Cloud-to-Device (ngrok Tunnel) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ [ngrok tests run here...] ✓ ngrok tunnel tests PASSED ╔════════════════════════════════════════════════════════════════╗ ║ TEST SUMMARY ║ ╚════════════════════════════════════════════════════════════════╝ Test Suites Run: 4 Passed: 4 Failed: 0 ╔════════════════════════════════════════════════════════════════╗ ║ 🎉 ALL TESTS PASSED! 🎉 ║ ╚════════════════════════════════════════════════════════════════╝ SmartPi Assistant is fully functional! Tested components: ✓ MCP Bridge Server (localhost:5055) ✓ SmartPi Pico W Device (10.0.0.230:5000) ✓ LED Matrix Display (64x64 HUB75) ✓ Message Queue & Priority Handling ✓ Themed Message Rendering ✓ Cloud-to-Device Pipeline (ngrok) Total test duration: 142s Test report saved to: test_results_20251114_102345.log ``` **Exit codes**: - `0` - All tests passed - `1` - One or more tests failed **Log files**: Results are automatically saved to `test_results_YYYYMMDD_HHMMSS.log` --- ## End-to-End Testing To test the complete system flow: ### 1. Manual End-to-End Test **Prerequisites**: - SmartPi device powered on and connected - Bridge server running - n8n workflow configured **Steps**: 1. Execute n8n workflow manually 2. Watch for execution success 3. Verify HTTP Request node returns 200 OK 4. Check SmartPi display shows messages 5. Verify messages rotate every 5 seconds 6. Confirm themes display correct colors ### 2. Component Testing Order Test in this order to isolate issues: ``` 1. SmartPi Device → python test_pico_endpoint.py 10.0.0.230:5000 2. Bridge Server → python test_bridge.py 3. n8n Integration → Execute workflow in n8n UI → Check execution logs 4. Full System → Schedule workflow → Wait for automated execution → Verify display updates ``` --- ## Test Configuration ### Update Device IP Both test scripts need the correct IP address: **test_pico_endpoint.py**: ```python PICO_IP = "10.0.0.230:5000" # Update this ``` **test_bridge.py**: ```python BRIDGE_URL = "http://localhost:5055" # Update if needed ``` Or pass as command-line argument: ```bash python test_pico_endpoint.py 10.0.0.230 python test_bridge.py http://localhost:5055 ``` ### Adjust Timeouts If your network is slow or device is laggy: ```python TEST_TIMEOUT = 10 # Increase if needed ``` --- ## Continuous Testing ### Automated Testing with cron Create a monitoring script that runs tests periodically: ```bash #!/bin/bash # test_monitor.sh # Run tests every hour and log results while true; do echo "=== Test run at $(date) ===" >> test_results.log python test_pico_endpoint.py 10.0.0.230:5000 >> test_results.log 2>&1 python test_bridge.py >> test_results.log 2>&1 sleep 3600 # 1 hour done ``` ### Integration with CI/CD If using version control: ```yaml # .github/workflows/test.yml name: SmartPi Tests on: push: branches: [ main ] schedule: - cron: '0 */6 * * *' # Every 6 hours jobs: test-bridge: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.9' - name: Install dependencies run: pip install requests - name: Test Bridge run: python tests/test_bridge.py ${{ secrets.BRIDGE_URL }} ``` --- ## Troubleshooting Tests ### test_pico_endpoint.py Failures **"Cannot connect to device"**: - Verify SmartPi is powered on - Check WiFi connection (serial console) - Verify IP address is correct - Ping device: `ping 10.0.0.230:5000` **"Connection timed out"**: - Increase `TEST_TIMEOUT` value - Check WiFi signal strength - Verify HTTP server is running on Pico **Messages not displaying**: - Check serial console for errors - Verify CircuitPython is running - Check display connections ### test_bridge.py Failures **"Cannot connect to bridge server"**: - Verify bridge is running: `ps aux | grep smartpi` - Check port 5055 is open: `lsof -i :5055` - Try starting bridge: `python bridge/smartpi_mcp_server.py` **"Device unreachable" in health check**: - Bridge can't reach SmartPi - Verify SmartPi IP in bridge `.env` file - Test direct connection: `curl http://10.0.0.230:5000/status` **Tool calls fail**: - Check bridge logs for details - Verify JSON format - Test with direct push first --- ## Adding Custom Tests ### Template for New Tests ```python def test_new_feature() -> bool: """Test description""" print_test("New Feature Name") try: # Your test code here response = requests.get(f"{PICO_URL}/endpoint") if response.status_code == 200: print_success("Test passed") return True else: print_error("Test failed") return False except Exception as e: print_error(f"Error: {e}") return False ``` Add to `tests` list in `run_all_tests()`. --- ## Performance Testing ### Load Testing Test device under load: ```python #!/usr/bin/env python3 import requests import time import concurrent.futures PICO_URL = "http://10.0.0.230:5000" def send_message(i): payload = { "messages": [{"text": f"Load test {i}", "theme": "neutral"}] } try: response = requests.post(f"{PICO_URL}/messages", json=payload, timeout=5) return response.status_code == 200 except: return False # Send 100 requests concurrently with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: results = list(executor.map(send_message, range(100))) success_rate = sum(results) / len(results) * 100 print(f"Success rate: {success_rate:.1f}%") ``` --- ### test_ngrok_tunnel.sh Tests the complete end-to-end flow through ngrok tunnel from cloud to device. **What it tests**: - ngrok tunnel accessibility - MCP bridge server through ngrok - Device communication via ngrok - Message delivery from cloud to display - Round-trip latency measurement - Error handling through tunnel - All MCP tools (push_messages, get_status, clear_queue) **Requirements**: - ngrok running (`ngrok http 5055`) - Bridge server running - SmartPi device online - `curl` and `jq` installed **Usage**: ```bash # Auto-detect ngrok URL (requires jq) ./test_ngrok_tunnel.sh # Or specify ngrok URL manually NGROK_URL=https://your-id.ngrok-free.app ./test_ngrok_tunnel.sh ``` **Expected output**: ``` ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ SmartPi ngrok Tunnel Test Suite ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ✓ Using ngrok URL: https://abc123.ngrok-free.app Testing: ngrok tunnel accessibility ✓ ngrok tunnel is accessible Testing: Push messages through ngrok (n8n simulation) ✓ Messages pushed successfully through ngrok ℹ Check the SmartPi display - you should see the test messages! ... ✓ All tests passed! ngrok tunnel is working correctly. ``` **What makes this test unique**: - Simulates exactly what n8n workflow does from the cloud - Tests the complete cloud→ngrok→bridge→device→display pipeline - Includes the `ngrok-skip-browser-warning` header (required for ngrok free tier) - Measures end-to-end latency - Validates all message types (calendar, weather, email) --- ## Test Results Archive Keep test results for debugging: ```bash # Run tests and save results python test_pico_endpoint.py 2>&1 | tee test_results_$(date +%Y%m%d_%H%M%S).log ``` --- ## Support If tests fail consistently: 1. Check device serial console 2. Review bridge server logs 3. Verify network connectivity 4. Consult [SETUP.md](../docs/SETUP.md) 5. See [DEPLOYMENT.md](../docs/DEPLOYMENT.md) --- © 2025 SmartPi Assistant — MAS.863