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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
# Focused coverage tests for browser_utils/model_management.py
# Targets specific missing lines to achieve >80% coverage

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

import pytest

from browser_utils.model_management import (
    _force_ui_state_settings,
    _force_ui_state_with_retry,
    _handle_initial_model_state_and_storage,
    _set_model_from_page_display,
    _verify_and_apply_ui_state,
    _verify_ui_state_settings,
    load_excluded_models,
    switch_ai_studio_model,
)


@pytest.fixture
def mock_page():
    """Simple mock page."""
    page = AsyncMock()
    page.locator = MagicMock()
    page.evaluate = AsyncMock(return_value=None)
    page.goto = AsyncMock()
    page.url = "https://aistudio.google.com/prompts/new_chat"
    return page


# ===== _verify_ui_state_settings Coverage =====


@pytest.mark.asyncio
async def test_verify_ui_missing_storage(mock_page):
    """Lines 49-57: localStorage missing."""
    mock_page.evaluate.return_value = None

    result = await _verify_ui_state_settings(mock_page, "req1")

    assert result["exists"] is False
    assert result["error"] == "localStorage not found"
    assert result["needsUpdate"] is True


@pytest.mark.asyncio
async def test_verify_ui_json_error(mock_page):
    """Lines 82-90: JSONDecodeError handling."""
    mock_page.evaluate.return_value = "invalid json"

    result = await _verify_ui_state_settings(mock_page, "req1")

    assert result["exists"] is False
    assert "JSON parse failed" in result["error"]


@pytest.mark.asyncio
async def test_verify_ui_general_exception(mock_page):
    """Lines 94-102: General exception handling."""
    mock_page.evaluate.side_effect = Exception("Page error")

    result = await _verify_ui_state_settings(mock_page, "req1")

    assert result["exists"] is False
    assert "Verification failed" in result["error"]


# ===== _force_ui_state_settings Coverage =====


@pytest.mark.asyncio
async def test_force_ui_no_update_needed(mock_page):
    """Lines 122-124: Early return when no update needed."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        return_value={"needsUpdate": False},
    ):
        result = await _force_ui_state_settings(mock_page, "req1")

        assert result is True
        mock_page.evaluate.assert_not_called()


@pytest.mark.asyncio
async def test_force_ui_verify_fail(mock_page):
    """Lines 147-149: Verification fails after setting."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        side_effect=[{"needsUpdate": True, "prefs": {}}, {"needsUpdate": True}],
    ):
        result = await _force_ui_state_settings(mock_page, "req1")

        assert result is False


# ===== _force_ui_state_with_retry Coverage =====


@pytest.mark.asyncio
async def test_retry_success_first_attempt(mock_page):
    """Lines 180-182: Success on first attempt."""
    with patch(
        "browser_utils.models.ui_state._force_ui_state_settings", return_value=True
    ):
        result = await _force_ui_state_with_retry(mock_page, max_retries=3)

        assert result is True


@pytest.mark.asyncio
async def test_retry_fail_all(mock_page):
    """Lines 184-189: All retries fail."""
    with patch(
        "browser_utils.models.ui_state._force_ui_state_settings", return_value=False
    ):
        result = await _force_ui_state_with_retry(
            mock_page, max_retries=2, retry_delay=0.01
        )

        assert result is False


# ===== _verify_and_apply_ui_state Coverage =====


@pytest.mark.asyncio
async def test_apply_ui_needs_update(mock_page):
    """Lines 214-216: Needs update path."""
    with (
        patch(
            "browser_utils.models.ui_state._verify_ui_state_settings",
            return_value={
                "exists": True,
                "needsUpdate": True,
                "isAdvancedOpen": False,
                "areToolsOpen": False,
            },
        ),
        patch(
            "browser_utils.models.ui_state._force_ui_state_with_retry",
            return_value=True,
        ),
    ):
        result = await _verify_and_apply_ui_state(mock_page, "req1")

        assert result is True


