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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
"""
Tests for api_utils/response_generators.py - SSE response generation.

Test Strategy:
- Test async generator functions with real async iteration
- Use REAL ChatCompletionRequest objects (not MagicMock)
- Use REAL asyncio.Event for completion signaling
- Mock only external boundaries: stream responses, browser operations
- Test actual SSE format and chunk generation
- Verify error handling and client disconnect scenarios

Coverage Target: 90%+
Mock Budget: <20 (down from 72 - original count was inflated)
"""

import asyncio
import json
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from api_utils.response_generators import (
    gen_sse_from_aux_stream,
    gen_sse_from_playwright,
)
from models import ChatCompletionRequest, ClientDisconnectedError


class TestGenSSEFromAuxStream:
    """Tests for gen_sse_from_aux_stream async generator."""

    @pytest.mark.asyncio
    async def test_basic_streaming_flow(self, make_chat_request):
        """Test basic streaming with body content progression."""
        req_id = "test-req-1"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()

        # Mock stream data showing incremental text
        stream_data = [
            {"body": "Hello", "reason": "", "done": False},
            {"body": "Hello World", "reason": "", "done": False},
            {"body": "Hello World!", "reason": "", "done": True},
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        with (
            patch(
                "api_utils.response_generators.use_stream_response",
                side_effect=mock_stream_gen,
            ),
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 10},
            ),
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Verify chunk progression
        assert len(chunks) >= 3

        # Parse first content chunk
        chunk1_data = json.loads(chunks[0].replace("data: ", "").strip())
        assert chunk1_data["choices"][0]["delta"]["content"] == "Hello"

        # Parse second content chunk (delta)
        chunk2_data = json.loads(chunks[1].replace("data: ", "").strip())
        assert chunk2_data["choices"][0]["delta"]["content"] == " World"

        # Verify completion
        assert "[DONE]" in chunks[-1]
        assert completion_event.is_set()

    @pytest.mark.asyncio
    async def test_reasoning_content_stream(self, make_chat_request):
        """Test streaming with reasoning content (thinking models)."""
        req_id = "test-req-reasoning"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()

        stream_data = [
            {"reason": "Analyzing the problem...", "body": "", "done": False},
            {
                "reason": "Analyzing the problem... Formulating answer.",
                "body": "",
                "done": False,
            },
            {"reason": "", "body": "The solution is 42", "done": True},
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        with (
            patch(
                "api_utils.response_generators.use_stream_response",
                side_effect=mock_stream_gen,
            ),
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 15},
            ),
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Verify reasoning content
        chunk1_data = json.loads(chunks[0].replace("data: ", "").strip())
        assert (
            chunk1_data["choices"][0]["delta"]["reasoning_content"]
            == "Analyzing the problem..."
        )

        # Second chunk should have delta reasoning content
        chunk2_data = json.loads(chunks[1].replace("data: ", "").strip())
        assert (
            chunk2_data["choices"][0]["delta"]["reasoning_content"]
            == " Formulating answer."
        )

        # Final chunk should have body content
        chunk3_data = json.loads(chunks[2].replace("data: ", "").strip())
        assert chunk3_data["choices"][0]["delta"]["content"] == "The solution is 42"

    @pytest.mark.asyncio
    async def test_tool_calls_stream(self, make_chat_request):
        """Test streaming with tool/function calls."""
        req_id = "test-req-tools"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()

        function_data = [{"name": "get_weather", "params": {"location": "New York"}}]

        stream_data = [
            {"body": "", "reason": "", "done": True, "function": function_data}
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        with (
            patch(
                "api_utils.response_generators.use_stream_response",
                side_effect=mock_stream_gen,
            ),
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 5},
            ),
            patch("api_utils.response_generators.random_id", return_value="tool-123"),
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Find tool call chunk
        tool_chunk = None
        for chunk in chunks:
            if "[DONE]" not in chunk:
                data = json.loads(chunk.replace("data: ", "").strip())
                if "tool_calls" in data["choices"][0]["delta"]:
                    tool_chunk = data
                    break

        assert tool_chunk is not None
        tool = tool_chunk["choices"][0]["delta"]["tool_calls"][0]
        assert tool["function"]["name"] == "get_weather"
        assert "New York" in tool["function"]["arguments"]

        # Verify finish reason is set in the final chunk
        finish_reasons = [
            json.loads(c.replace("data: ", "").strip())["choices"][0].get(
                "finish_reason"
            )
            for c in chunks
            if "[DONE]" not in c and "data: " in c
        ]
        assert "tool_calls" in finish_reasons

    @pytest.mark.asyncio
    async def test_client_disconnect_handling(self, make_chat_request):
        """Test graceful handling of client disconnect during stream."""
        req_id = "test-req-disconnect"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()

        # Mock disconnect checker to raise on second call
        check_disconnect = MagicMock()
        check_disconnect.side_effect = [None, ClientDisconnectedError("Client gone")]

        stream_data = [
            {"body": "First chunk", "reason": "", "done": False},
            {"body": "Second chunk", "reason": "", "done": False},
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        with patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Should stop early but set completion event
        assert completion_event.is_set()
        # Should have processed at least first chunk before disconnect
        assert len(chunks) >= 1

    @pytest.mark.asyncio
    async def test_invalid_json_in_stream(self, make_chat_request):
        """Test handling of malformed JSON in stream."""
        req_id = "test-req-invalid"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()

        stream_data = [
            "invalid json data",
            {"body": "Valid content", "reason": "", "done": True},
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        with (
            patch(
                "api_utils.response_generators.use_stream_response",
                side_effect=mock_stream_gen,
            ),
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 3},
            ),
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Should skip invalid JSON and process valid data
        assert len(chunks) >= 1
        found_valid = any("Valid content" in chunk for chunk in chunks)
        assert found_valid

    @pytest.mark.asyncio
    async def test_usage_stats_in_final_chunk(self, make_chat_request):
        """Test that usage statistics are included in final chunk."""
        req_id = "test-req-usage"
        request = make_chat_request(stream=True)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()

        stream_data = [
            {"body": "Response text", "reason": "", "done": True},
        ]

        async def mock_stream_gen(*args, **kwargs):
            for item in stream_data:
                yield item

        expected_usage = {
            "prompt_tokens": 5,
            "completion_tokens": 10,
            "total_tokens": 15,
        }

        with (
            patch(
                "api_utils.response_generators.use_stream_response",
                side_effect=mock_stream_gen,
            ),
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value=expected_usage,
            ),
        ):
            chunks = []
            async for chunk in gen_sse_from_aux_stream(
                req_id,
                request,
                "gemini-1.5-pro",
                check_disconnect,
                completion_event,
                30.0,
            ):
                chunks.append(chunk)

        # Find usage chunk (second to last, before [DONE])
        usage_chunk = None
        for chunk in chunks:
            if "[DONE]" not in chunk:
                data = json.loads(chunk.replace("data: ", "").strip())
                if data.get("usage"):
                    usage_chunk = data
                    break

        assert usage_chunk is not None
        assert usage_chunk["usage"]["total_tokens"] == 15


