import json

from src.integrations.cursor_cli import _assistant_text_from_stream_json


def test_extracts_cursor_assistant_content_parts() -> None:
    stdout = "\n".join(
        [
            json.dumps(
                {
                    "type": "tool_call",
                    "subtype": "completed",
                    "name": "Shell",
                }
            ),
            json.dumps(
                {
                    "type": "assistant",
                    "message": {
                        "role": "assistant",
                        "content": [
                            {
                                "type": "text",
                                "text": (
                                    "P2HS_USER_RESPONSE:\n"
                                    "Added TOTP as an MFA option on staff login."
                                ),
                            }
                        ],
                    },
                }
            ),
        ]
    )
    text = _assistant_text_from_stream_json(stdout)
    assert "Added TOTP as an MFA option on staff login." in text
    assert "Shell" not in text


def test_prefers_result_event_over_tool_noise() -> None:
    stdout = "\n".join(
        [
            json.dumps(
                {
                    "type": "assistant",
                    "message": {"content": [{"type": "text", "text": "Working..."}]},
                }
            ),
            json.dumps(
                {
                    "type": "result",
                    "result": "Updated the hero banner copy for students applying abroad.",
                }
            ),
        ]
    )
    assert _assistant_text_from_stream_json(stdout) == (
        "Updated the hero banner copy for students applying abroad."
    )


def test_ignores_git_create_mode_tool_events() -> None:
    stdout = json.dumps(
        {
            "type": "tool_call",
            "subtype": "completed",
            "result": "create mode 100644 server/api/jobs/reminder.send.post.ts",
        }
    )
    assert _assistant_text_from_stream_json(stdout) == ""
