"""Unit tests for LocalStateScanner class."""

from pathlib import Path

from electron_angle_patcher import AppInfo, LocalStateScanner


class TestLocalStateScanner:
    """Tests for LocalStateScanner class."""

    def test_init(self) -> None:
        """Test scanner initialization."""
        scanner = LocalStateScanner()
        assert scanner._skip_patterns == frozenset()

    def test_init_with_skip_patterns(self) -> None:
        """Test scanner with skip patterns."""
        patterns = frozenset(["test", "example"])
        scanner = LocalStateScanner(skip_patterns=patterns)
        assert scanner._skip_patterns == patterns

    def test_should_skip_hidden_directories(self, temp_dir: Path) -> None:
        """Test skipping hidden directories."""
        scanner = LocalStateScanner()
        assert scanner._should_skip_directory(temp_dir, ".hidden") is True
        assert scanner._should_skip_directory(temp_dir, "visible") is False

    def test_scan_finds_local_state(self, temp_dir: Path) -> None:
        """Test scanning finds Local State files."""
        # Create a Local State file
        app_dir = temp_dir / "Library/Application Support/TestApp"
        app_dir.mkdir(parents=True)
        (app_dir / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        assert len(results) > 0
        assert any(app.name == "TestApp" for app in results)

    def test_scan_skips_hidden(self, temp_dir: Path) -> None:
        """Test scanning skips hidden directories."""
        # Create Local State in hidden directory
        hidden_dir = temp_dir / ".hidden"
        hidden_dir.mkdir()
        (hidden_dir / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        # Should not find the hidden one
        assert not any(".hidden" in str(app.state_file) for app in results)

    def test_scan_with_skip_patterns(self, temp_dir: Path) -> None:
        """Test scanning respects skip patterns."""
        # Create Local State files
        test_app = temp_dir / "TestApp"
        test_app.mkdir()
        (test_app / "Local State").write_text('{"browser": {}}')

        lghub_dir = temp_dir / "lghub"
        lghub_dir.mkdir()
        (lghub_dir / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner(skip_patterns=frozenset(["lghub"]))
        results = scanner.scan(temp_dir)

        # Should find TestApp but not lghub
        assert any(app.name == "TestApp" for app in results)
        assert not any("lghub" in str(app.state_file).lower() for app in results)

    def test_scan_empty_directory(self, temp_dir: Path) -> None:
        """Test scanning empty directory returns empty list."""
        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)
        assert results == []

    def test_scan_recursive(self, temp_dir: Path) -> None:
        """Test scanning is recursive."""
        # Create nested structure
        nested = temp_dir / "level1/level2/TestApp"
        nested.mkdir(parents=True)
        (nested / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        assert len(results) > 0

    def test_extract_app_name_from_generic_folder(self, temp_dir: Path) -> None:
        """Test app name extraction skips generic folders."""
        # Create EBWebView structure (common pattern)
        app_dir = temp_dir / "Library/Application Support/MyApp/EBWebView"
        app_dir.mkdir(parents=True)
        (app_dir / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        # Should extract MyApp, not EBWebView
        assert any(app.name == "MyApp" for app in results)

    def test_scan_returns_appinfo_objects(self, temp_dir: Path) -> None:
        """Test scanning returns AppInfo objects."""
        app_dir = temp_dir / "TestApp"
        app_dir.mkdir()
        (app_dir / "Local State").write_text('{"browser": {}}')

        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        assert all(isinstance(app, AppInfo) for app in results)
        assert all(isinstance(app.state_file, Path) for app in results)
        assert all(isinstance(app.name, str) for app in results)

    def test_scan_with_expanduser(self, temp_dir: Path) -> None:
        """Test scanning handles ~ expansion."""
        app_dir = temp_dir / "TestApp"
        app_dir.mkdir()
        (app_dir / "Local State").write_text('{"browser": {}}')

        # Use ~ in path
        scanner = LocalStateScanner()
        results = scanner.scan(temp_dir)

        assert len(results) > 0