@pytest.mark.asyncio
async def test_apply_ui_already_ok(mock_page):
    """Lines 217-219: No update needed."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        return_value={
            "exists": True,
            "needsUpdate": False,
            "isAdvancedOpen": True,
            "areToolsOpen": True,
        },
    ):
        result = await _verify_and_apply_ui_state(mock_page, "req1")

        assert result is True


# ===== switch_ai_studio_model Coverage =====


@pytest.mark.asyncio
async def test_switch_model_json_error_original(mock_page):
    """Lines 246-248: JSONDecodeError on original prefs."""
    mock_page.evaluate.side_effect = ["invalid json", None, None, None]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="gemini-pro")
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "gemini-pro", "req1")

        # Should handle error and continue
        assert result in [True, False]  # May succeed or fail depending on verification


@pytest.mark.asyncio
async def test_switch_model_already_set_wrong_url(mock_page):
    """Lines 256-269: Model already set but URL wrong."""
    prefs = json.dumps({"promptModel": "models/gemini-pro"})
    mock_page.evaluate.return_value = prefs
    mock_page.url = "https://wrong.url"

    with patch("browser_utils.models.switcher.expect_async") as mock_expect:
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "gemini-pro", "req1")

        assert result is True
        mock_page.goto.assert_called_once()


@pytest.mark.asyncio
async def test_switch_model_ui_state_fail_warning(mock_page):
    """Lines 283-284: UI state fails but continues."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="new")
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=False,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
        patch("browser_utils.models.switcher.logger") as mock_logger,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await switch_ai_studio_model(mock_page, "new", "req1")

        # Verify warning logged
        warnings = [call.args[0] for call in mock_logger.warning.call_args_list]
        assert any("UI state setting failed" in str(w) for w in warnings)


@pytest.mark.asyncio
async def test_switch_model_final_ui_fail(mock_page):
    """Lines 303-307: Final UI state verification."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="new")
    mock_page.locator.return_value = mock_locator

    ui_calls = [True, False]  # Initial succeeds, final fails

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            side_effect=lambda *args: ui_calls.pop(0),
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "new", "req1")

        assert result is True  # Still succeeds despite warning


@pytest.mark.asyncio
async def test_switch_model_final_prefs_json_error(mock_page):
    """Lines 317-318: JSONDecodeError on final prefs."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [prefs, None, None, "invalid json"]

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
        patch("browser_utils.models.switcher.logger"),
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "new", "req1")

        assert result is False  # Fails due to verification failure


@pytest.mark.asyncio
async def test_switch_model_display_read_error(mock_page):
    """Lines 360-366: Exception reading displayed model."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(side_effect=Exception("Read failed"))
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "new", "req1")

        assert result is False


@pytest.mark.asyncio
async def test_switch_model_incognito_active(mock_page):
    """Lines 383-384: Incognito already active."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_model_loc = MagicMock()
    mock_model_loc.first.inner_text = AsyncMock(return_value="new")

    mock_incognito = MagicMock()
    mock_incognito.wait_for = AsyncMock()
    mock_incognito.get_attribute = AsyncMock(return_value="ms-button-active")

    def loc_side_effect(sel):
        if "model-name" in sel:
            return mock_model_loc
        if "Temporary" in sel:
            return mock_incognito
        return MagicMock()

    mock_page.locator.side_effect = loc_side_effect

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "new", "req1")

        assert result is True


