"""
Forensic Test Assertion Auditor.
Scans all test files in tests/ for:
1. Tautological assertions: assert True, assert 1 == 1, assert not False, etc.
2. Empty test functions: functions with only 'pass' or docstring.
3. Disabled/commented out assertions.
"""

import ast
import os
import sys

TEST_DIRS = [r"c:\Projects\FreeExile\tests"]

def audit_test_files():
    total_files = 0
    total_test_functions = 0
    tautologies = []
    empty_tests = []

    for test_dir in TEST_DIRS:
        for root, _, files in os.walk(test_dir):
            for fname in files:
                if fname.startswith("test_") and fname.endswith(".py"):
                    total_files += 1
                    fpath = os.path.join(root, fname)
                    relpath = os.path.relpath(fpath, r"c:\Projects\FreeExile")
                    try:
                        with open(fpath, "r", encoding="utf-8") as f:
                            source = f.read()
                        tree = ast.parse(source, filename=fpath)
                    except Exception as e:
                        print(f"Error parsing {fpath}: {e}")
                        continue

                    for node in ast.walk(tree):
                        if isinstance(node, ast.FunctionDef) and node.name.startswith("test_"):
                            total_test_functions += 1
                            # Check empty body
                            body = node.body
                            if len(body) == 1 and isinstance(body[0], ast.Pass):
                                empty_tests.append((relpath, node.name, node.lineno, "only pass"))
                            elif len(body) == 1 and isinstance(body[0], ast.Expr) and isinstance(body[0].value, ast.Constant):
                                empty_tests.append((relpath, node.name, node.lineno, "only docstring"))

                        if isinstance(node, ast.Assert):
                            # Check test expression
                            test_expr = node.test
                            if isinstance(test_expr, ast.Constant):
                                if test_expr.value is True:
                                    tautologies.append((relpath, node.lineno, "assert True"))
                                elif test_expr.value == 1:
                                    tautologies.append((relpath, node.lineno, "assert 1"))
                            elif isinstance(test_expr, ast.Compare):
                                if isinstance(test_expr.left, ast.Constant) and len(test_expr.comparators) == 1 and isinstance(test_expr.comparators[0], ast.Constant):
                                    if test_expr.left.value == test_expr.comparators[0].value:
                                        tautologies.append((relpath, node.lineno, f"assert {test_expr.left.value} == {test_expr.comparators[0].value}"))

    print("\n--- FORENSIC TEST ASSERTION AUDIT REPORT ---")
    print(f"Total Test Files Scanned: {total_files}")
    print(f"Total Test Functions Scanned: {total_test_functions}")
    print(f"Tautological Assertions Found: {len(tautologies)}")
    for t in tautologies:
        print("  TAUTOLOGY:", t)
    print(f"Empty Test Functions Found: {len(empty_tests)}")
    for e in empty_tests:
        print("  EMPTY TEST:", e)

    if tautologies or empty_tests:
        print("\nTEST ASSERTION VERDICT: INTEGRITY VIOLATION DETECTED!")
        sys.exit(1)
    else:
        print("\nTEST ASSERTION VERDICT: CLEAN")

if __name__ == "__main__":
    audit_test_files()
