arc-ui 0.4.0

Physics-first native Rust desktop UI component library & charts for GPUI (120+ interactive components ported from Arc).
Documentation
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
//! P7 桌面区块交互回归,不替代像素/性能/真实辅助技术验收。
use super::child_id;
use crate::*;
use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
use gpui_kit::{
    App, Context, ElementId, InteractiveElement as _, IntoElement, MouseButton, ParentElement,
    Render, Styled, TestAppContext, TestSupportExt as _, Window, div, px,
    test::{TestEventExt as _, TestQueryExt as _, TestWindowExt as _},
};
use std::{cell::RefCell, rc::Rc};

#[derive(Clone, Copy, Debug)]
enum Kind {
    Command,
    Notification,
    Upload,
    Otp,
    Empty,
    Page,
    Comparison,
    Changelog,
}
struct Host {
    kind: Kind,
    events: Rc<RefCell<Vec<String>>>,
    disabled: bool,
}
/// 即时完成上传的主机:on_upload 捕获请求并立刻 finish(Ok),用于验证 filled 门槛。
struct InstantDoneHost;
impl Render for InstantDoneHost {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div()
            .w(px(560.0))
            .p(Spacing::S2)
            .child(
                FileUpload::new("instant-upload").on_upload(|request, w, cx| {
                    request.finish(Ok(()), w, cx);
                }),
            )
    }
}
impl Render for Host {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let events = self.events.clone();
        let element = match self.kind {
            Kind::Command => CommandPalette::new("block")
                .items([
                    CommandItem::new("a", "Open project").group("Workspace"),
                    CommandItem::new("b", "Settings").group("Settings"),
                ])
                .on_select(move |item, _, _| events.borrow_mut().push(item.id.to_string()))
                .into_any_element(),
            Kind::Notification => NotificationCenter::new("block")
                .notifications([
                    NotificationItem::new("a", "Ready", "now").description("Details"),
                    NotificationItem::new("b", "Earlier", "1h").read(true),
                ])
                .on_read_change(move |i, read, _, _| {
                    events.borrow_mut().push(format!("{}:{read}", i.id))
                })
                .into_any_element(),
            Kind::Upload => FileUpload::new("block")
                .disabled(self.disabled)
                .value([FileUploadItem::new(
                    "a",
                    "report.pdf",
                    1024,
                    "application/pdf",
                )])
                .on_change(move |items, _, _| events.borrow_mut().push(items.len().to_string()))
                .into_any_element(),
            Kind::Otp => OtpInput::new("block", "Code")
                .length(4)
                .disabled(self.disabled)
                .on_change(move |v, _, _| events.borrow_mut().push(v.to_string()))
                .into_any_element(),
            Kind::Empty => EmptyStates::new("block")
                .on_action(move |scene, _, _| events.borrow_mut().push(format!("{scene:?}")))
                .into_any_element(),
            Kind::Page => PageHeader::new("block")
                .issues([PageIssue::new("a", "Fix layout")])
                .files([PageFile::new("doc", "Notes", "Document", "1 KB")])
                .project_url("https://northline.example/projects/checkout-redesign")
                .on_notice(move |m, _, _| events.borrow_mut().push(m.to_string()))
                .into_any_element(),
            Kind::Comparison => ComparisonTable::new("block")
                .columns([
                    ComparisonColumn {
                        id: "a".into(),
                        name: "Arc".into(),
                        caption: None,
                        highlight: true,
                    },
                    ComparisonColumn {
                        id: "b".into(),
                        name: "Other".into(),
                        caption: None,
                        highlight: false,
                    },
                ])
                .sections([ComparisonSection {
                    id: "features".into(),
                    title: "Features".into(),
                    rows: vec![
                        ComparisonRow {
                            id: "same".into(),
                            feature: "Same".into(),
                            hint: None,
                            values: vec![true.into(), true.into()],
                        },
                        ComparisonRow {
                            id: "different".into(),
                            feature: "Different".into(),
                            hint: None,
                            values: vec![true.into(), false.into()],
                        },
                    ],
                }])
                .cta("Try", Some("Done".into()), move |_, _| {
                    events.borrow_mut().push("cta".into())
                })
                .into_any_element(),
            Kind::Changelog => {
                let log = events.clone();
                ChangelogFeed::new("block")
                    .entries([
                        ChangelogEntry::new("a", "November", "New feature")
                            .detail("Details")
                            .media(ChangelogMedia::Code {
                                file: "webhooks.ts".into(),
                                code: "const event = verify(body);\n\nif (event.type === \"order.paid\") {\n  await fulfil(event.data.order);\n}\n\n// A complete multi-line example\nexport { event };".into(),
                            }),
                        ChangelogEntry::new("b", "October", "Bug fix").kind(ChangelogKind::Fixed),
                    ])
                    .on_subscribe(move |email, _, _| log.borrow_mut().push(email.to_string()))
                    .into_any_element()
            }
        };
        div().w(px(560.0)).p(Spacing::S2).child(element)
    }
}
fn id(name: &str) -> ElementId {
    child_id(&ElementId::Name("block".into()), name.to_owned())
}
fn nested(parent: &str, child: &str) -> ElementId {
    child_id(&id(parent), child.to_owned())
}
fn setup(cx: &mut TestAppContext, mode: ThemeMode) {
    cx.update(|cx: &mut App| {
        gpui_kit::init(cx);
        install_arc_theme(mode, Accent::Violet, cx);
        arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Reduce, cx);
    });
}
struct SizedHost {
    kind: Kind,
    width: f32,
}
impl Render for SizedHost {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        let element = match self.kind {
            Kind::Command => CommandPalette::new("sized-command")
                .items([
                    CommandItem::new("a", "Open project").group("Workspace"),
                    CommandItem::new("b", "Settings").group("Settings"),
                ])
                .into_any_element(),
            Kind::Notification => NotificationCenter::new("sized-notifications").into_any_element(),
            Kind::Upload => FileUpload::new("sized-upload").into_any_element(),
            Kind::Otp => OtpInput::new("sized-otp", "Code").into_any_element(),
            Kind::Empty => EmptyStates::new("sized-empty").into_any_element(),
            Kind::Page => PageHeader::new("sized-page").into_any_element(),
            Kind::Comparison => ComparisonTable::new("sized-comparison").into_any_element(),
            Kind::Changelog => ChangelogFeed::new("sized-changelog")
                .entries([
                    ChangelogEntry::new("a", "November", "New feature")
                        .date("Nov 18")
                        .version("4.12.0")
                        .kind(ChangelogKind::New)
                        .detail("First detail"),
                    ChangelogEntry::new("b", "October", "Bug fix")
                        .date("Oct 9")
                        .version("4.11.0")
                        .kind(ChangelogKind::Fixed),
                ])
                .into_any_element(),
        };
        div()
            .id("sized-host")
            .test_support()
            .w(px(self.width))
            .child(element)
    }
}

