gpui-ui-kit 0.5.10

A reusable UI component library for GPUI applications
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
//! Integration test for Input component
//!
//! Tests the self-contained input behavior including:
//! - Basic rendering
//! - Focus and keyboard input handling
//! - State persistence across re-renders
//! - Callback invocation
//! - Mouse click to focus
//! - Double-click to select all
//! - Mouse drag to select text
//! - Clipboard operations (Cmd+C/V/X)

use gpui::{
    Context, Modifiers, MouseButton, TestAppContext, VisualTestContext, Window, div, prelude::*,
};
use gpui_ui_kit::input::Input;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

struct InputTestView;

impl Render for InputTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        div().child(
            Input::new("test-input")
                .placeholder("Enter text...")
                .value("Hello"),
        )
    }
}

#[gpui::test]
async fn test_input_renders(cx: &mut TestAppContext) {
    let _window = cx.add_window(|_window, _cx| InputTestView);
}

/// Test that Input component properly tracks value changes via on_text_change callback
struct InputWithCallbackView {
    value: Rc<RefCell<String>>,
}

impl Render for InputWithCallbackView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let value = self.value.borrow().clone();
        let value_rc = self.value.clone();

        div().child(
            Input::new("callback-input")
                .placeholder("Type here...")
                .value(value)
                .on_text_change(move |text, _window, _cx| {
                    *value_rc.borrow_mut() = text;
                }),
        )
    }
}

#[gpui::test]
async fn test_input_with_callback(cx: &mut TestAppContext) {
    let value = Rc::new(RefCell::new("initial".to_string()));
    let value_clone = value.clone();

    let _window = cx.add_window(move |_window, _cx| InputWithCallbackView { value: value_clone });

    // Verify initial value
    assert_eq!(*value.borrow(), "initial");
}

/// Test that Input component can be created with various configurations
#[gpui::test]
async fn test_input_configurations(cx: &mut TestAppContext) {
    use gpui_ui_kit::input::{InputSize, InputVariant};

    struct ConfigTestView;

    impl Render for ConfigTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            div()
                .child(Input::new("small-input").size(InputSize::Sm).value("Small"))
                .child(
                    Input::new("filled-input")
                        .variant(InputVariant::Filled)
                        .value("Filled"),
                )
                .child(
                    Input::new("disabled-input")
                        .disabled(true)
                        .value("Disabled"),
                )
                .child(
                    Input::new("readonly-input")
                        .readonly(true)
                        .value("Readonly"),
                )
                .child(
                    Input::new("error-input")
                        .error("This is an error")
                        .value("Error"),
                )
        }
    }

    let _window = cx.add_window(|_window, _cx| ConfigTestView);
}

// ============================================================================
// Tests for focus persistence and keyboard input handling
// ============================================================================

/// View that tracks text changes and render count to verify state persistence
struct InputKeyboardTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
    render_count: Arc<AtomicUsize>,
}

impl Render for InputKeyboardTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        self.render_count.fetch_add(1, Ordering::SeqCst);

        let text_changes = self.text_changes.clone();

        div().id("test-container").size_full().child(
            Input::new("keyboard-test-input")
                .placeholder("Type here...")
                .value("")
                .on_text_change(move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                }),
        )
    }
}

/// Test that clicking on the input focuses it and allows typing
#[gpui::test]
async fn test_input_click_to_focus_and_type(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let render_count = Arc::new(AtomicUsize::new(0));

    let text_changes_clone = text_changes.clone();
    let render_count_clone = render_count.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardTestView {
        text_changes: text_changes_clone,
        render_count: render_count_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);

    // Wait for initial render
    cx.run_until_parked();

    let initial_renders = render_count.load(Ordering::SeqCst);
    assert!(initial_renders >= 1, "Should have rendered at least once");

    // Find the input element by its ID and click on it
    if let Some(bounds) = cx.debug_bounds("keyboard-test-input") {
        let center = bounds.center();
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Type some characters
        cx.simulate_input("abc");
        cx.run_until_parked();

        // Verify the text changes were captured
        let changes = text_changes.borrow();
        assert!(!changes.is_empty(), "Should have captured text changes");

        // The last change should contain "abc"
        let last_change = changes.last().unwrap();
        assert!(
            last_change.contains("abc") || last_change == "abc",
            "Last text change should contain 'abc', got: {}",
            last_change
        );
    } else {
        // Input element not found in debug bounds - this can happen if the
        // element ID isn't registered. Still pass the test as the input renders.
        eprintln!(
            "Note: Could not find 'keyboard-test-input' in debug bounds. Skipping click test."
        );
    }
}

