guise-ui 1.0.0

A component library for gpui, Zed's GPU-accelerated UI framework: a themed palette, sizing tokens, 130+ composable components, a reactive state layer, and an in-app Safari-style inspector.
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
//! Entity-level tests on gpui's test harness (`#[gpui::test]` +
//! `TestAppContext` — the same rig zed's own tests use). These cover the
//! wiring pure unit tests can't reach: signals, bindings, form observers,
//! entity events, and the theme global.
//!
//! Observer effects flush between `cx.update` blocks, so assertions that
//! depend on an observer firing sit in their own block.

use gpui::prelude::*;
use gpui::{div, Context, Entity, Modifiers, MouseButton, TestAppContext, Window};

use crate::ai::{AIChatView, AIComposer, AIComposerEvent, AITurn};
use crate::devtools::{
    DevTools, DevToolsEvent, DevToolsState, DevToolsTab, LogLevel, NetworkRecord, Probed,
    RequestState, SourceRef, StorageDomain, StorageEntry,
};
use crate::input::{Date, DatePicker, LineEditor as _, Select, TextInput};
use crate::reactive::{validators, Form, Signal};
use crate::theme::{theme, Color, Theme};
use crate::update::{
    is_installing, Release, UpdateNotice, UpdateNoticeEvent, UpdateOutcome, UpdatePrompt,
    UpdatePromptEvent, UpdateStage, Updater,
};
use crate::{Carousel, CarouselEvent};

#[gpui::test]
fn signal_binding_and_lens_round_trip(cx: &mut TestAppContext) {
    let count = cx.update(|cx| Signal::new(cx, 5_i32));
    let binding = count.binding();
    cx.update(|cx| {
        assert_eq!(binding.get(cx), 5);
        binding.set(cx, 9);
        assert_eq!(count.get(cx), 9);
    });

    #[derive(Clone, PartialEq)]
    struct Settings {
        muted: bool,
    }
    let settings = cx.update(|cx| Signal::new(cx, Settings { muted: false }));
    let muted = settings.lens(|s| s.muted, |s, v| s.muted = v);
    cx.update(|cx| {
        muted.set(cx, true);
        assert!(settings.read(cx).muted);
        // Mapped bindings convert both ways.
        let as_text = muted.map(|b| b.to_string(), |s: String| s == "true");
        assert_eq!(as_text.get(cx), "true");
        as_text.set(cx, "false".to_string());
        assert!(!settings.read(cx).muted);
    });
}

#[gpui::test]
fn form_validates_and_revalidates_live(cx: &mut TestAppContext) {
    let form = cx.update(|cx| {
        Form::new(cx)
            .field(cx, "email", "")
            .rule("email", validators::required())
            .rule("email", validators::email())
            .field(cx, "confirm", "")
            .rule_form("confirm", validators::equals_field("email", "Must match"))
    });

    cx.update(|cx| {
        assert!(!form.validate(cx));
        assert!(form.error(cx, "email").is_some());
        assert!(!form.is_valid(cx));
    });

    // Fixing the field re-validates it live (it carried an error) — the
    // observer fires on the effect flush between these blocks.
    cx.update(|cx| form.set(cx, "email", "a@b.com"));
    cx.update(|cx| {
        assert_eq!(form.error(cx, "email"), None);
        assert!(form.touched("email"));
    });

    cx.update(|cx| form.set(cx, "confirm", "a@b.com"));
    cx.update(|cx| {
        let values = form.submit(cx).expect("form should validate");
        assert_eq!(values["email"], "a@b.com");
    });

    // Cross-field: change email, confirm no longer matches.
    cx.update(|cx| form.set(cx, "email", "other@b.com"));
    cx.update(|cx| assert!(!form.validate(cx)));
}

#[gpui::test]
fn select_bind_follows_the_signal_both_ways(cx: &mut TestAppContext) {
    let choice = cx.update(|cx| Signal::new(cx, 2_usize));
    let select = cx.update(|cx| cx.new(|cx| Select::new(cx).data(["a", "b", "c"])));
    cx.update(|cx| Select::bind(&select, &choice, cx));

    // The signal is the source of truth: the picker adopts it immediately…
    cx.update(|cx| assert_eq!(select.read(cx).selected_index(), Some(2)));

    // …and follows later writes.
    cx.update(|cx| choice.set(cx, 0));
    cx.update(|cx| assert_eq!(select.read(cx).selected_index(), Some(0)));
}