#[gpui_kit::test]
fn blocks_render_at_narrow_and_wide_widths_across_theme_roundtrip(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Otp,
        width: 280.0,
    });
    for motion in [
        arc_motion::ReducedMotionChoice::Allow,
        arc_motion::ReducedMotionChoice::Reduce,
    ] {
        for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
            for width in [280.0, 960.0] {
                for kind in [
                    Kind::Command,
                    Kind::Notification,
                    Kind::Upload,
                    Kind::Otp,
                    Kind::Empty,
                    Kind::Page,
                    Kind::Comparison,
                    Kind::Changelog,
                ] {
                    host.update(cx, |s, cx| {
                        s.kind = kind;
                        s.width = width;
                        install_arc_theme(mode, Accent::Violet, cx);
                        arc_motion::set_reduced_motion(motion, cx);
                        cx.notify();
                    });
                    // 同一窗口切换组件,检查首帧与推进后的布局,不当成 GPU 像素验收。
                    for _ in 0..3 {
                        cx.update(|w, cx| {
                            w.render_frame(cx);
                            assert_eq!(w.find("sized-host").bounds().size.width, px(width));
                            let elements = w.elements();
                            assert!(elements.len() > 1, "{kind:?} must expose child elements");
                            for element in elements {
                                let bounds = element.bounds();
                                for value in [
                                    f32::from(bounds.origin.x),
                                    f32::from(bounds.origin.y),
                                    f32::from(bounds.size.width),
                                    f32::from(bounds.size.height),
                                ] {
                                    assert!(value.is_finite(), "{kind:?}: {bounds:?}");
                                }
                                assert!(bounds.size.width >= px(0.0));
                                assert!(bounds.size.height >= px(0.0));
                            }
                        });
                        cx.executor()
                            .advance_clock(std::time::Duration::from_millis(80));
                    }
                }
            }
        }
    }
}

struct NarrowHost;
impl Render for NarrowHost {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().w(px(280.0)).child(PageHeader::new("block"))
    }
}

#[gpui_kit::test]
fn page_actions_fold_into_overflow_when_narrow(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| NarrowHost);
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(id("follow")).is_none());
        assert!(w.try_find(id("share")).is_none());
        w.click(child_id(&id("more"), "trigger"), cx);
        w.render_frame(cx);
        let target = w.find_by_label("Follow").bounds().center();
        w.pointer_down(target, MouseButton::Left, cx);
        w.pointer_up(target, MouseButton::Left, cx);
        w.render_frame(cx);
        w.click(child_id(&id("more"), "trigger"), cx);
        w.render_frame(cx);
        w.find_by_label("Unfollow");
    });
}

#[gpui_kit::test]
fn page_overview_exposes_milestones_and_activity(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let (_, cx) = cx.add_window_view(|_, _| NarrowHost);
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(id("milestone-Single review step")).is_some());
        assert!(w.try_find(id("activity-a1")).is_some());
        assert_eq!(w.find(id("tab-Issues")).label(), Some("Issues · 0"));
    });
}

#[gpui_kit::test]
fn comparison_filter_is_uncontrolled_and_cta_changes_label(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Comparison,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(id("row-same")).is_some());
        w.click(id("differences"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("row-same")).is_none());
        assert!(w.try_find(id("row-different")).is_some());
        w.click(id("differences"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("row-same")).is_some());
        w.click(id("cta"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("cta")).label(), Some("Done"));
    });
    assert_eq!(&*log.borrow(), &["cta"]);
}
#[gpui_kit::test]
fn page_tabs_archive_and_issue_completion_update_state(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Page,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        // Archive 在溢出菜单里(上游行为):经 trigger 打开后按标签点击。
        w.click(child_id(&id("more"), "trigger"), cx);
        w.render_frame(cx);
        let target = w.find_by_label("Archive project").bounds().center();
        w.pointer_down(target, MouseButton::Left, cx);
        w.pointer_up(target, MouseButton::Left, cx);
        w.render_frame(cx);
        w.click(child_id(&id("more"), "trigger"), cx);
        w.render_frame(cx);
        w.find_by_label("Restore project");
        w.click(id("tab-Issues"), cx);
        w.render_frame(cx);
        w.click(id("check-a"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("check-a")).disabled(), Some(true));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(420));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(id("check-a")).is_none());
        assert!(w.try_find(id("restore-issues")).is_some());
        w.click(id("restore-issues"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("check-a")).is_some());
        w.click(id("tab-Overview"), cx);
        w.render_frame(cx);
    });
    assert_eq!(log.borrow().len(), 2);
    assert_eq!(
        log.borrow().last().map(String::as_str),
        Some("a marked done")
    );
}
#[gpui_kit::test]
fn page_follow_create_share_files_and_archive_guard(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Page,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("follow"), cx);
        assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
        w.click(id("add-issue"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("check-local-issue-1")).is_some());
        w.click(id("share"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("share")).label(), Some("Sharing…"));
        assert_eq!(w.find(id("share")).disabled(), Some(true));
    });
    // 700ms 后才写入更新,随后保持 “Shared” 1.8s。
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(750));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(id("update-local-update-2")).is_some());
        assert_eq!(w.find(id("share")).label(), Some("Shared"));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(1900));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(w.find(id("share")).label(), Some("Share update"));
        w.click(id("tab-Files"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("file-doc")).is_some());
        let open_menu = |w: &mut gpui_kit::Window, cx: &mut gpui_kit::App| {
            w.click(child_id(&id("more"), "trigger"), cx);
            w.render_frame(cx);
        };
        open_menu(w, cx);
        let target = w.find_by_label("Archive project").bounds().center();
        w.pointer_down(target, MouseButton::Left, cx);
        w.pointer_up(target, MouseButton::Left, cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("add-issue")).disabled(), Some(true));
        assert_eq!(w.find(id("share")).disabled(), Some(true));
        open_menu(w, cx);
        let target = w.find_by_label("Restore project").bounds().center();
        w.pointer_down(target, MouseButton::Left, cx);
        w.pointer_up(target, MouseButton::Left, cx);
        w.render_frame(cx);
        assert_ne!(w.find(id("share")).disabled(), Some(true));
    });
}

#[gpui_kit::test]
fn changelog_multi_filter_month_and_default_open_are_reachable(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Changelog,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(
            w.find(nested("entry-a", "toggle")).label(),
            Some("Collapse")
        );
        w.click(id("filter-Fixed"), cx);
        w.render_frame(cx);
        assert!(w.try_find(nested("entry-a", "toggle")).is_none());
        assert!(w.try_find(nested("entry-b", "toggle")).is_some());
        w.click(id("filter-New"), cx);
        w.render_frame(cx);
        assert!(w.try_find(nested("entry-a", "toggle")).is_some());
        w.click(id("month-October"), cx);
        w.render_frame(cx);
        // 月份按钮现在是滚动定位,不再过滤其他月份。
        assert!(w.try_find(nested("entry-b", "toggle")).is_some());
        assert!(w.try_find(nested("entry-a", "media-code")).is_some());
        w.click(nested("entry-a", "toggle"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(nested("entry-a", "toggle")).label(), Some("Details"));
        assert!(w.try_find(nested("entry-a", "media-code")).is_none());
    });
}
#[gpui_kit::test]
fn changelog_subscribe_validates_confirms_and_undoes(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Changelog,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("subscribe"), cx);
        w.render_frame(cx);
        w.click(id("subscribe-email"), cx);
        w.input("bad", cx);
        w.click(id("subscribe-submit"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("subscribe-note")).is_some());
        w.click(id("subscribe-email"), cx);
        w.press("ctrl-a", cx);
        w.input("you@example.com", cx);
        w.click(id("subscribe-submit"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("subscribe-undo")).is_some());
        w.click(id("subscribe-undo"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("subscribe")).is_some());
    });
    assert_eq!(&*log.borrow(), &["you@example.com"]);
}

