#!/usr/bin/env python3 """Validation tests for XIC Panel UI integration with FedMart telemetry.""" import sys import json from pathlib import Path # Add parent directory to path for imports sys.path.insert(0, str(Path(__file__).parent.parent)) print("=" * 70) print("XIC Panel UI Integration Validation Suite") print("=" * 70) # Test 1: HTML template exists and is valid print("\n[TEST 1] HTML template exists and is valid") try: html_path = Path("fedmart_ui/modules/xic_panel/index.html") assert html_path.exists(), f"HTML file not found at {html_path}" with open(html_path, 'r') as f: html_content = f.read() # Check for critical elements assert "" in html_content assert '
' in html_content assert 'id="runStatus"' in html_content assert 'id="timelineContent"' in html_content assert 'id="heatmapCanvas"' in html_content assert 'id="glyphSelect"' in html_content assert 'id="guardrailList"' in html_content assert 'id="specStatus"' in html_content assert '' in html_content print(" ✅ PASS: HTML template valid with all required elements") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 2: CSS stylesheet exists and is valid print("\n[TEST 2] CSS stylesheet exists and has critical styles") try: css_path = Path("fedmart_ui/modules/xic_panel/xic_panel.css") assert css_path.exists(), f"CSS file not found at {css_path}" with open(css_path, 'r') as f: css_content = f.read() # Check for critical CSS classes assert ".xic-monitor-container" in css_content assert ".xic-header" in css_content assert ".timeline-step" in css_content assert ".legend-bar" in css_content assert ".glyph-item" in css_content assert ".guardrail-event" in css_content assert ".spec-entry" in css_content assert ".heatmap-legend" in css_content print(" ✅ PASS: CSS stylesheet valid with all required styles") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 3: JavaScript file exists and has required functions print("\n[TEST 3] JavaScript file exists and has required functions") try: js_path = Path("fedmart_ui/modules/xic_panel/xic_panel.js") assert js_path.exists(), f"JS file not found at {js_path}" with open(js_path, 'r') as f: js_content = f.read() # Check for critical functions and classes assert "class XICMonitor" in js_content assert "connectToFeed()" in js_content assert "processTelemetry(telemetry)" in js_content assert "renderTimeline(telemetry)" in js_content assert "renderHeatmap(topGlyphs, globalScore)" in js_content assert "showGlyphMetrics(glyphId)" in js_content assert "showGuardrailAlerts(guardrails)" in js_content assert "pauseRun()" in js_content assert "throttleRun()" in js_content assert "DOMContentLoaded" in js_content print(" ✅ PASS: JavaScript file valid with all required functions") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 4: Telemetry mock data matches expected schema print("\n[TEST 4] Mock telemetry data matches expected schema") try: mock_telemetry = { "event_type": "symbolic_pipeline_run", "timestamp": "2026-05-21T12:00:00Z", "run_id": "xic_test_12345", "program": "demo_symbolic.gx.json", "chain_label": "test_chain", "glyph_ids": ["glyph://a", "glyph://b", "glyph://c"], "glyph_count": 3, "global_resonance_score": 0.847, "steps_executed": 20, "guardrails_triggered": [], "resonance_map_summary": { "top_glyphs": [ {"glyph_id": "glyph://a", "weight": 0.95}, {"glyph_id": "glyph://b", "weight": 0.73}, {"glyph_id": "glyph://c", "weight": 0.81}, ], "average_resonance": 0.83, }, "raw_payload": { "output_text": "Sample output", "fused_symbol_summary": { "summary": "Fused symbolic result", "glyph_ids": ["glyph://a", "glyph://b", "glyph://c"] } } } # Verify required fields required_fields = { "event_type", "timestamp", "run_id", "glyph_count", "global_resonance_score", "steps_executed", "guardrails_triggered" } assert required_fields.issubset(mock_telemetry.keys()), "Missing required fields" # Verify types assert isinstance(mock_telemetry["glyph_count"], int) assert isinstance(mock_telemetry["global_resonance_score"], float) assert isinstance(mock_telemetry["guardrails_triggered"], list) assert isinstance(mock_telemetry["resonance_map_summary"], dict) print(" ✅ PASS: Mock telemetry data valid") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 5: UI elements are accessible and properly configured print("\n[TEST 5] UI elements properly configured in HTML") try: with open(html_path, 'r') as f: html_content = f.read() # Check button configurations assert 'id="connectBtn"' in html_content and 'class="btn-primary"' in html_content assert 'id="pauseBtn"' in html_content and 'class="btn-warning"' in html_content assert 'id="throttleBtn"' in html_content and 'class="btn-warning"' in html_content # Check canvas and interactive elements assert 'id="heatmapCanvas"' in html_content and 'width="600"' in html_content assert 'id="glyphSelect"' in html_content # Check panels panels = ["run-timeline", "resonance-heatmap", "glyph-inspector", "guardrail-control", "spec-coverage"] for panel in panels: assert panel in html_content, f"Panel {panel} not found" print(" ✅ PASS: All UI elements properly configured") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 6: Color gradient function exists in JavaScript print("\n[TEST 6] Heatmap color gradient function exists") try: with open(js_path, 'r') as f: js_content = f.read() # Check for color gradient logic assert "colorForWeight(normalized)" in js_content assert "0x00cc66" in js_content or "204" in js_content # Green color assert "0x0066cc" in js_content or "255" in js_content # Blue color assert "0xff9900" in js_content or "153" in js_content # Orange color print(" ✅ PASS: Color gradient function properly implemented") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 7: WebSocket connection handling print("\n[TEST 7] WebSocket connection logic exists") try: with open(js_path, 'r') as f: js_content = f.read() assert "new WebSocket" in js_content assert "ws.onmessage" in js_content assert "ws.onerror" in js_content assert "ws.onclose" in js_content assert "/ws/fedmart/xic" in js_content print(" ✅ PASS: WebSocket connection logic properly implemented") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 8: Guardrail control endpoints are called print("\n[TEST 8] Guardrail control endpoint calls exist") try: with open(js_path, 'r') as f: js_content = f.read() assert "/fedmart/control/pause" in js_content assert "/fedmart/control/throttle" in js_content assert "fetch(" in js_content assert "application/json" in js_content print(" ✅ PASS: Guardrail control endpoints properly called") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 9: Telemetry data binding to UI print("\n[TEST 9] Telemetry data binding to UI elements") try: with open(js_path, 'r') as f: js_content = f.read() # Check that telemetry fields are used to update UI assert "getElementById('stepCount')" in js_content or "stepCountEl" in js_content assert "getElementById('execTime')" in js_content or "execTimeEl" in js_content assert "getElementById('runStatus')" in js_content or "runStatusEl" in js_content assert "getElementById('heatmapCanvas')" in js_content or "heatmapCanvas" in js_content print(" ✅ PASS: Telemetry data properly bound to UI") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) # Test 10: Error handling and graceful degradation print("\n[TEST 10] Error handling in JavaScript") try: with open(js_path, 'r') as f: js_content = f.read() assert "try" in js_content and "catch" in js_content assert "console.error" in js_content assert "Exception" in js_content or "Error" in js_content print(" ✅ PASS: Error handling properly implemented") except Exception as e: print(f" ❌ FAIL: {e}") sys.exit(1) print("\n" + "=" * 70) print("All 10 UI Integration Tests PASSED ✅") print("=" * 70) print("\nSummary:") print(" ✅ HTML template valid and complete") print(" ✅ CSS stylesheet includes all required styles") print(" ✅ JavaScript module fully functional") print(" ✅ Telemetry schema matches UI expectations") print(" ✅ UI elements properly configured") print(" ✅ Heatmap visualization implemented") print(" ✅ WebSocket connection handling complete") print(" ✅ Guardrail control actions wired") print(" ✅ Telemetry data binding verified") print(" ✅ Error handling in place") print("\nXIC Panel UI ready for deployment!")