ai_econsult_demo / pages /98_Test_Dashboard.py
Cardiosense-AG's picture
Create 98_Test_Dashboard.py
abe09ac verified
# pages/98_Test_Dashboard.py
from __future__ import annotations
"""Streamlit Test Dashboard for AI E-Consult V2.
Runs the internal smoke and integration tests (tests/smoke_v2.py, tests/integration_v2.py)
and displays their console output live within the UI.
"""
import io
import sys
import contextlib
import runpy
from pathlib import Path
import streamlit as st
st.title("πŸ§ͺ Test Dashboard β€” AI E-Consult V2")
st.caption("Run smoke and integration tests directly within the Hugging Face Space.")
tests_dir = Path(__file__).resolve().parents[1] / "tests"
smoke_path = tests_dir / "smoke_v2.py"
integ_path = tests_dir / "integration_v2.py"
if not smoke_path.exists() or not integ_path.exists():
st.error("❌ Test files not found. Please ensure tests/smoke_v2.py and tests/integration_v2.py exist.")
st.stop()
st.markdown("**Available tests:**")
st.markdown("- `smoke_v2.py` β€” basic schema and module checks")
st.markdown("- `integration_v2.py` β€” module wiring and export determinism")
run_btn = st.button("▢️ Run All Tests", use_container_width=True)
output_buf = io.StringIO()
def _run_script(path: Path):
print(f"\n=== Running {path.name} ===")
try:
runpy.run_path(str(path), run_name="__main__")
except SystemExit as e:
print(f"Exited with code {getattr(e, 'code', '?')}")
except Exception as e:
print(f"⚠️ Error running {path.name}: {e}")
if run_btn:
with contextlib.redirect_stdout(output_buf), contextlib.redirect_stderr(output_buf):
print("πŸ” Starting internal test suite...\n")
_run_script(smoke_path)
_run_script(integ_path)
print("\nβœ… Test run completed.")
st.success("Test run completed.")
st.markdown("**Console Output:**")
st.code(output_buf.getvalue(), language="bash")
# Offer download
st.download_button(
"πŸ’Ύ Download Results",
data=output_buf.getvalue(),
file_name="test_results_v2.txt",
mime="text/plain",
use_container_width=True,
)
else:
st.info("Click 'Run All Tests' to execute internal test scripts and view results here.")