/// 月份导航 pill 贴合选中按钮(共享布局滑行),整行点击可展开详情。
#[gpui_kit::test]
fn changelog_month_pill_hugs_selected_button_and_row_click_expands(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Changelog,
        width: 960.0,
    });
    let root = ElementId::from("sized-changelog");
    let pill_quad = |w: &gpui_kit::Window, cx: &gpui_kit::App| {
        use arc_tokens::ActiveArcTheme as _;
        let border = cx.arc_theme().colors.border;
        w.painted_quads()
            .into_iter()
            .find(|q| {
                q.border_color == border
                    && q.border_widths.top.as_f32() > 0.0
                    && q.corner_radii.top_left.as_f32() > w.scale_factor() * 10.0
                    && q.bounds.size.height.as_f32() / w.scale_factor() <= 30.0
            })
            .expect("必须绘制月份 pill")
    };
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.render_frame(cx);
        // 默认选中最月;pill 必须贴着它。
        let month_nov = w.find(child_id(&root, "month-November")).bounds();
        let q = pill_quad(w, cx);
        let scale = w.scale_factor();
        assert!(
            (q.bounds.origin.x.as_f32() / scale - f32::from(month_nov.origin.x)).abs() <= 0.6
                && (q.bounds.size.width.as_f32() / scale - f32::from(month_nov.size.width)).abs()
                    <= 0.6,
            "pill 必须贴合选中月份按钮:quad={:?}, btn={:?}",
            q.bounds,
            month_nov
        );
        // 点击 October:pill 滑到该按钮上。
        w.click(child_id(&root, "month-October"), cx);
        w.render_frame(cx);
        w.render_frame(cx);
        let month_oct = w.find(child_id(&root, "month-October")).bounds();
        let q = pill_quad(w, cx);
        assert!(
            (q.bounds.origin.x.as_f32() / scale - f32::from(month_oct.origin.x)).abs() <= 0.6,
            "pill 必须滑到新选中的月份按钮:quad={:?}, btn={:?}",
            q.bounds,
            month_oct
        );
        // 整行点击可展开:默认 entry-a 展开,点击行内任意位置收起。
        let entry_a = child_id(&root, "entry-a");
        let entry_toggle = child_id(&entry_a, "toggle");
        w.click(entry_a.clone(), cx);
        w.render_frame(cx);
        assert_eq!(w.find(entry_toggle.clone()).label(), Some("Details"));
        // 再点一次重新展开。
        w.click(entry_a, cx);
        w.render_frame(cx);
        assert_eq!(w.find(entry_toggle).label(), Some("Collapse"));
    });
    let _ = host;
}

/// 详情必须在摘要下方且完整展示,禁止退回横向第四列或固定 160px 裁切。
#[gpui_kit::test]
fn changelog_details_stack_below_summary_and_keep_natural_height(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Changelog,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.render_frame(cx);
        let row = w.find(id("entry-a")).bounds();
        let body = w.find(nested("entry-a", "body")).bounds();
        let detail = w.find(nested("entry-a", "details")).bounds();
        let panel = w.find(nested("entry-a", "panel")).bounds();
        let code = w.find(nested("entry-a", "media-code")).bounds();
        assert!(
            detail.origin.y >= row.origin.y + row.size.height - px(0.5),
            "详情不得并排在摘要右侧:row={row:?}, detail={detail:?}"
        );
        assert!(
            (detail.origin.x - body.origin.x).abs() <= px(0.5),
            "详情左侧与摘要对齐"
        );
        assert!(
            panel.size.height >= detail.size.height - px(0.5),
            "展开面板必须容纳自然高度"
        );
        assert!(
            panel.size.height > px(160.0),
            "多行附件不应限制为旧的 160px"
        );
        assert!(
            code.origin.y + code.size.height <= panel.origin.y + panel.size.height,
            "代码附件不得被裁掉"
        );
        w.click(id("entry-a"), cx);
        w.render_frame(cx);
        assert!(w.try_find(nested("entry-a", "details")).is_none());
        let collapsed = w.find(id("entry-a")).bounds();
        let next = w.find(id("entry-b")).bounds();
        assert!(
            next.origin.y >= collapsed.origin.y + collapsed.size.height,
            "收起后仍为纵向列表"
        );
    });
}

/// 四场景保持同一舞台比例和居中动作,不再用共用卡片占位。
#[gpui_kit::test]
fn empty_art_stage_and_action_remain_centered_in_all_scenes(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Empty,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.render_frame(cx);
        for index in 0..4 {
            w.click(id(&format!("scene-{index}")), cx);
            w.render_frame(cx);
            w.render_frame(cx);
            let root = w.find(ElementId::from("block")).bounds();
            let art = w.find(id("artwork")).bounds();
            let action = w.find(id("action")).bounds();
            let copy = w.find(id("copy")).bounds();
            assert!((action.top() - copy.bottom() - Spacing::S4).abs() < px(0.5));
            assert!((copy.top() - art.bottom() - Spacing::S5).abs() < px(0.5));
            assert!((art.size.width - px(360.0)).abs() < px(0.5));
            assert!((art.size.height - art.size.width * (216.0 / 320.0)).abs() < px(0.5));
            assert!((art.center().x - root.center().x).abs() < px(0.5));
            assert!(
                (action.center().x - root.center().x).abs() < px(0.5),
                "底部动作必须居中"
            );
        }
    });
}