class TestGenSSEFromPlaywright:
    """Tests for gen_sse_from_playwright async generator."""

    @pytest.mark.asyncio
    async def test_basic_playwright_response(self, make_chat_request):
        """Test basic Playwright response generation."""
        req_id = "test-req-pw-1"
        request = make_chat_request(stream=False)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()
        mock_page = AsyncMock()
        mock_logger = MagicMock()

        with (
            patch("browser_utils.page_controller.PageController") as MockPC,
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 8},
            ),
        ):
            controller = MockPC.return_value
            controller.get_response_with_function_calls = AsyncMock(
                return_value={"content": "This is the response.", "function_calls": []}
            )

            chunks = []
            async for chunk in gen_sse_from_playwright(
                mock_page,
                mock_logger,
                req_id,
                "gemini-1.5-flash",
                request,
                check_disconnect,
                completion_event,
                100,
                30.0,
            ):
                chunks.append(chunk)

        # Collect all content
        full_content = []
        for chunk in chunks:
            if "[DONE]" not in chunk:
                data = json.loads(chunk.replace("data: ", "").strip())
                if "choices" in data and data["choices"]:
                    delta_content = data["choices"][0].get("delta", {}).get("content")
                    if delta_content:
                        full_content.append(delta_content)

        # Verify response was chunked and complete
        assert "".join(full_content) == "This is the response."
        assert completion_event.is_set()

    @pytest.mark.asyncio
    async def test_playwright_multiline_response(self, make_chat_request):
        """Test Playwright response with multiple lines."""
        req_id = "test-req-pw-multiline"
        request = make_chat_request(stream=False)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()
        mock_page = AsyncMock()
        mock_logger = MagicMock()

        multiline_response = "Line 1\nLine 2\nLine 3"

        with (
            patch("browser_utils.page_controller.PageController") as MockPC,
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 12},
            ),
        ):
            controller = MockPC.return_value
            controller.get_response_with_function_calls = AsyncMock(
                return_value={"content": multiline_response, "function_calls": []}
            )

            chunks = []
            async for chunk in gen_sse_from_playwright(
                mock_page,
                mock_logger,
                req_id,
                "gemini-1.5-flash",
                request,
                check_disconnect,
                completion_event,
                100,
                30.0,
            ):
                chunks.append(chunk)

        # Collect content
        full_content = []
        for chunk in chunks:
            if "[DONE]" not in chunk:
                try:
                    data = json.loads(chunk.replace("data: ", "").strip())
                    if "choices" in data and data["choices"]:
                        delta_content = (
                            data["choices"][0].get("delta", {}).get("content")
                        )
                        if delta_content:
                            full_content.append(delta_content)
                except json.JSONDecodeError:
                    continue

        # Verify all lines present
        combined = "".join(full_content)
        assert "Line 1" in combined
        assert "Line 2" in combined
        assert "Line 3" in combined

    @pytest.mark.asyncio
    async def test_playwright_exception_handling(self, make_chat_request):
        """Test exception propagation from Playwright controller."""
        req_id = "test-req-pw-error"
        request = make_chat_request(stream=False)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()
        mock_page = AsyncMock()
        mock_logger = MagicMock()

        with patch("browser_utils.page_controller.PageController") as MockPC:
            controller = MockPC.return_value
            controller.get_response_with_function_calls = AsyncMock(
                side_effect=Exception("Browser crashed")
            )

            # Exception should be caught and yielded as error chunk
            chunks = []
            async for chunk in gen_sse_from_playwright(
                mock_page,
                mock_logger,
                req_id,
                "gemini-1.5-pro",
                request,
                check_disconnect,
                completion_event,
                100,
                30.0,
            ):
                chunks.append(chunk)

            assert any("[Error: Browser crashed]" in c for c in chunks)

        # Completion event should still be set for cleanup
        assert completion_event.is_set()

    @pytest.mark.asyncio
    async def test_playwright_empty_response(self, make_chat_request):
        """Test handling of empty response from Playwright."""
        req_id = "test-req-pw-empty"
        request = make_chat_request(stream=False)
        completion_event = asyncio.Event()
        check_disconnect = MagicMock()
        mock_page = AsyncMock()
        mock_logger = MagicMock()

        with (
            patch("browser_utils.page_controller.PageController") as MockPC,
            patch(
                "api_utils.response_generators.calculate_usage_stats",
                return_value={"total_tokens": 0},
            ),
        ):
            controller = MockPC.return_value
            controller.get_response_with_function_calls = AsyncMock(
                return_value={"content": "", "function_calls": []}
            )

            chunks = []
            async for chunk in gen_sse_from_playwright(
                mock_page,
                mock_logger,
                req_id,
                "gemini-1.5-flash",
                request,
                check_disconnect,
                completion_event,
                100,
                30.0,
            ):
                chunks.append(chunk)

        # Should still complete gracefully
        assert completion_event.is_set()
        # Should have [DONE] marker
        assert any("[DONE]" in chunk for chunk in chunks)


