"""Tests to verify host system protection.

These tests verify that the safety fixtures prevent modification of host files.
"""

from __future__ import annotations

from pathlib import Path

import pytest
from electron_angle_patcher.config import Config


class TestHostSystemProtection:
    """Tests to verify host system is protected from integration tests."""

    def test_default_paths_overridden(
        self,
        integration_temp_dir: Path,
    ) -> None:
        """Test that Config default paths point to temp directory."""
        # The protect_host_system fixture should have overridden these
        assert Config.DEFAULT_BRAVE_PATH.is_relative_to(integration_temp_dir)
        assert Config.DEFAULT_LGHUB_PATH.is_relative_to(integration_temp_dir)

    def test_scan_dirs_overridden(
        self,
        integration_temp_dir: Path,
    ) -> None:
        """Test that scan directories are within temp directory."""
        for scan_dir in Config.STANDARD_SCAN_DIRS:
            scan_path = Path(scan_dir).expanduser()
            # All scan dirs should be under temp directory
            assert str(integration_temp_dir) in str(scan_path)

    def test_specific_apps_cleared(
        self,
    ) -> None:
        """Test that SPECIFIC_APPS is empty to prevent scanning real apps."""
        # Should be empty to prevent accessing real user data
        assert len(Config.SPECIFIC_APPS) == 0

    def test_real_paths_not_accessible(
        self,
        integration_temp_dir: Path,
    ) -> None:
        """Test that real system paths are not used."""
        # Real paths should NOT be the defaults
        real_brave = Path("/Applications/Brave Browser.app")
        real_lghub = Path("/Applications/lghub.app")

        assert Config.DEFAULT_BRAVE_PATH != real_brave
        assert Config.DEFAULT_LGHUB_PATH != real_lghub

    def test_temp_dir_structure_created(
        self,
        integration_temp_dir: Path,
    ) -> None:
        """Test that temp directory structure is created."""
        # Check that the fake Application Support directory exists
        fake_app_support = integration_temp_dir / "Library" / "Application Support"
        assert fake_app_support.exists()

        # Check that fake Applications directory exists
        fake_applications = integration_temp_dir / "Applications"
        assert fake_applications.exists()


class TestSubprocessSafety:
    """Tests to verify subprocess mocking prevents real execution."""

    def test_pkill_is_mocked(
        self,
        integration_temp_dir: Path,
        mock_pkill_safe: MagicMock,
    ) -> None:
        """Test that pkill calls are mocked and don't kill real processes."""
        from electron_angle_patcher.utils import kill_app_by_name

        # Try to "kill" a process
        result = kill_app_by_name("SomeApp")

        # Should return False (no processes found) without actually running pkill
        assert result is False

    def test_codesign_is_mocked(
        self,
        mock_codesign_success: MagicMock,
    ) -> None:
        """Test that codesign calls are mocked."""
        import subprocess
        from electron_angle_patcher.utils import resign_application

        # Try to sign an app
        result = resign_application(Path("/tmp/fake.app"))

        # Should succeed without calling real codesign
        assert result is True


class TestFileWriteProtection:
    """Tests to verify file write protection."""

    def test_writing_outside_temp_raises_error(
        self,
        integration_temp_dir: Path,
        tmp_path: Path,
    ) -> None:
        """Test that writing outside temp directory raises error."""
        # This test verifies the track_temp_files fixture
        # Note: This test itself uses tmp_path, so writes to it should succeed
        test_file = tmp_path / "test_write.txt"
        test_file.write_text("test content")

        assert test_file.exists()
        assert test_file.read_text() == "test content"