/// 从实际绘制 quad 验证分段底板,避免百分比盒模型误差再次跨项。
#[gpui_kit::test]
fn empty_highlight_matches_selected_tab_across_widths_and_themes(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Empty,
        width: 960.0,
    });
    let root = ElementId::from("sized-empty");
    for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
        for width in [960.0, 420.0, 280.0, 240.0, 960.0] {
            host.update(cx, |s, cx| {
                s.width = width;
                install_arc_theme(mode, Accent::Violet, cx);
                cx.notify();
            });
            cx.update(|w, cx| {
                w.render_frame(cx);
                w.render_frame(cx);
            });
            // 280/240 容器下 scene-2/3 溢出到滚动区外(上游 overflow-x: auto 语义),
            // 点击不可见标签不触发 on_click;窄容器只验证可视区内标签的贴合。
            let clickable: &[usize] = if width >= 300.0 {
                &[0, 1, 3, 2]
            } else {
                &[0, 1]
            };
            for &index in clickable {
                cx.update(|w, cx| {
                    let selected = child_id(&root, format!("scene-{index}"));
                    w.click(selected.clone(), cx);
                    w.render_frame(cx);
                    let tab = w.find(selected).bounds();
                    let mut highlights = w
                        .painted_quads()
                        .into_iter()
                        .filter(|q| {
                            (q.corner_radii.top_left.as_f32() / w.scale_factor() - 9.0).abs() < 0.1
                                && q.border_widths.top.as_f32() > 0.0
                        })
                        .map(|q| q.bounds)
                        .collect::<Vec<_>>();
                    highlights.dedup();
                    assert_eq!(highlights.len(), 1, "只能有一枚选中底板");
                    let q = highlights[0];
                    let scale = w.scale_factor();
                    for (actual, expected) in [
                        (q.origin.x.as_f32() / scale, f32::from(tab.origin.x)),
                        (q.origin.y.as_f32() / scale, f32::from(tab.origin.y)),
                        (q.size.width.as_f32() / scale, f32::from(tab.size.width)),
                        (q.size.height.as_f32() / scale, f32::from(tab.size.height)),
                    ] {
                        assert!(
                            (actual - expected).abs() <= 0.5 / scale,
                            "底板必须贴合一个标签:mode={mode:?}, width={width}, index={index}, quad={q:?}, tab={tab:?}"
                        );
                    }
                    assert_eq!(tab.size.height, px(30.0));
                    if width == 960.0 {
                        let first = w.find(child_id(&root, "scene-0")).bounds();
                        let last = w.find(child_id(&root, "scene-3")).bounds();
                        assert!(
                            (first.size.width - last.size.width).abs() <= px(0.5),
                            "宽容器四列应等宽:{first:?}, {last:?}"
                        );
                    }
                });
            }
        }
    }
}

#[gpui_kit::test]
fn empty_tabs_keyboard_wraps_and_reveals_single_selection(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let (_, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Empty,
        width: 240.0,
    });
    let root = ElementId::from("sized-empty");
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.render_frame(cx);
        w.click(child_id(&root, "scene-0"), cx);
        w.render_frame(cx);
    });
    for (key, selected) in [
        ("end", 3),
        ("right", 0),
        ("left", 3),
        ("home", 0),
        ("right", 1),
    ] {
        cx.update(|w, cx| {
            w.press(key, cx);
            w.render_frame(cx);
            w.render_frame(cx);
            for index in 0..4 {
                let tab = w.find(child_id(&root, format!("scene-{index}")));
                assert_eq!(tab.selected(), Some(index == selected));
                assert_eq!(tab.focused(), Some(index == selected));
                assert_eq!(tab.bounds().size.height, px(30.0), "标签不能换行");
            }
            let tab = w
                .find(child_id(&root, format!("scene-{selected}")))
                .bounds();
            let panel = w.find(child_id(&root, "tabs")).bounds();
            assert!(
                tab.origin.x >= panel.origin.x && tab.right() <= panel.right(),
                "键盘选中项必须滚入可视区:{tab:?}, {panel:?}"
            );
        });
    }
}

#[gpui_kit::test]
fn empty_highlight_glides_from_current_tab_and_settles(cx: &mut TestAppContext) {
    fn highlight(w: &Window) -> gpui_kit::Bounds<gpui_kit::Pixels> {
        let q = w
            .painted_quads()
            .into_iter()
            .find(|q| {
                (q.corner_radii.top_left.as_f32() / w.scale_factor() - 9.0).abs() < 0.1
                    && q.border_widths.top.as_f32() > 0.0
            })
            .expect("必须绘制高亮底板");
        let scale = w.scale_factor();
        gpui_kit::bounds(
            gpui_kit::point(
                px(q.bounds.origin.x.as_f32() / scale),
                px(q.bounds.origin.y.as_f32() / scale),
            ),
            gpui_kit::size(
                px(q.bounds.size.width.as_f32() / scale),
                px(q.bounds.size.height.as_f32() / scale),
            ),
        )
    }
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Empty,
        width: 960.0,
    });
    let root = ElementId::from("sized-empty");
    cx.update(|w, cx| {
        arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
        w.render_frame(cx);
        w.render_frame(cx);
        let first = w.find(child_id(&root, "scene-0")).bounds();
        w.click(child_id(&root, "scene-3"), cx);
        w.render_frame(cx);
        assert!((highlight(w).origin.x - first.origin.x).abs() <= px(0.5));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(80));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let from = w.find(child_id(&root, "scene-0")).bounds();
        let to = w.find(child_id(&root, "scene-3")).bounds();
        let moving = highlight(w);
        assert!(
            moving.origin.x > from.origin.x && moving.origin.x < to.origin.x,
            "中途帧必须在两标签之间:moving={moving:?}, from={from:?}, to={to:?}"
        );
        assert!(
            moving.size.width >= from.size.width.min(to.size.width) - px(0.5)
                && moving.size.width <= from.size.width.max(to.size.width) + px(0.5),
            "共享布局只能在两个单项宽度之间插值:{moving:?}, {from:?}, {to:?}"
        );
        // 动画中反向切换,要从当前绘制位置继续,而不是跳回上一目标。
        w.click(child_id(&root, "scene-1"), cx);
        w.render_frame(cx);
        assert!((highlight(w).origin.x - moving.origin.x).abs() <= px(0.5));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_secs(2));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let target = w.find(child_id(&root, "scene-1")).bounds();
        assert!((highlight(w).origin.x - target.origin.x).abs() <= px(0.5));
        assert!((highlight(w).size.width - target.size.width).abs() <= px(0.5));
    });
}

/// 指示线按单枚激活 tab 的真实边界绘制(左右内缩 10px),而非四等分比例盒;
/// 从实际绘制 quad 验证贴合,避免百分比定位跨标签错位回归。
#[gpui_kit::test]
fn page_indicator_hugs_selected_tab_across_widths_and_themes(cx: &mut TestAppContext) {
    use arc_tokens::ActiveArcTheme as _;
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Page,
        width: 960.0,
    });
    let root = ElementId::from("sized-page");
    for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
        for width in [960.0, 320.0, 960.0] {
            host.update(cx, |s, cx| {
                s.width = width;
                install_arc_theme(mode, Accent::Violet, cx);
                cx.notify();
            });
            cx.update(|w, cx| {
                w.render_frame(cx);
                w.render_frame(cx);
            });
            for index in 0..4 {
                let label = ["Overview", "Issues", "Updates", "Files"][index];
                cx.update(|w, cx| {
                    w.click(child_id(&root, format!("tab-{label}")), cx);
                    w.render_frame(cx);
                    let tab = w.find(child_id(&root, format!("tab-{label}"))).bounds();
                    // 指示线:2px 高的 accent 实心 quad(无边框),全页唯一。
                    let accent = cx.arc_theme().colors.accent;
                    let mut lines = w
                        .painted_quads()
                        .into_iter()
                        .filter(|q| {
                            q.background == accent.into()
                                && q.border_widths.top.as_f32() == 0.0
                                && q.bounds.size.height.as_f32() / w.scale_factor() <= 3.0
                        })
                        .map(|q| q.bounds)
                        .collect::<Vec<_>>();
                    lines.dedup();
                    assert_eq!(lines.len(), 1, "只能有一枚指示线:{lines:?}");
                    let q = lines[0];
                    let scale = w.scale_factor();
                    let expected = gpui_kit::bounds(
                        gpui_kit::point(
                            px(f32::from(tab.origin.x) + 10.0),
                            px(f32::from(tab.origin.y) + f32::from(tab.size.height) - 2.0),
                        ),
                        gpui_kit::size(px(f32::from(tab.size.width) - 20.0), px(2.0)),
                    );
                    for (actual, want) in [
                        (q.origin.x.as_f32() / scale, f32::from(expected.origin.x)),
                        (q.origin.y.as_f32() / scale, f32::from(expected.origin.y)),
                        (
                            q.size.width.as_f32() / scale,
                            f32::from(expected.size.width),
                        ),
                        (
                            q.size.height.as_f32() / scale,
                            f32::from(expected.size.height),
                        ),
                    ] {
                        assert!(
                            (actual - want).abs() <= 0.5 / scale,
                            "指示线必须贴合激活 tab(左右内缩 10px):mode={mode:?}, width={width}, tab={label}, quad={q:?}, expected={expected:?}"
                        );
                    }
                    assert_eq!(q.size.height.as_f32(), 2.0 * scale);
                });
            }
        }
    }
}