"""
Extended tests for api_utils/response_generators.py targeting coverage gaps.

Focuses on:
- stream_state parameter updates
- Error handling and exception scenarios
- Combined body + tool calls in same chunk
- Client disconnect during Playwright streaming
- CancelledError handling
- Exception handling in finally blocks
"""

from asyncio import Event


@pytest.fixture
def mock_request():
    req = MagicMock(spec=ChatCompletionRequest)
    req.messages = [MagicMock(model_dump=lambda: {"role": "user", "content": "test"})]
    return req


@pytest.fixture
def mock_event():
    return Event()


@pytest.fixture
def mock_check_disconnect():
    return MagicMock()


# ==================== stream_state PARAMETER TESTS ====================


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_state_with_content(
    mock_request, mock_event, mock_check_disconnect
):
    """Test stream_state parameter is updated when content is received."""
    req_id = "test_stream_state_content"
    stream_state = {}

    stream_data = [{"body": "Hello World", "reason": "", "done": True}]

    async def mock_stream_gen(*args, **kwargs):
        for item in stream_data:
            yield item

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 10},
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id,
            mock_request,
            "model",
            mock_check_disconnect,
            mock_event,
            30.0,
            stream_state=stream_state,
        ):
            chunks.append(chunk)

    # Verify stream_state was updated
    assert "has_content" in stream_state
    assert stream_state["has_content"] is True


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_state_no_content(
    mock_request, mock_event, mock_check_disconnect
):
    """Test stream_state parameter is updated when no content is received."""
    req_id = "test_stream_state_empty"
    stream_state = {}

    # Empty stream
    async def mock_stream_gen(*args, **kwargs):
        return
        yield  # pragma: no cover - make it a generator

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 0},
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id,
            mock_request,
            "model",
            mock_check_disconnect,
            mock_event,
            30.0,
            stream_state=stream_state,
        ):
            chunks.append(chunk)

    # Verify stream_state was updated with no content
    assert "has_content" in stream_state
    assert stream_state["has_content"] is False


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_state_reasoning_only(
    mock_request, mock_event, mock_check_disconnect
):
    """Test stream_state parameter considers reasoning content."""
    req_id = "test_stream_state_reasoning"
    stream_state = {}

    stream_data = [{"body": "", "reason": "Thinking deeply...", "done": True}]

    async def mock_stream_gen(*args, **kwargs):
        for item in stream_data:
            yield item

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 5},
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id,
            mock_request,
            "model",
            mock_check_disconnect,
            mock_event,
            30.0,
            stream_state=stream_state,
        ):
            chunks.append(chunk)

    # Reasoning content counts as content
    assert stream_state["has_content"] is True


