"""Tests for JavaScript prompt loader."""

import pytest

from core.languages.python.optimizer.prompts import get_available_languages, get_system_prompt, get_user_prompt


class TestPromptLoader:
    """Tests for the prompt loader module."""

    def test_get_javascript_system_prompt(self) -> None:
        """Test loading JavaScript system prompt."""
        prompt = get_system_prompt("javascript", is_async=False)

        assert isinstance(prompt, str)
        assert len(prompt) > 0
        assert "JavaScript" in prompt or "javascript" in prompt.lower()
        assert "{language_version}" in prompt  # Should have placeholder
        assert "Behavioral Preservation" in prompt
        assert "Optimization" in prompt

    def test_get_javascript_async_system_prompt(self) -> None:
        """Test loading JavaScript async system prompt."""
        prompt = get_system_prompt("javascript", is_async=True)

        assert isinstance(prompt, str)
        assert len(prompt) > 0
        assert "async" in prompt.lower()
        assert "await" in prompt.lower()
        assert "Promise" in prompt

    def test_get_javascript_user_prompt(self) -> None:
        """Test loading JavaScript user prompt."""
        prompt = get_user_prompt("javascript", is_async=False)

        assert isinstance(prompt, str)
        assert len(prompt) > 0
        assert "{source_code}" in prompt  # Should have placeholder

    def test_get_javascript_async_user_prompt(self) -> None:
        """Test loading JavaScript async user prompt."""
        prompt = get_user_prompt("javascript", is_async=True)

        assert isinstance(prompt, str)
        assert len(prompt) > 0
        assert "async" in prompt.lower()

    def test_typescript_uses_javascript_prompts(self) -> None:
        """Test that TypeScript uses the same prompts as JavaScript."""
        js_prompt = get_system_prompt("javascript", is_async=False)
        ts_prompt = get_system_prompt("typescript", is_async=False)

        assert js_prompt == ts_prompt

    def test_invalid_language_raises_error(self) -> None:
        """Test that invalid language raises ValueError."""
        with pytest.raises(ValueError) as exc_info:
            get_system_prompt("rust", is_async=False)

        assert "rust" in str(exc_info.value).lower()

    def test_get_available_languages(self) -> None:
        """Test getting list of available languages."""
        languages = get_available_languages()

        assert isinstance(languages, list)
        assert "javascript" in languages
        assert "python" in languages

    def test_python_prompts_still_exist(self) -> None:
        """Test that Python prompts still work after adding JavaScript."""
        prompt = get_system_prompt("python", is_async=False)

        assert isinstance(prompt, str)
        assert len(prompt) > 0
        # Python prompts should reference Python-specific things
        assert "Python" in prompt or "python" in prompt.lower()

    def test_python_async_prompts_exist(self) -> None:
        """Test that Python async prompts still work."""
        prompt = get_system_prompt("python", is_async=True)

        assert isinstance(prompt, str)
        assert "async" in prompt.lower()


class TestPromptContent:
    """Tests for specific content in the prompts."""

    def test_javascript_prompt_has_v8_optimization_hints(self) -> None:
        """Test that JavaScript prompt includes V8 optimization guidance."""
        prompt = get_system_prompt("javascript", is_async=False)

        # Should mention V8-specific optimizations
        assert "V8" in prompt or "TypedArray" in prompt or "Map/Set" in prompt

    def test_javascript_prompt_has_response_format(self) -> None:
        """Test that JavaScript prompt specifies response format."""
        prompt = get_system_prompt("javascript", is_async=False)

        assert "Response Format" in prompt or "code block" in prompt.lower()
        assert "```javascript" in prompt or "```js" in prompt.lower()

    def test_javascript_prompt_preserves_behavior(self) -> None:
        """Test that JavaScript prompt emphasizes behavioral preservation."""
        prompt = get_system_prompt("javascript", is_async=False)

        assert "Behavioral Preservation" in prompt or "behavior" in prompt.lower()
        assert "MUST NOT change" in prompt or "must remain" in prompt.lower()

    def test_async_prompt_mentions_promise_all(self) -> None:
        """Test that async prompt mentions Promise.all for concurrency."""
        prompt = get_system_prompt("javascript", is_async=True)

        assert "Promise.all" in prompt

    def test_async_prompt_warns_about_sequential_awaits(self) -> None:
        """Test that async prompt addresses sequential vs parallel awaits."""
        prompt = get_system_prompt("javascript", is_async=True)

        # Should mention concurrent execution patterns
        assert "concurrent" in prompt.lower() or "parallel" in prompt.lower()