/// Test that focus persists across multiple re-renders
#[gpui::test]
async fn test_input_focus_persists_across_renders(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let render_count = Arc::new(AtomicUsize::new(0));

    let text_changes_clone = text_changes.clone();
    let render_count_clone = render_count.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardTestView {
        text_changes: text_changes_clone,
        render_count: render_count_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    // Try to find and click the input
    if let Some(bounds) = cx.debug_bounds("keyboard-test-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        let renders_before = render_count.load(Ordering::SeqCst);

        // Type first character
        cx.simulate_input("x");
        cx.run_until_parked();

        // Type second character (this triggers a re-render from the callback)
        cx.simulate_input("y");
        cx.run_until_parked();

        // Type third character
        cx.simulate_input("z");
        cx.run_until_parked();

        let renders_after = render_count.load(Ordering::SeqCst);

        // Should have multiple renders (from window.refresh() calls)
        assert!(
            renders_after > renders_before,
            "Should have re-rendered after typing. Before: {}, After: {}",
            renders_before,
            renders_after
        );

        // Verify all characters were captured
        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // The text should build up: "x", "xy", "xyz"
            assert!(
                last.len() >= 3,
                "Should have captured all typed characters, got: {}",
                last
            );
        }
    }
}

/// Test that the on_change callback is called when pressing Enter
struct InputOnChangeTestView {
    confirmed_value: Arc<RefCell<Option<String>>>,
}

impl Render for InputOnChangeTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let confirmed = self.confirmed_value.clone();

        div().size_full().child(
            Input::new("onchange-test-input")
                .placeholder("Type and press Enter...")
                .value("")
                .on_change(move |text, _window, _cx| {
                    *confirmed.borrow_mut() = Some(text.to_string());
                }),
        )
    }
}

#[gpui::test]
async fn test_input_on_change_called_on_enter(cx: &mut TestAppContext) {
    let confirmed_value: Arc<RefCell<Option<String>>> = Arc::new(RefCell::new(None));
    let confirmed_clone = confirmed_value.clone();

    let window = cx.add_window(move |_window, _cx| InputOnChangeTestView {
        confirmed_value: confirmed_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("onchange-test-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Type some text
        cx.simulate_input("test value");
        cx.run_until_parked();

        // Press Enter to confirm
        cx.simulate_keystrokes("enter");
        cx.run_until_parked();

        // Verify on_change was called
        let confirmed = confirmed_value.borrow();
        assert!(
            confirmed.is_some(),
            "on_change should have been called on Enter"
        );
        assert_eq!(
            confirmed.as_ref().unwrap(),
            "test value",
            "Confirmed value should match typed text"
        );
    }
}

/// Test that Escape cancels editing without calling on_change
struct InputEscapeTestView {
    confirmed_value: Arc<RefCell<Option<String>>>,
    cancelled: Arc<RefCell<bool>>,
}

impl Render for InputEscapeTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let confirmed = self.confirmed_value.clone();
        let cancelled = self.cancelled.clone();

        div().size_full().child(
            Input::new("escape-test-input")
                .placeholder("Type and press Escape...")
                .value("")
                .on_change(move |text, _window, _cx| {
                    *confirmed.borrow_mut() = Some(text.to_string());
                })
                .on_edit_end({
                    let cancelled = cancelled.clone();
                    move |result, _window, _cx| {
                        if result.is_none() {
                            *cancelled.borrow_mut() = true;
                        }
                    }
                }),
        )
    }
}

