# HAD Digital MVP - Test Infrastructure Documentation (`TEST_INFRA.md`)

## 1. Overview & Architecture

The **HAD Digital MVP** test infrastructure provides an end-to-end (E2E) verification framework for the Hospitalisation à Domicile (HAD) oncology monitoring platform. It is engineered with strict test isolation, zero external test dependencies for standalone verification, and a multi-tiered test suite.

```
+---------------------------------------------------------------------------------+
|                            HAD Digital Test Architecture                        |
+---------------------------------------------------------------------------------+
|  Programmatic Verification Script (05_Test/verify_mvp.py)                       |
|  - Zero external dependencies: pure Python standard library                     |
|  - Dual-mode target: Python source (app.py) or standalone executable (.exe)     |
|  - Autonomous ephemeral port allocation + isolated temp SQLite DB               |
|  - Validates full acceptance criteria from ORIGINAL_REQUEST.md                 |
+---------------------------------------------------------------------------------+
|  Multi-Tier Pytest E2E Suite (05_Test/)                                         |
|  ├── Tier 1: Feature Coverage (test_e2e_tier1.py) - 37 tests                    |
|  │   └── Auth, Reporting, CTCAE Grading, Tiered Alerts, Timeline, Roster       |
|  ├── Tier 2: Boundary Value Analysis (test_e2e_tier2.py) - 30 tests             |
|  │   └── CTCAE Cutoffs (0-11), Missing Payloads, 5-Fail Lockout, Unicode 10KB  |
|  ├── Tier 3: Cross-Feature & RBAC (test_e2e_tier3.py) - 9 tests                 |
|  │   └── Complete Event Chain, Multi-Symptom Stream, Patient Isolation, Matrix  |
|  ├── Tier 4: Clinical Workflows (test_e2e_tier4.py) - 4 tests                   |
|  │   └── Febrile Neutropenia Emergency, Home Nurse Visit, Chemo Cycles          |
|  └── Tier 5: Adversarial Hardening (test_e2e_tier5_adversarial.py) - 14 tests   |
|      └── Path Traversal, Brute Force, SQL Injection, XSS, WAL Concurrency       |
+---------------------------------------------------------------------------------+
|  Shared Test Harness (05_Test/conftest.py & pytest.ini)                         |
|  - Session & function scoped ephemeral servers (ThreadingHTTPServer)            |
|  - Temporary SQLite databases outside sys._MEIPASS with WAL mode                |
|  - Pre-authenticated role fixtures (Patient, Oncologist, HAD Nurse, Admin)      |
+---------------------------------------------------------------------------------+
```

---

## 2. Test File Inventory

All test files are strictly located in `05_Test/`:

| Path | Purpose | Test Count | Dependencies |
|------|---------|:----------:|--------------|
| `05_Test/verify_mvp.py` | Programmatic acceptance verification script | 8 steps (10 assertions) | Pure Python standard library (zero external packages) |
| `05_Test/pytest.ini` | Pytest configuration and custom tier markers | N/A | pytest |
| `05_Test/conftest.py` | Fixtures: ephemeral servers, temp DBs, role sessions | N/A | pytest, requests, sqlite3 |
| `05_Test/test_e2e_tier1.py` | Tier 1: Core Feature Coverage (>=5 tests per feature) | 37 tests | pytest, requests |
| `05_Test/test_e2e_tier2.py` | Tier 2: Boundary Value Analysis & Extreme Inputs | 30 tests | pytest, requests |
| `05_Test/test_e2e_tier3.py` | Tier 3: Cross-Feature Combinations & RBAC Matrix | 9 tests | pytest, requests |
| `05_Test/test_e2e_tier4.py` | Tier 4: Real-World Clinical Oncology Scenarios | 4 scenarios | pytest, requests |
| `05_Test/test_e2e_tier5_adversarial.py` | Tier 5: Security Hardening & Concurrency Stress | 14 tests | pytest, requests |
| **Total Automated Tests** | | **94 tests** | |

---

## 3. How to Execute Tests

### 3.1 Zero-Dependency Programmatic Acceptance Verification
This script requires **no external packages** and runs on any standard Python 3.10+ runtime.

```powershell
# Verify Python source implementation:
python 05_Test/verify_mvp.py --source

# Verify standalone Windows executable:
python 05_Test/verify_mvp.py --exe
# Or with explicit path:
python 05_Test/verify_mvp.py --exe-path "dist/HAD Digital.exe"
```

### 3.2 Comprehensive Pytest Suite
Run the entire 94-test multi-tier suite:

```powershell
# Run all tiers:
pytest 05_Test/

# Run with verbose output and short tracebacks:
pytest 05_Test/ -v --tb=short

# Run specific tiers by marker:
pytest 05_Test/ -m tier1
pytest 05_Test/ -m tier2
pytest 05_Test/ -m tier3
pytest 05_Test/ -m tier4
pytest 05_Test/ -m tier5

# Run a single test file:
pytest 05_Test/test_e2e_tier1.py
```

---

## 4. Test Isolation & Reliability Guarantees

1. **Port Isolation**: Every test server instance binds to `127.0.0.1:0` to acquire a cryptographically random, collision-free ephemeral port. Multiple test runs never encounter `EADDRINUSE`.
2. **Database Isolation**: Each test run generates a temporary directory (`tempfile.mkdtemp(prefix="had_pytest_")`) containing an isolated `test_had.db` initialized with WAL mode. The directory is automatically unlinked upon test completion.
3. **Storage Decoupling**: Database files are placed outside `sys._MEIPASS` using the `HAD_DB_PATH` environment variable override, validating persistent storage behavior.
4. **Clean Process Lifecycle**: Server processes are spawned via `subprocess.Popen` and terminated cleanly via `terminate()` / `kill()` in `finally` blocks, leaving zero zombie processes on the Windows host.
5. **Deterministic Data**: Demo seeding is executed idempotently on empty test databases, guaranteeing consistent baseline state for users and patients across runs.