@pytest.mark.asyncio
async def test_switch_model_incognito_exception(mock_page):
    """Lines 400-403: Incognito toggle fails."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_model_loc = MagicMock()
    mock_model_loc.first.inner_text = AsyncMock(return_value="new")

    mock_incognito = MagicMock()
    mock_incognito.wait_for = AsyncMock(side_effect=Exception("Button not found"))

    def loc_side_effect(sel):
        if "model-name" in sel:
            return mock_model_loc
        if "Temporary" in sel:
            return mock_incognito
        return MagicMock()

    mock_page.locator.side_effect = loc_side_effect

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        result = await switch_ai_studio_model(mock_page, "new", "req1")

        assert result is True  # Succeeds despite incognito failure


# ===== load_excluded_models Coverage =====


@pytest.mark.asyncio
async def test_load_excluded_file_exists():
    """Lines 597-609: Successful load."""
    from api_utils.server_state import state

    state.excluded_model_ids = set()

    with (
        patch("os.path.exists", return_value=True),
        patch("builtins.open", new_callable=MagicMock) as mock_open,
    ):
        mock_file = MagicMock()
        mock_file.__enter__.return_value = ["model-1\n", "model-2\n"]
        mock_open.return_value = mock_file

        load_excluded_models("test.txt")

        assert "model-1" in state.excluded_model_ids
        assert "model-2" in state.excluded_model_ids


@pytest.mark.asyncio
async def test_load_excluded_empty_file():
    """Lines 606-609: Empty file."""
    from api_utils.server_state import state

    state.excluded_model_ids = set()

    with (
        patch("os.path.exists", return_value=True),
        patch("builtins.open", new_callable=MagicMock) as mock_open,
    ):
        mock_file = MagicMock()
        mock_file.__enter__.return_value = []
        mock_open.return_value = mock_file

        load_excluded_models("empty.txt")

        assert len(state.excluded_model_ids) == 0


@pytest.mark.asyncio
async def test_load_excluded_file_not_found():
    """Lines 610-611: File not found."""
    from api_utils.server_state import state

    state.excluded_model_ids = set()

    with patch("os.path.exists", return_value=False):
        load_excluded_models("nonexistent.txt")

        assert len(state.excluded_model_ids) == 0


@pytest.mark.asyncio
async def test_load_excluded_exception():
    """Lines 612-613: Exception during load."""
    from api_utils.server_state import state

    state.excluded_model_ids = set()

    with patch("os.path.exists", side_effect=Exception("Disk error")):
        load_excluded_models("error.txt")

        # Should not crash, just log error
        assert len(state.excluded_model_ids) == 0


# ===== _handle_initial_model_state_and_storage Coverage =====


@pytest.mark.asyncio
async def test_handle_initial_missing_storage(mock_page):
    """Lines 632-635: Missing localStorage."""

    mock_page.evaluate.return_value = None
    mock_page.url = "https://test.url"

    with (
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await _handle_initial_model_state_and_storage(mock_page)

        # Should trigger reload flow
        assert mock_page.goto.called


@pytest.mark.asyncio
async def test_handle_initial_json_error(mock_page):
    """Lines 664-669: JSONDecodeError."""
    mock_page.evaluate.return_value = "invalid json"
    mock_page.url = "https://test.url"

    with (
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await _handle_initial_model_state_and_storage(mock_page)

        # Should trigger reload
        assert mock_page.goto.called


@pytest.mark.asyncio
async def test_handle_initial_exception_fallback(mock_page):
    """Lines 738-753: Exception with fallback."""
    mock_page.evaluate.side_effect = Exception("Critical error")

    with patch(
        "browser_utils.models.startup._set_model_from_page_display"
    ) as mock_fallback:
        await _handle_initial_model_state_and_storage(mock_page)

        # Should call fallback
        mock_fallback.assert_called_once()
        assert mock_fallback.call_args[1]["set_storage"] is False


# ===== _set_model_from_page_display Coverage =====


@pytest.mark.asyncio
async def test_set_model_display_basic(mock_page):
    """Lines 765-795: Basic display read."""
    from api_utils.server_state import state

    state.current_ai_studio_model_id = "old"

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="new-model")
    mock_page.locator.return_value = mock_loc

    await _set_model_from_page_display(mock_page, set_storage=False)

    assert state.current_ai_studio_model_id == "new-model"


@pytest.mark.asyncio
async def test_set_model_display_with_storage(mock_page):
    """Lines 797-860: Storage update path."""
    from api_utils.server_state import state

    # Setup event mock
    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set = MagicMock(return_value=True)
    state.model_list_fetch_event = mock_event

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="model-id")
    mock_page.locator.return_value = mock_loc

    # First call returns existing prefs, second call for setItem
    mock_page.evaluate.side_effect = [json.dumps({}), None]

    with patch(
        "browser_utils.models.startup._verify_and_apply_ui_state", return_value=True
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        # Verify setItem called
        calls = [str(c) for c in mock_page.evaluate.call_args_list]
        assert any("setItem" in c for c in calls)


@pytest.mark.asyncio
async def test_set_model_display_json_error_storage(mock_page):
    """Lines 807-811: JSONDecodeError on existing prefs."""
    from api_utils.server_state import state

    # Setup event mock
    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set = MagicMock(return_value=True)
    state.model_list_fetch_event = mock_event

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="model")
    mock_page.locator.return_value = mock_loc

    # First call returns invalid JSON, second call for setItem
    mock_page.evaluate.side_effect = ["invalid json", None]

    with patch(
        "browser_utils.models.startup._verify_and_apply_ui_state", return_value=True
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        # Should handle error and create new prefs
        assert mock_page.evaluate.call_count >= 2


@pytest.mark.asyncio
async def test_set_model_display_exception(mock_page):
    """Lines 861-864: Exception handling."""
    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(side_effect=Exception("Read failed"))
    mock_page.locator.return_value = mock_loc

    # Should not raise
    await _set_model_from_page_display(mock_page, set_storage=False)


# ===== Updated Switch Model Coverage (No revert logic anymore) =====


@pytest.mark.asyncio
async def test_switch_model_validation_fail_storage(mock_page):
    """Cover failure path when storage does not match target."""
    original_prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        original_prefs,  # get original
        None,  # set target
        None,  # set target (compat)
        original_prefs,  # get final (mismatch!)
    ]

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()
        result = await switch_ai_studio_model(mock_page, "new", "req1")
        assert result is False


@pytest.mark.asyncio
async def test_switch_model_validation_fail_display(mock_page):
    """Cover failure path when page display does not match target."""
    prefs = json.dumps({"promptModel": "models/new"})
    mock_page.evaluate.side_effect = [
        json.dumps({"promptModel": "models/old"}),  # original
        None,  # set
        None,  # set compat
        prefs,  # final check ok
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="wrong-display")
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()
        result = await switch_ai_studio_model(mock_page, "new", "req1")
        assert result is False


# ===== Additional Coverage for _handle_initial_model_state_and_storage =====


@pytest.mark.asyncio
async def test_handle_initial_reload_retry_all_fail(mock_page):
    """Lines 707-725: All reload retries fail."""
    from api_utils.server_state import state

    state.model_list_fetch_event = AsyncMock(spec=asyncio.Event)
    state.model_list_fetch_event.is_set = MagicMock(return_value=True)

    mock_page.evaluate.return_value = None  # Missing localStorage
    mock_page.url = "https://test.url"
    mock_page.goto.side_effect = Exception("Reload always fails")

    with (
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=False,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
        patch("browser_utils.operations.save_error_snapshot", new_callable=AsyncMock),
        patch(
            "browser_utils.models.startup.asyncio.sleep", new_callable=AsyncMock
        ),  # Skip retry delays
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        # Should not raise despite all retries failing
        await _handle_initial_model_state_and_storage(mock_page)

        # Should have tried 3 times
        assert mock_page.goto.call_count == 3


@pytest.mark.asyncio
async def test_handle_initial_valid_state_no_reload(mock_page):
    """Lines 734-737: Valid state, no reload needed."""
    from api_utils.server_state import state

    state.current_ai_studio_model_id = None

    prefs = json.dumps(
        {
            "promptModel": "models/valid-model",
            "isAdvancedOpen": True,
            "areToolsOpen": True,
        }
    )
    mock_page.evaluate.return_value = prefs

    with patch(
        "browser_utils.models.startup._verify_ui_state_settings",
        return_value={"needsUpdate": False},
    ):
        await _handle_initial_model_state_and_storage(mock_page)

        # Should not call goto since state is valid
        mock_page.goto.assert_not_called()
        assert state.current_ai_studio_model_id == "valid-model"


@pytest.mark.asyncio
async def test_set_model_display_wait_for_event_timeout(mock_page):
    """Lines 776-781: Wait for model list event timeout."""
    from api_utils.server_state import state

    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set = MagicMock(return_value=False)
    mock_event.wait = AsyncMock(side_effect=asyncio.TimeoutError())
    state.model_list_fetch_event = mock_event

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="model-from-page")
    mock_page.locator.return_value = mock_loc

    await _set_model_from_page_display(mock_page, set_storage=False)

    # Should handle timeout gracefully
    assert state.current_ai_studio_model_id == "model-from-page"


@pytest.mark.asyncio
async def test_set_model_display_ui_state_fail_fallback(mock_page):
    """Lines 816-824: UI state setting fails, use traditional method."""
    from api_utils.server_state import state

    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set = MagicMock(return_value=True)
    state.model_list_fetch_event = mock_event

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="test-model")
    mock_page.locator.return_value = mock_loc

    mock_page.evaluate.side_effect = [json.dumps({}), None]

    with patch(
        "browser_utils.models.startup._verify_and_apply_ui_state", return_value=False
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        # Should still call setItem with traditional method
        assert mock_page.evaluate.call_count >= 2


@pytest.mark.asyncio
async def test_set_model_display_no_model_id_found(mock_page):
    """Lines 832-835: No model ID found from display."""
    from api_utils.server_state import state

    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set = MagicMock(return_value=True)
    state.model_list_fetch_event = mock_event

    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(return_value="unknown-display")
    mock_page.locator.return_value = mock_loc

    # Return empty prefs without promptModel
    mock_page.evaluate.side_effect = [json.dumps({}), None]

    with patch(
        "browser_utils.models.startup._verify_and_apply_ui_state", return_value=True
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        # Should handle missing model ID gracefully
        assert state.current_ai_studio_model_id == "unknown-display"


# ===== Additional Edge Cases for 80% Coverage =====


@pytest.mark.asyncio
async def test_verify_ui_cancellederror(mock_page):
    """Lines 92-93: CancelledError propagation in verify."""
    mock_page.evaluate.side_effect = asyncio.CancelledError()

    with pytest.raises(asyncio.CancelledError):
        await _verify_ui_state_settings(mock_page, "req1")


@pytest.mark.asyncio
async def test_force_ui_cancellederror(mock_page):
    """Lines 151-152: CancelledError in force_ui_state_settings."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        side_effect=asyncio.CancelledError(),
    ):
        with pytest.raises(asyncio.CancelledError):
            await _force_ui_state_settings(mock_page, "req1")


