Spaces:
Sleeping
Sleeping
| """ | |
| Comprehensive Phase 4 UI Test | |
| Tests all UI pages and their API integrations end-to-end | |
| """ | |
| import time | |
| import json | |
| def test_all_features(): | |
| """Test all Phase 4 features""" | |
| import requests | |
| print("=" * 70) | |
| print("[TEST] COMPREHENSIVE PHASE 4 UI TEST") | |
| print("=" * 70) | |
| all_passed = True | |
| # 1. Test Visualization Page API | |
| print("\nTesting 3D Visualization Page APIs...") | |
| print("-" * 50) | |
| try: | |
| # Search API | |
| r = requests.post("http://localhost:8000/api/search", json={ | |
| "query": "kinase inhibitor", | |
| "top_k": 5, | |
| "use_mmr": True | |
| }, timeout=30) | |
| if r.status_code == 200: | |
| results = r.json().get("results", []) | |
| print(f"[OK] Search: {len(results)} results with MMR diversification") | |
| # Check evidence fields | |
| if results: | |
| has_evidence = any('evidence_links' in r or 'source' in r for r in results) | |
| print(f"[OK] Evidence links: {'Present' if has_evidence else 'Not found (may need enrichment)'}") | |
| else: | |
| print(f"[FAIL] Search failed: {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] Search error: {e}") | |
| all_passed = False | |
| # 2. Test Workflow Builder APIs | |
| print("\nTesting Workflow Builder APIs...") | |
| print("-" * 50) | |
| # 2.1 Generate API | |
| try: | |
| r = requests.post("http://localhost:8000/api/agents/generate", json={ | |
| "prompt": "anti-cancer compound", | |
| "mode": "text", | |
| "num_samples": 3 | |
| }, timeout=30) | |
| if r.status_code == 200: | |
| mols = r.json().get("molecules", []) | |
| print(f"[OK] Generate: {len(mols)} molecules from text") | |
| else: | |
| print(f"[FAIL] Generate failed: {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] Generate error: {e}") | |
| all_passed = False | |
| # 2.2 Validate API | |
| try: | |
| r = requests.post("http://localhost:8000/api/agents/validate", json={ | |
| "smiles": ["CCO", "CC(=O)OC1=CC=CC=C1C(=O)O"], | |
| "check_lipinski": True, | |
| "check_admet": True, | |
| "check_alerts": True | |
| }, timeout=30) | |
| if r.status_code == 200: | |
| vals = r.json().get("validations", []) | |
| print(f"[OK] Validate: {len(vals)} SMILES validated") | |
| else: | |
| print(f"[FAIL] Validate failed: {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] Validate error: {e}") | |
| all_passed = False | |
| # 2.3 Rank API | |
| try: | |
| r = requests.post("http://localhost:8000/api/agents/rank", json={ | |
| "candidates": [ | |
| {"smiles": "CCO", "name": "Ethanol", "score": 0.5}, | |
| {"smiles": "CCCCC", "name": "Pentane", "score": 0.6} | |
| ], | |
| "weights": {"qed": 0.5, "validity": 0.5}, | |
| "top_k": 5 | |
| }, timeout=30) | |
| if r.status_code == 200: | |
| ranked = r.json().get("ranked", []) | |
| print(f"[OK] Rank: {len(ranked)} candidates ranked") | |
| else: | |
| print(f"[FAIL] Rank failed: {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] Rank error: {e}") | |
| all_passed = False | |
| # 2.4 Full Workflow API | |
| try: | |
| r = requests.post("http://localhost:8000/api/agents/workflow", json={ | |
| "query": "drug for inflammation", | |
| "num_candidates": 5, | |
| "top_k": 3 | |
| }, timeout=60) | |
| if r.status_code == 200: | |
| data = r.json() | |
| print(f"[OK] Workflow: {data.get('steps_completed', 0)}/{data.get('total_steps', 0)} steps") | |
| print(f" Time: {data.get('execution_time_ms', 0):.1f}ms") | |
| top = data.get('top_candidates', []) | |
| print(f" Top candidates: {len(top)}") | |
| else: | |
| print(f"[FAIL] Workflow failed: {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] Workflow error: {e}") | |
| all_passed = False | |
| # 3. Test Export Functions (simulation) | |
| print("\nTesting Export Functions...") | |
| print("-" * 50) | |
| sample_data = [ | |
| {"id": "1", "content": "EGFR inhibitor", "score": 0.95, "modality": "text"}, | |
| {"id": "2", "content": "MTAAPRGPRL", "score": 0.88, "modality": "protein"}, | |
| {"id": "3", "content": "CCO", "score": 0.75, "modality": "molecule"} | |
| ] | |
| # CSV | |
| try: | |
| csv = "id,content,score,modality\n" + "\n".join( | |
| f'{d["id"]},"{d["content"]}",{d["score"]},{d["modality"]}' for d in sample_data | |
| ) | |
| print(f"[OK] CSV export: {len(csv)} bytes") | |
| except Exception as e: | |
| print(f"[ERROR] CSV error: {e}") | |
| all_passed = False | |
| # JSON | |
| try: | |
| js = json.dumps(sample_data, indent=2) | |
| print(f"[OK] JSON export: {len(js)} bytes") | |
| except Exception as e: | |
| print(f"[ERROR] JSON error: {e}") | |
| all_passed = False | |
| # FASTA | |
| try: | |
| fasta = "\n".join(f">{d['id']}\n{d['content']}" | |
| for d in sample_data if d['modality'] == 'protein') | |
| print(f"[OK] FASTA export: {len(fasta)} bytes (proteins only)") | |
| except Exception as e: | |
| print(f"[ERROR] FASTA error: {e}") | |
| all_passed = False | |
| # 4. Test UI Pages | |
| print("\nTesting UI Pages...") | |
| print("-" * 50) | |
| for page in ["visualization", "workflow"]: | |
| try: | |
| r = requests.get(f"http://localhost:3000/dashboard/{page}", timeout=10) | |
| if r.status_code == 200: | |
| print(f"[OK] /{page}: Renders correctly ({len(r.content)} bytes)") | |
| else: | |
| print(f"[FAIL] /{page}: Failed with {r.status_code}") | |
| all_passed = False | |
| except Exception as e: | |
| print(f"[ERROR] /{page}: {e}") | |
| all_passed = False | |
| # Summary | |
| print("\n" + "=" * 70) | |
| if all_passed: | |
| print("ALL PHASE 4 FEATURES WORKING CORRECTLY!") | |
| print("=" * 70) | |
| print(""" | |
| Phase 4 Deliverables Complete: | |
| [OK] 4.1 3D Visualization - Interactive embedding space explorer | |
| [OK] 4.2 Evidence Panel - Sources, citations, external links | |
| [OK] 4.3 Workflow Builder - Visual pipeline configuration | |
| [OK] 4.4 Export Features - CSV, JSON, FASTA export | |
| UI Pages: | |
| • /dashboard/visualization - 3D embedding explorer | |
| • /dashboard/workflow - Workflow builder | |
| All APIs tested and functional! | |
| """) | |
| else: | |
| print("[WARN] SOME TESTS FAILED - Review output above") | |
| print("=" * 70) | |
| return all_passed | |
| if __name__ == "__main__": | |
| test_all_features() | |