"""Tests for config detection module."""

from pathlib import Path

from config.detection import INSTRUCTION_PATTERNS, detect_config


class TestConfigDetection:
    """Test suite for config detection."""

    def test_no_instruction_file_returns_default(self, tmp_path: Path) -> None:
        """Empty directory should return default config."""
        result = detect_config(tmp_path)
        assert result.config_source == "default"
        assert result.config_hash == ""
        assert result.config_files == []
        assert result.captured_at is not None

    def test_claude_md_detected_as_custom(self, tmp_path: Path) -> None:
        """CLAUDE.md should trigger custom detection."""
        (tmp_path / "CLAUDE.md").write_text("# Test instructions", encoding="utf-8")
        result = detect_config(tmp_path)
        assert result.config_source == "custom"
        assert len(result.config_hash) == 12
        assert result.config_files == ["CLAUDE.md"]

    def test_multiple_files_combined_hash(self, tmp_path: Path) -> None:
        """Multiple instruction files should all be detected and hashed together."""
        (tmp_path / "CLAUDE.md").write_text("Claude instructions", encoding="utf-8")
        (tmp_path / ".github").mkdir()
        (tmp_path / ".github/copilot-instructions.md").write_text(
            "Copilot instructions",
            encoding="utf-8",
        )

        result = detect_config(tmp_path)
        assert result.config_source == "custom"
        assert len(result.config_files) == 2
        assert ".github/copilot-instructions.md" in result.config_files
        assert "CLAUDE.md" in result.config_files

    def test_hash_changes_with_content(self, tmp_path: Path) -> None:
        """Hash should change when file content changes."""
        (tmp_path / "CLAUDE.md").write_text("Version 1", encoding="utf-8")
        result1 = detect_config(tmp_path)

        (tmp_path / "CLAUDE.md").write_text("Version 2", encoding="utf-8")
        result2 = detect_config(tmp_path)

        assert result1.config_hash != result2.config_hash

    def test_each_supported_file_detected(self, tmp_path: Path) -> None:
        """Each supported instruction file pattern should be detected."""
        for pattern in INSTRUCTION_PATTERNS:
            for existing in INSTRUCTION_PATTERNS:
                existing_path = tmp_path / existing
                if existing_path.exists():
                    existing_path.unlink()
                if existing_path.parent != tmp_path and existing_path.parent.exists():
                    existing_path.parent.rmdir()

            file_path = tmp_path / pattern
            file_path.parent.mkdir(parents=True, exist_ok=True)
            file_path.write_text(f"Test content for {pattern}", encoding="utf-8")

            result = detect_config(tmp_path)
            assert result.config_source == "custom", f"Failed for {pattern}"
            assert pattern in result.config_files, f"Failed for {pattern}"

    def test_files_sorted_alphabetically(self, tmp_path: Path) -> None:
        """Config files should be sorted for deterministic hashing."""
        (tmp_path / "GEMINI.md").write_text("Gemini", encoding="utf-8")
        (tmp_path / "AGENTS.md").write_text("Agents", encoding="utf-8")
        (tmp_path / "CLAUDE.md").write_text("Claude", encoding="utf-8")

        result = detect_config(tmp_path)
        assert result.config_files == ["AGENTS.md", "CLAUDE.md", "GEMINI.md"]