@pytest.mark.asyncio
async def test_verify_apply_cancellederror(mock_page):
    """Lines 221-222: CancelledError in verify_and_apply."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        side_effect=asyncio.CancelledError(),
    ):
        with pytest.raises(asyncio.CancelledError):
            await _verify_and_apply_ui_state(mock_page, "req1")


@pytest.mark.asyncio
async def test_switch_model_cancellederror(mock_page):
    """Lines 556-557: CancelledError in switch_ai_studio_model."""
    mock_page.evaluate.side_effect = asyncio.CancelledError()

    with pytest.raises(asyncio.CancelledError):
        await switch_ai_studio_model(mock_page, "new-model", "req1")


@pytest.mark.asyncio
async def test_switch_model_incognito_cancellederror(mock_page):
    """Lines 400-401: CancelledError during incognito toggle."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_model_loc = MagicMock()
    mock_model_loc.first.inner_text = AsyncMock(return_value="new")

    mock_incognito = MagicMock()
    mock_incognito.wait_for = AsyncMock(side_effect=asyncio.CancelledError())

    def loc_side_effect(sel):
        if "model-name" in sel:
            return mock_model_loc
        if "Temporary" in sel:
            return mock_incognito
        return MagicMock()

    mock_page.locator.side_effect = loc_side_effect

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        with pytest.raises(asyncio.CancelledError):
            await switch_ai_studio_model(mock_page, "new", "req1")


