#!/usr/bin/env python3
"""Test that OPSEC violations are fixed.

Tests for:
1. sensor.* logger namespaces (should be __name__)
2. Hardcoded "bb" API user (should be generic)
3. Hardcoded "bb" relay_user (should be generic)
"""

import os
import re
import subprocess
from pathlib import Path


def test_no_sensor_logger_fingerprints():
    """Verify that no logging.getLogger(__name__) calls exist."""
    bb_root = Path(__file__).parent.parent

    # Search for sensor.* logger calls in Python files
    result = subprocess.run(
        ["grep", "-r", r'getLogger("sensor\.', str(bb_root), "--include=*.py"],
        capture_output=True,
        text=True,
    )

    # Should find no matches (empty stdout)
    assert result.stdout == "", (
        f"Found sensor.* logger fingerprints:\n{result.stdout}"
    )


def test_no_bb_api_user_hardcoded():
    """Verify that 'bb' is not hardcoded as bettercap API user."""
    bb_root = Path(__file__).parent.parent

    # Check config file
    config_file = bb_root / "config" / "bigbrother.yaml"
    with open(config_file) as f:
        config_content = f.read()

    # Look for api_user: "bb" (should be something else)
    match = re.search(r'api_user:\s*"([^"]+)"', config_content)
    assert match and match.group(1) != "bb", (
        f"Found hardcoded api_user as 'bb' in config. Should be generic."
    )

    # Check Python files for hardcoded "bb" as default user parameter
    result = subprocess.run(
        ["grep", "-r", 'user:\s*str\s*=\s*"bb"', str(bb_root), "--include=*.py"],
        capture_output=True,
        text=True,
    )

    assert result.stdout == "", (
        f"Found hardcoded 'bb' as default user in code:\n{result.stdout}"
    )


def test_no_bb_relay_user_hardcoded():
    """Verify that 'bb' is not hardcoded as relay_user."""
    bb_root = Path(__file__).parent.parent

    # Check config file
    config_file = bb_root / "config" / "bigbrother.yaml"
    with open(config_file) as f:
        config_content = f.read()

    # Look for relay_user: "bb" (should be something else)
    match = re.search(r'relay_user:\s*"([^"]+)"', config_content)
    assert match and match.group(1) != "bb", (
        f"Found hardcoded relay_user as 'bb' in config. Should be generic."
    )


if __name__ == "__main__":
    test_no_sensor_logger_fingerprints()
    test_no_bb_api_user_hardcoded()
    test_no_bb_relay_user_hardcoded()
    print("All OPSEC tests passed!")