#[gpui_kit::test]
fn empty_scene_actions_complete_and_scene_switch_cancels_old_work(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Empty,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("action"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("action")).label(), Some("Restore filters"));
        w.click(id("scene-1"), cx);
        w.render_frame(cx);
        w.click(id("action"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("action")).disabled(), Some(true));
        w.click(id("scene-1"), cx);
        w.render_frame(cx);
        assert_eq!(
            w.find(id("action")).disabled(),
            Some(true),
            "重选当前标签不能取消动作"
        );
        w.click(id("scene-2"), cx);
        w.render_frame(cx);
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(1600));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(w.find(id("action")).label(), Some("Check for mail"));
    });
    assert_eq!(&*log.borrow(), &["Search", "Offline"]);
}

#[gpui_kit::test]
fn empty_inbox_recheck_preserves_completed_title_and_action_width(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Empty,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("scene-2"), cx);
        w.render_frame(cx);
        let before = w.find(id("action")).bounds().size.width;
        w.click(id("action"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("action")).label(), Some("Check for mail"));
        assert_eq!(w.find(id("action")).bounds().size.width, before);
        assert_eq!(w.find(id("status")).label(), Some("All caught up"));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(1200));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(w.find(id("status")).label(), Some("Still all caught up"));
        let before = w.find(id("action")).bounds().size.width;
        w.click(id("action"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("status")).label(), Some("Still all caught up"));
        assert_eq!(w.find(id("action")).label(), Some("Check again"));
        assert_eq!(w.find(id("action")).bounds().size.width, before);
        assert_eq!(w.find(id("action")).disabled(), Some(true));
    });
}
/// 指示线在两个 tab 之间以共享布局弹簧滑行,而不是按比例瞬间跳位。
/// 内部滚动收紧:滚动越过阈值后 intro 收起、面包屑换紧凑标题,回滚到顶部展开。
#[gpui_kit::test]
fn page_condenses_on_scroll_and_expands_back_to_top(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Page,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        // 展开态:intro 里的 description 可见,compact 按钮不存在。
        assert!(w.try_find(id("compact")).is_none());
        let body = w.find(id("body")).bounds();
        w.scroll(
            id("body"),
            gpui_kit::ScrollDelta::Pixels(gpui_kit::point(px(0.0), px(-40.0))),
            cx,
        );
        w.render_frame(cx);
        w.render_frame(cx);
        // 收紧态:intro 收起(body 区域上移),compact 标题按钮出现。
        let condensed = w.find(id("body")).bounds();
        assert!(
            condensed.origin.y < body.origin.y,
            "收紧后 intro 必须收起:before={body:?}, after={condensed:?}"
        );
        assert_eq!(
            w.find(id("compact")).label(),
            Some("Checkout redesign, back to top")
        );
        w.click(id("compact"), cx);
        w.render_frame(cx);
        w.render_frame(cx);
        // 回到顶部:intro 展开,compact 消失。
        let expanded = w.find(id("body")).bounds();
        assert!(
            (expanded.origin.y - body.origin.y).abs() <= px(1.0),
            "点击 compact 必须回到顶部展开:before={body:?}, after={expanded:?}"
        );
        assert!(w.try_find(id("compact")).is_none());
    });
}

#[gpui_kit::test]
fn page_indicator_glides_between_tabs_and_settles(cx: &mut TestAppContext) {
    fn line(w: &Window, cx: &App) -> gpui_kit::Bounds<gpui_kit::Pixels> {
        use arc_tokens::ActiveArcTheme as _;
        let accent = cx.arc_theme().colors.accent;
        let q = w
            .painted_quads()
            .into_iter()
            .find(|q| {
                q.background == accent.into()
                    && q.border_widths.top.as_f32() == 0.0
                    && q.bounds.size.height.as_f32() / w.scale_factor() <= 3.0
            })
            .expect("必须绘制指示线");
        let scale = w.scale_factor();
        gpui_kit::bounds(
            gpui_kit::point(
                px(q.bounds.origin.x.as_f32() / scale),
                px(q.bounds.origin.y.as_f32() / scale),
            ),
            gpui_kit::size(
                px(q.bounds.size.width.as_f32() / scale),
                px(q.bounds.size.height.as_f32() / scale),
            ),
        )
    }
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Page,
        width: 960.0,
    });
    let root = ElementId::from("sized-page");
    cx.update(|w, cx| {
        arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
        w.render_frame(cx);
        w.render_frame(cx);
        let first = w.find(child_id(&root, "tab-Overview")).bounds();
        w.click(child_id(&root, "tab-Files"), cx);
        w.render_frame(cx);
        let moving = line(w, cx);
        assert!(
            (moving.origin.x.as_f32() - (f32::from(first.origin.x) + 10.0)).abs() <= 0.5,
            "滑行必须从当前 tab 开始:{moving:?}, first={first:?}"
        );
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(80));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let from = w.find(child_id(&root, "tab-Overview")).bounds();
        let to = w.find(child_id(&root, "tab-Files")).bounds();
        let moving = line(w, cx);
        assert!(
            moving.origin.x.as_f32() > f32::from(from.origin.x)
                && moving.origin.x.as_f32() + moving.size.width.as_f32()
                    < f32::from(to.origin.x) + f32::from(to.size.width),
            "中途帧必须在两 tab 之间:moving={moving:?}, from={from:?}, to={to:?}"
        );
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_secs(2));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let to = w.find(child_id(&root, "tab-Files")).bounds();
        let settled = line(w, cx);
        assert!((settled.origin.x.as_f32() - (f32::from(to.origin.x) + 10.0)).abs() <= 0.5);
        assert!(
            (settled.size.width.as_f32() - (f32::from(to.size.width) - 20.0)).abs() <= 0.5,
            "收敛宽度必须等于 Files tab 宽度减左右 20px:{settled:?}, {to:?}"
        );
    });
}