#[gpui::test]
fn datepicker_bind_adopts_signal_writes(cx: &mut TestAppContext) {
    let date = Date::new(2026, 7, 14).unwrap();
    let picked = cx.update(|cx| Signal::new(cx, None::<Date>));
    let picker = cx.update(|cx| cx.new(DatePicker::new));
    cx.update(|cx| DatePicker::bind(&picker, &picked, cx));

    cx.update(|cx| assert_eq!(picker.read(cx).selected_date(), None));
    cx.update(|cx| picked.set(cx, Some(date)));
    cx.update(|cx| assert_eq!(picker.read(cx).selected_date(), Some(date)));
}

#[gpui::test]
fn carousel_navigates_and_emits(cx: &mut TestAppContext) {
    use std::cell::RefCell;
    use std::rc::Rc;

    let deck = cx.update(|cx| {
        cx.new(|cx| {
            Carousel::new(cx)
                .slide(|_, _| gpui::Empty)
                .slide(|_, _| gpui::Empty)
                .slide(|_, _| gpui::Empty)
        })
    });
    let seen: Rc<RefCell<Vec<usize>>> = Rc::default();
    let log = seen.clone();
    cx.update(|cx| {
        cx.subscribe(&deck, move |_deck, event: &CarouselEvent, _cx| {
            log.borrow_mut().push(event.0);
        })
        .detach();
    });

    deck.update(cx, |deck, cx| {
        deck.next(cx);
        deck.next(cx);
        deck.next(cx); // wraps to 0
        deck.prev(cx); // wraps back to 2
        deck.go_to(1, cx);
        deck.go_to(1, cx); // no-op, no event
    });
    assert_eq!(*seen.borrow(), vec![1, 2, 0, 2, 1]);
    cx.update(|cx| assert_eq!(deck.read(cx).current(), 1));
}

/// The name of each event, so a subscription can be asserted on without
/// `UpdatePromptEvent` having to be `PartialEq`.
fn prompt_event_name(event: &UpdatePromptEvent) -> &'static str {
    match event {
        UpdatePromptEvent::Started => "started",
        UpdatePromptEvent::Stage(_) => "stage",
        UpdatePromptEvent::Installed(_) => "installed",
        UpdatePromptEvent::Failed(_) => "failed",
        UpdatePromptEvent::Dismissed => "dismissed",
    }
}

fn offered_release() -> Release {
    Release {
        version: "2.0.0".to_string(),
        url: "https://example.com/releases/2.0.0".to_string(),
        assets: Vec::new(),
    }
}

/// A test binary is neither an installed `.app` nor an AppImage, so `detect()`
/// reports `Unknown` — the case where the prompt must not promise an install it
/// can't perform. Accepting has to open the release page and stand down instead
/// of starting one.
#[gpui::test]
fn update_prompt_offers_the_page_when_it_cannot_install_in_place(cx: &mut TestAppContext) {
    use std::cell::RefCell;
    use std::rc::Rc;

    let updater = Updater::github("Acme", "1.0.0", "acme/acme");
    let prompt = cx.update(|cx| cx.new(|cx| UpdatePrompt::new(updater, offered_release(), cx)));
    let seen: Rc<RefCell<Vec<&'static str>>> = Rc::default();
    let log = seen.clone();
    cx.update(|cx| {
        cx.subscribe(&prompt, move |_prompt, event: &UpdatePromptEvent, _cx| {
            log.borrow_mut().push(prompt_event_name(event));
        })
        .detach();
    });

    prompt.update(cx, |prompt, cx| prompt.accept(cx));
    assert_eq!(*seen.borrow(), vec!["dismissed"]);
    cx.update(|cx| {
        assert!(!prompt.read(cx).busy());
        // Nothing was started, so no other prompt is locked out.
        assert!(!is_installing(cx));
    });
}