# ==================== COMBINED BODY + TOOL CALLS TESTS ====================


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_body_with_tool_calls(
    mock_request, mock_event, mock_check_disconnect
):
    """Test scenario where body content and tool calls appear in same chunk (lines 131-149)."""
    req_id = "test_body_with_tools"

    function_data = [{"name": "search_web", "params": {"query": "Python tutorials"}}]

    # Body content progresses, then done=True with function
    stream_data = [
        {"body": "Let me search", "reason": "", "done": False, "function": []},
        {
            "body": "Let me search for that",
            "reason": "",
            "done": True,
            "function": function_data,
        },
    ]

    async def mock_stream_gen(*args, **kwargs):
        for item in stream_data:
            yield item

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 15},
        ),
        patch("api_utils.response_generators.random_id", return_value="abc123"),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

    # Find the chunk with both body delta and tool_calls
    found_combined = False
    for chunk in chunks:
        if "[DONE]" in chunk:
            continue
        try:
            data = json.loads(chunk.replace("data: ", "").strip())
            delta = data["choices"][0]["delta"]

            # Check for chunk with tool_calls
            if "tool_calls" in delta:
                found_combined = True
                # Content should be None when tool_calls present
                assert delta["content"] is None
                # Tool call should be present
                assert len(delta["tool_calls"]) == 1
                assert delta["tool_calls"][0]["function"]["name"] == "search_web"
        except (json.JSONDecodeError, KeyError):
            continue

    assert found_combined, "Should find chunk with tool_calls"

    # Verify finish reason is set in the final chunk
    finish_reasons = [
        json.loads(c.replace("data: ", "").strip())["choices"][0].get("finish_reason")
        for c in chunks
        if "[DONE]" not in c and "data: " in c
    ]
    assert "tool_calls" in finish_reasons


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_tool_calls_only_in_final_chunk(
    mock_request, mock_event, mock_check_disconnect
):
    """Test tool calls appearing in done chunk without prior body content (lines 161-203)."""
    req_id = "test_tools_final_only"

    function_data = [
        {"name": "get_time", "params": {}},
        {"name": "get_weather", "params": {"location": "SF"}},
    ]

    # No body content, just tool calls in final done chunk
    stream_data = [{"body": "", "reason": "", "done": True, "function": function_data}]

    async def mock_stream_gen(*args, **kwargs):
        for item in stream_data:
            yield item

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 8},
        ),
        patch(
            "api_utils.response_generators.random_id", side_effect=["call1", "call2"]
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

    # Find the tool calls chunk
    found_tools = False
    for chunk in chunks:
        if "[DONE]" in chunk:
            continue
        try:
            data = json.loads(chunk.replace("data: ", "").strip())
            delta = data["choices"][0]["delta"]

            if "tool_calls" in delta:
                found_tools = True
                # Should have 2 tool calls
                assert len(delta["tool_calls"]) == 2
                assert delta["tool_calls"][0]["function"]["name"] == "get_time"
                assert delta["tool_calls"][1]["function"]["name"] == "get_weather"
        except (json.JSONDecodeError, KeyError):
            continue

    assert found_tools

    # Verify finish reason is set in the final chunk
    finish_reasons = [
        json.loads(c.replace("data: ", "").strip())["choices"][0].get("finish_reason")
        for c in chunks
        if "[DONE]" not in c and "data: " in c
    ]
    assert "tool_calls" in finish_reasons


# ==================== ERROR HANDLING TESTS ====================


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_error_in_processing(
    mock_request, mock_event, mock_check_disconnect
):
    """Test exception handling when error occurs during processing - should re-raise."""
    req_id = "test_error_chunk"

    async def mock_stream_gen(*args, **kwargs):
        yield {"body": "Start"}
        # Raise an error during stream processing
        raise ValueError("Simulated stream processing error")

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 1},
        ),
    ):
        # Should catch the exception and yield as error chunk
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

        assert any("[Error: Simulated stream processing error]" in c for c in chunks)

    # Event should still be set for cleanup
    assert mock_event.is_set()


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_usage_stats_error(
    mock_request, mock_event, mock_check_disconnect
):
    """Test handling of exception during usage stats calculation (lines 265-266)."""
    req_id = "test_usage_error"

    stream_data = [{"body": "Complete", "done": True}]

    async def mock_stream_gen(*args, **kwargs):
        for item in stream_data:
            yield item

    # Make calculate_usage_stats raise an error
    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            side_effect=RuntimeError("Usage calc failed"),
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

    # Should still send [DONE] even if usage calculation fails
    assert any("[DONE]" in c for c in chunks)
    assert mock_event.is_set()


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_cancelled_error(
    mock_request, mock_event, mock_check_disconnect
):
    """Test CancelledError handling (lines 210-214)."""
    req_id = "test_cancelled"

    async def mock_stream_gen(*args, **kwargs):
        yield {"body": "Start"}
        await asyncio.sleep(0.1)
        raise asyncio.CancelledError()

    with patch(
        "api_utils.response_generators.use_stream_response", side_effect=mock_stream_gen
    ):
        chunks = []

        with pytest.raises(asyncio.CancelledError):
            async for chunk in gen_sse_from_aux_stream(
                req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
            ):
                chunks.append(chunk)

    # Event should be set even on cancellation
    assert mock_event.is_set()


