#!/usr/bin/env python3
"""Test runner for {{ module_name }} module."""

import subprocess
import sys
from pathlib import Path


def run_tests():
    """Run all tests for the {{ module_name }} module."""
    test_dir = Path(__file__).parent
    
    print(f"🧪 Running tests for {{ module_name }} module...")
    print(f"📁 Test directory: {test_dir}")
    print("=" * 50)
    
    # Run pytest with coverage
    cmd = [
        sys.executable, "-m", "pytest",
        str(test_dir),
        "-v",
        "--tb=short",
        "--cov={{ module_name }}",
        "--cov-report=term-missing",
        "--cov-report=html:htmlcov",
        "--cov-report=xml:coverage.xml"
    ]
    
    try:
        result = subprocess.run(cmd, capture_output=False, text=True)
        return result.returncode
    except Exception as e:
        print(f"❌ Error running tests: {e}")
        return 1


def run_specific_test(test_name):
    """Run a specific test by name."""
    test_dir = Path(__file__).parent
    
    print(f"🧪 Running specific test: {test_name}")
    print(f"📁 Test directory: {test_dir}")
    print("=" * 50)
    
    cmd = [
        sys.executable, "-m", "pytest",
        str(test_dir),
        "-v",
        "-k", test_name,
        "--tb=short"
    ]
    
    try:
        result = subprocess.run(cmd, capture_output=False, text=True)
        return result.returncode
    except Exception as e:
        print(f"❌ Error running test: {e}")
        return 1


if __name__ == "__main__":
    if len(sys.argv) > 1:
        # Run specific test
        test_name = sys.argv[1]
        exit_code = run_specific_test(test_name)
    else:
        # Run all tests
        exit_code = run_tests()
    
    sys.exit(exit_code)