/// The states a host driving its own install moves the prompt through, and the
/// guarantee that a running install can't be dismissed out from under itself.
#[gpui::test]
fn update_prompt_tracks_the_stages_a_host_drives(cx: &mut TestAppContext) {
    use std::cell::RefCell;
    use std::rc::Rc;

    let updater = Updater::github("Acme", "1.0.0", "acme/acme");
    let prompt = cx.update(|cx| cx.new(|cx| UpdatePrompt::new(updater, offered_release(), cx)));
    let seen: Rc<RefCell<Vec<&'static str>>> = Rc::default();
    let log = seen.clone();
    cx.update(|cx| {
        cx.subscribe(&prompt, move |_prompt, event: &UpdatePromptEvent, _cx| {
            log.borrow_mut().push(prompt_event_name(event));
        })
        .detach();
    });

    prompt.update(cx, |prompt, cx| {
        prompt.set_stage(UpdateStage::Preparing, cx);
        // Inert while installing: the progress must not be closable.
        prompt.dismiss(cx);
    });
    cx.update(|cx| {
        let prompt = prompt.read(cx);
        assert!(prompt.busy());
        assert_eq!(prompt.stage(), Some(&UpdateStage::Preparing));
        assert_eq!(prompt.error(), None);
    });
    assert!(seen.borrow().is_empty());

    prompt.update(cx, |prompt, cx| prompt.set_failed("no disk space", cx));
    cx.update(|cx| {
        let prompt = prompt.read(cx);
        assert!(!prompt.busy());
        assert_eq!(prompt.error(), Some("no disk space"));
    });

    // A failure is retryable, and dismissable again.
    prompt.update(cx, |prompt, cx| {
        prompt.reset(cx);
        prompt.dismiss(cx);
    });
    cx.update(|cx| assert_eq!(prompt.read(cx).error(), None));
    assert_eq!(*seen.borrow(), vec!["dismissed"]);
}

#[gpui::test]
fn update_notice_answers_a_check_and_dismisses(cx: &mut TestAppContext) {
    use std::cell::RefCell;
    use std::rc::Rc;

    let updater = Updater::github("Acme", "1.31.0", "acme/acme");
    let notice = cx.update(|cx| {
        cx.new(|cx| UpdateNotice::new(updater, UpdateOutcome::Pending("1.32.0".into()), cx))
    });
    let dismissed: Rc<RefCell<usize>> = Rc::default();
    let count = dismissed.clone();
    cx.update(|cx| {
        cx.subscribe(&notice, move |_notice, event: &UpdateNoticeEvent, _cx| {
            let UpdateNoticeEvent::Dismissed = event;
            *count.borrow_mut() += 1;
        })
        .detach();
    });

    cx.update(|cx| {
        assert_eq!(
            notice.read(cx).outcome(),
            &UpdateOutcome::Pending("1.32.0".to_string())
        );
    });
    notice.update(cx, |notice, cx| notice.dismiss(cx));
    assert_eq!(*dismissed.borrow(), 1);
}

#[gpui::test]
fn theme_presets_install_and_resolve(cx: &mut TestAppContext) {
    cx.update(|cx| {
        Theme::catppuccin().init(cx);
        let t = theme(cx);
        assert!(t.scheme.is_dark());
        assert_eq!(t.primary(), Color::hex("#89b4fa"));
        assert_eq!(t.body(), Color::hex("#1e1e2e"));

        // Swapping the global restyles everything that reads theme(cx).
        Theme::solarized_light().init(cx);
        let t = theme(cx);
        assert!(!t.scheme.is_dark());
        assert_eq!(t.primary(), Color::hex("#268bd2"));
    });
}

// --- single-line fields -----------------------------------------------------
//
// These drive a real window: `simulate_input` dispatches the key event first
// and only then hands the character to the platform's input handler, exactly
// as macOS, X11, and Windows do. That is the path a text field now takes, so
// nothing here works unless the field is wired up the way the real thing is.

/// Two fields in a window — the smallest form that can show focus moving.
struct Pair {
    first: Entity<TextInput>,
    second: Entity<TextInput>,
}

impl Render for Pair {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        div()
            .flex()
            .flex_col()
            .size_full()
            .child(self.first.clone())
            .child(self.second.clone())
            .probe("Pair")
    }
}

fn pair(cx: &mut TestAppContext) -> (Entity<Pair>, &mut gpui::VisualTestContext) {
    cx.update(|cx| Theme::light().init(cx));
    cx.add_window_view(|_window, cx| Pair {
        first: cx.new(|cx| TextInput::new(cx).placeholder("first")),
        second: cx.new(|cx| TextInput::new(cx).placeholder("second")),
    })
}

/// Focus a field and let the window lay out again, so the tab-stop ring and
/// the field's shaped line are both current.
fn focus(field: &Entity<TextInput>, cx: &mut gpui::VisualTestContext) {
    let handle = field.read_with(cx, |field, _| field.focus_handle());
    cx.update(|window, _| window.focus(&handle));
    cx.run_until_parked();
}