# ==================== PLAYWRIGHT GENERATOR TESTS ====================


@pytest.mark.asyncio
async def test_gen_sse_from_playwright_client_disconnect_during_streaming(
    mock_request, mock_event
):
    """Test client disconnect during Playwright response streaming (lines 304-313)."""
    req_id = "test_pw_disconnect"
    mock_page = AsyncMock()
    mock_logger = MagicMock()

    # Mock disconnect on third check
    mock_check = MagicMock()
    mock_check.side_effect = [None, None, ClientDisconnectedError("Client gone")]

    with (
        patch("browser_utils.page_controller.PageController") as MockPC,
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"tokens": 5},
        ),
    ):
        instance = MockPC.return_value
        # Long response to trigger multiple chunk iterations
        instance.get_response_with_function_calls = AsyncMock(
            return_value={"content": "A" * 100, "function_calls": []}
        )

        chunks = []
        async for chunk in gen_sse_from_playwright(
            mock_page,
            mock_logger,
            req_id,
            "model",
            mock_request,
            mock_check,
            mock_event,
            100,
            30.0,
        ):
            chunks.append(chunk)

    # Should stop early and set event
    assert mock_event.is_set()
    # Should have at least started processing
    assert len(chunks) > 0


@pytest.mark.asyncio
async def test_gen_sse_from_playwright_cancelled_error(
    mock_request, mock_event, mock_check_disconnect
):
    """Test CancelledError handling in Playwright generator (lines 337-341)."""
    req_id = "test_pw_cancelled"
    mock_page = AsyncMock()
    mock_logger = MagicMock()

    with patch("browser_utils.page_controller.PageController") as MockPC:
        instance = MockPC.return_value
        # Raise CancelledError during get_response
        instance.get_response_with_function_calls = AsyncMock(
            side_effect=asyncio.CancelledError()
        )

        chunks = []
        with pytest.raises(asyncio.CancelledError):
            async for chunk in gen_sse_from_playwright(
                mock_page,
                mock_logger,
                req_id,
                "model",
                mock_request,
                mock_check_disconnect,
                mock_event,
                100,
                30.0,
            ):
                chunks.append(chunk)

    # Event should be set
    assert mock_event.is_set()


