"""Tests for the SSE parser in kanoniv.cli.http."""
import pytest

from kanoniv.cli.http import _parse_sse_block


class TestParseSSEBlock:
    def test_event_and_data(self):
        block = "event: delta\ndata: hello world"
        event_type, data = _parse_sse_block(block)
        assert event_type == "delta"
        assert data == "hello world"

    def test_data_only(self):
        block = "data: just data"
        event_type, data = _parse_sse_block(block)
        assert event_type is None
        assert data == "just data"

    def test_event_only(self):
        block = "event: done"
        event_type, data = _parse_sse_block(block)
        assert event_type == "done"
        assert data is None

    def test_empty_block(self):
        event_type, data = _parse_sse_block("")
        assert event_type is None
        assert data is None

    def test_comment_line_ignored(self):
        block = ": this is a comment\nevent: delta\ndata: payload"
        event_type, data = _parse_sse_block(block)
        assert event_type == "delta"
        assert data == "payload"

    def test_multiline_data(self):
        block = "event: text\ndata: line one\ndata: line two\ndata: line three"
        event_type, data = _parse_sse_block(block)
        assert event_type == "text"
        assert data == "line one\nline two\nline three"

    def test_data_with_json_payload(self):
        block = 'event: usage\ndata: {"tokens": 42, "model": "gpt-4"}'
        event_type, data = _parse_sse_block(block)
        assert event_type == "usage"
        assert data == '{"tokens": 42, "model": "gpt-4"}'

    def test_data_strips_exactly_one_space(self):
        # SSE spec: strip exactly one space after "data:", not all whitespace
        block = "data:  two spaces"
        _, data = _parse_sse_block(block)
        assert data == " two spaces"  # One space stripped, one remains

    def test_data_no_space_after_colon(self):
        block = "data:no-space"
        _, data = _parse_sse_block(block)
        assert data == "no-space"

    def test_event_with_extra_whitespace(self):
        block = "event:   conversation_id  "
        event_type, _ = _parse_sse_block(block)
        assert event_type == "conversation_id"

    def test_unknown_field_ignored(self):
        block = "id: 12345\nevent: delta\ndata: content\nretry: 1000"
        event_type, data = _parse_sse_block(block)
        assert event_type == "delta"
        assert data == "content"

    def test_data_with_colon_in_value(self):
        block = "data: key: value: nested"
        _, data = _parse_sse_block(block)
        assert data == "key: value: nested"

    def test_error_event(self):
        block = "event: error\ndata: Rate limit exceeded"
        event_type, data = _parse_sse_block(block)
        assert event_type == "error"
        assert data == "Rate limit exceeded"

    def test_conversation_id_event(self):
        block = "event: conversation_id\ndata: conv-abc-123"
        event_type, data = _parse_sse_block(block)
        assert event_type == "conversation_id"
        assert data == "conv-abc-123"

    def test_done_event_no_data(self):
        block = "event: done"
        event_type, data = _parse_sse_block(block)
        assert event_type == "done"
        assert data is None

    def test_empty_data_line(self):
        block = "data: "
        _, data = _parse_sse_block(block)
        assert data == ""

    def test_data_colon_only(self):
        # "data:" with nothing after it
        block = "data:"
        _, data = _parse_sse_block(block)
        assert data == ""