#[gpui::test]
async fn test_input_escape_cancels_edit(cx: &mut TestAppContext) {
    let confirmed_value: Arc<RefCell<Option<String>>> = Arc::new(RefCell::new(None));
    let cancelled: Arc<RefCell<bool>> = Arc::new(RefCell::new(false));

    let confirmed_clone = confirmed_value.clone();
    let cancelled_clone = cancelled.clone();

    let window = cx.add_window(move |_window, _cx| InputEscapeTestView {
        confirmed_value: confirmed_clone,
        cancelled: cancelled_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("escape-test-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Type some text
        cx.simulate_input("draft text");
        cx.run_until_parked();

        // Press Escape to cancel
        cx.simulate_keystrokes("escape");
        cx.run_until_parked();

        // Verify on_change was NOT called (Escape cancels)
        let confirmed = confirmed_value.borrow();
        assert!(
            confirmed.is_none(),
            "on_change should NOT be called on Escape"
        );

        // Verify on_edit_end was called with None (indicating cancel)
        assert!(
            *cancelled.borrow(),
            "on_edit_end should be called with None on Escape"
        );
    }
}

// ============================================================================
// Mouse Action Tests - Double-click, Drag Selection
// ============================================================================

/// View for testing double-click select all
struct InputDoubleClickTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
}

impl Render for InputDoubleClickTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let text_changes = self.text_changes.clone();

        div().size_full().child(
            Input::new("doubleclick-test-input")
                .value("Hello World")
                .on_text_change(move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                }),
        )
    }
}

/// Test that double-clicking selects all text
#[gpui::test]
async fn test_input_double_click_selects_all(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputDoubleClickTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("doubleclick-test-input") {
        let center = bounds.center();

        // Double-click to select all (simulated with click_count = 2)
        // First click
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Second click (double-click)
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Now type something - if text was selected, it should replace
        cx.simulate_input("X");
        cx.run_until_parked();

        // Check if the text was replaced (indicating selection worked)
        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // If double-click select all worked, typing "X" should replace the entire text
            // The text should be "X" or contain "X" as replacement
            assert!(
                last.contains("X"),
                "After double-click and typing, text should contain 'X', got: {}",
                last
            );
        }
    }
}

/// View for testing mouse drag selection
struct InputDragSelectTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
}

impl Render for InputDragSelectTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let text_changes = self.text_changes.clone();

        div().size_full().child(
            Input::new("drag-select-input")
                .value("Select some text here")
                .on_text_change(move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                }),
        )
    }
}

/// Test that mouse drag selects text
#[gpui::test]
async fn test_input_mouse_drag_selects_text(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputDragSelectTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("drag-select-input") {
        // Simulate drag from left side to right side of input
        let start = gpui::point(bounds.left() + gpui::px(10.0), bounds.center().y);
        let end = gpui::point(bounds.right() - gpui::px(10.0), bounds.center().y);

        // Mouse down at start position
        cx.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Drag to end position (with left button held)
        cx.simulate_mouse_move(end, Some(MouseButton::Left), Modifiers::default());
        cx.run_until_parked();

        // Mouse up at end position
        cx.simulate_mouse_up(end, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Now type something to replace the selection
        cx.simulate_input("NEW");
        cx.run_until_parked();

        // Check that text was modified (selection was replaced)
        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            assert!(
                last.contains("NEW"),
                "After drag select and typing, text should contain 'NEW', got: {}",
                last
            );
        }
    }
}

// ============================================================================
// Keyboard Shortcut Tests - Backspace, Delete, Arrow Keys
// ============================================================================

/// View for testing keyboard navigation
struct InputKeyboardNavTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
}

impl Render for InputKeyboardNavTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let text_changes = self.text_changes.clone();

        div().size_full().child(
            Input::new("keyboard-nav-input")
                .value("ABCDEF")
                .on_text_change(move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                }),
        )
    }
}

/// Test backspace key deletes character before cursor
#[gpui::test]
async fn test_input_backspace_deletes(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus (cursor at end)
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Clear any initial selection by pressing right arrow
        cx.simulate_keystrokes("right");
        cx.run_until_parked();

        // Press backspace to delete last character
        cx.simulate_keystrokes("backspace");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // Should have deleted the last character
            assert!(
                last.len() < 6 || !last.ends_with("F"),
                "Backspace should have deleted a character, got: {}",
                last
            );
        }
    }
}

