Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.
test_runner_web.py · 28.0 KB · 892 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | DBBasic Test Runner Web Interface |
| 4 | Run tests from browser, see real-time results with red/green status |
| 5 | """ |
| 6 | |
| 7 | from fastapi import FastAPI, WebSocket, BackgroundTasks |
| 8 | from fastapi.responses import HTMLResponse, JSONResponse |
| 9 | import asyncio |
| 10 | import subprocess |
| 11 | import json |
| 12 | import time |
| 13 | from datetime import datetime |
| 14 | from typing import Dict, Any, List |
| 15 | import re |
| 16 | import sys |
| 17 | import io |
| 18 | from contextlib import redirect_stdout, redirect_stderr |
| 19 | |
| 20 | app = FastAPI(title="DBBasic Test Runner") |
| 21 | |
| 22 | # Store test results |
| 23 | test_results = { |
| 24 | "last_run": None, |
| 25 | "tests": [], |
| 26 | "summary": {}, |
| 27 | "running": False, |
| 28 | "output": [] |
| 29 | } |
| 30 | |
| 31 | # WebSocket connections |
| 32 | active_connections: List[WebSocket] = [] |
| 33 | |
| 34 | async def broadcast_message(message: dict): |
| 35 | """Broadcast to all connected clients""" |
| 36 | for connection in active_connections: |
| 37 | try: |
| 38 | await connection.send_json(message) |
| 39 | except: |
| 40 | pass |
| 41 | |
| 42 | async def run_tests_async(test_file: str = "test_dbbasic_channels.py"): |
| 43 | """Run tests and parse output in real-time""" |
| 44 | global test_results |
| 45 | |
| 46 | test_results["running"] = True |
| 47 | test_results["output"] = [] |
| 48 | test_results["tests"] = [] |
| 49 | test_results["last_run"] = datetime.now().isoformat() |
| 50 | |
| 51 | await broadcast_message({ |
| 52 | "type": "test_started", |
| 53 | "timestamp": datetime.now().isoformat() |
| 54 | }) |
| 55 | |
| 56 | try: |
| 57 | # Run pytest without JSON plugin (simpler and more reliable) |
| 58 | process = await asyncio.create_subprocess_exec( |
| 59 | sys.executable, "-m", "pytest", test_file, |
| 60 | "-v", "--tb=short", "--color=no", |
| 61 | stdout=asyncio.subprocess.PIPE, |
| 62 | stderr=asyncio.subprocess.STDOUT # Combine stderr into stdout |
| 63 | ) |
| 64 | |
| 65 | # Read output line by line |
| 66 | while True: |
| 67 | line = await process.stdout.readline() |
| 68 | if not line: |
| 69 | break |
| 70 | |
| 71 | line_text = line.decode().strip() |
| 72 | if line_text: |
| 73 | test_results["output"].append(line_text) |
| 74 | |
| 75 | # Parse test result lines |
| 76 | if "PASSED" in line_text: |
| 77 | # Extract test name from format: test_file.py::ClassName::test_name PASSED |
| 78 | if "::" in line_text: |
| 79 | parts = line_text.split("::") |
| 80 | test_name = parts[-1].split(" ")[0] if len(parts) > 0 else "Unknown" |
| 81 | else: |
| 82 | test_name = "Unknown" |
| 83 | |
| 84 | test_results["tests"].append({ |
| 85 | "name": test_name, |
| 86 | "status": "passed", |
| 87 | "time": datetime.now().isoformat() |
| 88 | }) |
| 89 | await broadcast_message({ |
| 90 | "type": "test_result", |
| 91 | "name": test_name, |
| 92 | "status": "passed", |
| 93 | "line": line_text |
| 94 | }) |
| 95 | |
| 96 | elif "FAILED" in line_text: |
| 97 | # Extract test name from format: test_file.py::ClassName::test_name FAILED |
| 98 | if "::" in line_text: |
| 99 | parts = line_text.split("::") |
| 100 | test_name = parts[-1].split(" ")[0] if len(parts) > 0 else "Unknown" |
| 101 | else: |
| 102 | test_name = "Unknown" |
| 103 | |
| 104 | test_results["tests"].append({ |
| 105 | "name": test_name, |
| 106 | "status": "failed", |
| 107 | "time": datetime.now().isoformat() |
| 108 | }) |
| 109 | await broadcast_message({ |
| 110 | "type": "test_result", |
| 111 | "name": test_name, |
| 112 | "status": "failed", |
| 113 | "line": line_text |
| 114 | }) |
| 115 | |
| 116 | elif "ERROR" in line_text: |
| 117 | test_name = line_text.split("::")[1].split(" ")[0] if "::" in line_text else "Unknown" |
| 118 | test_results["tests"].append({ |
| 119 | "name": test_name, |
| 120 | "status": "error", |
| 121 | "time": datetime.now().isoformat() |
| 122 | }) |
| 123 | await broadcast_message({ |
| 124 | "type": "test_result", |
| 125 | "name": test_name, |
| 126 | "status": "error", |
| 127 | "line": line_text |
| 128 | }) |
| 129 | |
| 130 | # Broadcast output line |
| 131 | await broadcast_message({ |
| 132 | "type": "output", |
| 133 | "line": line_text |
| 134 | }) |
| 135 | |
| 136 | await process.wait() |
| 137 | |
| 138 | # Parse summary |
| 139 | try: |
| 140 | with open("/tmp/test_report.json", "r") as f: |
| 141 | report = json.load(f) |
| 142 | test_results["summary"] = report.get("summary", {}) |
| 143 | except: |
| 144 | # Fallback summary from output |
| 145 | passed = sum(1 for t in test_results["tests"] if t["status"] == "passed") |
| 146 | failed = sum(1 for t in test_results["tests"] if t["status"] == "failed") |
| 147 | test_results["summary"] = { |
| 148 | "passed": passed, |
| 149 | "failed": failed, |
| 150 | "total": len(test_results["tests"]) |
| 151 | } |
| 152 | |
| 153 | except Exception as e: |
| 154 | test_results["output"].append(f"Error running tests: {str(e)}") |
| 155 | await broadcast_message({ |
| 156 | "type": "error", |
| 157 | "message": str(e) |
| 158 | }) |
| 159 | |
| 160 | finally: |
| 161 | test_results["running"] = False |
| 162 | await broadcast_message({ |
| 163 | "type": "test_completed", |
| 164 | "summary": test_results["summary"], |
| 165 | "timestamp": datetime.now().isoformat() |
| 166 | }) |
| 167 | |
| 168 | async def run_selenium_tests_async(): |
| 169 | """Run Selenium tests and parse output in real-time""" |
| 170 | global test_results |
| 171 | |
| 172 | test_results["running"] = True |
| 173 | test_results["output"] = [] |
| 174 | test_results["tests"] = [] |
| 175 | test_results["last_run"] = datetime.now().isoformat() |
| 176 | |
| 177 | await broadcast_message({ |
| 178 | "type": "test_started", |
| 179 | "test_type": "selenium", |
| 180 | "timestamp": datetime.now().isoformat() |
| 181 | }) |
| 182 | |
| 183 | try: |
| 184 | # Run Selenium tests |
| 185 | process = await asyncio.create_subprocess_exec( |
| 186 | sys.executable, "test_with_selenium.py", |
| 187 | stdout=asyncio.subprocess.PIPE, |
| 188 | stderr=asyncio.subprocess.STDOUT |
| 189 | ) |
| 190 | |
| 191 | # Read output line by line |
| 192 | while True: |
| 193 | line = await process.stdout.readline() |
| 194 | if not line: |
| 195 | break |
| 196 | |
| 197 | line_text = line.decode().strip() |
| 198 | if line_text: |
| 199 | test_results["output"].append(line_text) |
| 200 | |
| 201 | # Parse Selenium test results |
| 202 | if "✅ PASSED:" in line_text: |
| 203 | test_name = line_text.split("✅ PASSED:")[1].strip() |
| 204 | test_results["tests"].append({ |
| 205 | "name": test_name, |
| 206 | "status": "passed", |
| 207 | "time": datetime.now().isoformat() |
| 208 | }) |
| 209 | await broadcast_message({ |
| 210 | "type": "test_result", |
| 211 | "name": test_name, |
| 212 | "status": "passed", |
| 213 | "line": line_text |
| 214 | }) |
| 215 | |
| 216 | elif "❌ FAILED:" in line_text or "❌ FAIL:" in line_text: |
| 217 | test_name = line_text.split("❌")[1].split(":")[1].strip() if ":" in line_text else "Unknown" |
| 218 | test_results["tests"].append({ |
| 219 | "name": test_name, |
| 220 | "status": "failed", |
| 221 | "time": datetime.now().isoformat() |
| 222 | }) |
| 223 | await broadcast_message({ |
| 224 | "type": "test_result", |
| 225 | "name": test_name, |
| 226 | "status": "failed", |
| 227 | "line": line_text |
| 228 | }) |
| 229 | |
| 230 | elif "Testing" in line_text and line_text.endswith("..."): |
| 231 | # Mark test as running |
| 232 | test_name = line_text.replace("Testing", "").replace("...", "").strip() |
| 233 | await broadcast_message({ |
| 234 | "type": "test_running", |
| 235 | "name": test_name, |
| 236 | "line": line_text |
| 237 | }) |
| 238 | |
| 239 | # Broadcast output line |
| 240 | await broadcast_message({ |
| 241 | "type": "output", |
| 242 | "line": line_text |
| 243 | }) |
| 244 | |
| 245 | await process.wait() |
| 246 | |
| 247 | # Generate summary |
| 248 | passed = sum(1 for t in test_results["tests"] if t["status"] == "passed") |
| 249 | failed = sum(1 for t in test_results["tests"] if t["status"] == "failed") |
| 250 | test_results["summary"] = { |
| 251 | "passed": passed, |
| 252 | "failed": failed, |
| 253 | "total": len(test_results["tests"]) |
| 254 | } |
| 255 | |
| 256 | except Exception as e: |
| 257 | test_results["output"].append(f"Error running Selenium tests: {str(e)}") |
| 258 | await broadcast_message({ |
| 259 | "type": "error", |
| 260 | "message": str(e) |
| 261 | }) |
| 262 | |
| 263 | finally: |
| 264 | test_results["running"] = False |
| 265 | await broadcast_message({ |
| 266 | "type": "test_completed", |
| 267 | "test_type": "selenium", |
| 268 | "summary": test_results["summary"], |
| 269 | "timestamp": datetime.now().isoformat() |
| 270 | }) |
| 271 | |
| 272 | @app.get("/") |
| 273 | async def root(): |
| 274 | """Serve the test runner interface""" |
| 275 | html_content = """ |
| 276 | <!DOCTYPE html> |
| 277 | <html lang="en"> |
| 278 | <head> |
| 279 | <meta charset="UTF-8"> |
| 280 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 281 | <title>DBBasic Test Runner - 402M rows/sec</title> |
| 282 | <style> |
| 283 | * { margin: 0; padding: 0; box-sizing: border-box; } |
| 284 | |
| 285 | body { |
| 286 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 287 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 288 | min-height: 100vh; |
| 289 | padding: 2rem; |
| 290 | } |
| 291 | |
| 292 | .container { |
| 293 | max-width: 1400px; |
| 294 | margin: 0 auto; |
| 295 | } |
| 296 | |
| 297 | .header { |
| 298 | background: white; |
| 299 | border-radius: 12px; |
| 300 | padding: 2rem; |
| 301 | margin-bottom: 2rem; |
| 302 | box-shadow: 0 10px 40px rgba(0,0,0,0.1); |
| 303 | display: flex; |
| 304 | justify-content: space-between; |
| 305 | align-items: center; |
| 306 | } |
| 307 | |
| 308 | .logo { |
| 309 | font-size: 32px; |
| 310 | font-weight: bold; |
| 311 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 312 | -webkit-background-clip: text; |
| 313 | -webkit-text-fill-color: transparent; |
| 314 | } |
| 315 | |
| 316 | .subtitle { |
| 317 | color: #666; |
| 318 | font-size: 14px; |
| 319 | margin-top: 0.5rem; |
| 320 | } |
| 321 | |
| 322 | .run-btn { |
| 323 | padding: 1rem 2rem; |
| 324 | background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 325 | color: white; |
| 326 | border: none; |
| 327 | border-radius: 8px; |
| 328 | font-size: 16px; |
| 329 | font-weight: 600; |
| 330 | cursor: pointer; |
| 331 | transition: all 0.3s; |
| 332 | } |
| 333 | |
| 334 | .run-btn:hover { |
| 335 | transform: translateY(-2px); |
| 336 | box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4); |
| 337 | } |
| 338 | |
| 339 | .run-btn:disabled { |
| 340 | opacity: 0.5; |
| 341 | cursor: not-allowed; |
| 342 | } |
| 343 | |
| 344 | .main-grid { |
| 345 | display: grid; |
| 346 | grid-template-columns: 400px 1fr; |
| 347 | gap: 2rem; |
| 348 | height: calc(100vh - 200px); |
| 349 | } |
| 350 | |
| 351 | .test-list { |
| 352 | background: white; |
| 353 | border-radius: 12px; |
| 354 | padding: 1.5rem; |
| 355 | overflow-y: auto; |
| 356 | box-shadow: 0 10px 40px rgba(0,0,0,0.1); |
| 357 | } |
| 358 | |
| 359 | .test-item { |
| 360 | padding: 0.75rem; |
| 361 | margin-bottom: 0.5rem; |
| 362 | border-radius: 8px; |
| 363 | display: flex; |
| 364 | justify-content: space-between; |
| 365 | align-items: center; |
| 366 | transition: all 0.2s; |
| 367 | } |
| 368 | |
| 369 | .test-item.passed { |
| 370 | background: #d4edda; |
| 371 | border: 1px solid #28a745; |
| 372 | } |
| 373 | |
| 374 | .test-item.failed { |
| 375 | background: #f8d7da; |
| 376 | border: 1px solid #dc3545; |
| 377 | } |
| 378 | |
| 379 | .test-item.running { |
| 380 | background: #fff3cd; |
| 381 | border: 1px solid #ffc107; |
| 382 | animation: pulse 1s infinite; |
| 383 | } |
| 384 | |
| 385 | .test-item.pending { |
| 386 | background: #f8f9fa; |
| 387 | border: 1px solid #dee2e6; |
| 388 | } |
| 389 | |
| 390 | @keyframes pulse { |
| 391 | 0%, 100% { opacity: 1; } |
| 392 | 50% { opacity: 0.7; } |
| 393 | } |
| 394 | |
| 395 | .test-name { |
| 396 | font-size: 14px; |
| 397 | font-weight: 500; |
| 398 | overflow: hidden; |
| 399 | text-overflow: ellipsis; |
| 400 | white-space: nowrap; |
| 401 | flex: 1; |
| 402 | } |
| 403 | |
| 404 | .test-status { |
| 405 | padding: 0.25rem 0.75rem; |
| 406 | border-radius: 15px; |
| 407 | font-size: 12px; |
| 408 | font-weight: 600; |
| 409 | text-transform: uppercase; |
| 410 | } |
| 411 | |
| 412 | .status-passed { |
| 413 | background: #28a745; |
| 414 | color: white; |
| 415 | } |
| 416 | |
| 417 | .status-failed { |
| 418 | background: #dc3545; |
| 419 | color: white; |
| 420 | } |
| 421 | |
| 422 | .status-running { |
| 423 | background: #ffc107; |
| 424 | color: #000; |
| 425 | } |
| 426 | |
| 427 | .status-pending { |
| 428 | background: #6c757d; |
| 429 | color: white; |
| 430 | } |
| 431 | |
| 432 | .output-section { |
| 433 | background: #1e1e1e; |
| 434 | border-radius: 12px; |
| 435 | padding: 1.5rem; |
| 436 | overflow: hidden; |
| 437 | display: flex; |
| 438 | flex-direction: column; |
| 439 | box-shadow: 0 10px 40px rgba(0,0,0,0.1); |
| 440 | } |
| 441 | |
| 442 | .output-header { |
| 443 | display: flex; |
| 444 | justify-content: space-between; |
| 445 | align-items: center; |
| 446 | margin-bottom: 1rem; |
| 447 | padding-bottom: 1rem; |
| 448 | border-bottom: 1px solid #333; |
| 449 | } |
| 450 | |
| 451 | .output-title { |
| 452 | color: #4CAF50; |
| 453 | font-size: 14px; |
| 454 | font-weight: 600; |
| 455 | text-transform: uppercase; |
| 456 | } |
| 457 | |
| 458 | .output-console { |
| 459 | flex: 1; |
| 460 | overflow-y: auto; |
| 461 | font-family: 'Monaco', 'Menlo', monospace; |
| 462 | font-size: 13px; |
| 463 | line-height: 1.5; |
| 464 | color: #d4d4d4; |
| 465 | } |
| 466 | |
| 467 | .output-line { |
| 468 | padding: 0.25rem 0; |
| 469 | } |
| 470 | |
| 471 | .output-line.error { |
| 472 | color: #f48771; |
| 473 | } |
| 474 | |
| 475 | .output-line.success { |
| 476 | color: #89d185; |
| 477 | } |
| 478 | |
| 479 | .output-line.warning { |
| 480 | color: #ffd700; |
| 481 | } |
| 482 | |
| 483 | .summary { |
| 484 | background: white; |
| 485 | border-radius: 12px; |
| 486 | padding: 1.5rem; |
| 487 | margin-top: 2rem; |
| 488 | box-shadow: 0 10px 40px rgba(0,0,0,0.1); |
| 489 | } |
| 490 | |
| 491 | .summary-grid { |
| 492 | display: grid; |
| 493 | grid-template-columns: repeat(4, 1fr); |
| 494 | gap: 2rem; |
| 495 | text-align: center; |
| 496 | } |
| 497 | |
| 498 | .summary-item { |
| 499 | padding: 1rem; |
| 500 | border-radius: 8px; |
| 501 | background: #f8f9fa; |
| 502 | } |
| 503 | |
| 504 | .summary-value { |
| 505 | font-size: 36px; |
| 506 | font-weight: bold; |
| 507 | margin-bottom: 0.5rem; |
| 508 | } |
| 509 | |
| 510 | .summary-label { |
| 511 | color: #666; |
| 512 | font-size: 14px; |
| 513 | text-transform: uppercase; |
| 514 | } |
| 515 | |
| 516 | .summary-passed .summary-value { |
| 517 | color: #28a745; |
| 518 | } |
| 519 | |
| 520 | .summary-failed .summary-value { |
| 521 | color: #dc3545; |
| 522 | } |
| 523 | |
| 524 | .summary-total .summary-value { |
| 525 | color: #667eea; |
| 526 | } |
| 527 | |
| 528 | .summary-time .summary-value { |
| 529 | color: #17a2b8; |
| 530 | } |
| 531 | |
| 532 | .spinner { |
| 533 | border: 3px solid #f3f3f3; |
| 534 | border-top: 3px solid #667eea; |
| 535 | border-radius: 50%; |
| 536 | width: 20px; |
| 537 | height: 20px; |
| 538 | animation: spin 1s linear infinite; |
| 539 | display: inline-block; |
| 540 | margin-left: 10px; |
| 541 | } |
| 542 | |
| 543 | @keyframes spin { |
| 544 | 0% { transform: rotate(0deg); } |
| 545 | 100% { transform: rotate(360deg); } |
| 546 | } |
| 547 | |
| 548 | .benchmark-badge { |
| 549 | background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); |
| 550 | color: white; |
| 551 | padding: 0.5rem 1rem; |
| 552 | border-radius: 20px; |
| 553 | font-size: 14px; |
| 554 | font-weight: 600; |
| 555 | display: inline-block; |
| 556 | } |
| 557 | </style> |
| 558 | </head> |
| 559 | <body> |
| 560 | <div class="container"> |
| 561 | <div class="header"> |
| 562 | <div> |
| 563 | <div class="logo">DBBasic Test Runner</div> |
| 564 | <div class="subtitle">Testing at 402M rows/sec capability</div> |
| 565 | </div> |
| 566 | <div style="display: flex; align-items: center; gap: 1rem;"> |
| 567 | <div class="benchmark-badge">⚡ 402M rows/sec</div> |
| 568 | <button class="run-btn" id="runBtn" onclick="runTests()"> |
| 569 | Run Unit Tests |
| 570 | </button> |
| 571 | <button class="run-btn" id="seleniumBtn" onclick="runSeleniumTests()"> |
| 572 | Run Selenium Tests |
| 573 | </button> |
| 574 | </div> |
| 575 | </div> |
| 576 | |
| 577 | <div class="main-grid"> |
| 578 | <div class="test-list"> |
| 579 | <h3 style="margin-bottom: 1rem; color: #333;">Test Suite</h3> |
| 580 | <div id="testList"></div> |
| 581 | </div> |
| 582 | |
| 583 | <div class="output-section"> |
| 584 | <div class="output-header"> |
| 585 | <div class="output-title">Test Output</div> |
| 586 | <div id="statusIndicator" style="color: #999;">Ready</div> |
| 587 | </div> |
| 588 | <div class="output-console" id="outputConsole"> |
| 589 | <div class="output-line">Waiting for tests to run...</div> |
| 590 | <div class="output-line" style="color: #666;">Click "Run All Tests" to begin</div> |
| 591 | </div> |
| 592 | </div> |
| 593 | </div> |
| 594 | |
| 595 | <div class="summary" id="summary" style="display: none;"> |
| 596 | <div class="summary-grid"> |
| 597 | <div class="summary-item summary-passed"> |
| 598 | <div class="summary-value" id="passedCount">0</div> |
| 599 | <div class="summary-label">Passed</div> |
| 600 | </div> |
| 601 | <div class="summary-item summary-failed"> |
| 602 | <div class="summary-value" id="failedCount">0</div> |
| 603 | <div class="summary-label">Failed</div> |
| 604 | </div> |
| 605 | <div class="summary-item summary-total"> |
| 606 | <div class="summary-value" id="totalCount">0</div> |
| 607 | <div class="summary-label">Total</div> |
| 608 | </div> |
| 609 | <div class="summary-item summary-time"> |
| 610 | <div class="summary-value" id="timeCount">0s</div> |
| 611 | <div class="summary-label">Duration</div> |
| 612 | </div> |
| 613 | </div> |
| 614 | </div> |
| 615 | </div> |
| 616 | |
| 617 | <script> |
| 618 | const ws = new WebSocket('ws://localhost:8006/ws'); |
| 619 | const outputConsole = document.getElementById('outputConsole'); |
| 620 | const testList = document.getElementById('testList'); |
| 621 | const runBtn = document.getElementById('runBtn'); |
| 622 | const seleniumBtn = document.getElementById('seleniumBtn'); |
| 623 | const statusIndicator = document.getElementById('statusIndicator'); |
| 624 | const summary = document.getElementById('summary'); |
| 625 | |
| 626 | let testItems = {}; |
| 627 | let startTime = null; |
| 628 | |
| 629 | // Test names from our test suites |
| 630 | const unitTestNames = [ |
| 631 | 'test_publish_single_message', |
| 632 | 'test_subscribe_and_receive', |
| 633 | 'test_wildcard_subscription', |
| 634 | 'test_channel_worker_processing', |
| 635 | 'test_message_ttl_expiration', |
| 636 | 'test_concurrent_publishing', |
| 637 | 'test_channel_statistics', |
| 638 | 'test_add_single_task', |
| 639 | 'test_bulk_add_tasks', |
| 640 | 'test_get_next_task_priority', |
| 641 | 'test_task_locking', |
| 642 | 'test_complete_task', |
| 643 | 'test_fail_task_with_retry', |
| 644 | 'test_metrics_calculation', |
| 645 | 'test_task_worker', |
| 646 | 'test_benchmark_insert_speed', |
| 647 | 'test_benchmark_concurrent_workers', |
| 648 | 'test_benchmark_pub_sub_latency', |
| 649 | 'test_integration_full_pipeline' |
| 650 | ]; |
| 651 | |
| 652 | const seleniumTestNames = [ |
| 653 | 'Dashboard Links', |
| 654 | 'Config Count', |
| 655 | 'Syntax Highlighting' |
| 656 | ]; |
| 657 | |
| 658 | let currentTestType = 'unit'; |
| 659 | |
| 660 | // Initialize test list |
| 661 | function initTestList(testType = 'unit') { |
| 662 | testList.innerHTML = ''; |
| 663 | testItems = {}; |
| 664 | currentTestType = testType; |
| 665 | |
| 666 | const testNames = testType === 'selenium' ? seleniumTestNames : unitTestNames; |
| 667 | testNames.forEach(name => { |
| 668 | const item = document.createElement('div'); |
| 669 | item.className = 'test-item pending'; |
| 670 | item.innerHTML = ` |
| 671 | <span class="test-name" title="${name}">${name}</span> |
| 672 | <span class="test-status status-pending">pending</span> |
| 673 | `; |
| 674 | testList.appendChild(item); |
| 675 | testItems[name] = item; |
| 676 | }); |
| 677 | } |
| 678 | |
| 679 | ws.onopen = () => { |
| 680 | console.log('Connected to test runner'); |
| 681 | loadTestStatus(); |
| 682 | }; |
| 683 | |
| 684 | ws.onmessage = (event) => { |
| 685 | const data = JSON.parse(event.data); |
| 686 | |
| 687 | if (data.type === 'test_started') { |
| 688 | outputConsole.innerHTML = ''; |
| 689 | const testType = data.test_type || 'unit'; |
| 690 | initTestList(testType); |
| 691 | summary.style.display = 'none'; |
| 692 | startTime = Date.now(); |
| 693 | statusIndicator.innerHTML = '<span>Running</span><div class="spinner"></div>'; |
| 694 | statusIndicator.style.color = '#ffc107'; |
| 695 | runBtn.disabled = true; |
| 696 | seleniumBtn.disabled = true; |
| 697 | if (testType === 'selenium') { |
| 698 | seleniumBtn.textContent = 'Running...'; |
| 699 | runBtn.textContent = 'Run Unit Tests'; |
| 700 | } else { |
| 701 | runBtn.textContent = 'Running...'; |
| 702 | seleniumBtn.textContent = 'Run Selenium Tests'; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | else if (data.type === 'test_result') { |
| 707 | updateTestItem(data.name, data.status); |
| 708 | addOutput(data.line, data.status === 'passed' ? 'success' : 'error'); |
| 709 | } |
| 710 | |
| 711 | else if (data.type === 'output') { |
| 712 | addOutput(data.line); |
| 713 | } |
| 714 | |
| 715 | else if (data.type === 'test_completed') { |
| 716 | const duration = ((Date.now() - startTime) / 1000).toFixed(1); |
| 717 | statusIndicator.innerHTML = 'Completed'; |
| 718 | statusIndicator.style.color = '#28a745'; |
| 719 | runBtn.disabled = false; |
| 720 | seleniumBtn.disabled = false; |
| 721 | runBtn.textContent = 'Run Unit Tests'; |
| 722 | seleniumBtn.textContent = 'Run Selenium Tests'; |
| 723 | |
| 724 | // Update summary |
| 725 | updateSummary(data.summary, duration); |
| 726 | summary.style.display = 'block'; |
| 727 | } |
| 728 | |
| 729 | else if (data.type === 'error') { |
| 730 | addOutput('Error: ' + data.message, 'error'); |
| 731 | statusIndicator.innerHTML = 'Error'; |
| 732 | statusIndicator.style.color = '#dc3545'; |
| 733 | runBtn.disabled = false; |
| 734 | seleniumBtn.disabled = false; |
| 735 | runBtn.textContent = 'Run Unit Tests'; |
| 736 | seleniumBtn.textContent = 'Run Selenium Tests'; |
| 737 | } |
| 738 | }; |
| 739 | |
| 740 | function updateTestItem(name, status) { |
| 741 | if (testItems[name]) { |
| 742 | testItems[name].className = `test-item ${status}`; |
| 743 | const statusSpan = testItems[name].querySelector('.test-status'); |
| 744 | statusSpan.className = `test-status status-${status}`; |
| 745 | statusSpan.textContent = status; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | function addOutput(line, type = '') { |
| 750 | const div = document.createElement('div'); |
| 751 | div.className = 'output-line ' + type; |
| 752 | |
| 753 | // Color code output |
| 754 | if (line.includes('PASSED')) { |
| 755 | div.className = 'output-line success'; |
| 756 | } else if (line.includes('FAILED') || line.includes('ERROR')) { |
| 757 | div.className = 'output-line error'; |
| 758 | } else if (line.includes('WARNING')) { |
| 759 | div.className = 'output-line warning'; |
| 760 | } |
| 761 | |
| 762 | div.textContent = line; |
| 763 | outputConsole.appendChild(div); |
| 764 | outputConsole.scrollTop = outputConsole.scrollHeight; |
| 765 | } |
| 766 | |
| 767 | function updateSummary(summary, duration) { |
| 768 | document.getElementById('passedCount').textContent = summary.passed || 0; |
| 769 | document.getElementById('failedCount').textContent = summary.failed || 0; |
| 770 | document.getElementById('totalCount').textContent = summary.total || 0; |
| 771 | document.getElementById('timeCount').textContent = duration + 's'; |
| 772 | } |
| 773 | |
| 774 | async function runTests() { |
| 775 | const response = await fetch('/api/run-tests', { method: 'POST' }); |
| 776 | const result = await response.json(); |
| 777 | console.log('Tests started:', result); |
| 778 | } |
| 779 | |
| 780 | async function runSeleniumTests() { |
| 781 | const response = await fetch('/api/run-selenium-tests', { method: 'POST' }); |
| 782 | const result = await response.json(); |
| 783 | console.log('Selenium tests started:', result); |
| 784 | } |
| 785 | |
| 786 | async function loadTestStatus() { |
| 787 | const response = await fetch('/api/status'); |
| 788 | const status = await response.json(); |
| 789 | |
| 790 | if (status.tests && status.tests.length > 0) { |
| 791 | initTestList(); |
| 792 | status.tests.forEach(test => { |
| 793 | updateTestItem(test.name, test.status); |
| 794 | }); |
| 795 | |
| 796 | if (status.summary) { |
| 797 | updateSummary(status.summary, '0'); |
| 798 | summary.style.display = 'block'; |
| 799 | } |
| 800 | } else { |
| 801 | initTestList(); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | // Initialize |
| 806 | initTestList(); |
| 807 | </script> |
| 808 | </body> |
| 809 | </html> |
| 810 | """ |
| 811 | return HTMLResponse(content=html_content) |
| 812 | |
| 813 | @app.websocket("/ws") |
| 814 | async def websocket_endpoint(websocket: WebSocket): |
| 815 | """WebSocket for real-time test updates""" |
| 816 | await websocket.accept() |
| 817 | active_connections.append(websocket) |
| 818 | |
| 819 | try: |
| 820 | while True: |
| 821 | data = await websocket.receive_text() |
| 822 | # Keep connection alive |
| 823 | except: |
| 824 | active_connections.remove(websocket) |
| 825 | |
| 826 | @app.post("/api/run-tests") |
| 827 | async def run_tests(background_tasks: BackgroundTasks): |
| 828 | """Start running tests""" |
| 829 | if test_results["running"]: |
| 830 | return JSONResponse({ |
| 831 | "status": "error", |
| 832 | "message": "Tests already running" |
| 833 | }) |
| 834 | |
| 835 | # Run tests in background |
| 836 | background_tasks.add_task(run_tests_async) |
| 837 | |
| 838 | return JSONResponse({ |
| 839 | "status": "started", |
| 840 | "timestamp": datetime.now().isoformat() |
| 841 | }) |
| 842 | |
| 843 | @app.post("/api/run-selenium-tests") |
| 844 | async def run_selenium_tests(background_tasks: BackgroundTasks): |
| 845 | """Start running Selenium tests""" |
| 846 | if test_results["running"]: |
| 847 | return JSONResponse({ |
| 848 | "status": "error", |
| 849 | "message": "Tests already running" |
| 850 | }) |
| 851 | |
| 852 | # Run Selenium tests in background |
| 853 | background_tasks.add_task(run_selenium_tests_async) |
| 854 | |
| 855 | return JSONResponse({ |
| 856 | "status": "started", |
| 857 | "test_type": "selenium", |
| 858 | "timestamp": datetime.now().isoformat() |
| 859 | }) |
| 860 | |
| 861 | @app.get("/api/status") |
| 862 | async def get_status(): |
| 863 | """Get current test status""" |
| 864 | return JSONResponse(test_results) |
| 865 | |
| 866 | @app.get("/api/benchmark") |
| 867 | async def run_benchmark(): |
| 868 | """Quick benchmark test""" |
| 869 | from dbbasic_task_queue import DBBasicTaskQueue |
| 870 | |
| 871 | queue = DBBasicTaskQueue(db_path=":memory:") |
| 872 | |
| 873 | # Benchmark bulk insert |
| 874 | tasks = [{'task_type': 'bench', 'payload': {'i': i}} for i in range(10000)] |
| 875 | start = time.time() |
| 876 | queue.bulk_add_tasks(tasks) |
| 877 | elapsed = time.time() - start |
| 878 | rate = 10000 / elapsed |
| 879 | |
| 880 | return JSONResponse({ |
| 881 | "tasks_inserted": 10000, |
| 882 | "time_seconds": round(elapsed, 3), |
| 883 | "rate_per_second": round(rate), |
| 884 | "vs_postgresql": f"{round(rate/10000, 1)}x faster" |
| 885 | }) |
| 886 | |
| 887 | if __name__ == "__main__": |
| 888 | import uvicorn |
| 889 | print("\n🚀 DBBasic Test Runner Web Interface") |
| 890 | print(" View tests at: http://localhost:8006") |
| 891 | print(" Run tests and see real-time red/green results!\n") |
| 892 | uvicorn.run(app, host="0.0.0.0", port=8006) |