Files
home-automation/tools/test_dashboard.py

211 lines
6.1 KiB
Python

"""
Acceptance tests for Dashboard Route.
Tests:
1. GET /dashboard loads without errors
2. Rooms are shown in layout.yaml order
3. Devices within each room are sorted by rank (ascending)
4. GET / redirects to dashboard
"""
import sys
import httpx
def test_dashboard_loads():
"""Test 1: Dashboard loads without errors."""
print("Test 1: Dashboard loads")
print("-" * 60)
try:
response = httpx.get("http://localhost:8002/dashboard", timeout=5.0)
response.raise_for_status()
html = response.text
# Check for essential elements
if "Home Automation" not in html:
print(" ✗ FAILED: Missing title 'Home Automation'")
return False
if "Wohnzimmer" not in html:
print(" ✗ FAILED: Missing room 'Wohnzimmer'")
return False
if "Schlafzimmer" not in html:
print(" ✗ FAILED: Missing room 'Schlafzimmer'")
return False
print(" ✓ Dashboard loads successfully")
print(" ✓ Contains expected rooms")
print()
return True
except Exception as e:
print(f" ✗ FAILED: {e}")
print()
return False
def test_room_order():
"""Test 2: Rooms appear in layout.yaml order."""
print("Test 2: Room order matches layout.yaml")
print("-" * 60)
try:
response = httpx.get("http://localhost:8002/dashboard", timeout=5.0)
response.raise_for_status()
html = response.text
# Find positions of room titles
wohnzimmer_pos = html.find('class="room-title">Wohnzimmer<')
schlafzimmer_pos = html.find('class="room-title">Schlafzimmer<')
if wohnzimmer_pos == -1:
print(" ✗ FAILED: Room 'Wohnzimmer' not found")
return False
if schlafzimmer_pos == -1:
print(" ✗ FAILED: Room 'Schlafzimmer' not found")
return False
# Wohnzimmer should appear before Schlafzimmer
if wohnzimmer_pos > schlafzimmer_pos:
print(" ✗ FAILED: Room order incorrect")
print(f" Wohnzimmer at position {wohnzimmer_pos}")
print(f" Schlafzimmer at position {schlafzimmer_pos}")
return False
print(" ✓ Rooms appear in correct order:")
print(" 1. Wohnzimmer")
print(" 2. Schlafzimmer")
print()
return True
except Exception as e:
print(f" ✗ FAILED: {e}")
print()
return False
def test_device_rank_sorting():
"""Test 3: Devices are sorted by rank (ascending)."""
print("Test 3: Devices sorted by rank")
print("-" * 60)
try:
response = httpx.get("http://localhost:8002/dashboard", timeout=5.0)
response.raise_for_status()
html = response.text
# In Wohnzimmer: Deckenlampe (rank=5) should come before Stehlampe (rank=10)
deckenlampe_pos = html.find('device-title">Deckenlampe<')
stehlampe_pos = html.find('device-title">Stehlampe<')
if deckenlampe_pos == -1:
print(" ✗ FAILED: Device 'Deckenlampe' not found")
return False
if stehlampe_pos == -1:
print(" ✗ FAILED: Device 'Stehlampe' not found")
return False
if deckenlampe_pos > stehlampe_pos:
print(" ✗ FAILED: Devices not sorted by rank")
print(f" Deckenlampe (rank=5) at position {deckenlampe_pos}")
print(f" Stehlampe (rank=10) at position {stehlampe_pos}")
return False
print(" ✓ Devices sorted by rank (ascending):")
print(" Wohnzimmer: Deckenlampe (rank=5) → Stehlampe (rank=10)")
print()
return True
except Exception as e:
print(f" ✗ FAILED: {e}")
print()
return False
def test_root_redirect():
"""Test 4: GET / shows dashboard."""
print("Test 4: Root path (/) shows dashboard")
print("-" * 60)
try:
response = httpx.get("http://localhost:8002/", timeout=5.0, follow_redirects=True)
response.raise_for_status()
html = response.text
# Should contain dashboard elements
if "Home Automation" not in html:
print(" ✗ FAILED: Root path doesn't show dashboard")
return False
if "Wohnzimmer" not in html:
print(" ✗ FAILED: Root path missing rooms")
return False
print(" ✓ Root path shows dashboard")
print()
return True
except Exception as e:
print(f" ✗ FAILED: {e}")
print()
return False
def main():
"""Run all acceptance tests."""
print("=" * 60)
print("Testing Dashboard Route")
print("=" * 60)
print()
# Check if UI is running
print("Prerequisites: Checking if UI is running on port 8002...")
try:
response = httpx.get("http://localhost:8002/dashboard", timeout=3.0)
print("✓ UI is running")
print()
except Exception as e:
print(f"✗ Cannot reach UI: {e}")
print(" Start UI with: poetry run uvicorn apps.ui.main:app --port 8002")
print()
sys.exit(1)
results = []
# Run tests
results.append(("Dashboard loads", test_dashboard_loads()))
results.append(("Room order", test_room_order()))
results.append(("Device rank sorting", test_device_rank_sorting()))
results.append(("Root redirect", test_root_redirect()))
# Summary
print("=" * 60)
passed = sum(1 for _, result in results if result)
total = len(results)
print(f"Results: {passed}/{total} tests passed")
print("=" * 60)
for name, result in results:
status = "" if result else ""
print(f" {status} {name}")
if passed == total:
print()
print("All tests passed!")
sys.exit(0)
else:
print()
print("Some tests failed.")
sys.exit(1)
if __name__ == "__main__":
main()