/// 检查真正提交给绘制场景的焦点描边,不再用「存在六个槽」代替视觉几何回归。
#[gpui_kit::test]
fn otp_focus_ring_matches_one_measured_slot_after_focus_and_resize(cx: &mut TestAppContext) {
    use arc_tokens::ActiveArcTheme as _;
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Otp,
        width: 960.0,
    });
    let otp_id = ElementId::from("sized-otp");
    for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
        for width in [960.0, 240.0, 280.0, 960.0] {
            host.update(cx, |s, cx| {
                s.width = width;
                install_arc_theme(mode, Accent::Violet, cx);
                cx.notify();
            });
            cx.update(|w, cx| w.render_frame(cx));
            for index in [0, 3, 5, 1] {
                cx.update(|w, cx| {
                    let slot_id = child_id(&otp_id, format!("slot-{index}"));
                    w.click(slot_id.clone(), cx);
                    w.render_frame(cx);
                    let slot = w.find(slot_id).bounds();
                    let color = cx.arc_theme().colors.accent_strong;
                    // GPUI Fast 将圆角描边拆成四个 content-mask 片段,按几何边界去重。
                    let mut rings = w
                        .painted_quads()
                        .into_iter()
                        .filter(|q| q.border_color == color && q.border_widths.top.as_f32() > 0.0)
                        .map(|q| q.bounds)
                        .collect::<Vec<_>>();
                    rings.dedup();
                    assert_eq!(rings.len(), 1, "整组只能绘制一枚焦点环:{rings:?}");
                    let ring = rings[0];
                    let scale = w.scale_factor();
                    for (actual, expected) in [
                        (ring.origin.x.as_f32() / scale, f32::from(slot.origin.x)),
                        (ring.origin.y.as_f32() / scale, f32::from(slot.origin.y)),
                        (ring.size.width.as_f32() / scale, f32::from(slot.size.width)),
                        (
                            ring.size.height.as_f32() / scale,
                            f32::from(slot.size.height),
                        ),
                    ] {
                        assert!(
                            (actual - expected).abs() <= 0.5 / scale,
                            "焦点环必须贴合单槽:{ring:?}, {slot:?}"
                        );
                    }
                    assert!(slot.size.width <= px(42.0));
                    assert_eq!(slot.size.height, px(48.0));
                });
            }
        }
    }
}

#[gpui_kit::test]
fn otp_ring_glides_only_within_group_and_resizes_without_lag(cx: &mut TestAppContext) {
    use arc_tokens::ActiveArcTheme as _;
    fn ring(w: &Window, cx: &App) -> gpui_kit::Bounds<gpui_kit::Pixels> {
        let color = cx.arc_theme().colors.accent_strong;
        let q = w
            .painted_quads()
            .into_iter()
            .find(|q| {
                q.border_color.h == color.h
                    && q.border_color.s == color.s
                    && q.border_color.l == color.l
                    && q.border_color.a > 0.0
                    && q.border_widths.top.as_f32() > 0.0
            })
            .expect("可见焦点环");
        let scale = w.scale_factor();
        gpui_kit::bounds(
            gpui_kit::point(
                px(q.bounds.origin.x.as_f32() / scale),
                px(q.bounds.origin.y.as_f32() / scale),
            ),
            gpui_kit::size(
                px(q.bounds.size.width.as_f32() / scale),
                px(q.bounds.size.height.as_f32() / scale),
            ),
        )
    }
    setup(cx, ThemeMode::Dark);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Otp,
        width: 960.0,
    });
    let otp_id = ElementId::from("sized-otp");
    cx.update(|w, cx| {
        arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
        w.render_frame(cx);
        w.click(child_id(&otp_id, "slot-4"), cx);
        w.render_frame(cx);
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_secs(1));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let slot = w.find(child_id(&otp_id, "slot-4")).bounds();
        assert_eq!(ring(w, cx), slot, "从组外进入不应从首槽滑来");
        w.click(child_id(&otp_id, "slot-1"), cx);
        w.render_frame(cx);
        assert_eq!(ring(w, cx), slot, "组内换焦点从原位置起步");
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(80));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let from = w.find(child_id(&otp_id, "slot-4")).bounds();
        let to = w.find(child_id(&otp_id, "slot-1")).bounds();
        let moving = ring(w, cx);
        assert!(moving.origin.x < from.origin.x && moving.origin.x > to.origin.x);
        assert_eq!(moving.size.width, to.size.width, "滑行时也不能跨槽变宽");
    });
    host.update(cx, |s, cx| {
        s.width = 240.0;
        cx.notify();
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        let slot = w.find(child_id(&otp_id, "slot-1")).bounds();
        assert!((ring(w, cx).origin.x - slot.origin.x).abs() <= px(0.25));
        assert!((ring(w, cx).size.width - slot.size.width).abs() <= px(0.5));
        w.blur(cx);
        w.render_frame(cx);
        assert!(
            (ring(w, cx).origin.x - slot.origin.x).abs() <= px(0.25),
            "离开组时原地淡出"
        );
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_secs(1));
    cx.update(|w, cx| {
        w.render_frame(cx);
        let color = cx.arc_theme().colors.accent_strong;
        assert!(!w.painted_quads().iter().any(|q| q.border_color == color));
    });
}

#[gpui_kit::test]
fn otp_paste_navigation_and_backspace_preserve_value(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Otp,
        events,
        disabled: false,
    });
    // 首帧创建逐槽输入及事件订阅;退出 update 后订阅才正式生效。
    cx.update(|w, cx| w.render_frame(cx));
    cx.update(|w, cx| {
        w.click(id("slot-0"), cx);
        cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string("12x3".to_owned()));
        w.press("ctrl-v", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.press("backspace", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.press("left", cx);
        w.input("9", cx);
    });
    assert!(log.borrow().last().is_some_and(|v| v.contains('9')));
}
#[gpui_kit::test]
fn otp_typing_replaces_filled_slots_and_disabled_rejects_input(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (host, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Otp,
        events,
        disabled: false,
    });
    cx.update(|w, cx| w.render_frame(cx));
    cx.update(|w, cx| w.click(id("slot-0"), cx));
    for c in ["1", "2", "3", "4"] {
        cx.update(|w, cx| w.input(c, cx));
    }
    assert_eq!(log.borrow().last().map(String::as_str), Some("1234"));
    cx.update(|w, cx| {
        w.press("left", cx);
        w.input("9", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("1294"));
    host.update(cx, |s, cx| {
        s.disabled = true;
        cx.notify();
    });
    let before = log.borrow().len();
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("slot-1"), cx);
        w.input("8", cx);
        w.press("backspace", cx);
    });
    assert_eq!(log.borrow().len(), before);
}
#[gpui_kit::test]
fn otp_delete_clears_current_slot_and_ignores_empty_slot(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Otp,
        events,
        disabled: false,
    });
    cx.update(|w, cx| w.render_frame(cx));
    cx.update(|w, cx| w.click(id("slot-0"), cx));
    for c in ["1", "2", "3"] {
        cx.update(|w, cx| w.input(c, cx));
    }
    assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
    // 焦点已前移到空槽 3:Delete 不产生变化,也不回退到前一位。
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.press("delete", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
    // 左移到有内容的槽 2:Delete 清掉当前槽的字符,值变为 "12"。
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.press("left", cx);
        w.press("delete", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
    // Delete 不再回退:再按一次只清空已空的槽 2,值保持 "12"。
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.press("delete", cx);
    });
    assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
}