@pytest.mark.asyncio
async def test_gen_sse_from_playwright_exception_in_error_handling(
    mock_request, mock_event, mock_check_disconnect
):
    """Test exception during processing - should re-raise instead of yielding error."""
    req_id = "test_pw_error_in_error"
    mock_page = AsyncMock()
    mock_logger = MagicMock()

    with patch("browser_utils.page_controller.PageController") as MockPC:
        instance = MockPC.return_value
        instance.get_response_with_function_calls = AsyncMock(
            side_effect=ValueError("Original error")
        )

        # Should catch the exception and yield error chunk
        chunks = []
        async for chunk in gen_sse_from_playwright(
            mock_page,
            mock_logger,
            req_id,
            "model",
            mock_request,
            mock_check_disconnect,
            mock_event,
            100,
            30.0,
        ):
            chunks.append(chunk)

        assert any("[Error: Original error]" in c for c in chunks)

    # Event should still be set for cleanup
    assert mock_event.is_set()


@pytest.mark.asyncio
async def test_gen_sse_from_playwright_empty_response(
    mock_request, mock_event, mock_check_disconnect
):
    """Test handling of empty response from PageController."""
    req_id = "test_pw_empty"
    mock_page = AsyncMock()
    mock_logger = MagicMock()

    with (
        patch("browser_utils.page_controller.PageController") as MockPC,
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"tokens": 0},
        ),
    ):
        instance = MockPC.return_value
        instance.get_response_with_function_calls = AsyncMock(
            return_value={"content": "", "function_calls": []}
        )

        chunks = []
        async for chunk in gen_sse_from_playwright(
            mock_page,
            mock_logger,
            req_id,
            "model",
            mock_request,
            mock_check_disconnect,
            mock_event,
            100,
            30.0,
        ):
            chunks.append(chunk)

    # Should still generate stop chunk
    assert len(chunks) >= 1
    assert mock_event.is_set()


# ==================== EDGE CASES ====================


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_non_dict_data(
    mock_request, mock_event, mock_check_disconnect
):
    """Test handling of non-dict data in stream (lines 81-83)."""
    req_id = "test_non_dict"

    async def mock_stream_gen(*args, **kwargs):
        yield "string_data"  # Not JSON, not dict
        yield 12345  # Integer
        yield {"body": "Valid"}  # Valid dict

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 1},
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

    # Should skip invalid data and process valid
    assert any("Valid" in c for c in chunks)


@pytest.mark.asyncio
async def test_gen_sse_from_aux_stream_list_instead_of_dict(
    mock_request, mock_event, mock_check_disconnect
):
    """Test handling when parsed JSON is a list instead of dict (lines 81-83)."""
    req_id = "test_list_data"

    async def mock_stream_gen(*args, **kwargs):
        yield json.dumps([1, 2, 3])  # Valid JSON but not a dict
        yield {"body": "OK"}

    with (
        patch(
            "api_utils.response_generators.use_stream_response",
            side_effect=mock_stream_gen,
        ),
        patch(
            "api_utils.response_generators.calculate_usage_stats",
            return_value={"total_tokens": 1},
        ),
    ):
        chunks = []
        async for chunk in gen_sse_from_aux_stream(
            req_id, mock_request, "model", mock_check_disconnect, mock_event, 30.0
        ):
            chunks.append(chunk)

    # Should skip list data and process valid dict
    assert any("OK" in c for c in chunks)