# 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.")