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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
import asyncio
from unittest.mock import AsyncMock, MagicMock, call, patch

import pytest

from browser_utils.page_controller_modules.parameters import ParameterController
from config import (
    MAT_CHIP_REMOVE_BUTTON_SELECTOR,
    STOP_SEQUENCE_INPUT_SELECTOR,
    TEMPERATURE_INPUT_SELECTOR,
    TOP_P_INPUT_SELECTOR,
)
from models import ClientDisconnectedError


@pytest.fixture
def mock_page():
    page = AsyncMock()
    page.locator = MagicMock()
    # Setup default locator behavior to return an AsyncMock that can be awaited/called
    locator_mock = AsyncMock()
    locator_mock.input_value.return_value = "0.5"
    locator_mock.get_attribute.return_value = "false"
    locator_mock.count.return_value = 0
    page.locator.return_value = locator_mock
    return page


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


@pytest.fixture
def controller(mock_page, mock_logger):
    return ParameterController(mock_page, mock_logger, "test_req_id")


@pytest.fixture
def mock_check_disconnect():
    return MagicMock(return_value=False)


@pytest.fixture
def mock_lock():
    return asyncio.Lock()


@pytest.fixture(autouse=True)
def mock_expect_async():
    with patch("browser_utils.page_controller_modules.parameters.expect_async") as mock:
        mock.return_value.to_be_visible = AsyncMock()
        mock.return_value.to_have_class = AsyncMock()
        yield mock


@pytest.fixture(autouse=True)
def mock_save_snapshot():
    with patch(
        "browser_utils.operations.save_error_snapshot", new_callable=AsyncMock
    ) as mock:
        yield mock