#[gpui::test]
fn text_input_types_through_the_platform_input_handler(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let field = view.read_with(cx, |view, _| view.first.clone());
    focus(&field, cx);

    cx.simulate_input("hello");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "hello");

    // Backspace and the arrows still come from key handling.
    cx.simulate_keystrokes("backspace left left");
    cx.simulate_input("L");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "heLll");
}

#[gpui::test]
fn text_input_tab_moves_focus_instead_of_typing(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let (first, second) = view.read_with(cx, |view, _| (view.first.clone(), view.second.clone()));
    focus(&first, cx);
    cx.simulate_input("one");

    cx.simulate_keystrokes("tab");
    // The platform reports a `\t` for Tab. A field that typed it would be the
    // bug this replaced.
    assert_eq!(first.read_with(cx, |field, _| field.text()), "one");
    let second_handle = second.read_with(cx, |field, _| field.focus_handle());
    assert!(cx.update(|window, _| second_handle.is_focused(window)));

    cx.simulate_input("two");
    assert_eq!(second.read_with(cx, |field, _| field.text()), "two");

    cx.simulate_keystrokes("shift-tab");
    let first_handle = first.read_with(cx, |field, _| field.focus_handle());
    assert!(cx.update(|window, _| first_handle.is_focused(window)));
    assert_eq!(second.read_with(cx, |field, _| field.text()), "two");
}

#[gpui::test]
fn text_input_cuts_copies_and_pastes(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let (first, second) = view.read_with(cx, |view, _| (view.first.clone(), view.second.clone()));
    focus(&first, cx);
    cx.simulate_input("copy me");

    cx.simulate_keystrokes("cmd-a cmd-c");
    focus(&second, cx);
    cx.simulate_keystrokes("cmd-v cmd-v");
    assert_eq!(
        second.read_with(cx, |field, _| field.text()),
        "copy mecopy me"
    );

    // Cut empties the source and leaves the clipboard usable again.
    focus(&first, cx);
    cx.simulate_keystrokes("cmd-a cmd-x");
    assert_eq!(first.read_with(cx, |field, _| field.text()), "");
    cx.simulate_keystrokes("cmd-v");
    assert_eq!(first.read_with(cx, |field, _| field.text()), "copy me");
}

#[gpui::test]
fn text_input_pasting_flattens_line_breaks(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let field = view.read_with(cx, |view, _| view.first.clone());
    focus(&field, cx);
    cx.write_to_clipboard(gpui::ClipboardItem::new_string("one\ntwo\r\nthree".into()));
    cx.simulate_keystrokes("cmd-v");
    assert_eq!(
        field.read_with(cx, |field, _| field.text()),
        "one two  three"
    );
}

#[gpui::test]
fn text_input_undoes_by_word(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let field = view.read_with(cx, |view, _| view.first.clone());
    focus(&field, cx);
    cx.simulate_input("alpha beta");

    cx.simulate_keystrokes("cmd-z");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "alpha ");
    cx.simulate_keystrokes("cmd-z");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "");
    cx.simulate_keystrokes("cmd-shift-z cmd-shift-z");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "alpha beta");
}

#[gpui::test]
fn text_input_click_places_the_caret_and_drag_selects(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let field = view.read_with(cx, |view, _| view.first.clone());
    focus(&field, cx);
    cx.simulate_input("hello world");

    // Ask the shaped line itself where a character sits, so the test doesn't
    // depend on the font's metrics.
    let (bounds, x_of) = field.read_with(cx, |field, _| {
        let state = field.line();
        let shaped = state.shaped.clone().expect("the field has been painted");
        let bounds = state.bounds.expect("the field has been painted");
        (bounds, move |index: usize| shaped.x_for_index(index))
    });
    let at = |index: usize| gpui::point(bounds.left() + x_of(index), bounds.center().y);

    cx.simulate_click(at(2), Modifiers::none());
    assert_eq!(field.read_with(cx, |field, _| field.edit().cursor()), 2);

    // Press at 2, drag to 7, release: "llo w" is selected.
    cx.simulate_mouse_down(at(2), MouseButton::Left, Modifiers::none());
    cx.simulate_mouse_move(at(7), MouseButton::Left, Modifiers::none());
    assert_eq!(
        field.read_with(cx, |field, _| field.edit().selected_text()),
        Some("llo w".to_string())
    );
    cx.simulate_mouse_up(at(7), MouseButton::Left, Modifiers::none());

    // Typing replaces the selection, as it would in a browser.
    cx.simulate_input("X");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "heXorld");
}