@pytest.mark.asyncio
async def test_switch_model_revert_cancellederror(mock_page):
    """Lines 360-361: CancelledError during display name read."""
    # We trigger CancelledError when reading the displayed model name
    # after navigation, which is part of the validation flow.
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(side_effect=asyncio.CancelledError())
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        with pytest.raises(asyncio.CancelledError):
            await switch_ai_studio_model(mock_page, "new", "req1")


@pytest.mark.asyncio
async def test_switch_model_exception_recovery_cancellederror(mock_page):
    """Lines 208-209: CancelledError propagation."""
    # We trigger CancelledError during the initial evaluate
    mock_page.evaluate.side_effect = asyncio.CancelledError()

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        with pytest.raises(asyncio.CancelledError):
            await switch_ai_studio_model(mock_page, "new-model", "req1")


@pytest.mark.asyncio
async def test_handle_initial_cancellederror(mock_page):
    """Lines 738-739: CancelledError in handle_initial."""
    mock_page.evaluate.side_effect = asyncio.CancelledError()

    with pytest.raises(asyncio.CancelledError):
        await _handle_initial_model_state_and_storage(mock_page)


@pytest.mark.asyncio
async def test_handle_initial_exception_fallback_cancellederror(mock_page):
    """Lines 750-751: CancelledError in fallback path."""
    mock_page.evaluate.side_effect = Exception("Error")

    with patch(
        "browser_utils.models.startup._set_model_from_page_display",
        side_effect=asyncio.CancelledError(),
    ):
        with pytest.raises(asyncio.CancelledError):
            await _handle_initial_model_state_and_storage(mock_page)


