"""
Tests for Jira async commands and write_async_status function.
"""

from unittest.mock import patch

import pytest

from agentic_devtools.cli.jira.async_commands import (
    get_issue_async,
)


def _get_script_from_call(mock_popen):
    """Extract the Python script from the Popen call args."""
    call_args = mock_popen.call_args[0][0]  # First positional arg is the command list
    # Script is the third element: [python, -c, <script>]
    return call_args[2] if len(call_args) > 2 else ""


def _assert_function_in_script(script, module_path, function_name):
    """Assert the script calls the expected function from the expected module."""
    assert f"module_path = '{module_path}'" in script, f"Expected module_path = '{module_path}' in script"
    assert f"function_name = '{function_name}'" in script, f"Expected function_name = '{function_name}' in script"


class TestGetIssueAsync:
    """Tests for get_issue_async command."""

    def test_requires_issue_key(self, mock_background_and_state):
        """Test get_issue_async requires issue_key."""
        with patch("agentic_devtools.cli.jira.async_commands.get_jira_value", return_value=None):
            with pytest.raises(SystemExit):
                get_issue_async()

    def test_spawns_background_task(self, mock_background_and_state, capsys):
        """Test get_issue_async spawns a background task."""
        with patch("agentic_devtools.cli.jira.async_commands.get_jira_value", return_value="PROJECT-456"):
            get_issue_async()

        captured = capsys.readouterr()
        assert "Background task started" in captured.out
        assert "PROJECT-456" in captured.out
