File size: 10,265 Bytes
94cb318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Master Unified Test Runner for CF-Intelligence.

Executes all test suites across the repository:
1. Frontend Suite (Vitest: 87 test files, 375 component/integration tests)
2. Frontend Visual, Responsive & A11y Suite (Playwright: 45 tests across 18 spec files)
3. Backend Suite (Pytest: 5,371 unit, integration, chaos, and property-based tests)
4. Scientific Verification Suite (Pytest: 410 tests across all 21 modules)
5. Python SDK Suite (Pytest: 11 tests)
6. EVM Smart Contracts Suite (Hardhat: 31 tests for Shapley token settlements)

Usage:
    python scripts/run_all_tests.py              # Run frontend + backend + verification
    python scripts/run_all_tests.py --frontend   # Run frontend unit & e2e tests only
    python scripts/run_all_tests.py --visual     # Run Playwright visual regression tests only
    python scripts/run_all_tests.py --backend    # Run backend tests only
    python scripts/run_all_tests.py --verification # Run scientific verification only
    python scripts/run_all_tests.py --contracts  # Run smart contracts only
    python scripts/run_all_tests.py --all        # Run all test suites including visual & contracts
"""

from __future__ import annotations

import argparse
import shutil
import subprocess
import sys
import time
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent


def print_banner(title: str) -> None:
    line = "=" * 78
    print(f"\n{line}\n  {title}\n{line}")


def run_command(cmd: list[str], cwd: Path = REPO_ROOT) -> subprocess.CompletedProcess:
    """Execute command cross-platform by resolving executable via PATH."""
    executable = shutil.which(cmd[0]) or cmd[0]
    return subprocess.run([executable] + cmd[1:], cwd=cwd)


def run_frontend_tests() -> bool:
    print_banner("1. RUNNING FRONTEND TEST SUITE (Vitest: Unit, Integration & E2E)")
    cmd = ["npm", "--prefix", "frontend", "test"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Frontend Test Suite {status} in {duration:.2f}s")
    return success


def run_responsive_tests() -> bool:
    print_banner("2. RUNNING FRONTEND RESPONSIVE & DEVICE TEST SUITE (Playwright)")
    cmd = ["npm", "--prefix", "frontend", "run", "test:responsive"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Frontend Responsive Suite {status} in {duration:.2f}s")
    return success


def run_visual_tests() -> bool:
    print_banner("3. RUNNING FRONTEND VISUAL REGRESSION SUITE (Playwright VRT)")
    cmd = ["npm", "--prefix", "frontend", "run", "test:visual"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Frontend Visual Regression Suite {status} in {duration:.2f}s")
    return success


def run_a11y_tests() -> bool:
    print_banner("4. RUNNING FRONTEND ACCESSIBILITY & KEYBOARD E2E SUITE (Playwright + Axe-Core)")
    cmd = ["npm", "--prefix", "frontend", "run", "test:a11y"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Frontend Accessibility Suite {status} in {duration:.2f}s")
    return success


def run_backend_tests() -> bool:
    print_banner("5. RUNNING BACKEND TEST SUITE (Pytest)")
    cmd = [sys.executable, "-m", "pytest", "backend/tests/", "-v"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Backend Test Suite {status} in {duration:.2f}s")
    return success


def run_api_contract_tests() -> bool:
    print_banner("6. RUNNING END-TO-END API CONTRACT SUITE (Frontend <-> OpenAPI <-> Backend)")
    be_cmd = [sys.executable, "-m", "pytest", "backend/tests/contract/", "-v"]
    fe_cmd = ["npm", "--prefix", "frontend", "test", "--", "api-contract.test.ts"]
    start = time.perf_counter()
    be_res = run_command(be_cmd)
    fe_res = run_command(fe_cmd)
    duration = time.perf_counter() - start
    success = (be_res.returncode == 0) and (fe_res.returncode == 0)
    status = "PASSED" if success else "FAILED"
    print(f"\n>> API Contract Test Suite {status} in {duration:.2f}s")
    return success


def run_verification_tests() -> bool:
    print_banner("7. RUNNING SCIENTIFIC VERIFICATION SUITE")
    cmd = [sys.executable, "-m", "pytest", "verification/", "-v"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Scientific Verification Suite {status} in {duration:.2f}s")
    return success


def run_contract_tests() -> bool:
    print_banner("8. RUNNING EVM SMART CONTRACTS TEST SUITE (Hardhat)")
    cmd = ["npm", "--prefix", "contracts", "test"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Smart Contracts Suite {status} in {duration:.2f}s")
    return success


def run_mutation_tests() -> bool:
    print_banner("9. RUNNING MUTATION TESTING & FAULT INJECTION SUITE (Stryker + Python)")
    cmd = [sys.executable, "scripts/run_mutation_tests.py"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Mutation Testing Suite {status} in {duration:.2f}s")
    return success


def run_coverage_audit() -> bool:
    print_banner("10. RUNNING MULTI-DIMENSIONAL CODE & BRANCH COVERAGE AUDIT")
    cmd = [sys.executable, "scripts/run_coverage_audit.py"]
    start = time.perf_counter()
    res = run_command(cmd)
    duration = time.perf_counter() - start
    success = res.returncode == 0
    status = "PASSED" if success else "FAILED"
    print(f"\n>> Code & Branch Coverage Audit {status} in {duration:.2f}s")
    return success


def main() -> int:
    parser = argparse.ArgumentParser(description="Master Unified Test Runner for CF-Intelligence")
    parser.add_argument("--frontend", action="store_true", help="Run frontend unit & E2E tests only")
    parser.add_argument("--responsive", action="store_true", help="Run frontend responsive & multi-device tests only")
    parser.add_argument("--visual", action="store_true", help="Run frontend visual regression tests only")
    parser.add_argument("--a11y", action="store_true", help="Run frontend accessibility & keyboard E2E tests only")
    parser.add_argument("--backend", action="store_true", help="Run backend tests only")
    parser.add_argument("--api-contract", action="store_true", help="Run frontend <-> backend API contract tests only")
    parser.add_argument("--mutation", action="store_true", help="Run frontend & backend mutation testing suites only")
    parser.add_argument("--coverage", action="store_true", help="Run 4-tier (statements, branches, functions, lines) coverage audit only")
    parser.add_argument("--verification", action="store_true", help="Run verification tests only")
    parser.add_argument("--contracts", action="store_true", help="Run smart contract tests only")
    parser.add_argument("--all", action="store_true", help="Run all test suites including responsive, visual, a11y, contracts, API contracts, mutation testing & coverage")

    args = parser.parse_args()

    # Default: run frontend and backend if no specific flags
    is_default = (
        not args.frontend
        and not args.responsive
        and not args.visual
        and not args.a11y
        and not args.backend
        and not args.api_contract
        and not args.mutation
        and not args.coverage
        and not args.verification
        and not args.contracts
        and not args.all
    )
    run_fe = args.frontend or args.all or is_default
    run_resp = args.responsive or args.all
    run_vrt = args.visual or args.all
    run_a11y = args.a11y or args.all
    run_be = args.backend or args.all or is_default
    run_api_ct = args.api_contract or args.all
    run_mut = args.mutation or args.all
    run_cov = args.coverage or args.all
    run_ver = args.verification or args.all
    run_ct = args.contracts or args.all

    results: dict[str, bool] = {}
    overall_start = time.perf_counter()

    if run_fe:
        results["Frontend (Vitest)"] = run_frontend_tests()
    if run_resp:
        results["Frontend (Responsive)"] = run_responsive_tests()
    if run_vrt:
        results["Frontend (Visual VRT)"] = run_visual_tests()
    if run_a11y:
        results["Frontend (Accessibility a11y)"] = run_a11y_tests()
    if run_be:
        results["Backend (Pytest)"] = run_backend_tests()
    if run_api_ct:
        results["API Contracts (FE <-> BE)"] = run_api_contract_tests()
    if run_mut:
        results["Mutation Testing (Stryker/Py)"] = run_mutation_tests()
    if run_cov:
        results["Coverage Audit (4-Tier)"] = run_coverage_audit()
    if run_ver:
        results["Verification (Audit)"] = run_verification_tests()
    if run_ct:
        results["Smart Contracts (Hardhat)"] = run_contract_tests()

    total_duration = time.perf_counter() - overall_start

    print_banner("TEST EXECUTION SUMMARY REPORT")
    all_passed = True
    for suite, passed in results.items():
        tag = "[PASS]" if passed else "[FAIL]"
        verdict = "PASSED" if passed else "FAILED"
        if not passed:
            all_passed = False
        print(f"  {tag:<8} {suite:<30}: {verdict}")

    print(f"\nTotal Execution Time: {total_duration:.2f}s")
    if all_passed:
        print("\nALL TEST SUITES PASSED CLEANLY (100% SUCCESS)!\n")
        return 0
    else:
        print("\nSOME TEST SUITES REPORTED FAILURES.\n")
        return 1


if __name__ == "__main__":
    sys.exit(main())