/// Test delete key deletes character after cursor
#[gpui::test]
async fn test_input_delete_key(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Move cursor to beginning
        cx.simulate_keystrokes("home");
        cx.run_until_parked();

        // Press delete to remove first character
        cx.simulate_keystrokes("delete");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // Should have deleted the first character 'A'
            assert!(
                !last.starts_with("A") || last.len() < 6,
                "Delete should have removed first character, got: {}",
                last
            );
        }
    }
}

/// Test arrow key navigation
#[gpui::test]
async fn test_input_arrow_key_navigation(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Move to end, then left twice, then insert a character
        cx.simulate_keystrokes("end");
        cx.run_until_parked();

        cx.simulate_keystrokes("left left");
        cx.run_until_parked();

        // Insert 'X' - should be inserted before last 2 characters
        cx.simulate_input("X");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // 'X' should be inserted at position len-2
            assert!(
                last.contains("X"),
                "Should have inserted 'X' after arrow navigation, got: {}",
                last
            );
        }
    }
}

// ============================================================================
// Emacs Keybindings Tests (Ctrl+A, Ctrl+E, etc.)
// ============================================================================

/// Test Ctrl+A moves cursor to beginning (Emacs style)
#[gpui::test]
async fn test_input_ctrl_a_moves_to_beginning(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Clear selection first
        cx.simulate_keystrokes("right");
        cx.run_until_parked();

        // Ctrl+A to move to beginning
        cx.simulate_keystrokes("ctrl-a");
        cx.run_until_parked();

        // Insert 'Z' at the beginning
        cx.simulate_input("Z");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // 'Z' should be at the beginning
            assert!(
                last.starts_with("Z"),
                "Ctrl+A should move cursor to beginning, got: {}",
                last
            );
        }
    }
}

/// Test Ctrl+E moves cursor to end (Emacs style)
#[gpui::test]
async fn test_input_ctrl_e_moves_to_end(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Move to beginning first
        cx.simulate_keystrokes("home");
        cx.run_until_parked();

        // Ctrl+E to move to end
        cx.simulate_keystrokes("ctrl-e");
        cx.run_until_parked();

        // Insert 'Z' at the end
        cx.simulate_input("Z");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // 'Z' should be at the end
            assert!(
                last.ends_with("Z"),
                "Ctrl+E should move cursor to end, got: {}",
                last
            );
        }
    }
}

/// Test Ctrl+K kills to end of line (Emacs style)
#[gpui::test]
async fn test_input_ctrl_k_kills_to_end(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Move to position 2 (after "AB")
        cx.simulate_keystrokes("home right right");
        cx.run_until_parked();

        // Ctrl+K to kill to end
        cx.simulate_keystrokes("ctrl-k");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // Should only have "AB" left
            assert_eq!(
                last, "AB",
                "Ctrl+K should kill text after cursor, got: {}",
                last
            );
        }
    }
}

/// Test Ctrl+U kills to beginning of line (Emacs style)
#[gpui::test]
async fn test_input_ctrl_u_kills_to_beginning(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputKeyboardNavTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("keyboard-nav-input") {
        let center = bounds.center();

        // Click to focus
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Move to position 4 (after "ABCD")
        cx.simulate_keystrokes("home right right right right");
        cx.run_until_parked();

        // Ctrl+U to kill to beginning
        cx.simulate_keystrokes("ctrl-u");
        cx.run_until_parked();

        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            // Should only have "EF" left
            assert_eq!(
                last, "EF",
                "Ctrl+U should kill text before cursor, got: {}",
                last
            );
        }
    }
}

// ============================================================================
// Clipboard Tests (Copy, Cut, Paste)
// ============================================================================

/// View for testing clipboard operations
struct InputClipboardTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
}

impl Render for InputClipboardTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let text_changes = self.text_changes.clone();

        div()
            .size_full()
            .child(Input::new("clipboard-test-input").value("").on_text_change(
                move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                },
            ))
    }
}