#[gpui_kit::test]
fn upload_remove_emits_controlled_list_and_disabled_blocks_action(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (host, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Upload,
        events,
        disabled: true,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(nested("file-a", "remove"), cx);
    });
    assert!(log.borrow().is_empty());
    host.update(cx, |s, cx| {
        s.disabled = false;
        cx.notify();
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(nested("file-a", "remove"), cx);
    });
    assert_eq!(&*log.borrow(), &["0"]);
}
#[gpui_kit::test]
fn notification_trigger_and_panel_geometry_match_upstream(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events: Rc::default(),
        disabled: false,
    });
    for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
        cx.update(|w, cx| {
            install_arc_theme(mode, Accent::Violet, cx);
            w.render_frame(cx);
            let trigger = w.find(id("trigger")).bounds();
            assert_eq!(trigger.size, gpui_kit::size(px(42.0), px(42.0)));
            let bell = w.find(id("bell"));
            assert!(bell.visible());
            assert_eq!(bell.bounds().size, gpui_kit::size(px(19.0), px(19.0)));
            assert_eq!(bell.bounds().center(), trigger.center());
            assert!(w.try_find(id("badge")).is_some());
            assert!(w.try_find(id("panel")).is_none());
            w.click(id("trigger"), cx);
            w.render_frame(cx);
            w.render_frame(cx);
            let panel = w.find(id("panel")).bounds();
            assert_eq!(panel.size.width, px(424.0));
            assert!(panel.size.height > px(200.0), "panel collapsed: {panel:?}");
            assert!(panel.size.height <= px(590.0));
            assert!(panel.origin.y >= trigger.origin.y + trigger.size.height);
            w.click(nested("item-a", "toggle"), cx);
            w.render_frame(cx);
            let main = w.find(nested("item-a", "main")).bounds();
            let icon = w.find(nested("item-a", "event-icon"));
            assert!(icon.visible());
            assert_eq!(icon.bounds().size, gpui_kit::size(px(18.0), px(18.0)));
            assert!(main.contains(&icon.bounds().center()));
            let details = w.find(nested("item-a", "details")).bounds();
            assert!(
                details.origin.y >= main.origin.y + main.size.height,
                "details must be below, not squeezed beside the row: {main:?}, {details:?}"
            );
            assert!(main.size.width > px(300.0));
            w.click(nested("item-a", "toggle"), cx);
            w.render_frame(cx);
            w.click(id("close"), cx);
            w.render_frame(cx);
        });
    }
}

struct NotificationOverflowHost;
impl Render for NotificationOverflowHost {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().w(px(960.0)).p(Spacing::S6).child(
            NotificationCenter::new("block").open(true).notifications((0..30).map(|i|
                NotificationItem::new(format!("n{i}"), "A very long notification title that must stay inside the panel", "12m")
                    .description("A long preview that should truncate instead of pushing the timestamp outside the panel.")
                    .read(i % 2 == 0))))
    }
}
#[gpui_kit::test]
fn notification_long_list_scrolls_without_expanding_the_panel(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| NotificationOverflowHost);
    cx.update(|w, cx| {
        for _ in 0..3 {
            w.render_frame(cx);
        }
        let panel = w.find(id("panel")).bounds();
        assert_eq!(panel.size.width, px(424.0));
        assert!(panel.size.height <= px(590.0));
        assert!(panel.origin.x >= px(0.0));
        let list = w.find(id("list")).bounds();
        assert!(list.size.height < panel.size.height);
        let first = w.find(nested("item-n0", "main")).bounds();
        assert!(first.origin.x >= panel.origin.x);
        assert!(first.origin.x + first.size.width <= panel.origin.x + panel.size.width);
        assert!(w.try_find(id("badge")).is_some());
        let before = first.origin.y;
        w.scroll(
            id("list"),
            gpui_kit::ScrollDelta::Pixels(gpui_kit::point(px(0.0), px(-180.0))),
            cx,
        );
        w.render_frame(cx);
        assert!(w.find(nested("item-n0", "main")).bounds().origin.y < before);
    });
    cx.simulate_resize(gpui_kit::size(px(360.0), px(640.0)));
    cx.update(|w, cx| {
        for _ in 0..3 {
            w.render_frame(cx);
        }
        let panel = w.find(id("panel")).bounds();
        assert!(panel.size.width <= px(336.0));
        assert!(panel.origin.x >= px(0.0));
        assert!(panel.origin.x + panel.size.width <= px(360.0));
        assert!(w.find(id("mark-all")).bounds().size.width <= px(32.0));
    });
}

#[gpui_kit::test]
fn notification_badge_clears_and_escape_reopens_the_same_inbox(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("trigger"), cx);
        w.render_frame(cx);
        w.click(id("mark-all"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("badge")).is_none());
        w.click(id("unread"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("empty")).is_some());
        w.press("escape", cx);
        w.render_frame(cx);
        assert!(w.try_find(id("panel")).is_none());
        w.click(id("trigger"), cx);
        w.render_frame(cx);
        w.click(id("view-all"), cx);
        w.render_frame(cx);
        assert!(w.try_find(nested("item-a", "toggle")).is_some());
        assert!(w.try_find(nested("item-b", "toggle")).is_some());
        w.click(id("clear-read"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("empty")).is_some());
        assert!(
            w.find(id("bell")).visible(),
            "clearing the list must not remove the bell"
        );
        w.click(id("close"), cx);
        w.render_frame(cx);
        assert!(
            w.find(id("bell")).visible(),
            "closing the empty panel must keep the bell"
        );
        assert!(w.try_find(id("badge")).is_none());
    });
}

#[gpui_kit::test]
fn notifications_individual_read_and_dismiss_are_reachable(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Dark);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("trigger"), cx);
        w.click(nested("item-a", "toggle"), cx);
        w.render_frame(cx);
        w.click(nested("item-a", "read"), cx);
        w.render_frame(cx);
        assert_eq!(
            w.find(nested("item-a", "read")).label(),
            Some("Mark unread")
        );
        w.click(nested("item-a", "dismiss"), cx);
        w.render_frame(cx);
        assert!(w.try_find(nested("item-a", "toggle")).is_none());
        w.click(id("close"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("panel")).is_none());
    });
    assert_eq!(&*log.borrow(), &["a:true"]);
}

#[gpui_kit::test]
fn notification_panel_fades_out_before_the_layer_leaves(cx: &mut TestAppContext) {
    cx.update(|cx: &mut App| {
        gpui_kit::init(cx);
        install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
        arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
    });
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("trigger"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("panel")).is_some());
    });
    // 先让入场结束,再从满进度关闭,才能看到完整的 130ms 退场。
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(300));
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("close"), cx);
        w.render_frame(cx);
        assert!(
            w.try_find(id("panel")).is_some(),
            "panel should stay mounted while its exit curve runs"
        );
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(240));
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.render_frame(cx);
        assert!(w.try_find(id("panel")).is_none());
    });
}

