#!/usr/bin/env python3
"""
#exonware/xwsystem/tests/1.unit/codec_tests/runner.py

Test runner for codec module tests.

Company: eXonware.com
Author: Eng. Muhammad AlShehri
Email: connect@exonware.com
Version: 0.0.1
Generation Date: 04-Nov-2025
"""

import sys
import pytest
from pathlib import Path
from datetime import datetime

# 🚨 CRITICAL: Configure UTF-8 encoding for Windows console (GUIDELINES_TEST.md compliance)
if sys.platform == "win32":
    try:
        import io
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
        sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
    except Exception:
        pass


def main():
    """Run codec module tests."""
    # Add src to Python path
    src_path = Path(__file__).parent.parent.parent.parent / "src"
    sys.path.insert(0, str(src_path))
    
    module_name = "codec"
    print(f"🧩 Testing module: {module_name}")
    
    # Run module tests
    exit_code = pytest.main([
        "-v",
        "--tb=short",
        "-x",  # Stop on first failure (GUIDELINES_TEST.md)
        str(Path(__file__).parent),
        "-m", "xsystem_unit"
    ])
    
    # Save Markdown output
    status = "PASSED" if exit_code == 0 else "FAILED"
    output_file = Path(__file__).parent / "runner_out.md"
    
    markdown_content = f"""# Module Test Results: {module_name}

**Module:** {module_name}
**Layer:** 1.unit
**Generated:** {datetime.now().strftime("%d-%b-%Y %H:%M:%S")}
**Status:** {status}

---

## Test Execution

**Command:** `python tests/1.unit/codec_tests/runner.py`

**Markers:** `xsystem_unit`

**Options:**
- Verbose output (`-v`)
- Short traceback (`--tb=short`)
- Stop on first failure (`-x`)

---

## Result

{'✅ PASSED' if exit_code == 0 else '❌ FAILED'}

**Exit Code:** {exit_code}

---

*Generated by eXonware test runner - Excellence Edition*
"""
    
    output_file.write_text(markdown_content, encoding='utf-8')
    print(f"\n📝 Results saved to: {output_file}")
    
    sys.exit(exit_code)


if __name__ == "__main__":
    main()