#[gpui::test]
fn text_input_double_click_takes_a_word(cx: &mut TestAppContext) {
    let (view, cx) = pair(cx);
    let field = view.read_with(cx, |view, _| view.first.clone());
    focus(&field, cx);
    cx.simulate_input("hello world");

    let (bounds, x_of) = field.read_with(cx, |field, _| {
        let state = field.line();
        let shaped = state.shaped.clone().expect("the field has been painted");
        let bounds = state.bounds.expect("the field has been painted");
        (bounds, move |index: usize| shaped.x_for_index(index))
    });
    let position = gpui::point(bounds.left() + x_of(8), bounds.center().y);

    cx.simulate_event(gpui::MouseDownEvent {
        button: MouseButton::Left,
        position,
        modifiers: Modifiers::none(),
        click_count: 2,
        first_mouse: false,
    });
    assert_eq!(
        field.read_with(cx, |field, _| field.edit().selected_text()),
        Some("world".to_string())
    );

    cx.simulate_event(gpui::MouseDownEvent {
        button: MouseButton::Left,
        position,
        modifiers: Modifiers::none(),
        click_count: 3,
        first_mouse: false,
    });
    assert_eq!(
        field.read_with(cx, |field, _| field.edit().selected_text()),
        Some("hello world".to_string())
    );
}

#[gpui::test]
fn text_input_honours_max_length(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (field, cx) = cx.add_window_view(|_window, cx| TextInput::new(cx).max_length(4));
    focus(&field, cx);
    cx.simulate_input("abcdefg");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "abcd");
    cx.write_to_clipboard(gpui::ClipboardItem::new_string("xyz".into()));
    cx.simulate_keystrokes("cmd-v");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "abcd");
}

#[gpui::test]
fn text_input_read_only_selects_but_never_edits(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (field, cx) =
        cx.add_window_view(|_window, cx| TextInput::new(cx).read_only(true).value("locked"));
    focus(&field, cx);
    cx.simulate_input("nope");
    cx.simulate_keystrokes("backspace");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "locked");
    // Read-only still selects and copies.
    cx.simulate_keystrokes("cmd-a cmd-c");
    assert_eq!(
        cx.read_from_clipboard().and_then(|item| item.text()),
        Some("locked".to_string())
    );
}

#[gpui::test]
fn text_input_never_copies_a_password(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    cx.write_to_clipboard(gpui::ClipboardItem::new_string("untouched".into()));
    let (field, cx) = cx.add_window_view(|_window, cx| TextInput::new(cx).password(true));
    focus(&field, cx);
    cx.simulate_input("hunter2");
    cx.simulate_keystrokes("cmd-a cmd-c");
    assert_eq!(
        cx.read_from_clipboard().and_then(|item| item.text()),
        Some("untouched".to_string())
    );
    cx.simulate_keystrokes("cmd-x");
    assert_eq!(field.read_with(cx, |field, _| field.text()), "hunter2");
}

// --- AI components ----------------------------------------------------------

#[gpui::test]
fn chat_view_streams_a_reply_and_closes_it(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (chat, cx) = cx.add_window_view(|_window, cx| AIChatView::new(cx));

    chat.update(cx, |chat, cx| {
        chat.push(AITurn::user("hello"), cx);
        chat.begin_reply(cx);
        chat.push_delta("Hi", cx);
        chat.push_delta(" there", cx);
        chat.push_reasoning("weighing it up", cx);
    });
    chat.read_with(cx, |chat, _| {
        assert_eq!(chat.turn_count(), 2);
        let reply = chat.turn(1).unwrap();
        assert_eq!(reply.body, "Hi there");
        assert_eq!(reply.reasoning.as_deref(), Some("weighing it up"));
        assert!(reply.streaming);
    });

    chat.update(cx, |chat, cx| chat.end_reply(cx));
    chat.read_with(cx, |chat, _| assert!(!chat.turn(1).unwrap().streaming));

    // A delta that lands after the turn closed — a cancelled request whose
    // last chunk was already in flight — must not reopen it.
    chat.update(cx, |chat, cx| chat.push_delta(" and more", cx));
    chat.read_with(cx, |chat, _| {
        assert_eq!(chat.turn(1).unwrap().body, "Hi there")
    });
}

