CalejoControl/test_dashboard_local.py

226 lines
7.1 KiB
Python

#!/usr/bin/env python3
"""
Local Dashboard Test Script
Tests the dashboard functionality without Docker
"""
import os
import sys
import time
import requests
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def test_dashboard_files():
"""Test that all dashboard files exist"""
print("📁 Testing dashboard file structure...")
required_files = [
"src/dashboard/api.py",
"src/dashboard/templates.py",
"src/dashboard/router.py",
"static/dashboard.js",
"DASHBOARD.md",
"DASHBOARD_TESTING.md"
]
all_exist = True
for file_path in required_files:
if os.path.exists(file_path):
print(f"{file_path}")
else:
print(f"{file_path} - MISSING")
all_exist = False
return all_exist
def test_dashboard_imports():
"""Test that dashboard modules can be imported"""
print("\n🔧 Testing dashboard imports...")
try:
from src.dashboard.api import dashboard_router
from src.dashboard.templates import DASHBOARD_HTML
from src.dashboard.router import main_dashboard_router
print(" ✅ All dashboard imports successful")
return True
except Exception as e:
print(f" ❌ Import failed: {e}")
return False
def test_dashboard_api():
"""Test dashboard API endpoints"""
print("\n🌐 Testing dashboard API endpoints...")
# This would normally test against a running server
# For now, we'll just verify the API module structure
try:
from src.dashboard.api import (
SystemConfig, DatabaseConfig, OPCUAConfig, ModbusConfig,
RESTAPIConfig, MonitoringConfig, SecurityConfig,
validate_configuration, ValidationResult
)
print(" ✅ Dashboard API models and functions available")
# Test creating a configuration
config = SystemConfig(
database=DatabaseConfig(),
opcua=OPCUAConfig(),
modbus=ModbusConfig(),
rest_api=RESTAPIConfig(),
monitoring=MonitoringConfig(),
security=SecurityConfig()
)
print(" ✅ Configuration creation successful")
# Test validation
result = validate_configuration(config)
print(f" ✅ Configuration validation: {result.valid}")
return True
except Exception as e:
print(f" ❌ API test failed: {e}")
return False
def test_javascript_file():
"""Test that the JavaScript file exists and has required functions"""
print("\n📜 Testing JavaScript file...")
try:
with open("static/dashboard.js", "r") as f:
js_content = f.read()
required_functions = ["showTab", "loadStatus", "loadConfiguration", "loadLogs", "saveConfiguration"]
missing_functions = []
for func in required_functions:
if func in js_content:
print(f" ✅ Function '{func}' found")
else:
print(f" ❌ Function '{func}' missing")
missing_functions.append(func)
if len(missing_functions) == 0:
print(" ✅ All required JavaScript functions present")
return True
else:
print(f" ❌ Missing functions: {missing_functions}")
return False
except Exception as e:
print(f" ❌ JavaScript test failed: {e}")
return False
def test_html_template():
"""Test that the HTML template is valid"""
print("\n📄 Testing HTML template...")
try:
from src.dashboard.templates import DASHBOARD_HTML
if len(DASHBOARD_HTML) > 1000:
print(f" ✅ HTML template size: {len(DASHBOARD_HTML)} characters")
# Check for key elements
required_elements = [
"Calejo Control Adapter Dashboard",
"tab-content",
"status-tab",
"config-tab",
"logs-tab",
"actions-tab"
]
missing_elements = []
for element in required_elements:
if element in DASHBOARD_HTML:
print(f" ✅ Element '{element}' found")
else:
print(f" ❌ Element '{element}' missing")
missing_elements.append(element)
if len(missing_elements) == 0:
print(" ✅ All required HTML elements present")
return True
else:
print(f" ❌ Missing elements: {missing_elements}")
return False
else:
print(" ❌ HTML template too small")
return False
except Exception as e:
print(f" ❌ HTML template test failed: {e}")
return False
def test_integration():
"""Test integration with REST API"""
print("\n🔗 Testing REST API integration...")
try:
from src.protocols.rest_api import RESTAPIServer
# Check if dashboard routes are integrated
import inspect
source = inspect.getsource(RESTAPIServer._setup_routes)
if 'dashboard' in source.lower():
print(" ✅ Dashboard routes integrated in REST API")
return True
else:
print(" ❌ Dashboard routes not found in REST API")
return False
except Exception as e:
print(f" ❌ Integration test failed: {e}")
return False
def main():
"""Run all dashboard tests"""
print("🚀 Calejo Control Adapter - Dashboard Deployment Test")
print("=" * 60)
tests = [
("File Structure", test_dashboard_files),
("Module Imports", test_dashboard_imports),
("API Endpoints", test_dashboard_api),
("JavaScript", test_javascript_file),
("HTML Template", test_html_template),
("REST API Integration", test_integration)
]
results = []
for test_name, test_func in tests:
result = test_func()
results.append((test_name, result))
print("\n" + "=" * 60)
print("📊 TEST RESULTS")
print("=" * 60)
passed = 0
total = len(results)
for test_name, result in results:
status = "✅ PASS" if result else "❌ FAIL"
print(f"{status} {test_name}")
if result:
passed += 1
print(f"\n🎯 Summary: {passed}/{total} tests passed")
if passed == total:
print("\n🎉 SUCCESS: Dashboard is ready for deployment!")
print("\n📋 Next steps:")
print(" 1. Review the dashboard at: http://localhost:8080/dashboard")
print(" 2. Run full test suite: python -m pytest tests/unit/test_dashboard_*.py tests/integration/test_dashboard_*.py -v")
print(" 3. Deploy with: docker-compose up -d")
return 0
else:
print("\n❌ Some tests failed. Please check the dashboard implementation.")
return 1
if __name__ == "__main__":
sys.exit(main())