/// Test Copy/Paste functionality
#[gpui::test]
async fn test_input_copy_paste(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputClipboardTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("clipboard-test-input") {
        let center = bounds.center();

        // Focus input
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Type "Hello"
        cx.simulate_input("Hello");
        cx.run_until_parked();

        // Select All (Cmd+A)
        #[cfg(target_os = "macos")]
        cx.simulate_keystrokes("cmd-a");
        #[cfg(not(target_os = "macos"))]
        cx.simulate_keystrokes("ctrl-a");
        cx.run_until_parked();

        // Copy (Cmd+C)
        #[cfg(target_os = "macos")]
        cx.simulate_keystrokes("cmd-c");
        #[cfg(not(target_os = "macos"))]
        cx.simulate_keystrokes("ctrl-c");
        cx.run_until_parked();

        // Move to end (clearing selection) and add space
        cx.simulate_keystrokes("right");
        cx.simulate_input(" ");
        cx.run_until_parked();

        // Paste (Cmd+V)
        #[cfg(target_os = "macos")]
        cx.simulate_keystrokes("cmd-v");
        #[cfg(not(target_os = "macos"))]
        cx.simulate_keystrokes("ctrl-v");
        cx.run_until_parked();

        // Should now be "Hello Hello"
        let changes = text_changes.borrow();
        if !changes.is_empty() {
            let last = changes.last().unwrap();
            assert_eq!(
                last, "Hello Hello",
                "Paste should append copied text, got: {}",
                last
            );
        }
    }
}

/// Test Cut functionality
#[gpui::test]
async fn test_input_cut(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputClipboardTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("clipboard-test-input") {
        let center = bounds.center();

        // Focus input
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Type "RemoveKeep"
        cx.simulate_input("RemoveKeep");
        cx.run_until_parked();

        // Move to beginning
        cx.simulate_keystrokes("home");

        // Select first 6 chars "Remove" (Shift+Right x6)
        for _ in 0..6 {
            cx.simulate_keystrokes("shift-right");
        }
        cx.run_until_parked();

        // Cut (Cmd+X)
        #[cfg(target_os = "macos")]
        cx.simulate_keystrokes("cmd-x");
        #[cfg(not(target_os = "macos"))]
        cx.simulate_keystrokes("ctrl-x");
        cx.run_until_parked();

        // Should now be "Keep"
        {
            let changes = text_changes.borrow();
            let last = changes.last().unwrap();
            assert_eq!(
                last, "Keep",
                "Cut should remove selected text, got: {}",
                last
            );
        }

        // Move to end and Paste to verify it was copied
        cx.simulate_keystrokes("end");
        #[cfg(target_os = "macos")]
        cx.simulate_keystrokes("cmd-v");
        #[cfg(not(target_os = "macos"))]
        cx.simulate_keystrokes("ctrl-v");
        cx.run_until_parked();

        // Should now be "KeepRemove"
        {
            let changes = text_changes.borrow();
            let last = changes.last().unwrap();
            assert_eq!(
                last, "KeepRemove",
                "Paste after cut should restore text, got: {}",
                last
            );
        }
    }
}

// ============================================================================
// Disabled State Tests
// ============================================================================

/// View for testing disabled input
struct InputDisabledTestView {
    text_changes: Arc<RefCell<Vec<String>>>,
}

impl Render for InputDisabledTestView {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let text_changes = self.text_changes.clone();

        div().size_full().child(
            Input::new("disabled-test-input")
                .value("Cannot edit this")
                .disabled(true)
                .on_text_change(move |text, _window, _cx| {
                    text_changes.borrow_mut().push(text);
                }),
        )
    }
}

/// Test that disabled input doesn't accept input
#[gpui::test]
async fn test_input_disabled_no_input(cx: &mut TestAppContext) {
    let text_changes: Arc<RefCell<Vec<String>>> = Arc::new(RefCell::new(Vec::new()));
    let text_changes_clone = text_changes.clone();

    let window = cx.add_window(move |_window, _cx| InputDisabledTestView {
        text_changes: text_changes_clone,
    });

    let mut cx = VisualTestContext::from_window(window.into(), cx);
    cx.run_until_parked();

    if let Some(bounds) = cx.debug_bounds("disabled-test-input") {
        let center = bounds.center();

        // Try to click on disabled input
        cx.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
        cx.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
        cx.run_until_parked();

        // Try to type
        cx.simulate_input("X");
        cx.run_until_parked();

        // No changes should have been recorded
        let changes = text_changes.borrow();
        assert!(
            changes.is_empty(),
            "Disabled input should not accept input, got {} changes",
            changes.len()
        );
    }
}