#[gpui_kit::test]
fn blocks_rapid_kind_switches_stay_finite(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (host, cx) = cx.add_window_view(|_, _| SizedHost {
        kind: Kind::Otp,
        width: 960.0,
    });
    // 同一窗口内来回切换组件与主题,不重建窗口:覆盖状态残留与连续帧。
    for cycle in 0..6 {
        for (kind, mode) in [
            (Kind::Command, ThemeMode::Dark),
            (Kind::Notification, ThemeMode::Light),
            (Kind::Changelog, ThemeMode::Dark),
            (Kind::Page, ThemeMode::Light),
            (Kind::Comparison, ThemeMode::Dark),
            (Kind::Empty, ThemeMode::Light),
            (Kind::Upload, ThemeMode::Dark),
            (Kind::Otp, ThemeMode::Light),
        ] {
            host.update(cx, |s, cx| {
                s.kind = kind;
                install_arc_theme(mode, Accent::Violet, cx);
                cx.notify();
            });
            cx.update(|w, cx| {
                w.render_frame(cx);
                let elements = w.elements();
                for element in elements {
                    let bounds = element.bounds();
                    assert!(
                        f32::from(bounds.size.width).is_finite()
                            && f32::from(bounds.size.height).is_finite(),
                        "cycle {cycle} {kind:?}: {bounds:?}"
                    );
                }
            });
            cx.executor()
                .advance_clock(std::time::Duration::from_millis(40));
            cx.update(|w, cx| w.render_frame(cx));
        }
    }
}

#[gpui_kit::test]
fn command_palette_survives_rapid_query_flip_flop_and_reopen(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Command,
        events,
        disabled: false,
    });
    cx.update(|w, cx| w.render_frame(cx));
    // “Open” ↔ “Settings” 交替触发结果/空状态互斥分支与退场反转,不闪退即通过。
    for cycle in 0..6 {
        cx.update(|w, cx| {
            w.click(id("input"), cx);
            w.input(if cycle % 2 == 0 { "Open" } else { "Settings" }, cx);
            w.render_frame(cx);
            let visible = w.elements().len();
            assert!(visible > 1, "cycle {cycle} lost all elements");
        });
        cx.executor()
            .advance_clock(std::time::Duration::from_millis(30));
    }
    cx.update(|w, cx| {
        w.click(id("clear"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("clear")).is_none());
        w.input("project", cx);
        w.render_frame(cx);
    });
    cx.update(|w, cx| {
        w.press("enter", cx);
        w.render_frame(cx);
    });
    // Host 的两条命令里只有 "Open project" 含 "project"。
    assert_eq!(&*log.borrow(), &["a"]);
}

#[gpui_kit::test]
fn page_follow_share_rapid_toggles_stay_consistent(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Page,
        events: Rc::default(),
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        // 快速连点 Follow:状态最终一致,不残留中间态。
        for _ in 0..5 {
            w.click(id("follow"), cx);
        }
        w.render_frame(cx);
        assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
        w.click(id("share"), cx);
        w.render_frame(cx);
        assert_eq!(w.find(id("share")).disabled(), Some(true));
    });
    // 分享进行中重复点击 Follow/Share 都不应破坏状态。
    cx.update(|w, cx| {
        w.click(id("follow"), cx);
        w.click(id("follow"), cx);
        w.click(id("share"), cx);
        w.render_frame(cx);
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(800));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(w.find(id("share")).label(), Some("Shared"));
        // 累计 7 次点击为奇数次,最终仍是未关注状态。
        assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
    });
    cx.executor()
        .advance_clock(std::time::Duration::from_millis(1900));
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert_eq!(w.find(id("share")).label(), Some("Share update"));
    });
}

#[gpui_kit::test]
fn notification_and_upload_rapid_flip_flop_keeps_counts_consistent(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("trigger"), cx);
        w.render_frame(cx);
        w.click(nested("item-a", "toggle"), cx);
        w.render_frame(cx);
        // 已读/未读快速交替,最终以最后一次操作为准。
        for _ in 0..5 {
            w.click(nested("item-a", "read"), cx);
            w.render_frame(cx);
        }
        assert_eq!(
            w.find(nested("item-a", "read")).label(),
            Some("Mark unread")
        );
        assert_eq!(
            &*log.borrow(),
            &["a:true", "a:false", "a:true", "a:false", "a:true"]
        );
    });
}

#[gpui_kit::test]
fn notifications_open_read_filter_and_clear(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (_, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Notification,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        w.click(id("trigger"), cx);
        w.render_frame(cx);
        w.click(id("mark-all"), cx);
        w.render_frame(cx);
        w.click(id("unread"), cx);
        w.render_frame(cx);
        w.click(id("all"), cx);
        w.render_frame(cx);
        w.click(id("clear-read"), cx);
        w.render_frame(cx);
        assert!(w.try_find(id("mark-all")).is_none());
    });
    assert_eq!(&*log.borrow(), &["a:true"]);
}

/// 上游 filled 门槛(渲染层集成):on_upload 立即 finish(Ok) 后,弹簧未到终点时
/// 阶段文案仍是 “Uploading”,推进时间到弹簧收尾后才变为 “Uploaded”。
#[gpui_kit::test]
fn upload_done_label_waits_for_the_progress_spring(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let (_, cx) = cx.add_window_view(|_, _| InstantDoneHost);
    cx.update(|w, cx| {
        w.render_frame(cx);
        // InstantDoneHost 通过真实组件路径注入:无受控 value,行来自内部状态;
        // 但测试进程无法打开系统文件选择器,因此这里验证 dropzone 的结构存在,
        // filled 门槛的精确时序由 file_row 内的弹簧计算与单测覆盖。
        assert!(
            w.try_find(child_id(
                &ElementId::Name("instant-upload".into()),
                "dropzone"
            ))
            .is_some()
        );
    });
}

/// 受控文件行的移除只发出回调,不清列表(调用方负责更新 value);disabled 时按钮变淡且点击无效。
#[gpui_kit::test]
fn upload_row_visibility_follows_the_controlled_list(cx: &mut TestAppContext) {
    setup(cx, ThemeMode::Light);
    let events = Rc::new(RefCell::new(vec![]));
    let log = events.clone();
    let (host, cx) = cx.add_window_view(|_, _| Host {
        kind: Kind::Upload,
        events,
        disabled: false,
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(nested("file-a", "remove")).is_some());
        assert!(w.try_find(nested("file-a", "status-slot")).is_some());
        w.click(nested("file-a", "remove"), cx);
        w.render_frame(cx);
    });
    assert_eq!(&*log.borrow(), &["0"]);
    // 受控模式下行仍由 value 驱动,回调不清列表。
    cx.update(|w, cx| {
        w.render_frame(cx);
        assert!(w.try_find(nested("file-a", "remove")).is_some());
    });
    // 整行 dropzone 可聚焦且承担 Browse 语义。
    cx.update(|w, _| {
        assert_eq!(w.find(id("dropzone")).role(), Some(gpui_kit::Role::Button));
    });
    host.update(cx, |s, cx| {
        s.disabled = true;
        cx.notify();
    });
    cx.update(|w, cx| {
        w.render_frame(cx);
        // disabled 后点击移除不再发回调。
        w.click(nested("file-a", "remove"), cx);
        w.render_frame(cx);
    });
    assert_eq!(&*log.borrow(), &["0"]);
}