@pytest.mark.asyncio
async def test_adjust_temperature_cache_hit(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    page_params_cache = {"temperature": 0.7}

    await controller._adjust_temperature(
        0.7, page_params_cache, mock_lock, mock_check_disconnect
    )

    # Should not interact with page
    mock_page.locator.assert_not_called()
    assert page_params_cache["temperature"] == 0.7


@pytest.mark.asyncio
async def test_adjust_temperature_update_success(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    page_params_cache = {"temperature": 0.5}
    target_temp = 0.8

    # Mock locator interactions
    temp_locator = AsyncMock()
    # First read: 0.5, Second read (after update): 0.8
    temp_locator.input_value.side_effect = ["0.5", "0.8"]
    mock_page.locator.return_value = temp_locator

    await controller._adjust_temperature(
        target_temp, page_params_cache, mock_lock, mock_check_disconnect
    )

    mock_page.locator.assert_called_with(TEMPERATURE_INPUT_SELECTOR)
    temp_locator.fill.assert_called_with(str(target_temp), timeout=5000)
    assert page_params_cache["temperature"] == target_temp


@pytest.mark.asyncio
async def test_adjust_temperature_verify_fail(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    page_params_cache = {}
    target_temp = 0.8

    temp_locator = AsyncMock()
    # First read: 0.5, Second read (after update): 0.5 (update failed)
    temp_locator.input_value.side_effect = ["0.5", "0.5"]
    mock_page.locator.return_value = temp_locator

    await controller._adjust_temperature(
        target_temp, page_params_cache, mock_lock, mock_check_disconnect
    )

    assert "temperature" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_temperature_value_error(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    page_params_cache = {}

    temp_locator = AsyncMock()
    temp_locator.input_value.return_value = "invalid"
    mock_page.locator.return_value = temp_locator

    await controller._adjust_temperature(
        0.5, page_params_cache, mock_lock, mock_check_disconnect
    )

    assert "temperature" not in page_params_cache


@pytest.mark.asyncio
async def test_adjust_max_tokens_from_model_config(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    page_params_cache = {}
    parsed_model_list = [{"id": "model-a", "supported_max_output_tokens": 1024}]

    tokens_locator = AsyncMock()
    tokens_locator.input_value.side_effect = ["512", "1024"]
    mock_page.locator.return_value = tokens_locator

    await controller._adjust_max_tokens(
        2048,  # Requesting more than supported
        page_params_cache,
        mock_lock,
        "model-a",
        parsed_model_list,
        mock_check_disconnect,
    )

    # Should be clamped to 1024
    tokens_locator.fill.assert_called_with("1024", timeout=5000)
    assert page_params_cache["max_output_tokens"] == 1024


@pytest.mark.asyncio
async def test_adjust_max_tokens_verify_fail(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    page_params_cache = {}

    tokens_locator = AsyncMock()
    tokens_locator.input_value.side_effect = ["100", "100"]
    mock_page.locator.return_value = tokens_locator

    await controller._adjust_max_tokens(
        200, page_params_cache, mock_lock, None, [], mock_check_disconnect
    )

    assert "max_output_tokens" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_stop_sequences(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test stop sequence adjustment with removal and addition."""
    page_params_cache = {}
    stop_sequences = ["stop1", "stop2"]

    input_locator = AsyncMock()

    # Mock for specific remove buttons (for removal of old1, old2)
    remove_old1_btn = AsyncMock()
    remove_old1_btn.count = AsyncMock(return_value=1)
    remove_old2_btn = AsyncMock()
    remove_old2_btn.count = AsyncMock(return_value=1)

    def get_locator(selector):
        if selector == STOP_SEQUENCE_INPUT_SELECTOR:
            return input_locator
        elif selector == 'mat-chip button.remove-button[aria-label="Remove old1"]':
            return remove_old1_btn
        elif selector == 'mat-chip button.remove-button[aria-label="Remove old2"]':
            return remove_old2_btn
        # Default for other selectors
        return AsyncMock()

    mock_page.locator.side_effect = get_locator

    # Patch _get_current_stop_sequences to return existing stops first, then final state
    call_count = [0]

    async def mock_get_current():
        call_count[0] += 1
        if call_count[0] == 1:
            # Initial state: has old1 and old2
            return {"old1", "old2"}
        else:
            # After removal and addition: has stop1 and stop2
            return {"stop1", "stop2"}

    with patch.object(controller, "_get_current_stop_sequences", mock_get_current):
        await controller._adjust_stop_sequences(
            stop_sequences, page_params_cache, mock_lock, mock_check_disconnect
        )

    # Should remove existing chips (old1, old2)
    assert remove_old1_btn.first.click.call_count == 1
    assert remove_old2_btn.first.click.call_count == 1

    # Should add new sequences
    assert input_locator.fill.call_count == 2
    input_locator.fill.assert_has_calls(
        [call("stop1", timeout=3000), call("stop2", timeout=3000)], any_order=True
    )
    assert input_locator.press.call_count == 2

    assert page_params_cache["stop_sequences"] == {"stop1", "stop2"}


@pytest.mark.asyncio
async def test_adjust_top_p_update(controller, mock_check_disconnect, mock_page):
    target_top_p = 0.9

    locator = AsyncMock()
    locator.input_value.side_effect = ["0.5", "0.9"]
    mock_page.locator.return_value = locator

    await controller._adjust_top_p(target_top_p, mock_check_disconnect)

    mock_page.locator.assert_called_with(TOP_P_INPUT_SELECTOR)
    locator.fill.assert_called_with(str(target_top_p), timeout=5000)


@pytest.mark.asyncio
async def test_ensure_tools_panel_expanded(
    controller, mock_check_disconnect, mock_page
):
    # Setup: panel is collapsed
    collapse_btn = AsyncMock()
    # locator() is sync, so we need to mock it as MagicMock on the AsyncMock
    collapse_btn.locator = MagicMock()

    grandparent = AsyncMock()
    grandparent.get_attribute.return_value = "some-class"  # not expanded

    collapse_btn.locator.return_value = grandparent
    mock_page.locator.return_value = collapse_btn

    await controller._ensure_tools_panel_expanded(mock_check_disconnect)

    collapse_btn.click.assert_called_once()


@pytest.mark.asyncio
async def test_ensure_tools_panel_already_expanded(
    controller, mock_check_disconnect, mock_page
):
    # Setup: panel is expanded
    collapse_btn = AsyncMock()
    # locator() is sync
    collapse_btn.locator = MagicMock()

    grandparent = AsyncMock()
    grandparent.get_attribute.return_value = "some-class expanded"

    collapse_btn.locator.return_value = grandparent
    mock_page.locator.return_value = collapse_btn

    await controller._ensure_tools_panel_expanded(mock_check_disconnect)

    collapse_btn.click.assert_not_called()


@pytest.mark.asyncio
async def test_open_url_content(controller, mock_check_disconnect, mock_page):
    # Setup: switch is off
    switch = AsyncMock()
    switch.get_attribute.return_value = "false"
    mock_page.locator.return_value = switch

    await controller._open_url_content(mock_check_disconnect)

    switch.click.assert_called_once()


@pytest.mark.asyncio
async def test_should_enable_google_search(controller):
    # Case 1: No tools -> Default (True/False based on config, assuming True for test if config not mocked, but config is imported)
    # We need to check what ENABLE_GOOGLE_SEARCH is in config.
    # In parameters.py it imports ENABLE_GOOGLE_SEARCH.
    # Let's assume we want to test the logic based on tools param.

    # Case 2: Tools with googleSearch
    params_with_search = {"tools": [{"function": {"name": "googleSearch"}}]}
    assert controller._should_enable_google_search(params_with_search) is True

    # Case 3: Tools with google_search_retrieval
    params_with_retrieval = {"tools": [{"google_search_retrieval": {}}]}
    assert controller._should_enable_google_search(params_with_retrieval) is True

    # Case 4: Tools without search
    params_no_search = {"tools": [{"function": {"name": "otherTool"}}]}
    assert controller._should_enable_google_search(params_no_search) is False


@pytest.mark.asyncio
async def test_adjust_google_search(controller, mock_check_disconnect, mock_page):
    # Setup: Request wants search enabled, currently disabled
    request_params = {"tools": [{"function": {"name": "googleSearch"}}]}

    toggle = AsyncMock()
    # Mock get_attribute for all calls in _adjust_google_search:
    # 1. "aria-checked" -> "false" (initial check)
    # 2. "disabled" -> None (toggle is not disabled)
    # 3. "class" -> "" (no disabled class)
    # 4. "aria-checked" -> "true" (after click verification)
    toggle.get_attribute.side_effect = [
        "false",  # Initial aria-checked check
        None,  # disabled attribute check
        "",  # class attribute check
        "true",  # aria-checked after click
    ]
    mock_page.locator.return_value = toggle

    # Mock _supports_google_search to return True so the function doesn't skip early
    with patch.object(controller, "_supports_google_search", return_value=True):
        await controller._adjust_google_search(
            request_params, "gemini-2.0-flash", mock_check_disconnect
        )

    toggle.click.assert_called_once()


@pytest.mark.asyncio
async def test_adjust_parameters_full_flow(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    # Mock all internal adjust methods to verify orchestration
    with (
        patch.object(
            controller, "_adjust_temperature", new_callable=AsyncMock
        ) as mock_temp,
        patch.object(
            controller, "_adjust_max_tokens", new_callable=AsyncMock
        ) as mock_tokens,
        patch.object(
            controller, "_adjust_stop_sequences", new_callable=AsyncMock
        ) as mock_stop,
        patch.object(controller, "_adjust_top_p", new_callable=AsyncMock) as mock_top_p,
        patch.object(
            controller, "_ensure_tools_panel_expanded", new_callable=AsyncMock
        ) as mock_panel,
        patch.object(controller, "_open_url_content", new_callable=AsyncMock),
        patch.object(
            controller, "_adjust_google_search", new_callable=AsyncMock
        ) as mock_search,
    ):
        # Mock _handle_thinking_budget if it were to exist (dynamically added in real usage)
        controller._handle_thinking_budget = AsyncMock()

        request_params = {
            "temperature": 0.9,
            "max_output_tokens": 100,
            "stop": ["stop"],
            "top_p": 0.95,
        }
        page_params_cache = {}

        await controller.adjust_parameters(
            request_params,
            page_params_cache,
            mock_lock,
            "model-id",
            [],
            mock_check_disconnect,
        )

        mock_temp.assert_called_once()
        mock_tokens.assert_called_once()
        mock_stop.assert_called_once()
        mock_top_p.assert_called_once()
        mock_panel.assert_called_once()
        # mock_url called if ENABLE_URL_CONTEXT is True.
        # We can't easily control ENABLE_URL_CONTEXT here without patching config before import or reloading module.
        # But we can check if it was called or not based on default.

        controller._handle_thinking_budget.assert_called_once()
        mock_search.assert_called_once()


@pytest.mark.asyncio
async def test_client_disconnected_error(controller, mock_lock, mock_check_disconnect):
    mock_check_disconnect.side_effect = lambda stage: True

    with pytest.raises(ClientDisconnectedError):
        await controller.adjust_parameters(
            {}, {}, mock_lock, None, [], mock_check_disconnect
        )


@pytest.mark.asyncio
async def test_adjust_temperature_clamping(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test temperature clamping warning (line 117)."""
    page_params_cache = {}

    temp_locator = AsyncMock()
    temp_locator.input_value.side_effect = ["0.5", "2.0"]
    mock_page.locator.return_value = temp_locator

    # Request temperature > 2.0, should be clamped
    await controller._adjust_temperature(
        3.5, page_params_cache, mock_lock, mock_check_disconnect
    )

    # Should clamp to 2.0 and log warning
    temp_locator.fill.assert_called_with("2.0", timeout=5000)
    assert page_params_cache["temperature"] == 2.0


@pytest.mark.asyncio
async def test_adjust_temperature_page_already_matches(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test when page temperature already matches request (lines 148-151)."""
    page_params_cache = {}
    target_temp = 0.8

    temp_locator = AsyncMock()
    # Page already has the correct value
    temp_locator.input_value.return_value = "0.8"
    mock_page.locator.return_value = temp_locator

    await controller._adjust_temperature(
        target_temp, page_params_cache, mock_lock, mock_check_disconnect
    )

    # Should NOT call fill (no need to update)
    temp_locator.fill.assert_not_called()
    # Should update cache
    assert page_params_cache["temperature"] == 0.8


@pytest.mark.asyncio
async def test_adjust_temperature_general_exception(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test general exception handling in temperature adjustment (lines 189-197)."""
    page_params_cache = {"temperature": 0.5}

    temp_locator = AsyncMock()
    # Simulate Playwright exception
    temp_locator.input_value.side_effect = Exception("Playwright error")
    mock_page.locator.return_value = temp_locator

    await controller._adjust_temperature(
        0.8, page_params_cache, mock_lock, mock_check_disconnect
    )

    # Should clear cache and save snapshot
    assert "temperature" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_temperature_cancelled_error(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test CancelledError is re-raised (line 190-191)."""
    page_params_cache = {}

    temp_locator = AsyncMock()
    temp_locator.input_value.side_effect = asyncio.CancelledError()
    mock_page.locator.return_value = temp_locator

    with pytest.raises(asyncio.CancelledError):
        await controller._adjust_temperature(
            0.8, page_params_cache, mock_lock, mock_check_disconnect
        )


@pytest.mark.asyncio
async def test_adjust_temperature_client_disconnected_exception(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test ClientDisconnectedError is re-raised (line 196-197)."""
    page_params_cache = {}

    temp_locator = AsyncMock()
    temp_locator.input_value.side_effect = ClientDisconnectedError(
        "test_req", "test stage"
    )
    mock_page.locator.return_value = temp_locator

    with pytest.raises(ClientDisconnectedError):
        await controller._adjust_temperature(
            0.8, page_params_cache, mock_lock, mock_check_disconnect
        )

    # Should still save snapshot before re-raising
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_max_tokens_invalid_supported_tokens(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test handling of invalid supported_max_output_tokens (lines 231-237)."""
    page_params_cache = {}
    parsed_model_list = [
        {"id": "model-a", "supported_max_output_tokens": -100},  # Invalid: negative
        {
            "id": "model-b",
            "supported_max_output_tokens": "invalid",
        },  # Invalid: non-numeric
    ]

    tokens_locator = AsyncMock()
    tokens_locator.input_value.side_effect = ["100", "1000"]
    mock_page.locator.return_value = tokens_locator

    # Test with model-a (negative value)
    await controller._adjust_max_tokens(
        1000,
        page_params_cache,
        mock_lock,
        "model-a",
        parsed_model_list,
        mock_check_disconnect,
    )

    # Should log warning and use default max (65536)
    assert page_params_cache["max_output_tokens"] == 1000

    # Test with model-b (non-numeric value)
    page_params_cache = {}
    tokens_locator.input_value.side_effect = ["100", "1000"]

    await controller._adjust_max_tokens(
        1000,
        page_params_cache,
        mock_lock,
        "model-b",
        parsed_model_list,
        mock_check_disconnect,
    )

    # Should handle ValueError/TypeError gracefully
    assert page_params_cache["max_output_tokens"] == 1000


@pytest.mark.asyncio
async def test_adjust_max_tokens_cache_hit(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test max tokens cache hit (lines 252-255)."""
    page_params_cache = {"max_output_tokens": 2048}

    await controller._adjust_max_tokens(
        2048, page_params_cache, mock_lock, None, [], mock_check_disconnect
    )

    # Should not interact with page
    mock_page.locator.assert_not_called()
    assert page_params_cache["max_output_tokens"] == 2048


@pytest.mark.asyncio
async def test_adjust_max_tokens_page_already_matches(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test when page max tokens already matches request (lines 271-274)."""
    page_params_cache = {}
    target_tokens = 4096

    tokens_locator = AsyncMock()
    # Page already has the correct value
    tokens_locator.input_value.return_value = "4096"
    mock_page.locator.return_value = tokens_locator

    await controller._adjust_max_tokens(
        target_tokens, page_params_cache, mock_lock, None, [], mock_check_disconnect
    )

    # Should NOT call fill (no need to update)
    tokens_locator.fill.assert_not_called()
    # Should update cache
    assert page_params_cache["max_output_tokens"] == 4096


@pytest.mark.asyncio
async def test_adjust_parameters_url_context_disabled(
    controller, mock_lock, mock_check_disconnect
):
    """Test adjust_parameters when ENABLE_URL_CONTEXT is False (line 92)."""
    with (
        patch.object(controller, "_adjust_temperature", new_callable=AsyncMock),
        patch.object(controller, "_adjust_max_tokens", new_callable=AsyncMock),
        patch.object(controller, "_adjust_stop_sequences", new_callable=AsyncMock),
        patch.object(controller, "_adjust_top_p", new_callable=AsyncMock),
        patch.object(
            controller, "_ensure_tools_panel_expanded", new_callable=AsyncMock
        ),
        patch.object(
            controller, "_open_url_content", new_callable=AsyncMock
        ) as mock_url,
        patch.object(controller, "_adjust_google_search", new_callable=AsyncMock),
        patch(
            "browser_utils.page_controller_modules.parameters.ENABLE_URL_CONTEXT", False
        ),
    ):
        controller._handle_thinking_budget = AsyncMock()

        await controller.adjust_parameters(
            {}, {}, mock_lock, None, [], mock_check_disconnect
        )

        # Should NOT call _open_url_content when ENABLE_URL_CONTEXT is False
        mock_url.assert_not_called()


@pytest.mark.asyncio
async def test_adjust_max_tokens_value_error(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test ValueError handling in max tokens adjustment (lines 307-311)."""
    page_params_cache = {}

    tokens_locator = AsyncMock()
    tokens_locator.input_value.return_value = "invalid_number"
    mock_page.locator.return_value = tokens_locator

    await controller._adjust_max_tokens(
        1000, page_params_cache, mock_lock, None, [], mock_check_disconnect
    )

    # Should clear cache and save snapshot
    assert "max_output_tokens" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_max_tokens_general_exception(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test general exception handling in max tokens (lines 312-320)."""
    page_params_cache = {}

    tokens_locator = AsyncMock()
    tokens_locator.input_value.side_effect = Exception("Playwright error")
    mock_page.locator.return_value = tokens_locator

    await controller._adjust_max_tokens(
        1000, page_params_cache, mock_lock, None, [], mock_check_disconnect
    )

    # Should clear cache and save snapshot
    assert "max_output_tokens" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_max_tokens_cancelled_error(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test CancelledError is re-raised in max tokens."""
    page_params_cache = {}

    tokens_locator = AsyncMock()
    tokens_locator.input_value.side_effect = asyncio.CancelledError()
    mock_page.locator.return_value = tokens_locator

    with pytest.raises(asyncio.CancelledError):
        await controller._adjust_max_tokens(
            1000, page_params_cache, mock_lock, None, [], mock_check_disconnect
        )


@pytest.mark.asyncio
async def test_adjust_stop_sequences_single_string(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test stop sequences with single string input normalizes to set."""
    page_params_cache = {}

    input_locator = AsyncMock()

    def get_locator(selector):
        if selector == STOP_SEQUENCE_INPUT_SELECTOR:
            return input_locator
        return AsyncMock()

    mock_page.locator.side_effect = get_locator

    # Patch _get_current_stop_sequences: initially empty, then has STOP after addition
    call_count = [0]

    async def mock_get_current():
        call_count[0] += 1
        if call_count[0] == 1:
            return set()  # Initially empty
        else:
            return {"STOP"}  # After addition

    with patch.object(controller, "_get_current_stop_sequences", mock_get_current):
        # Pass single string instead of list
        await controller._adjust_stop_sequences(
            "STOP", page_params_cache, mock_lock, mock_check_disconnect
        )

    # Should normalize to set and add it
    input_locator.fill.assert_called_once()
    assert page_params_cache["stop_sequences"] == {"STOP"}


@pytest.mark.asyncio
async def test_adjust_stop_sequences_page_matches_request(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test when page state already matches request - no changes needed."""
    page_params_cache = {}

    # Patch _get_current_stop_sequences: page already has the requested stops
    async def mock_get_current():
        return {"stop1", "stop2"}

    with patch.object(controller, "_get_current_stop_sequences", mock_get_current):
        await controller._adjust_stop_sequences(
            ["stop1", "stop2"], page_params_cache, mock_lock, mock_check_disconnect
        )

    # Should only call _get_current_stop_sequences, no add/remove operations
    # The locator for input should not be called
    assert page_params_cache["stop_sequences"] == {"stop1", "stop2"}


@pytest.mark.asyncio
async def test_adjust_stop_sequences_removal_exception(
    controller, mock_lock, mock_check_disconnect, mock_page
):
    """Test exception during chip removal (lines 377-378)."""
    page_params_cache = {}

    input_locator = AsyncMock()
    remove_btn_locator = AsyncMock()
    remove_btn_locator.count.side_effect = [
        2,
        2,
    ]  # Has chips, then exception during removal
    remove_btn_locator.first.click = AsyncMock(side_effect=Exception("Click failed"))

    def get_locator(selector):
        if selector == STOP_SEQUENCE_INPUT_SELECTOR:
            return input_locator
        elif selector == MAT_CHIP_REMOVE_BUTTON_SELECTOR:
            return remove_btn_locator
        return AsyncMock()

    mock_page.locator.side_effect = get_locator

    await controller._adjust_stop_sequences(
        ["new_stop"], page_params_cache, mock_lock, mock_check_disconnect
    )

    # Should handle exception and continue
    assert "stop_sequences" in page_params_cache


@pytest.mark.asyncio
async def test_adjust_stop_sequences_general_exception(
    controller, mock_lock, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test general exception during stop sequence adjustment."""
    page_params_cache = {}

    # Patch _get_current_stop_sequences to succeed initially
    # but then cause an error in the locator for input
    input_locator = AsyncMock()
    input_locator.fill.side_effect = Exception("Fill failed")

    def get_locator(selector):
        if selector == STOP_SEQUENCE_INPUT_SELECTOR:
            return input_locator
        return AsyncMock()

    mock_page.locator.side_effect = get_locator

    # First call returns empty, second would verify but exception is raised first
    call_count = [0]

    async def mock_get_current():
        call_count[0] += 1
        return set()

    with patch.object(controller, "_get_current_stop_sequences", mock_get_current):
        await controller._adjust_stop_sequences(
            ["stop"], page_params_cache, mock_lock, mock_check_disconnect
        )

    # Should clear cache and save snapshot
    assert "stop_sequences" not in page_params_cache
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_top_p_clamping(controller, mock_check_disconnect, mock_page):
    """Test top_p clamping warning (line 407)."""
    locator = AsyncMock()
    locator.input_value.side_effect = ["0.5", "1.0"]
    mock_page.locator.return_value = locator

    # Request top_p > 1.0, should be clamped
    await controller._adjust_top_p(1.5, mock_check_disconnect)

    # Should clamp to 1.0 and log warning
    locator.fill.assert_called_with("1.0", timeout=5000)


@pytest.mark.asyncio
async def test_adjust_top_p_value_error(
    controller, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test top_p ValueError handling (lines 543-547)."""
    locator = AsyncMock()
    locator.input_value.return_value = "invalid"
    mock_page.locator.return_value = locator

    await controller._adjust_top_p(0.9, mock_check_disconnect)

    # Should save snapshot on ValueError
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_top_p_general_exception(
    controller, mock_check_disconnect, mock_page, mock_save_snapshot
):
    """Test top_p general exception handling (lines 548-556)."""
    locator = AsyncMock()
    locator.input_value.side_effect = Exception("Playwright error")
    mock_page.locator.return_value = locator

    await controller._adjust_top_p(0.9, mock_check_disconnect)

    # Should save snapshot on exception
    mock_save_snapshot.assert_called()


@pytest.mark.asyncio
async def test_adjust_top_p_cancelled_error(
    controller, mock_check_disconnect, mock_page
):
    """Test top_p CancelledError is re-raised (line 549-550)."""
    locator = AsyncMock()
    locator.input_value.side_effect = asyncio.CancelledError()
    mock_page.locator.return_value = locator

    with pytest.raises(asyncio.CancelledError):
        await controller._adjust_top_p(0.9, mock_check_disconnect)


@pytest.mark.asyncio
async def test_adjust_top_p_client_disconnected(
    controller, mock_check_disconnect, mock_page
):
    """Test top_p ClientDisconnectedError is re-raised (lines 555-556)."""
    locator = AsyncMock()
    locator.input_value.side_effect = ClientDisconnectedError("test_req", "test stage")
    mock_page.locator.return_value = locator

    with pytest.raises(ClientDisconnectedError):
        await controller._adjust_top_p(0.9, mock_check_disconnect)


@pytest.mark.asyncio
async def test_ensure_tools_panel_expanded_exception(
    controller, mock_check_disconnect, mock_page
):
    """Test tools panel expansion exception handling (lines 585-591)."""
    collapse_btn = AsyncMock()
    collapse_btn.locator = MagicMock()
    collapse_btn.locator.return_value.get_attribute.side_effect = Exception(
        "Playwright error"
    )
    mock_page.locator.return_value = collapse_btn

    # Should not raise, just log error
    await controller._ensure_tools_panel_expanded(mock_check_disconnect)


@pytest.mark.asyncio
async def test_ensure_tools_panel_expanded_cancelled_error(
    controller, mock_check_disconnect, mock_page
):
    """Test tools panel CancelledError is re-raised (line 586-587)."""
    collapse_btn = AsyncMock()
    collapse_btn.locator = MagicMock()
    collapse_btn.locator.return_value.get_attribute.side_effect = (
        asyncio.CancelledError()
    )
    mock_page.locator.return_value = collapse_btn

    with pytest.raises(asyncio.CancelledError):
        await controller._ensure_tools_panel_expanded(mock_check_disconnect)


@pytest.mark.asyncio
async def test_ensure_tools_panel_expanded_client_disconnected(
    controller, mock_check_disconnect, mock_page
):
    """Test tools panel ClientDisconnectedError is re-raised (lines 590-591)."""
    collapse_btn = AsyncMock()
    collapse_btn.locator = MagicMock()
    collapse_btn.locator.return_value.get_attribute.side_effect = (
        ClientDisconnectedError("test_req", "test stage")
    )
    mock_page.locator.return_value = collapse_btn

    with pytest.raises(ClientDisconnectedError):
        await controller._ensure_tools_panel_expanded(mock_check_disconnect)


@pytest.mark.asyncio
async def test_open_url_content_exception(controller, mock_check_disconnect, mock_page):
    """Test URL content exception handling (lines 610-615)."""
    switch = AsyncMock()
    switch.get_attribute.side_effect = Exception("Playwright error")
    mock_page.locator.return_value = switch

    # Should not raise, just log error
    await controller._open_url_content(mock_check_disconnect)


@pytest.mark.asyncio
async def test_open_url_content_cancelled_error(
    controller, mock_check_disconnect, mock_page
):
    """Test URL content CancelledError is re-raised (line 611-612)."""
    switch = AsyncMock()
    switch.get_attribute.side_effect = asyncio.CancelledError()
    mock_page.locator.return_value = switch

    with pytest.raises(asyncio.CancelledError):
        await controller._open_url_content(mock_check_disconnect)


@pytest.mark.asyncio
async def test_open_url_content_client_disconnected(
    controller, mock_check_disconnect, mock_page
):
    """Test URL content ClientDisconnectedError is re-raised (lines 614-615)."""
    switch = AsyncMock()
    switch.get_attribute.side_effect = ClientDisconnectedError("test_req", "test stage")
    mock_page.locator.return_value = switch

    with pytest.raises(ClientDisconnectedError):
        await controller._open_url_content(mock_check_disconnect)


@pytest.mark.asyncio
async def test_adjust_google_search_model_not_supported(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search skipped for unsupported models (lines 661-663)."""
    request_params = {"tools": [{"function": {"name": "googleSearch"}}]}

    # Model doesn't support Google Search
    with patch.object(controller, "_supports_google_search", return_value=False):
        await controller._adjust_google_search(
            request_params, "gemini-2.0-flash-lite", mock_check_disconnect
        )

    # Should not interact with page
    mock_page.locator.assert_not_called()


@pytest.mark.asyncio
async def test_adjust_google_search_toggle_not_visible(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search when toggle not visible (AssertionError case, lines 715-716)."""
    request_params = {}
    toggle = AsyncMock()
    mock_page.locator.return_value = toggle

    with (
        patch.object(controller, "_supports_google_search", return_value=True),
        patch(
            "browser_utils.page_controller_modules.parameters.expect_async"
        ) as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock(
            side_effect=AssertionError("Locator expected to be visible")
        )

        # Should not raise, just log debug message
        await controller._adjust_google_search(
            request_params, "gemini-flash", mock_check_disconnect
        )


@pytest.mark.asyncio
async def test_adjust_google_search_general_exception(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search general exception (lines 717-718)."""
    request_params = {}
    toggle = AsyncMock()
    toggle.get_attribute.side_effect = RuntimeError("Unexpected error")
    mock_page.locator.return_value = toggle

    with patch.object(controller, "_supports_google_search", return_value=True):
        # Should not raise, just log error
        await controller._adjust_google_search(
            request_params, "gemini-flash", mock_check_disconnect
        )


@pytest.mark.asyncio
async def test_adjust_google_search_cancelled_error(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search CancelledError is re-raised (line 711-712)."""
    request_params = {}
    toggle = AsyncMock()
    toggle.get_attribute.side_effect = asyncio.CancelledError()
    mock_page.locator.return_value = toggle

    with patch.object(controller, "_supports_google_search", return_value=True):
        with pytest.raises(asyncio.CancelledError):
            await controller._adjust_google_search(
                request_params, "gemini-flash", mock_check_disconnect
            )


@pytest.mark.asyncio
async def test_adjust_google_search_client_disconnected(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search ClientDisconnectedError is re-raised (lines 719-720)."""
    request_params = {}
    toggle = AsyncMock()
    toggle.get_attribute.side_effect = ClientDisconnectedError("test_req", "test stage")
    mock_page.locator.return_value = toggle

    with patch.object(controller, "_supports_google_search", return_value=True):
        with pytest.raises(ClientDisconnectedError):
            await controller._adjust_google_search(
                request_params, "gemini-flash", mock_check_disconnect
            )


@pytest.mark.asyncio
async def test_adjust_google_search_update_failed(
    controller, mock_check_disconnect, mock_page
):
    """Test Google Search toggle update verification failure (lines 704-708)."""
    request_params = {"tools": [{"function": {"name": "googleSearch"}}]}

    toggle = AsyncMock()
    # Mock get_attribute for all calls:
    # 1. "aria-checked" -> "false" (initial check)
    # 2. "disabled" -> None (toggle is not disabled)
    # 3. "class" -> "" (no disabled class)
    # 4. "aria-checked" -> "false" (after click - update failed, stays off)
    toggle.get_attribute.side_effect = [
        "false",  # Initial aria-checked check
        None,  # disabled attribute check
        "",  # class attribute check
        "false",  # aria-checked after click (update failed)
    ]
    mock_page.locator.return_value = toggle

    with patch.object(controller, "_supports_google_search", return_value=True):
        await controller._adjust_google_search(
            request_params, "gemini-flash", mock_check_disconnect
        )

    # Should log warning about failed update
    toggle.click.assert_called_once()


@pytest.mark.asyncio
async def test_supports_google_search_gemini20(controller):
    """Test _supports_google_search returns False for Gemini 2.0."""
    assert controller._supports_google_search("gemini-2.0-flash") is False
    assert controller._supports_google_search("gemini2.0-flash-exp") is False


@pytest.mark.asyncio
async def test_adjust_url_context_disable(controller, mock_check_disconnect, mock_page):
    """Test disabling URL context."""
    switch = AsyncMock()
    # Initially enabled
    switch.get_attribute.return_value = "true"
    switch.count.return_value = 1
    mock_page.locator.return_value = switch

    await controller._adjust_url_context(False, mock_check_disconnect)

    # Should click to disable
    switch.click.assert_called_once()


@pytest.mark.asyncio
async def test_adjust_parameters_fc_active_disables_url_context(
    controller, mock_lock, mock_check_disconnect
):
    """Test that active function calling force disables URL context."""
    # Add the method to controller if it doesn't exist (it doesn't in ParameterController)
    controller.is_function_calling_enabled = AsyncMock(return_value=True)

    with (
        patch.object(controller, "_adjust_temperature", new_callable=AsyncMock),
        patch.object(controller, "_adjust_max_tokens", new_callable=AsyncMock),
        patch.object(controller, "_adjust_stop_sequences", new_callable=AsyncMock),
        patch.object(controller, "_adjust_top_p", new_callable=AsyncMock),
        patch.object(
            controller, "_ensure_tools_panel_expanded", new_callable=AsyncMock
        ),
        patch.object(
            controller, "_adjust_url_context", new_callable=AsyncMock
        ) as mock_url_adj,
        patch.object(controller, "_adjust_google_search", new_callable=AsyncMock),
    ):
        controller._handle_thinking_budget = AsyncMock()

        # Note: We are testing ParameterController.adjust_parameters here.
        # We need to make sure it HAS the logic.
        await controller.adjust_parameters(
            {}, {}, mock_lock, None, [], mock_check_disconnect
        )

        # Verify it called _adjust_url_context(False, ...)
        mock_url_adj.assert_called_with(False, mock_check_disconnect)