diff --git a/test/hardware/proxy_e2e/README.md b/test/hardware/proxy_e2e/README.md index 8790897..1f26ba0 100644 --- a/test/hardware/proxy_e2e/README.md +++ b/test/hardware/proxy_e2e/README.md @@ -126,11 +126,19 @@ npx tsx run-e2e.ts COM3 # With custom timeout (default 120 seconds): npx tsx run-e2e.ts COM3 180000 # 3 minutes + +# With pre-configured device address (if serial output is missed): +npx tsx run-e2e.ts COM3 --address 0xABCD1234... + +# All options: +npx tsx run-e2e.ts COM15 --timeout 180000 --address 0x123... ``` +**Note:** The runner automatically resets the ESP32 when it opens the serial port (DTR/RTS pulse). This ensures it captures all serial output from boot. If the device address is missed, use `--address` flag or set `TELEMETRY_API_KEY` for automatic fallback. + ## Test Protocol -The test firmware follows this sequence: +The test firmware and runner follow this sequence: ``` 1. E2E:START @@ -145,10 +153,16 @@ The test firmware follows this sequence: 10. E2E:KEEPALIVE count=1 11. E2E:KEEPALIVE count=2 12. E2E:KEEPALIVE count=3 -13. E2E:API_CALL name="..." count=1 (if triggered) -14. E2E:DONE PASS time_ms=45000 +13. E2E:WAITING_FOR_API + → Runner calls: GET https://scriptproxy.percolate.one/api//e2eTest + → Proxy forwards opcode 0x04 to device + → Device receives API call and responds with opcode 0x08 +14. E2E:API_CALL name="e2eTest" count=1 +15. E2E:DONE PASS time_ms=45000 ``` +**Note:** The test runner actively triggers an API call when it sees `WAITING_FOR_API`. The device will **fail** if no API call is received within 60 seconds, as this tests the complete end-to-end flow through the public proxy. + ## Serial Line Format All test lines start with `E2E:` prefix and use key=value pairs: @@ -171,7 +185,9 @@ E2E:TEST=STATUS key1=value1 key2="quoted value" | `E2E:LOGIN=OK opcode=0x09` | TCP bridge authenticated | | `E2E:DIAG_SENT opcode=0x0A` | Diagnostics sent | | `E2E:KEEPALIVE count=N` | Keep-alive message N | +| `E2E:WAITING_FOR_API` | Waiting for API call from runner | | `E2E:API_CALL name="..." count=N` | API call N handled | +| `E2E:API_CALLS_RECEIVED=N` | Total API calls received | | `E2E:DONE PASS time_ms=T` | Test passed in T ms | | `E2E:DONE FAIL` | Test failed | | `E2E:FAIL reason="..."` | Failure reason | diff --git a/test/hardware/proxy_e2e/git_version.py b/test/hardware/proxy_e2e/git_version.py index 7cd8896..e420685 100644 --- a/test/hardware/proxy_e2e/git_version.py +++ b/test/hardware/proxy_e2e/git_version.py @@ -2,29 +2,72 @@ import subprocess import os -try: - # Get short git SHA from the Web3E repo root - repo_root = os.path.join(env['PROJECT_DIR'], '../../..') - git_sha = subprocess.check_output( - ['git', 'rev-parse', '--short', 'HEAD'], - cwd=repo_root - ).decode('ascii').strip() - - fw_version = 'e2e-{}'.format(git_sha) - - # Remove any existing FW_VERSION definitions - env['CPPDEFINES'] = [ - define for define in env['CPPDEFINES'] - if not (isinstance(define, tuple) and define[0] == 'FW_VERSION') - and not (isinstance(define, str) and define.startswith('FW_VERSION=')) - ] - - # Add our FW_VERSION - env.Append(CPPDEFINES=[('FW_VERSION', '\\"{}\\"'.format(fw_version))]) - print('Building with firmware version: {}'.format(fw_version)) - -except Exception as e: - print('Warning: Could not get git SHA: {}'.format(e)) - # Fallback to a default - env.Append(CPPDEFINES=[('FW_VERSION', '\\"e2e-unknown\\"')]) - print('Using fallback firmware version: e2e-unknown') +def get_git_version(): + """Get e2e-[-dirty] version string, or 'e2e-unknown' if git unavailable.""" + try: + # Get absolute path to Web3E repo root (3 levels up from this script) + # SCons exec context: use env['PROJECT_DIR'] if available + try: + project_dir = env['PROJECT_DIR'] + except: + # Fallback: assume this script is in test/hardware/proxy_e2e/ + project_dir = os.path.dirname(os.path.abspath(__file__)) if '__file__' in globals() else os.getcwd() + + repo_root = os.path.abspath(os.path.join(project_dir, '..', '..', '..')) + + # Check if git is available and this is a git repo + try: + subprocess.check_output( + ['git', 'rev-parse', '--git-dir'], + cwd=repo_root, + stderr=subprocess.STDOUT + ) + except (subprocess.CalledProcessError, OSError): + return 'e2e-unknown' + + # Get short git SHA + git_sha = subprocess.check_output( + ['git', 'rev-parse', '--short', 'HEAD'], + cwd=repo_root, + stderr=subprocess.STDOUT + ).decode('ascii').strip() + + # Check if working tree is dirty (ignore untracked files to avoid build artifacts) + try: + # Use git diff to check tracked files only + subprocess.check_output( + ['git', 'diff', '--quiet', 'HEAD'], + cwd=repo_root, + stderr=subprocess.STDOUT + ) + is_dirty = False + except subprocess.CalledProcessError: + # Exit code 1 means there are differences + is_dirty = True + except: + is_dirty = False + + # Build version string (max 32 ASCII chars) + fw_version = 'e2e-{}{}'.format(git_sha, '-dirty' if is_dirty else '') + return fw_version[:32] # Ensure <=32 chars + + except Exception as e: + print('Warning: Could not get git version: {}'.format(e)) + return 'e2e-unknown' + +fw_version = get_git_version() + +# Initialize CPPDEFINES if not present +if 'CPPDEFINES' not in env: + env['CPPDEFINES'] = [] + +# Remove any existing FW_VERSION definitions +env['CPPDEFINES'] = [ + define for define in env.get('CPPDEFINES', []) + if not (isinstance(define, tuple) and define[0] == 'FW_VERSION') + and not (isinstance(define, str) and define.startswith('FW_VERSION=')) +] + +# Add our FW_VERSION +env.Append(CPPDEFINES=[('FW_VERSION', '\\"{}\\"'.format(fw_version))]) +print('Building with firmware version: {}'.format(fw_version)) diff --git a/test/hardware/proxy_e2e/run-e2e.ts b/test/hardware/proxy_e2e/run-e2e.ts index 4500f40..253eb80 100644 --- a/test/hardware/proxy_e2e/run-e2e.ts +++ b/test/hardware/proxy_e2e/run-e2e.ts @@ -49,6 +49,7 @@ class E2ETestRunner { private serialPath: string, private proxyBaseUrl: string, private telemetryApiKey: string | undefined, + private deviceAddress: string | undefined, private timeoutMs: number = 120000 // 2 minutes default ) { this.result = { @@ -61,11 +62,25 @@ class E2ETestRunner { telemetryVerified: false, telemetryHasDiagnostics: false }; + + // If device address was provided, set it (case-insensitive normalization) + if (deviceAddress) { + this.result.deviceAddress = this.normalizeAddress(deviceAddress); + } + } + + private normalizeAddress(addr: string): string { + // Normalize to lowercase with 0x prefix + const clean = addr.trim().toLowerCase(); + return clean.startsWith('0x') ? clean : '0x' + clean; } async run(): Promise { console.log(`Starting E2E test on ${this.serialPath}`); console.log(`Proxy: ${this.proxyBaseUrl}`); + if (this.result.deviceAddress) { + console.log(`Device address: ${this.result.deviceAddress} (pre-configured)`); + } console.log(`Timeout: ${this.timeoutMs}ms\n`); return new Promise((resolve, reject) => { @@ -84,20 +99,41 @@ class E2ETestRunner { baudRate: 115200 }); - this.parser = this.port.pipe(new ReadlineParser({ delimiter: '\n' })); - this.port.on('error', (err) => { this.result.error = `Serial port error: ${err.message}`; this.cleanup(); resolve(this.result); }); - this.parser.on('data', (line: string) => { - this.handleSerialLine(line.trim()); + this.port.on('open', async () => { + console.log('✓ Serial port opened'); + + // Reset ESP32 by pulsing EN pin (DTR=false, RTS=true for 200ms, then RTS=false) + // This is the standard esptool reset sequence for ESP32 DevKit boards + try { + console.log('⏳ Resetting ESP32...'); + await this.port!.set({ dtr: false, rts: true }); + await this.sleep(200); + await this.port!.set({ rts: false }); + await this.sleep(100); // Brief stabilization delay + console.log('✓ ESP32 reset complete, monitoring serial output...\n'); + } catch (err) { + console.log(`⚠ Reset failed (${err}), continuing anyway...`); + } + + // Now start parsing serial data + this.parser = this.port!.pipe(new ReadlineParser({ delimiter: '\n' })); + this.parser.on('data', (line: string) => { + this.handleSerialLine(line.trim()); + }); }); }); } + private sleep(ms: number): Promise { + return new Promise(resolve => setTimeout(resolve, ms)); + } + private handleSerialLine(line: string) { // Echo all lines for debugging console.log(`[DEVICE] ${line}`); @@ -140,8 +176,13 @@ class E2ETestRunner { console.log(`✓ WiFi connected: ${kv.ip}`); } else if (message.startsWith('DEVICE_ADDRESS=')) { const kv = parseKV(message); - this.result.deviceAddress = kv.DEVICE_ADDRESS; - console.log(`✓ Device address: ${this.result.deviceAddress}`); + // Only set if not already configured via --address flag + if (!this.result.deviceAddress) { + this.result.deviceAddress = this.normalizeAddress(kv.DEVICE_ADDRESS); + console.log(`✓ Device address: ${this.result.deviceAddress}`); + } else { + console.log(`✓ Device address from serial: ${kv.DEVICE_ADDRESS} (using pre-configured: ${this.result.deviceAddress})`); + } } else if (message.startsWith('LOGIN=OK')) { const kv = parseKV(message); this.result.loginSuccess = true; @@ -154,6 +195,9 @@ class E2ETestRunner { const kv = parseKV(message); this.result.keepaliveCount = parseInt(kv.count || '0', 10); console.log(`✓ Keep-alive ${this.result.keepaliveCount}`); + } else if (message.startsWith('WAITING_FOR_API')) { + console.log('⏳ Device waiting for API call - triggering test API call...'); + this.triggerApiCall(); } else if (message.startsWith('API_CALL')) { const kv = parseKV(message); this.result.apiCallCount = parseInt(kv.count || '0', 10); @@ -190,6 +234,84 @@ class E2ETestRunner { } } + private async triggerApiCall() { + // Try to get device address from telemetry if we don't have it yet + if (!this.result.deviceAddress && this.telemetryApiKey) { + console.log(' ⏳ Device address not captured from serial, fetching from telemetry...'); + await this.fetchDeviceAddressFromTelemetry(); + } + + if (!this.result.deviceAddress) { + console.log(' ✗ Cannot trigger API call - device address not available'); + console.log(' Hint: Use --address flag or ensure TELEMETRY_API_KEY is set'); + return; + } + + // Call the proxy API to forward a test method to the device + // Format: GET /api/
/?param1=value1¶m2=value2 + const method = 'e2eTest'; + const apiUrl = `${this.proxyBaseUrl}/api/${this.result.deviceAddress}/${method}`; + + console.log(` → Calling ${apiUrl}`); + + try { + const response = await axios.get(apiUrl, { + params: { + testParam: 'e2e-runner', + timestamp: Date.now() + }, + timeout: 30000 // 30 second timeout + }); + + if (response.status === 201) { + console.log(` ✓ API call succeeded: ${response.data}`); + } else { + console.log(` ⚠ API call returned status ${response.status}: ${response.data}`); + } + } catch (error: any) { + console.log(` ✗ API call failed: ${error.message}`); + console.log(' (Device may still handle it if proxy forwarded before timeout)'); + } + } + + private async fetchDeviceAddressFromTelemetry() { + if (!this.telemetryApiKey) { + return; + } + + try { + // Get recent connection events from telemetry + const response = await axios.get( + `${this.proxyBaseUrl}/api/telemetry/feed`, + { + headers: { + 'Authorization': `Bearer ${this.telemetryApiKey}` + }, + params: { + limit: 50 + }, + timeout: 5000 + } + ); + + if (response.status === 200 && response.data && response.data.rows) { + // Find most recent connect event + const recentConnect = response.data.rows + .filter((e: any) => e.eventType === 'connect') + .sort((a: any, b: any) => b.timestamp - a.timestamp)[0]; + + if (recentConnect && recentConnect.device) { + this.result.deviceAddress = this.normalizeAddress(recentConnect.device); + console.log(` ✓ Found device address in telemetry: ${this.result.deviceAddress}`); + } else { + console.log(' ⚠ No recent device connections in telemetry'); + } + } + } catch (error: any) { + console.log(` ⚠ Could not fetch from telemetry: ${error.message}`); + } + } + private async verifyTelemetry() { if (!this.telemetryApiKey || !this.result.deviceAddress) { console.log('\n⚠ Skipping telemetry verification (no API key or device address)'); @@ -272,25 +394,54 @@ class E2ETestRunner { async function main() { const args = process.argv.slice(2); - if (args.length < 1) { - console.error('Usage: tsx run-e2e.ts [timeout-ms]'); + // Parse arguments + let serialPath: string | undefined; + let timeoutMs = 120000; + let deviceAddress: string | undefined; + + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === '--address' && i + 1 < args.length) { + deviceAddress = args[++i]; + } else if (arg === '--timeout' && i + 1 < args.length) { + timeoutMs = parseInt(args[++i], 10); + } else if (!serialPath && !arg.startsWith('--')) { + serialPath = arg; + } + } + + // Support legacy positional args: [timeout] + if (!serialPath && args.length >= 1 && !args[0].startsWith('--')) { + serialPath = args[0]; + if (args.length >= 2 && !args[1].startsWith('--')) { + timeoutMs = parseInt(args[1], 10); + } + } + + if (!serialPath) { + console.error('Usage: tsx run-e2e.ts [timeout-ms] [--address ]'); + console.error(' tsx run-e2e.ts --address --timeout '); + console.error(''); + console.error('Examples:'); + console.error(' tsx run-e2e.ts COM3 120000'); + console.error(' tsx run-e2e.ts /dev/ttyUSB0 --address 0xABCD1234...'); + console.error(' tsx run-e2e.ts COM15 --timeout 180000'); console.error(''); - console.error('Example: tsx run-e2e.ts COM3 120000'); - console.error(' tsx run-e2e.ts /dev/ttyUSB0'); + console.error('Options:'); + console.error(' --address Pre-configure device address (if serial misses it)'); + console.error(' --timeout Test timeout in milliseconds (default: 120000)'); console.error(''); console.error('Environment variables:'); - console.error(' PROXY_BASE_URL - SmartLayerProxy URL (default: https://scriptproxy.percolate.one)'); - console.error(' TELEMETRY_API_KEY - API key for telemetry verification (optional)'); + console.error(' PROXY_BASE_URL SmartLayerProxy URL (default: https://scriptproxy.percolate.one)'); + console.error(' TELEMETRY_API_KEY API key for telemetry fallback & verification (optional)'); console.error(''); process.exit(1); } - const serialPath = args[0]; - const timeoutMs = args[1] ? parseInt(args[1], 10) : 120000; const proxyBaseUrl = process.env.PROXY_BASE_URL || 'https://scriptproxy.percolate.one'; const telemetryApiKey = process.env.TELEMETRY_API_KEY; - const runner = new E2ETestRunner(serialPath, proxyBaseUrl, telemetryApiKey, timeoutMs); + const runner = new E2ETestRunner(serialPath, proxyBaseUrl, telemetryApiKey, deviceAddress, timeoutMs); const result = await runner.run(); // Print summary diff --git a/test/hardware/proxy_e2e/src/main.cpp b/test/hardware/proxy_e2e/src/main.cpp index 7351db6..1e629b6 100644 --- a/test/hardware/proxy_e2e/src/main.cpp +++ b/test/hardware/proxy_e2e/src/main.cpp @@ -276,13 +276,13 @@ void loop() { break; case E2E_API_WAIT: - // Wait for at least one API call, or timeout + // Wait for at least one API call from the proxy if (apiCallCount > 0) { + e2ePrintResult("API_CALLS_RECEIVED", String(apiCallCount).c_str()); e2ePass(); } else if (now - stateStartTime > API_WAIT_TIMEOUT_MS) { - // No API call received, but that's OK for basic test - Serial.println("E2E:API_CALL count=0 (none received, OK for basic test)"); - e2ePass(); + // No API call received - FAIL (runner should trigger one) + e2eFail("No API call received from proxy"); } break;