#[gpui::test]
fn chat_view_keeps_partial_text_when_a_reply_fails(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (chat, cx) = cx.add_window_view(|_window, cx| AIChatView::new(cx));

    chat.update(cx, |chat, cx| {
        chat.begin_reply(cx);
        chat.push_delta("partial", cx);
        chat.fail_reply("connection reset", cx);
    });
    chat.read_with(cx, |chat, _| {
        let reply = chat.turn(0).unwrap();
        assert_eq!(reply.body, "partial");
        assert_eq!(reply.error.as_deref(), Some("connection reset"));
        assert!(!reply.streaming);
    });

    // Failing with nothing open is a no-op, not a panic.
    chat.update(cx, |chat, cx| chat.fail_reply("late", cx));
    chat.read_with(cx, |chat, _| {
        assert_eq!(
            chat.turn(0).unwrap().error.as_deref(),
            Some("connection reset")
        );
    });
}

#[gpui::test]
fn chat_view_edits_are_bounds_checked(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (chat, cx) = cx.add_window_view(|_window, cx| AIChatView::new(cx));
    // Editing a turn that isn't there must not panic.
    chat.update(cx, |chat, cx| {
        chat.update_turn(9, |turn| turn.body.push_str("nope"), cx);
        assert!(chat.turn(9).is_none());
    });
}

/// A long transcript must not re-parse every turn's markdown on every frame,
/// and skipping the ones off screen must not move anything that is on it.
#[gpui::test]
fn chat_view_skips_turns_that_are_far_off_screen(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let body = "## Heading\n\nA paragraph with **bold** and `code` in it.\n\n                1. one\n2. two\n\nAnother paragraph to give the turn some height.";
    let turns: Vec<AITurn> = (0..60)
        .map(|i| {
            if i % 2 == 0 {
                AITurn::user(format!("Question {i}"))
            } else {
                AITurn::assistant(body)
            }
        })
        .collect();

    let (chat, cx) = cx.add_window_view(|_window, cx| AIChatView::new(cx).turns(turns.clone()));
    cx.run_until_parked();

    // The first frame has nothing measured, so everything is built.
    // By the second, only the turns near the viewport are.
    let drawn = chat.update(cx, |chat, _| chat.drawn_count());
    assert!(
        drawn < 60,
        "expected some turns to be skipped, drew {drawn}"
    );
    assert!(drawn > 0, "the visible turns must still be built");

    // Skipping them must leave the scroll extent exactly where it was: the
    // spacers carry each turn's measured height.
    let extent = chat.read_with(cx, |chat, _| chat.scroll_extent());
    cx.run_until_parked();
    let after = chat.read_with(cx, |chat, _| chat.scroll_extent());
    assert_eq!(
        extent, after,
        "content height moved when turns became spacers"
    );

    // A resize reflows every turn, so the measured heights are stale and all
    // of them have to be drawn once more to re-measure — clearing the heights
    // alone wouldn't do it, since an off-screen turn's bounds are its
    // spacer's and would be read straight back.
    cx.simulate_resize(gpui::size(gpui::px(500.0), gpui::px(700.0)));
    cx.run_until_parked();
    assert_eq!(
        chat.update(cx, |chat, _| chat.drawn_count()),
        60,
        "a resize must re-measure every turn"
    );

    // Turning it off builds every turn again.
    let (all, cx) = cx
        .add_window_view(|_window, cx| AIChatView::new(cx).turns(turns.clone()).virtualize(false));
    cx.run_until_parked();
    assert_eq!(all.update(cx, |chat, _| chat.drawn_count()), 60);
}

#[gpui::test]
fn composer_sends_on_enter_and_refuses_blank_drafts(cx: &mut TestAppContext) {
    cx.update(|cx| Theme::light().init(cx));
    let (composer, cx) = cx.add_window_view(|_window, cx| AIComposer::new(cx));

    let sent = std::rc::Rc::new(std::cell::RefCell::new(Vec::<String>::new()));
    let sink = sent.clone();
    cx.update(|_, cx| {
        cx.subscribe(&composer, move |_composer, event: &AIComposerEvent, _cx| {
            if let AIComposerEvent::Submit(text) = event {
                sink.borrow_mut().push(text.clone());
            }
        })
        .detach();
    });

    let input = composer.read_with(cx, |composer, _| composer.input().clone());
    let handle = input.read_with(cx, |input, _| input.focus_handle());
    cx.update(|window, _| window.focus(&handle));
    cx.run_until_parked();

    // Whitespace is not a prompt.
    cx.simulate_input("   ");
    cx.simulate_keystrokes("enter");
    assert!(sent.borrow().is_empty());

    cx.simulate_input("write a haiku");
    cx.simulate_keystrokes("enter");
    assert_eq!(sent.borrow().as_slice(), ["   write a haiku"]);
    // The box clears itself, so the next prompt starts empty.
    assert_eq!(composer.read_with(cx, |composer, cx| composer.text(cx)), "");

    // Shift+Enter is a newline, not a send.
    cx.simulate_input("one");
    cx.simulate_keystrokes("shift-enter");
    cx.simulate_input("two");
    assert_eq!(sent.borrow().len(), 1);
    assert_eq!(
        composer.read_with(cx, |composer, cx| composer.text(cx)),
        "one\ntwo"
    );

    // While a reply is streaming the composer will not send.
    composer.update(cx, |composer, cx| composer.set_busy(true, cx));
    cx.simulate_keystrokes("enter");
    assert_eq!(sent.borrow().len(), 1);
}

