File size: 65,992 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
import asyncio
import json
import sys
from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch

import pytest

# Mock server module before importing model_management components if needed
# But we already imported them. Let's patch where necessary.
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():
    page = AsyncMock()
    # locator is synchronous in Playwright
    page.locator = MagicMock()
    # Default evaluate returns None (empty localStorage)
    page.evaluate.return_value = None
    return page


@pytest.fixture
def mock_server():
    mock = MagicMock()
    mock.processing_lock = MagicMock()
    mock.processing_lock.__aenter__ = AsyncMock(return_value=None)
    mock.processing_lock.__aexit__ = AsyncMock(return_value=None)
    mock.excluded_model_ids = set()
    mock.current_ai_studio_model_id = "initial-model"
    mock.parsed_model_list = []
    # Use AsyncMock for async event, not MagicMock
    mock.model_list_fetch_event = AsyncMock(spec=asyncio.Event)
    mock.model_list_fetch_event.is_set.return_value = True
    return mock


@pytest.mark.asyncio
@pytest.mark.timeout(5)
@pytest.mark.parametrize(
    "evaluate_result,evaluate_side_effect,expected_exists,expected_needs_update,expected_advanced,expected_tools,expected_error_contains,test_id",
    [
        (
            json.dumps({"isAdvancedOpen": True, "areToolsOpen": True}),
            None,
            True,
            False,
            True,
            True,
            None,
            "valid",
        ),
        (
            json.dumps({"isAdvancedOpen": False, "areToolsOpen": True}),
            None,
            True,
            True,
            None,
            None,
            None,
            "needs_update",
        ),
        (None, None, False, True, None, None, "localStorage not found", "missing"),
        (
            "invalid-json",
            None,
            False,
            True,
            None,
            None,
            "JSON parse failed",
            "json_error",
        ),
        (
            None,
            Exception("Eval Error"),
            False,
            True,
            None,
            None,
            "Verification failed",
            "eval_error",
        ),
    ],
)
async def test_verify_ui_state_settings(
    mock_page,
    evaluate_result,
    evaluate_side_effect,
    expected_exists,
    expected_needs_update,
    expected_advanced,
    expected_tools,
    expected_error_contains,
    test_id,
):
    """Test UI state settings verification with various scenarios."""
    if evaluate_side_effect:
        mock_page.evaluate.side_effect = evaluate_side_effect
    else:
        mock_page.evaluate.return_value = evaluate_result

    with patch("browser_utils.models.ui_state.logger"):
        result = await _verify_ui_state_settings(mock_page, "req1")

    assert result["exists"] is expected_exists
    assert result["needsUpdate"] is expected_needs_update

    if expected_advanced is not None:
        assert result["isAdvancedOpen"] is expected_advanced
    if expected_tools is not None:
        assert result["areToolsOpen"] is expected_tools
    if expected_error_contains:
        assert expected_error_contains in result["error"]


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_force_ui_state_settings_success(mock_page):
    # Initial state: needs update
    initial_prefs = {"isAdvancedOpen": False}

    with (
        patch("browser_utils.models.ui_state._verify_ui_state_settings") as mock_verify,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_verify.side_effect = [
            {"needsUpdate": True, "prefs": initial_prefs},  # First call
            {"needsUpdate": False},  # Second call
        ]

        result = await _force_ui_state_settings(mock_page, "req1")

        assert result is True
        # Check if setItem was called
        assert mock_page.evaluate.call_count == 1
        args = mock_page.evaluate.call_args[0]
        assert "localStorage.setItem" in args[0]
        # Check if prefs were updated to True
        saved_prefs = json.loads(args[1])
        assert saved_prefs["isAdvancedOpen"] is True
        assert saved_prefs["areToolsOpen"] is True


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_force_ui_state_settings_no_update_needed(mock_page):
    with (
        patch("browser_utils.models.ui_state._verify_ui_state_settings") as mock_verify,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_verify.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
@pytest.mark.timeout(5)
async def test_force_ui_state_settings_fail_verify(mock_page):
    with (
        patch("browser_utils.models.ui_state._verify_ui_state_settings") as mock_verify,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_verify.side_effect = [
            {"needsUpdate": True, "prefs": {}},
            {"needsUpdate": True},  # Still needs update after set
        ]

        result = await _force_ui_state_settings(mock_page, "req1")

        assert result is False
        mock_page.evaluate.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_force_ui_state_with_retry_success(mock_page):
    with (
        patch("browser_utils.models.ui_state._force_ui_state_settings") as mock_force,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_force.side_effect = [False, True]  # Fail first, succeed second

        result = await _force_ui_state_with_retry(
            mock_page, "req1", max_retries=3, retry_delay=0.01
        )

        assert result is True
        assert mock_force.call_count == 2


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_force_ui_state_with_retry_fail(mock_page):
    with (
        patch("browser_utils.models.ui_state._force_ui_state_settings") as mock_force,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_force.return_value = False

        result = await _force_ui_state_with_retry(
            mock_page, "req1", max_retries=2, retry_delay=0.01
        )

        assert result is False
        assert mock_force.call_count == 2


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_verify_and_apply_ui_state_needs_update(mock_page):
    with (
        patch("browser_utils.models.ui_state._verify_ui_state_settings") as mock_verify,
        patch("browser_utils.models.ui_state._force_ui_state_with_retry") as mock_retry,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_verify.return_value = {
            "exists": True,
            "isAdvancedOpen": False,
            "areToolsOpen": False,
            "needsUpdate": True,
        }
        mock_retry.return_value = True

        result = await _verify_and_apply_ui_state(mock_page, "req1")

        assert result is True
        mock_retry.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_verify_and_apply_ui_state_ok(mock_page):
    with (
        patch("browser_utils.models.ui_state._verify_ui_state_settings") as mock_verify,
        patch("browser_utils.models.ui_state._force_ui_state_with_retry") as mock_retry,
        patch("browser_utils.models.ui_state.logger"),
    ):
        mock_verify.return_value = {
            "exists": True,
            "isAdvancedOpen": True,
            "areToolsOpen": True,
            "needsUpdate": False,
        }

        result = await _verify_and_apply_ui_state(mock_page, "req1")

        assert result is True
        mock_retry.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_load_excluded_models(tmp_path):
    # Create a dummy exclusion file
    d = tmp_path / "config"
    d.mkdir()
    p = d / "excluded_models.txt"
    p.write_text("model-a\nmodel-b\n", encoding="utf-8")

    # Mock server state
    mock_state = MagicMock()
    mock_state.excluded_model_ids = set()

    with (
        patch("api_utils.server_state.state", mock_state),
        patch("os.path.exists") as mock_exists,
        patch("builtins.open", new_callable=MagicMock) as mock_open,
        patch("browser_utils.models.switcher.logger"),
    ):
        mock_exists.return_value = True
        mock_file = MagicMock()
        mock_file.__enter__.return_value = ["model-a\n", "model-b\n"]
        mock_open.return_value = mock_file

        load_excluded_models("excluded_models.txt")

        assert "model-a" in mock_state.excluded_model_ids
        assert "model-b" in mock_state.excluded_model_ids


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_already_set(mock_page):
    model_id = "gemini-pro"
    full_model_path = f"models/{model_id}"
    prefs = {"promptModel": full_model_path}

    mock_page.evaluate.return_value = json.dumps(prefs)
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    with (
        patch("browser_utils.models.switcher.logger"),
        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, model_id, "req1")

    assert result is True
    # Should not navigate if already on new_chat
    mock_page.goto.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_success(mock_page):
    model_id = "gemini-pro"
    full_model_path = f"models/{model_id}"

    initial_prefs = {"promptModel": "models/other-model"}

    # Mock server module
    mock_server = MagicMock()
    mock_server.parsed_model_list = [{"id": model_id, "display_name": "Gemini Pro"}]

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.logger"),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        # Stateful evaluate mock
        evaluate_mock = AsyncMock()
        mock_page.evaluate = evaluate_mock

        call_count = 0

        async def evaluate_side_effect(script, *args):
            nonlocal call_count
            if "localStorage.getItem" in script:
                call_count += 1
                if call_count == 1:  # Initial check
                    return json.dumps(initial_prefs)
                if call_count == 2:  # Final verification
                    return json.dumps({"promptModel": full_model_path})
            return None

        evaluate_mock.side_effect = evaluate_side_effect

        # Mock page elements
        mock_locator = MagicMock()
        mock_locator.first.inner_text = AsyncMock(
            return_value=model_id
        )  # Matches target
        mock_page.locator.return_value = mock_locator

        # Mock incognito button
        mock_incognito = MagicMock()
        mock_incognito.get_attribute = AsyncMock(return_value="ms-button-active")

        def locator_side_effect(selector):
            if 'data-test-id="model-name"' in selector:
                return mock_locator
            if "Temporary chat toggle" in selector:
                return mock_incognito
            return MagicMock()

        mock_page.locator.side_effect = locator_side_effect

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

        assert result is True
        mock_page.goto.assert_called()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display(mock_page):
    # Mock server state
    mock_state = MagicMock()
    mock_state.current_ai_studio_model_id = "old-model"

    # CRITICAL: Use AsyncMock for async event, not MagicMock
    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set.return_value = True  # Event is already set, no wait needed
    mock_state.model_list_fetch_event = mock_event
    mock_state.parsed_model_list = []

    # Mock locator
    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="new-model")
    mock_page.locator.return_value = mock_locator

    with (
        patch("api_utils.server_state.state", mock_state),
        patch("browser_utils.models.startup.logger"),
    ):
        await _set_model_from_page_display(mock_page, set_storage=False)

    assert mock_state.current_ai_studio_model_id == "new-model"


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_needs_reload(mock_page):
    # Mock empty localStorage -> needs reload
    mock_page.evaluate.return_value = None
    mock_page.url = "http://test.url"

    # Mock server
    mock_server = MagicMock()

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._set_model_from_page_display"
        ) as mock_set_model,
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.logger"),
        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 call _set_model_from_page_display twice
        assert mock_set_model.call_count == 2
        assert mock_set_model.call_args_list[0][1]["set_storage"] is True
        assert mock_set_model.call_args_list[1][1]["set_storage"] is False

        # Should reload page
        mock_page.goto.assert_called_with(
            "http://test.url", wait_until="domcontentloaded", timeout=40000
        )


@pytest.mark.asyncio
async def test_switch_ai_studio_model_revert_logic(mock_page):
    """Test the revert logic when model switch fails validation."""
    model_id = "gemini-pro"
    full_model_path = f"models/{model_id}"
    initial_prefs = {"promptModel": "models/original-model"}
    original_prefs_str = json.dumps(initial_prefs)

    # Mock server module
    mock_server = MagicMock()
    mock_server.parsed_model_list = [{"id": model_id, "display_name": "Gemini Pro"}]

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.logger"),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        # Setup stateful evaluate mock to simulate storage state changes
        call_count = 0

        async def evaluate_side_effect(script, *args):
            nonlocal call_count
            if "localStorage.getItem" in script:
                call_count += 1
                if call_count == 1:  # Initial check
                    return original_prefs_str
                elif call_count == 2:  # Final verification (switched)
                    return json.dumps({"promptModel": full_model_path})
                elif call_count == 3:  # Revert logic
                    return json.dumps({"promptModel": full_model_path})
            return None

        mock_page.evaluate = AsyncMock(side_effect=evaluate_side_effect)

        # Simulate mismatch: page displays "Original Model" but storage has new model
        mock_locator = MagicMock()
        mock_locator.first.inner_text = AsyncMock(return_value="Original Model")
        mock_page.locator.return_value = mock_locator

        # Execute - should fail validation
        result = await switch_ai_studio_model(mock_page, model_id, "req1")

        # Verify failure occurred
        assert result is False

        # Check setItem calls
        set_item_calls = [
            args
            for args in mock_page.evaluate.call_args_list
            if "localStorage.setItem" in args[0][0]
        ]

        # Verify target prefs were set, but no revert
        assert len(set_item_calls) == 2
        last_set_call = set_item_calls[-1]
        target_prefs = json.loads(last_set_call[0][1])
        assert target_prefs["promptModel"] == full_model_path
        assert target_prefs["isAdvancedOpen"] is True
        assert target_prefs["areToolsOpen"] is True


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_incognito_toggle(mock_page):
    """Test incognito toggle logic when model switch succeeds"""
    model_id = "gemini-pro"
    full_model_path = f"models/{model_id}"

    # Mock server module
    mock_server = MagicMock()
    mock_server.parsed_model_list = [{"id": model_id, "display_name": "Gemini Pro"}]

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.logger"),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        # Mock evaluate for success path
        # It needs to return a DIFFERENT model initially so it doesn't return early
        call_count = 0

        def evaluate_side_effect(script, *args):
            nonlocal call_count
            if "localStorage.getItem" in script:
                call_count += 1
                if call_count == 1:  # Initial check -> return old model
                    return json.dumps({"promptModel": "models/old-model"})
                if call_count == 2:  # Final verification -> return new model
                    return json.dumps({"promptModel": full_model_path})
            return None

        mock_page.evaluate.side_effect = evaluate_side_effect

        # Mock page elements
        mock_locator = MagicMock()
        mock_locator.first.inner_text = AsyncMock(return_value=model_id)

        # Mock incognito button - INACTIVE initially
        mock_incognito = MagicMock()
        mock_incognito.wait_for = AsyncMock()
        mock_incognito.click = AsyncMock()
        # First check returns inactive, second check (after click) returns active
        mock_incognito.get_attribute = AsyncMock(
            side_effect=[
                "ms-button",  # inactive
                "ms-button-active ms-button",  # active
            ]
        )

        def locator_side_effect(selector):
            if 'data-test-id="model-name"' in selector:
                return mock_locator
            if "Temporary chat toggle" in selector:
                return mock_incognito
            return MagicMock()

        mock_page.locator.side_effect = locator_side_effect

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

        assert result is True
        mock_incognito.click.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_exception_handling_coverage(mock_page):
    """Cover exception handlers in various functions"""

    # 1. _force_ui_state_settings exception
    with (
        patch(
            "browser_utils.models.ui_state._verify_ui_state_settings",
            side_effect=Exception("Force Error"),
        ),
        patch("browser_utils.models.ui_state.logger"),
    ):
        assert await _force_ui_state_settings(mock_page) is False

    # 2. _verify_and_apply_ui_state exception
    with (
        patch(
            "browser_utils.models.ui_state._verify_ui_state_settings",
            side_effect=Exception("Verify Apply Error"),
        ),
        patch("browser_utils.models.ui_state.logger"),
    ):
        assert await _verify_and_apply_ui_state(mock_page) is False

    # 3. switch_ai_studio_model JSON decode error is tested in test_handle_initial_model_state_exceptions
    # The JSONDecodeError handler falls back to empty prefs and continues execution


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_nav_only(mock_page):
    """Test navigation when model already matches but URL is wrong"""
    model_id = "gemini-pro"
    full_model_path = f"models/{model_id}"
    prefs = {"promptModel": full_model_path}

    mock_page.evaluate.return_value = json.dumps(prefs)
    mock_page.url = "https://other.url"  # Not new_chat

    with (
        patch("browser_utils.models.switcher.logger"),
        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, model_id, "req1")

    assert result is True
    # Should navigate
    mock_page.goto.assert_called()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_load_excluded_models_edge_cases(tmp_path):
    """Test edge cases for load_excluded_models"""
    # 1. File does not exist
    # Mock server module - use api_utils.server_state.state which is what the implementation uses
    mock_state = MagicMock()
    mock_state.excluded_model_ids = set()

    with (
        patch("api_utils.server_state.state", mock_state),
        patch("browser_utils.models.switcher.logger") as mock_logger,
    ):
        load_excluded_models("non_existent.txt")
        # Implementation uses logger.debug, not logger.info
        debug_calls = [call[0][0] for call in mock_logger.debug.call_args_list]
        assert any("not found" in msg.lower() for msg in debug_calls)

    # 2. File exists but is empty - tested in the next block with mocked file I/O

    # Let's mock os.path.exists/open for easier testing of logic
    with (
        patch("api_utils.server_state.state", mock_state),
        patch("os.path.exists", return_value=True),
        patch("builtins.open", new_callable=MagicMock) as mock_open,
        patch("browser_utils.models.switcher.logger") as mock_logger,
    ):
        # Empty file
        mock_file = MagicMock()
        mock_file.__enter__.return_value = []  # Empty lines
        mock_open.return_value = mock_file

        load_excluded_models("empty.txt")
        # Implementation uses logger.debug
        debug_calls = [call[0][0] for call in mock_logger.debug.call_args_list]
        assert any("empty" in msg.lower() for msg in debug_calls)

    # 3. Exception
    with (
        patch("api_utils.server_state.state", mock_state),
        patch("os.path.exists", side_effect=Exception("Disk Error")),
        patch("browser_utils.models.switcher.logger") as mock_logger,
    ):
        load_excluded_models("error.txt")
        assert mock_logger.error.called


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_exceptions(mock_page):
    """Test exception handling in _handle_initial_model_state_and_storage"""
    mock_server = MagicMock()

    # 1. JSON Decode Error
    mock_page.evaluate.return_value = "invalid-json"

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._set_model_from_page_display"
        ) as mock_set_model,
        patch("browser_utils.models.startup.logger") as mock_logger,
    ):
        # Should trigger reload path due to JSON error
        # We'll mock _set_model_from_page_display to raise Exception to test the outer try/except
        mock_set_model.side_effect = Exception("Inner Error")

        await _handle_initial_model_state_and_storage(mock_page)

        # Verify error log
        # Check that we have the catastrophic error log
        # It catches "Inner Error" in the outer except block
        error_calls = [args[0][0] for args in mock_logger.error.call_args_list]
        assert any("critical error" in msg.lower() for msg in error_calls)


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_reload_retry_logic(mock_page):
    """Test reload retry logic"""
    mock_server = MagicMock()
    mock_page.evaluate.return_value = None  # Trigger reload

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._set_model_from_page_display"
        ) as mock_set_model,
        patch("browser_utils.models.startup.logger"),
        patch("asyncio.sleep", new_callable=AsyncMock),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        # Mock goto to fail twice then succeed
        mock_page.goto.side_effect = [Exception("Fail 1"), Exception("Fail 2"), None]

        await _handle_initial_model_state_and_storage(mock_page)

        assert mock_page.goto.call_count == 3
        # Should eventually succeed and call set_model twice (start and end)
        assert mock_set_model.call_count == 2


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display_timeout(mock_page):
    """Test timeout when waiting for model list"""
    mock_state = MagicMock()
    mock_state.current_ai_studio_model_id = None
    mock_state.parsed_model_list = []

    # Use AsyncMock for async event
    mock_event = AsyncMock(spec=asyncio.Event)
    mock_event.is_set.return_value = False  # Event not set, will wait
    mock_state.model_list_fetch_event = mock_event

    # Mock locator
    mock_locator = MagicMock()
    mock_locator.first.inner_text = AsyncMock(return_value="displayed-model")
    mock_page.locator.return_value = mock_locator

    with (
        patch("api_utils.server_state.state", mock_state),
        patch("browser_utils.models.startup.logger") as mock_logger,
        patch("asyncio.wait_for", side_effect=asyncio.TimeoutError),
    ):
        await _set_model_from_page_display(mock_page, set_storage=False)

        # Should log warning about timeout
        assert any(
            "timeout waiting for model list" in str(arg).lower()
            for arg in mock_logger.warning.call_args_list[0][0]
        )
        # Should still update global ID using display name as fallback
        assert mock_state.current_ai_studio_model_id == "displayed-model"


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display_storage_logic(mock_page):
    """Test storage update logic in _set_model_from_page_display"""
    mock_state = MagicMock()
    mock_state.current_ai_studio_model_id = "old"
    # Create a set event to avoid hanging on wait()
    mock_event = asyncio.Event()
    mock_event.set()
    mock_state.model_list_fetch_event = mock_event
    mock_state.parsed_model_list = []

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

    # Mock existing prefs
    existing_prefs = {"someKey": "someVal"}
    mock_page.evaluate.return_value = json.dumps(existing_prefs)

    with (
        patch("api_utils.server_state.state", mock_state),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.logger"),
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        # Check that setItem was called with updated prefs
        assert mock_page.evaluate.call_count == 2  # getItem, setItem

        # Verify setItem args
        set_call = mock_page.evaluate.call_args_list[1]
        assert "localStorage.setItem" in set_call[0][0]
        saved_prefs = json.loads(set_call[0][1])

        assert saved_prefs["isAdvancedOpen"] is True
        assert saved_prefs["promptModel"] == "models/new-model"
        # Check default keys added
        assert "bidiModel" in saved_prefs


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_catastrophic_error(mock_page):
    """Test top-level exception handling in switch_ai_studio_model"""
    # Force an error immediately
    mock_page.evaluate.side_effect = Exception("Catastrophic Failure")

    with (
        patch("browser_utils.models.switcher.logger") as mock_logger,
        patch(
            "browser_utils.operations.save_error_snapshot", new_callable=AsyncMock
        ) as mock_snapshot,
    ):
        result = await switch_ai_studio_model(mock_page, "model-id", "req1")

        assert result is False
        mock_snapshot.assert_called()
        assert mock_logger.exception.called


# ============================================================================
# EXTENDED COVERAGE - Edge Cases, Error Handling, Revert Logic
# ============================================================================

# === Section 1: UI State Verification Tests ===


@pytest.mark.asyncio
async def test_verify_ui_state_missing_storage(mock_page):
    """Test verification when localStorage item is missing."""
    mock_page.evaluate.return_value = None

    result = await _verify_ui_state_settings(mock_page)

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


@pytest.mark.asyncio
async def test_verify_ui_state_json_error(mock_page):
    """Test verification with invalid JSON in storage."""
    mock_page.evaluate.return_value = "invalid json"

    result = await _verify_ui_state_settings(mock_page)

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


@pytest.mark.asyncio
async def test_verify_ui_state_exception(mock_page):
    """Test verification when evaluate raises exception."""
    mock_page.evaluate.side_effect = Exception("Page error")

    result = await _verify_ui_state_settings(mock_page)

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


# === Section 2: Force UI State Tests ===


@pytest.mark.asyncio
async def test_force_ui_state_already_correct(mock_page):
    """Test force update when state is already correct."""
    mock_page.evaluate.return_value = json.dumps(
        {"isAdvancedOpen": True, "areToolsOpen": True}
    )

    result = await _force_ui_state_settings(mock_page)

    assert result is True
    assert mock_page.evaluate.call_count == 1


@pytest.mark.asyncio
async def test_force_ui_state_verify_failure(mock_page):
    """Test force update when verification fails after setting."""
    initial_state = json.dumps({"isAdvancedOpen": False})
    final_state = json.dumps({"isAdvancedOpen": False})  # Still wrong

    mock_page.evaluate.side_effect = [
        initial_state,  # verify 1
        None,  # setItem
        final_state,  # verify 2
    ]

    result = await _force_ui_state_settings(mock_page)

    assert result is False


@pytest.mark.asyncio
async def test_force_ui_state_exception(mock_page):
    """Test force update exception handling."""
    mock_page.evaluate.side_effect = Exception("Set error")

    result = await _force_ui_state_settings(mock_page)

    assert result is False


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_force_ui_state_settings_exception_via_verify(mock_page):
    """Test exception handling in _force_ui_state_settings via verify."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        side_effect=Exception("Test Error"),
    ):
        result = await _force_ui_state_settings(mock_page, "req1")
        assert result is False


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "mock_force_side_effect,mock_force_return_value,max_retries,retry_delay,expected_result,expected_call_count,test_id",
    [
        ([False, True], None, 3, None, True, 2, "success"),
        (None, False, 2, 0.01, False, 2, "failure"),
    ],
)
async def test_force_ui_state_retry(
    mock_page,
    mock_force_side_effect,
    mock_force_return_value,
    max_retries,
    retry_delay,
    expected_result,
    expected_call_count,
    test_id,
):
    """Test retry logic for forcing UI state settings (eventually succeeding or failing all attempts)."""
    with patch("browser_utils.models.ui_state._force_ui_state_settings") as mock_force:
        if mock_force_side_effect:
            mock_force.side_effect = mock_force_side_effect
        else:
            mock_force.return_value = mock_force_return_value

        kwargs = {"max_retries": max_retries}
        if retry_delay is not None:
            kwargs["retry_delay"] = retry_delay

        result = await _force_ui_state_with_retry(mock_page, **kwargs)

        assert result is expected_result
        assert mock_force.call_count == expected_call_count


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_verify_and_apply_ui_state_exception(mock_page):
    """Test exception handling in _verify_and_apply_ui_state."""
    with patch(
        "browser_utils.models.ui_state._verify_ui_state_settings",
        side_effect=Exception("Test Error"),
    ):
        result = await _verify_and_apply_ui_state(mock_page, "req1")
        assert result is False


# === Section 3: Load Excluded Models Tests ===


@pytest.mark.asyncio
async def test_load_excluded_models_success(mock_server):
    """Test loading excluded models from file."""
    mock_content = "model-1\nmodel-2\n"

    mock_file = MagicMock()
    mock_file.__enter__.return_value = mock_content.splitlines()

    with (
        patch("builtins.open", return_value=mock_file),
        patch("os.path.exists", return_value=True),
        patch.dict(sys.modules, {"server": mock_server}),
    ):
        load_excluded_models("excluded.txt")

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


# === Section 4: Handle Initial Model State Tests ===


@pytest.mark.asyncio
async def test_handle_initial_state_missing_storage(mock_page, mock_server):
    """Test handling initial state when storage is missing."""
    mock_page.evaluate.return_value = None  # No storage
    mock_page.url = "http://test.url"

    with (
        patch(
            "browser_utils.models.startup._set_model_from_page_display"
        ) as mock_set_model,
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.startup.expect_async") as mock_expect_async,
    ):
        mock_expect = MagicMock()
        mock_expect.to_be_visible = AsyncMock()
        mock_expect_async.return_value = mock_expect

        await _handle_initial_model_state_and_storage(mock_page)

        assert mock_set_model.call_count == 2
        assert mock_page.goto.call_count == 1


@pytest.mark.asyncio
async def test_handle_initial_state_valid_no_reload(mock_page, mock_server):
    """Test handling initial state when everything is valid."""
    mock_page.evaluate.return_value = json.dumps(
        {"promptModel": "models/valid-model", "isAdvancedOpen": True}
    )

    with patch("browser_utils.models.startup._verify_ui_state_settings") as mock_verify:
        mock_verify.return_value = {"needsUpdate": False}

        with patch.dict(sys.modules, {"server": mock_server}):
            await _handle_initial_model_state_and_storage(mock_page)

            mock_page.goto.assert_not_called()
            assert mock_server.current_ai_studio_model_id == "valid-model"


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_and_storage_success(mock_page, mock_server):
    """Test successful initial model state handling."""
    initial_prefs = json.dumps(
        {
            "promptModel": "models/gemini-pro",
            "isAdvancedOpen": True,
            "areToolsOpen": True,
        }
    )
    mock_page.evaluate.return_value = initial_prefs

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

        assert mock_server.current_ai_studio_model_id == "gemini-pro"
        mock_page.goto.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_exception(mock_page):
    """Test exception handling in _handle_initial_model_state_and_storage."""
    mock_page.evaluate.side_effect = Exception("Init Error")

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

        mock_fallback.assert_called_once()


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_json_error(mock_page, mock_server):
    """Test JSON error handling in initial state check."""
    mock_page.evaluate.return_value = "invalid-json"

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch("browser_utils.models.startup.logger") as mock_logger,
        patch(
            "browser_utils.models.startup._set_model_from_page_display"
        ) as mock_set_model,
        patch("browser_utils.models.startup.expect_async") as mock_expect,
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await _handle_initial_model_state_and_storage(mock_page)

        mock_set_model.assert_called()
        errors = [call.args[0] for call in mock_logger.error.call_args_list]
        assert any(
            "Failed to parse localStorage.aiStudioUserPreference JSON" in e
            for e in errors
        )


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_handle_initial_model_state_reload_retry(mock_page, mock_server):
    """Test reload retry logic."""
    mock_server = MagicMock()
    mock_page.evaluate.return_value = None  # Trigger reload

    mock_page.goto.side_effect = [Exception("Load failed"), None]

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch("browser_utils.models.startup.logger") as mock_logger,
        patch("browser_utils.models.startup._set_model_from_page_display"),
        patch("browser_utils.models.startup.expect_async") as mock_expect,
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("asyncio.sleep", new_callable=AsyncMock),
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()

        await _handle_initial_model_state_and_storage(mock_page)

        assert mock_page.goto.call_count == 2
        warnings = [call.args[0] for call in mock_logger.warning.call_args_list]
        assert any("page reload attempt 1/3 failed" in w.lower() for w in warnings)


# === Section 5: Set Model from Page Display Tests ===


@pytest.mark.asyncio
async def test_set_model_from_display_basic(mock_page, mock_server):
    """Test setting model from page display."""
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "displayed-model"

    mock_locator = AsyncMock()
    mock_locator.first = mock_element

    mock_page.locator.return_value = mock_locator

    mock_server.current_ai_studio_model_id = "old-model"

    with patch.dict(sys.modules, {"server": mock_server}):
        await _set_model_from_page_display(mock_page, set_storage=False)

        assert mock_server.current_ai_studio_model_id == "displayed-model"
        assert mock_page.evaluate.call_count == 0


@pytest.mark.asyncio
async def test_set_model_from_display_with_storage(mock_page, mock_server):
    """Test setting model from page display and updating storage."""
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "displayed-model"

    mock_locator = AsyncMock()
    mock_locator.first = mock_element
    mock_page.locator.return_value = mock_locator

    mock_page.evaluate.return_value = json.dumps({})

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        assert any(
            "setItem" in str(args) for args, _ in mock_page.evaluate.call_args_list
        )


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display_success(mock_page, mock_server):
    """Test _set_model_from_page_display success."""
    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "gemini-ultra"
    type(mock_locator).first = PropertyMock(return_value=mock_element)
    mock_page.locator.return_value = mock_locator

    with (
        patch.dict("sys.modules", {"server": mock_server}),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        assert mock_server.current_ai_studio_model_id == "gemini-ultra"
        assert mock_page.evaluate.called


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display_set_storage_defaults(mock_page, mock_server):
    """Test set_storage=True logic with default keys."""
    mock_page.locator.return_value.first.inner_text = AsyncMock(
        return_value="gemini-pro"
    )
    mock_page.evaluate.return_value = None  # No existing prefs

    with (
        patch.dict(sys.modules, {"server": mock_server}),
        patch(
            "browser_utils.models.startup._verify_and_apply_ui_state",
            return_value=True,
        ),
    ):
        await _set_model_from_page_display(mock_page, set_storage=True)

        args = mock_page.evaluate.call_args[0]
        assert "localStorage.setItem" in args[0]
        prefs = json.loads(args[1])
        assert prefs["isAdvancedOpen"] is True
        assert prefs["promptModel"] == "models/gemini-pro"
        assert prefs["getCodeLanguage"] == "Node.js"


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_set_model_from_page_display_same_id(mock_page):
    """Test when displayed ID matches current server ID."""
    mock_state = MagicMock()
    mock_state.current_ai_studio_model_id = "gemini-pro"
    mock_state.parsed_model_list = []
    mock_event = asyncio.Event()
    mock_event.set()  # Already set
    mock_state.model_list_fetch_event = mock_event

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

    with (
        patch("api_utils.server_state.state", mock_state),
        patch("browser_utils.models.startup.logger") as mock_logger,
    ):
        await _set_model_from_page_display(mock_page)

        # Model ID should not have changed
        assert mock_state.current_ai_studio_model_id == "gemini-pro"
        # Implementation doesn't log when unchanged, so just verify debug was called for reading
        debug_calls = [call[0][0] for call in mock_logger.debug.call_args_list]
        assert any("gemini-pro" in msg for msg in debug_calls)


# === Section 6: Switch Model Tests ===


@pytest.mark.asyncio
async def test_switch_model_recovery_logic(mock_page):
    """Test switch model failure triggering recovery logic."""
    req_id = "test_req"
    model_id = "new-model"

    mock_page.evaluate.return_value = json.dumps({"promptModel": "models/old-model"})
    mock_page.url = "http://example.com"

    mock_locator = AsyncMock()
    mock_locator.first.inner_text.return_value = "old-model"
    mock_page.locator.return_value = mock_locator

    with (
        patch(
            "server.parsed_model_list",
            [{"id": "old-model", "display_name": "Old Model"}],
        ),
        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, model_id, req_id)

        found_target = False
        for call in mock_page.evaluate.call_args_list:
            args, _ = call
            if len(args) > 1 and "models/new-model" in str(args[1]):
                found_target = True
                break

        assert found_target
        assert result is False


@pytest.mark.asyncio
async def test_switch_model_json_error_original(mock_page):
    """Test switch model when original prefs are invalid JSON."""
    mock_page.evaluate.return_value = "invalid json"

    mock_locator = AsyncMock()
    mock_locator.first.inner_text.return_value = "unknown-model"
    mock_page.locator.return_value = mock_locator

    with patch(
        "browser_utils.models.switcher._verify_and_apply_ui_state", return_value=True
    ):
        result = await switch_ai_studio_model(mock_page, "new-model", "req_id")

        assert result is False


@pytest.mark.asyncio
async def test_switch_model_already_set_nav_needed(mock_page):
    """Test when model is already set in storage but URL is wrong."""
    mock_page.evaluate.return_value = json.dumps({"promptModel": "models/target-model"})
    mock_page.url = "http://wrong.url"

    mock_locator = AsyncMock()
    mock_locator.first.inner_text.return_value = "target-model"

    mock_incognito = AsyncMock()
    mock_incognito.get_attribute.return_value = ["ms-button-active"]

    def locator_side_effect(selector):
        if "model-name" in selector:
            return mock_locator
        if "Temporary chat toggle" in selector:
            return mock_incognito
        return AsyncMock()

    mock_page.locator.side_effect = locator_side_effect

    mock_expect = MagicMock()
    mock_expect.to_be_visible = AsyncMock()

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async", return_value=mock_expect),
    ):
        result = await switch_ai_studio_model(mock_page, "target-model", "req_id")

    assert result is True
    mock_page.goto.assert_called()


@pytest.mark.asyncio
async def test_switch_model_success_flow(mock_page, mock_server):
    """Test full success flow: not set -> set -> navigate -> verify -> success."""
    req_id = "test_req"
    model_id = "target-model"

    initial_state = json.dumps({"promptModel": "models/old-model"})
    final_state = json.dumps({"promptModel": "models/target-model"})

    mock_page.evaluate.side_effect = [
        initial_state,  # 1. get initial
        None,  # 2. set item
        None,  # 3. set item (compat)
        final_state,  # 4. get final
        initial_state,  # 5. get revert (if failure)
        None,  # 6. set revert
    ]

    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "target-model"

    mock_locator = AsyncMock()
    mock_locator.first = mock_element

    mock_incognito = AsyncMock()
    mock_incognito.get_attribute.return_value = "ms-button-active custom-class"
    mock_incognito.wait_for.return_value = None
    mock_incognito.click.return_value = None

    def locator_side_effect(selector):
        if "model-name" in selector:
            return mock_locator
        if "Temporary chat toggle" in selector:
            return mock_incognito
        return AsyncMock()

    mock_page.locator.side_effect = locator_side_effect

    mock_expect = MagicMock()
    mock_expect.to_be_visible = AsyncMock()

    mock_server.parsed_model_list = [
        {"id": "target-model", "display_name": "Target Model"}
    ]

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=True,
        ),
        patch("browser_utils.models.switcher.expect_async", return_value=mock_expect),
        patch.dict(sys.modules, {"server": mock_server}),
    ):
        result = await switch_ai_studio_model(mock_page, model_id, req_id)

    assert result is True
    mock_page.goto.assert_called()
    assert any("setItem" in str(args) for args, _ in mock_page.evaluate.call_args_list)


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_json_error_original_prefs(mock_page):
    """Test JSONDecodeError when parsing original prefs."""
    mock_page.evaluate.side_effect = [
        "invalid-json",  # 1. original
        None,  # 2. set promptModel
        None,  # 3. set UI state manual
        json.dumps({"promptModel": "models/target-model"}),  # 4. final
    ]
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "target-model"
    type(mock_locator).first = PropertyMock(return_value=mock_element)
    mock_page.locator.return_value = mock_locator

    mock_locator.get_attribute = AsyncMock(return_value=["ms-button-active"])

    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, "target-model", "req1")

        assert result is True


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_json_error_final_prefs(mock_page):
    """Test JSONDecodeError when parsing final prefs."""
    initial_prefs = json.dumps({"promptModel": "models/old-model"})

    mock_page.evaluate.side_effect = [
        initial_prefs,  # 1
        None,  # 2
        None,  # 3
        "invalid-json",  # 4 (final check -> fails)
        initial_prefs,  # 5 (revert: read current LS)
        None,  # 6 (revert: write LS)
    ]
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "target-model"
    type(mock_locator).first = PropertyMock(return_value=mock_element)

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

    def locator_side_effect(selector):
        if "model-name" in selector:
            return mock_locator
        if "Temporary chat toggle" in selector:
            return mock_incognito
        return MagicMock()

    mock_page.locator.side_effect = locator_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, "target-model", "req1")

        assert result is False


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_read_model_name_exception(mock_page):
    """Test exception when reading displayed model name."""
    initial_prefs = json.dumps({"promptModel": "models/old-model"})

    mock_page.evaluate.side_effect = [
        initial_prefs,
        None,
        None,
        json.dumps({"promptModel": "models/target-model"}),
        None,  # Revert to original
    ]
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.side_effect = Exception("Read Error")
    type(mock_locator).first = PropertyMock(return_value=mock_element)

    def locator_side_effect(selector):
        if "model-name" in selector:
            return mock_locator
        return MagicMock()

    mock_page.locator.side_effect = locator_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, "target-model", "req1")

        assert result is False


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_incognito_retry(mock_page):
    """Test retrying to enable incognito mode."""
    initial_prefs = json.dumps({"promptModel": "models/old-model"})
    mock_page.evaluate.side_effect = [
        initial_prefs,
        None,
        None,
        json.dumps({"promptModel": "models/target-model"}),
    ]
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "target-model"
    type(mock_locator).first = PropertyMock(return_value=mock_element)

    mock_incognito = MagicMock()
    mock_incognito.wait_for = AsyncMock()
    mock_incognito.get_attribute = AsyncMock(side_effect=[[], ["ms-button-active"]])
    mock_incognito.click = AsyncMock()

    def locator_side_effect(selector):
        if "model-name" in selector:
            return mock_locator
        if "Temporary chat toggle" in selector:
            return mock_incognito
        return MagicMock()

    mock_page.locator.side_effect = locator_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, "target-model", "req1")

        assert result is True
        assert mock_incognito.click.called


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_revert_cant_read_display(mock_page):
    """Test revert flow when display name cannot be read."""
    initial_prefs = json.dumps({"promptModel": "models/original-model"})

    mock_page.evaluate.side_effect = [
        initial_prefs,  # 1
        None,  # 2
        None,  # 3
        json.dumps({"promptModel": "models/target-model"}),  # 4
        None,  # 5
    ]
    mock_page.url = "https://aistudio.google.com/prompts/new_chat"

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "wrong-model"
    type(mock_locator).first = PropertyMock(return_value=mock_element)

    mock_revert_locator = MagicMock()
    mock_revert_element = AsyncMock()
    mock_revert_element.inner_text.side_effect = Exception("Revert Read Error")
    type(mock_revert_locator).first = PropertyMock(return_value=mock_revert_element)

    locators = iter([mock_locator, mock_revert_locator])

    def locator_side_effect(selector):
        if "model-name" in selector:
            return next(locators)
        return MagicMock()

    mock_page.locator.side_effect = locator_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, "target-model", "req1")

        assert result is False
        set_calls = [
            args
            for args, _ in mock_page.evaluate.call_args_list
            if "setItem" in str(args)
        ]
        # Should have set the target model, but no revert
        last_set_arg = set_calls[-1][1]
        assert "target-model" in last_set_arg


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_general_exception(mock_page):
    """Test general exception handling in switch_ai_studio_model."""
    mock_page.evaluate.side_effect = Exception("Catastrophic Failure")

    result = await switch_ai_studio_model(mock_page, "target-model", "req1")

    assert result is False


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_json_error_logging(mock_page):
    """Test handling of JSONDecodeError in original prefs with logging."""
    mock_page.evaluate.side_effect = [
        "invalid-json",  # original_prefs_str
        None,  # setItem 1
        None,  # setItem 2
        None,  # final_prefs_str (check)
        None,  # check for revert (current_ls_content_str)
    ]

    with (
        patch("browser_utils.models.switcher.logger") as mock_logger,
        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()

        mock_page.locator.return_value.first.inner_text = AsyncMock(
            return_value="Different Model"
        )

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

        warnings = [call.args[0] for call in mock_logger.warning.call_args_list]
        assert any(
            "failed to parse original aistudiouserpreference json string" in w.lower()
            for w in warnings
        )


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_ui_state_fail(mock_page):
    """Test warning when UI state verification fails but process continues."""
    mock_page.evaluate.return_value = json.dumps({"promptModel": "models/old-model"})

    with (
        patch("browser_utils.models.switcher.logger") as mock_logger,
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            return_value=False,
        ),
        patch("browser_utils.models.switcher.expect_async") as mock_expect,
    ):
        mock_expect.return_value.to_be_visible = AsyncMock()
        mock_page.locator.return_value.first.inner_text = AsyncMock(
            return_value="gemini-pro"
        )

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

        warnings = [call.args[0] for call in mock_logger.warning.call_args_list]
        assert any(
            "UI state setting failed, but continuing model switching flow" in str(w)
            for w in warnings
        )


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_ai_studio_model_final_storage_mismatch(mock_page):
    """Test error when final storage does not match target model."""
    mock_page.evaluate.side_effect = [
        json.dumps({"promptModel": "models/old-model"}),  # 1. Get original
        None,  # 2. Set item 1
        None,  # 3. Set item 2
        json.dumps({"promptModel": "models/old-model"}),  # 4. Get final (still old)
        None,  # 5. Set item (revert)
        json.dumps({"promptModel": "models/old-model"}),  # 6. Get item (revert logic)
        None,  # 7. Set item (revert)
    ]

    with (
        patch("browser_utils.models.switcher.logger") as mock_logger,
        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()
        mock_page.locator.return_value.first.inner_text = AsyncMock(
            return_value="gemini-pro"
        )

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

        errors = [call.args[0] for call in mock_logger.error.call_args_list]
        assert any("AI Studio did not accept model change" in e for e in errors)


# === Section 7: Revert Logic Tests ===


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_revert_success(mock_page, mock_server):
    """Test revert logic when switch fails but revert works by reading displayed model."""
    req_id = "test_revert_success"
    model_id = "target-model"

    with patch.dict(sys.modules, {"server": mock_server}):
        initial_state = json.dumps({"promptModel": "models/old-model"})
        final_state = json.dumps({"promptModel": "models/target-model"})

        mock_page.evaluate.side_effect = [
            initial_state,  # 1. get initial
            None,  # 2. set item
            None,  # 3. set item (compat)
            final_state,  # 4. consumed by revert logic (get storage)
            initial_state,  # 5. consumed by revert logic (set revert storage)
            None,  # 6. set revert storage (extra safety)
        ] + [None] * 20

        mock_element = AsyncMock()
        mock_element.inner_text.return_value = "old-model"

        mock_locator = MagicMock()
        type(mock_locator).first = PropertyMock(return_value=mock_element)
        mock_page.locator.return_value = mock_locator

        mock_server.parsed_model_list = [
            {"id": "target-model", "display_name": "Target Model"},
            {"id": "old-model", "display_name": "Old Model"},
        ]

        mock_expect = MagicMock()
        mock_expect.to_be_visible = AsyncMock()

        with (
            patch(
                "browser_utils.models.switcher._verify_and_apply_ui_state",
                new_callable=AsyncMock,
                side_effect=[False, True, True, True],
            ) as mock_verify,
            patch(
                "browser_utils.models.switcher.expect_async", return_value=mock_expect
            ),
            patch(
                "browser_utils.operations.save_error_snapshot", new_callable=AsyncMock
            ),
        ):
            result = await switch_ai_studio_model(mock_page, model_id, req_id)

            assert result is False

            set_calls = [
                args
                for args, _ in mock_page.evaluate.call_args_list
                if "setItem" in str(args)
            ]
            assert len(set_calls) == 2
            last_set_arg = set_calls[-1][1]
            assert "models/target-model" in last_set_arg

            assert mock_verify.call_count == 2


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_revert_failure_fallback(mock_page, mock_server):
    """Test revert logic when reading displayed model fails, falls back to original storage."""
    req_id = "test_revert_fallback"
    model_id = "target-model"

    initial_state = json.dumps({"promptModel": "models/original-model"})

    mock_page.evaluate.side_effect = [
        initial_state,  # 1. get initial
        None,  # 2. set item
        None,  # 3. set item (compat)
        None,  # 4. set fallback storage
    ] + [None] * 10

    mock_locator = MagicMock()
    mock_element = AsyncMock()
    mock_element.inner_text.side_effect = Exception("Locator fail")
    type(mock_locator).first = PropertyMock(return_value=mock_element)
    mock_page.locator.return_value = mock_locator

    mock_server.parsed_model_list = [
        {"id": "target-model", "display_name": "Target Model"}
    ]

    mock_expect = MagicMock()
    mock_expect.to_be_visible = AsyncMock()

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            new_callable=AsyncMock,
            return_value=False,
        ),
        patch("browser_utils.models.switcher.expect_async", return_value=mock_expect),
    ):
        result = await switch_ai_studio_model(mock_page, model_id, req_id)

        assert result is False

        set_calls = [
            args
            for args, _ in mock_page.evaluate.call_args_list
            if "setItem" in str(args)
        ]
        last_set_arg = set_calls[-1][1]
        assert "models/target-model" in last_set_arg


@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_switch_model_revert_blind_trust(mock_page, mock_server):
    """Test revert logic uses displayed model name as ID blindly (current behavior)."""
    req_id = "test_revert_blind"
    model_id = "target-model"

    initial_state = json.dumps({"promptModel": "models/original-model"})

    mock_page.evaluate.side_effect = [
        initial_state,  # 1. get initial
        None,  # 2. set item
        None,  # 3. set item (compat)
        initial_state,  # 4. get revert storage
        None,  # 5. set revert storage
    ] + [None] * 10

    mock_element = AsyncMock()
    mock_element.inner_text.return_value = "Unknown Model"

    mock_locator = MagicMock()
    type(mock_locator).first = PropertyMock(return_value=mock_element)
    mock_page.locator.return_value = mock_locator

    mock_server.parsed_model_list = [
        {"id": "target-model", "display_name": "Target Model"}
    ]

    mock_expect = MagicMock()
    mock_expect.to_be_visible = AsyncMock()

    with (
        patch(
            "browser_utils.models.switcher._verify_and_apply_ui_state",
            new_callable=AsyncMock,
            side_effect=[False, True, True, True],
        ),
        patch("browser_utils.models.switcher.expect_async", return_value=mock_expect),
        patch("browser_utils.operations.save_error_snapshot", new_callable=AsyncMock),
    ):
        result = await switch_ai_studio_model(mock_page, model_id, req_id)

        assert result is False

        set_calls = [
            args
            for args, _ in mock_page.evaluate.call_args_list
            if "setItem" in str(args)
        ]
        last_set_arg = set_calls[-1][1]
        assert "models/target-model" in last_set_arg