@pytest.mark.asyncio
async def test_set_model_display_cancellederror(mock_page):
    """Lines 861-862: CancelledError in set_model_from_page_display."""
    mock_loc = MagicMock()
    mock_loc.first.inner_text = AsyncMock(side_effect=asyncio.CancelledError())
    mock_page.locator.return_value = mock_loc

    with pytest.raises(asyncio.CancelledError):
        await _set_model_from_page_display(mock_page, set_storage=False)


@pytest.mark.asyncio
async def test_switch_model_display_cancellederror(mock_page):
    """Lines 360-361: CancelledError when reading display model."""
    prefs = json.dumps({"promptModel": "models/old"})
    mock_page.evaluate.side_effect = [
        prefs,
        None,
        None,
        json.dumps({"promptModel": "models/new"}),
    ]

    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(side_effect=asyncio.CancelledError())
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        with pytest.raises(asyncio.CancelledError):
            await switch_ai_studio_model(mock_page, "new", "req1")


@pytest.mark.asyncio
async def test_handle_initial_reload_cancellederror(mock_page):
    """Lines 707-708: CancelledError during page reload."""
    from api_utils.server_state import state

    state.model_list_fetch_event = AsyncMock(spec=asyncio.Event)
    state.model_list_fetch_event.is_set = MagicMock(return_value=True)

    mock_page.evaluate.return_value = None  # Missing localStorage
    mock_page.url = "https://test.url"
    mock_page.goto.side_effect = asyncio.CancelledError()

    with (
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=False,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        with pytest.raises(asyncio.CancelledError):
            await _handle_initial_model_state_and_storage(mock_page)


@pytest.mark.asyncio
async def test_handle_initial_invalid_promptmodel(mock_page):
    """Lines 646-649: Invalid promptModel in localStorage."""

    # promptModel is empty string (invalid)
    prefs = json.dumps({"promptModel": "   ", "isAdvancedOpen": False})
    mock_page.evaluate.return_value = prefs
    mock_page.url = "https://test.url"

    with (
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await _handle_initial_model_state_and_storage(mock_page)

        # Should trigger reload due to invalid promptModel
        assert mock_page.goto.called