// --- devtools ---------------------------------------------------------------

/// A window holding the inspector next to something worth inspecting. The
/// recorder only runs while a `DevTools` is alive, so the two have to share a
/// frame for any of this to be observable.
struct Inspected {
    devtools: Entity<DevTools>,
}

impl Render for Inspected {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        div()
            .flex()
            .size_full()
            .child(
                crate::layout::Stack::new()
                    .child(crate::Button::new("save", "Save"))
                    .child(crate::Badge::new("new")),
            )
            .child(self.devtools.clone())
            .probe("Inspected")
    }
}

fn inspected(cx: &mut TestAppContext) -> (Entity<Inspected>, &mut gpui::VisualTestContext) {
    cx.update(|cx| {
        Theme::light().init(cx);
        DevToolsState::new().init(cx);
    });
    cx.add_window_view(|_window, cx| Inspected {
        devtools: cx.new(DevTools::new),
    })
}

#[gpui::test]
fn the_recorder_rebuilds_the_component_tree(cx: &mut TestAppContext) {
    let (view, cx) = inspected(cx);
    cx.run_until_parked();
    // The tree the panel reads is the one the *previous* frame recorded, so a
    // second frame has to land before anything is visible.
    view.update(cx, |_this, cx| cx.notify());
    cx.run_until_parked();

    let names: Vec<String> = view.read_with(cx, |this, cx| {
        this.devtools
            .read(cx)
            .tree()
            .nodes
            .iter()
            .map(|node| node.name.to_string())
            .collect()
    });

    assert!(names.contains(&"Inspected".to_string()), "{names:?}");
    assert!(names.contains(&"Stack".to_string()), "{names:?}");
    assert!(names.contains(&"Button".to_string()), "{names:?}");
    assert!(names.contains(&"Badge".to_string()), "{names:?}");
}

#[gpui::test]
fn a_recorded_node_carries_its_attributes_style_and_source(cx: &mut TestAppContext) {
    let (view, cx) = inspected(cx);
    cx.run_until_parked();
    view.update(cx, |_this, cx| cx.notify());
    cx.run_until_parked();

    view.read_with(cx, |this, cx| {
        let tree = this.devtools.read(cx).tree().clone();
        let button = tree
            .nodes
            .iter()
            .find(|node| node.name.as_ref() == "Button")
            .expect("the button should have reported itself");

        // Attributes come from the component's own props.
        assert!(button
            .attrs
            .iter()
            .any(|(name, value)| name.as_ref() == "variant" && value.as_ref() == "filled"));
        assert!(button.attrs.iter().any(|(name, _)| name.as_ref() == "size"));

        // The style snapshot is what fills the Styles sidebar.
        let style = button
            .style
            .as_ref()
            .expect("a styled root reports its style");
        let declarations = crate::devtools::declarations(style);
        assert!(declarations
            .iter()
            .any(|d| d.property.as_ref() == "background-color"));

        // `#[track_caller]` has to resolve to the component, not to the probe.
        let source = button.source.as_ref().expect("a probe records its caller");
        assert_eq!(source.basename(), "button.rs");

        // Bounds are captured during prepaint, so a laid-out button has some.
        assert!(f32::from(button.bounds.size.width) > 0.0);
    });
}

#[gpui::test]
fn the_recorder_is_inert_until_an_inspector_exists(cx: &mut TestAppContext) {
    assert!(!crate::devtools::is_recording());

    let (view, cx) = inspected(cx);
    cx.run_until_parked();
    assert!(crate::devtools::is_recording());

    // Dropping the inspector stops the recording, and with it the per-frame
    // cost every probe in the app would otherwise keep paying.
    view.update(cx, |this, cx| {
        this.devtools = cx.new(DevTools::new);
    });
    cx.run_until_parked();
    assert!(crate::devtools::is_recording());
}

