test: fix zone_ctl fixture, pattern assertions, and browser cleanup

Made-with: Cursor
This commit is contained in:
pi
2026-04-12 00:27:43 +12:00
parent fbd4295302
commit 264eb7296f
4 changed files with 114 additions and 63 deletions

View File

@@ -82,19 +82,19 @@ def test_connection(client: TestClient) -> bool:
print(f"✗ Connection error: {e}")
return False
def test_tabs(client: TestClient) -> bool:
"""Test tabs endpoints."""
print("\n=== Testing Tabs Endpoints ===")
def test_zones(client: TestClient) -> bool:
"""Test zones endpoints."""
print("\n=== Testing Zones Endpoints ===")
passed = 0
total = 0
# Test 1: List tabs
# Test 1: List zones
total += 1
try:
response = client.get('/zones')
if response.status_code == 200:
data = response.json()
print(f"✓ GET /zones - Found {len(data.get('zones', {}))} tabs")
print(f"✓ GET /zones - Found {len(data.get('zones', {}))} zones")
passed += 1
else:
print(f"✗ GET /zones - Status: {response.status_code}")
@@ -104,17 +104,17 @@ def test_tabs(client: TestClient) -> bool:
# Test 2: Create zone
total += 1
try:
tab_data = {
zone_data = {
"name": "Test Zone",
"names": ["1", "2"]
}
response = client.post('/zones', json_data=tab_data)
response = client.post('/zones', json_data=zone_data)
if response.status_code == 201:
created_tab = response.json()
# Response format: {zone_id: {tab_data}}
if isinstance(created_tab, dict):
created_zone = response.json()
# Response format: {zone_id: {zone object}}
if isinstance(created_zone, dict):
# Get the first key which should be the zone ID
zone_id = next(iter(created_tab.keys())) if created_tab else None
zone_id = next(iter(created_zone.keys())) if created_zone else None
else:
zone_id = None
print(f"✓ POST /zones - Created zone: {zone_id}")
@@ -203,7 +203,7 @@ def test_tabs(client: TestClient) -> bool:
import traceback
traceback.print_exc()
print(f"\nTabs tests: {passed}/{total} passed")
print(f"\nZones tests: {passed}/{total} passed")
return passed == total
def test_profiles(client: TestClient) -> bool:
@@ -405,10 +405,33 @@ def test_patterns(client: TestClient) -> bool:
except Exception as e:
print(f"✗ GET /patterns/definitions - Error: {e}")
# Test 3: Firmware-only on/off — no OTA file (400 from API).
total += 1
try:
r = client.get("/patterns/ota/file/on.py")
if r.status_code == 400 and "error" in r.json():
print("✓ GET /patterns/ota/file/on.py - Rejected as built-in")
passed += 1
else:
print(f"✗ GET /patterns/ota/file/on.py - Expected 400, got {r.status_code}")
except Exception as e:
print(f"✗ GET /patterns/ota/file/on.py - Error: {e}")
total += 1
try:
r = client.post("/patterns/off/send", json_data={})
if r.status_code == 400 and "error" in r.json():
print("✓ POST /patterns/off/send - Rejected as built-in")
passed += 1
else:
print(f"✗ POST /patterns/off/send - Expected 400, got {r.status_code}")
except Exception as e:
print(f"✗ POST /patterns/off/send - Error: {e}")
print(f"\nPatterns tests: {passed}/{total} passed")
return passed == total
def test_tab_edit_workflow(client: TestClient) -> bool:
def test_zone_edit_workflow(client: TestClient) -> bool:
"""Test complete zone edit workflow like a browser would."""
print("\n=== Testing Zone Edit Workflow ===")
passed = 0
@@ -417,11 +440,11 @@ def test_tab_edit_workflow(client: TestClient) -> bool:
# Step 1: Create a zone to edit
total += 1
try:
tab_data = {
zone_data = {
"name": "Zone to Edit",
"names": ["1"]
}
response = client.post('/zones', json_data=tab_data)
response = client.post('/zones', json_data=zone_data)
if response.status_code == 201:
created = response.json()
if isinstance(created, dict):
@@ -437,8 +460,8 @@ def test_tab_edit_workflow(client: TestClient) -> bool:
total += 1
response = client.get(f'/zones/{zone_id}')
if response.status_code == 200:
original_tab = response.json()
print(f"✓ Retrieved zone - Name: '{original_tab.get('name')}', IDs: {original_tab.get('names')}")
original_zone = response.json()
print(f"✓ Retrieved zone - Name: '{original_zone.get('name')}', IDs: {original_zone.get('names')}")
passed += 1
# Step 3: Edit the zone (simulate browser edit form submission)
@@ -493,7 +516,7 @@ def test_tab_edit_workflow(client: TestClient) -> bool:
import traceback
traceback.print_exc()
print(f"\nTab edit workflow tests: {passed}/{total} passed")
print(f"\nZone edit workflow tests: {passed}/{total} passed")
return passed == total
def test_static_files(client: TestClient) -> bool:
@@ -543,8 +566,8 @@ def main():
results = []
# Run all tests
results.append(("Tabs", test_tabs(client)))
results.append(("Zone Edit Workflow", test_tab_edit_workflow(client)))
results.append(("Zones", test_zones(client)))
results.append(("Zone Edit Workflow", test_zone_edit_workflow(client)))
results.append(("Profiles", test_profiles(client)))
results.append(("Presets", test_presets(client)))
results.append(("Patterns", test_patterns(client)))