#[gpui::test]
fn reported_records_read_back_out_of_the_store(cx: &mut TestAppContext) {
    cx.update(|cx| {
        DevToolsState::new().init(cx);

        crate::devtools::log(cx, LogLevel::Warning, "cache miss");
        crate::devtools::log(cx, LogLevel::Warning, "cache miss");
        crate::devtools::log(cx, LogLevel::Error, "boom");

        let id = crate::devtools::network_begin(
            cx,
            NetworkRecord::new("GET", "https://api.example.com/v1/items"),
        )
        .expect("the store is installed");
        crate::devtools::network_update(cx, id, |record| {
            record.state = RequestState::Finished;
            record.status = Some(200);
        });

        crate::devtools::storage_set(
            cx,
            StorageDomain::new("prefs", "app.preferences")
                .entry(StorageEntry::new("theme", "dark")),
        );
    });

    cx.update(|cx| {
        let state = cx.global::<DevToolsState>();
        // The two identical warnings coalesced into one row with a count of 2.
        assert_eq!(state.logs().len(), 2);
        assert_eq!(state.log_issues(), (2, 1));
        // `log` is `#[track_caller]`, so the line knows where it came from.
        assert_eq!(
            state.logs()[0].source.as_ref().map(|s| s.basename()),
            Some("apptests.rs")
        );
        assert_eq!(state.network()[0].status, Some(200));
        assert_eq!(state.storage()[0].entries.len(), 1);
    });
}

#[gpui::test]
fn reporting_without_the_store_installed_is_a_no_op(cx: &mut TestAppContext) {
    // No `DevToolsState::init`, which is the state a release build is in.
    cx.update(|cx| {
        crate::devtools::log(cx, LogLevel::Error, "nobody is listening");
        assert!(crate::devtools::network_begin(cx, NetworkRecord::new("GET", "/a")).is_none());
        crate::devtools::storage_set(cx, StorageDomain::new("prefs", "Preferences"));
        crate::devtools::clear(cx);
        assert!(!cx.has_global::<DevToolsState>());
    });
}

#[gpui::test]
fn clicking_a_source_link_switches_to_sources_and_tells_the_host(cx: &mut TestAppContext) {
    let (view, cx) = inspected(cx);
    let revealed = std::rc::Rc::new(std::cell::RefCell::new(Vec::<String>::new()));
    let sink = revealed.clone();

    view.update(cx, |this, cx| {
        cx.subscribe(
            &this.devtools,
            move |_this, _devtools, event: &DevToolsEvent, _cx| {
                if let DevToolsEvent::RevealSource(source) = event {
                    sink.borrow_mut().push(source.short());
                }
            },
        )
        .detach();
    });

    view.update(cx, |this, cx| {
        this.devtools.update(cx, |devtools, cx| {
            devtools.reveal_source(SourceRef::new("crates/guise/src/button.rs", 42, 9), cx);
        });
    });
    cx.run_until_parked();

    assert_eq!(revealed.borrow().as_slice(), ["button.rs:42:9"]);
    assert_eq!(
        view.read_with(cx, |this, cx| this.devtools.read(cx).active_tab()),
        DevToolsTab::Sources
    );
}

#[gpui::test]
fn picking_selects_the_deepest_node_under_the_point(cx: &mut TestAppContext) {
    let (view, cx) = inspected(cx);
    cx.run_until_parked();
    view.update(cx, |_this, cx| cx.notify());
    cx.run_until_parked();

    let button_bounds = view.read_with(cx, |this, cx| {
        this.devtools
            .read(cx)
            .tree()
            .nodes
            .iter()
            .find(|node| node.name.as_ref() == "Button")
            .map(|node| node.bounds)
            .expect("the button should have reported itself")
    });

    view.update(cx, |this, cx| {
        this.devtools.update(cx, |devtools, cx| {
            devtools.set_picking(true, cx);
            assert!(devtools.is_picking());
            assert!(devtools.pick_at(button_bounds.center(), cx));
            // A hit disarms the picker, the way Safari's does.
            assert!(!devtools.is_picking());
            assert_eq!(devtools.active_tab(), DevToolsTab::Elements);
            assert_eq!(devtools.selected_bounds(), Some(button_bounds));
        });
    });
}