fresh-editor 0.2.11

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! E2E tests for paste handling
//!
//! These tests verify paste behavior including:
//! - Paste with selection (should replace selection)
//! - Multi-cursor paste
//! - Paste undo atomicity
//!
//! Issue #372: External paste should behave like internal paste

use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};

/// Test that paste replaces the current selection
/// Bug: Current paste() doesn't delete selection before inserting
#[test]
fn test_paste_replaces_selection() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type some text
    harness.type_text("hello world").unwrap();
    harness.assert_buffer_content("hello world");

    // Select "world" (positions 6-11)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    for _ in 0..6 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }

    // Verify selection
    let primary = harness.editor().active_cursors().primary();
    assert_eq!(primary.position, 11, "Cursor should be at end of 'world'");
    assert_eq!(
        primary.anchor,
        Some(6),
        "Anchor should be at start of 'world'"
    );

    // Set clipboard content and paste (use test-only paste to avoid system clipboard interference)
    harness
        .editor_mut()
        .set_clipboard_for_test("universe".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    // "world" should be replaced with "universe"
    harness.assert_buffer_content("hello universe");
}

/// Test that paste works with multiple cursors
/// Bug: Current paste() only handles primary cursor
#[test]
fn test_paste_with_multiple_cursors() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Create three lines
    harness.type_text("aaa\nbbb\nccc").unwrap();
    harness.assert_buffer_content("aaa\nbbb\nccc");

    // Go to start and add cursors on each line
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.editor_mut().add_cursor_below();
    harness.editor_mut().add_cursor_below();

    // Should have 3 cursors
    assert_eq!(harness.editor().active_cursors().count(), 3);

    // Set clipboard and paste
    harness.editor_mut().set_clipboard_for_test("X".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    // Should have X inserted at start of each line
    let content = harness.get_buffer_content().unwrap();
    let x_count = content.matches('X').count();
    assert_eq!(
        x_count, 3,
        "Should have 3 X's (one per cursor), got {}. Buffer:\n{}",
        x_count, content
    );
    harness.assert_buffer_content("Xaaa\nXbbb\nXccc");
}

/// Test that paste with multiple cursors and selections replaces all selections
#[test]
fn test_paste_replaces_multiple_selections() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Create text with repeated words
    harness.type_text("foo bar foo baz foo").unwrap();
    harness.assert_buffer_content("foo bar foo baz foo");

    // Select the first "foo" (positions 0-3)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();

    // Add cursor at next "foo" match (Ctrl+D behavior)
    harness.editor_mut().add_cursor_at_next_match();
    harness.render().unwrap();

    // Add cursor at third "foo" match
    harness.editor_mut().add_cursor_at_next_match();
    harness.render().unwrap();

    // Should have 3 cursors, each selecting "foo"
    assert_eq!(harness.editor().active_cursors().count(), 3);

    // Set clipboard and paste
    harness
        .editor_mut()
        .set_clipboard_for_test("XXX".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    // All "foo"s should be replaced with "XXX"
    harness.assert_buffer_content("XXX bar XXX baz XXX");
}

/// Test that paste is atomic for undo (single undo step)
#[test]
fn test_paste_undo_is_atomic() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type initial text
    harness.type_text("hello").unwrap();
    harness.assert_buffer_content("hello");

    // Paste some text
    harness
        .editor_mut()
        .set_clipboard_for_test(" world".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();
    harness.assert_buffer_content("hello world");

    // Undo should remove entire paste in one step
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_buffer_content("hello");

    // Redo should restore entire paste in one step
    harness
        .send_key(KeyCode::Char('y'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_buffer_content("hello world");
}

/// Test that multi-cursor paste is atomic for undo
#[test]
fn test_multi_cursor_paste_undo_is_atomic() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Create three lines
    harness.type_text("aaa\nbbb\nccc").unwrap();
    harness.assert_buffer_content("aaa\nbbb\nccc");

    // Go to start and add cursors
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.editor_mut().add_cursor_below();
    harness.editor_mut().add_cursor_below();

    // Should have 3 cursors
    assert_eq!(harness.editor().active_cursors().count(), 3);

    // Paste
    harness.editor_mut().set_clipboard_for_test("X".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    // Verify paste worked
    let content = harness.get_buffer_content().unwrap();
    let x_count = content.matches('X').count();
    assert_eq!(x_count, 3, "Should have 3 X's. Buffer:\n{}", content);

    // Single undo should remove ALL X's
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let content_after_undo = harness.get_buffer_content().unwrap();
    let x_count_after_undo = content_after_undo.matches('X').count();
    assert_eq!(
        x_count_after_undo, 0,
        "Single undo should remove all X's. Buffer:\n{}",
        content_after_undo
    );
    harness.assert_buffer_content("aaa\nbbb\nccc");
}

/// Test paste with selection replacement is atomic for undo
/// This is the most complex case: delete selection + insert = one undo step
#[test]
fn test_paste_with_selection_undo_is_atomic() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type text
    harness.type_text("hello world").unwrap();
    harness.assert_buffer_content("hello world");

    // Select "world"
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    for _ in 0..6 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }

    // Paste to replace selection
    harness
        .editor_mut()
        .set_clipboard_for_test("universe".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();
    harness.assert_buffer_content("hello universe");

    // Single undo should restore "world" (undo both delete and insert)
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_buffer_content("hello world");

    // Redo should replace "world" with "universe" again
    harness
        .send_key(KeyCode::Char('y'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_buffer_content("hello universe");
}

/// Test that pasting multiline text works correctly
#[test]
fn test_paste_multiline_text() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Start with empty buffer
    harness.assert_buffer_content("");

    // Paste multiline text
    harness
        .editor_mut()
        .set_clipboard_for_test("line1\nline2\nline3".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    harness.assert_buffer_content("line1\nline2\nline3");

    // Single undo should remove all three lines
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_buffer_content("");
}

/// Test that paste at end of line works correctly
#[test]
fn test_paste_at_end_of_line() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    harness.type_text("hello").unwrap();
    harness.assert_buffer_content("hello");

    // Cursor is already at end of line after typing
    harness
        .editor_mut()
        .set_clipboard_for_test(" world".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    harness.assert_buffer_content("hello world");
}

/// Test that paste in middle of text works correctly
#[test]
fn test_paste_in_middle() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    harness.type_text("helloworld").unwrap();
    harness.assert_buffer_content("helloworld");

    // Move to position 5 (between "hello" and "world")
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }

    harness.editor_mut().set_clipboard_for_test(" ".to_string());
    harness.editor_mut().paste_for_test();
    harness.render().unwrap();

    harness.assert_buffer_content("hello world");
}

// ============================================================================
// Prompt paste tests
// ============================================================================

/// Test that external paste (bracketed paste / Ctrl+Shift+V) goes to prompt when prompt is open
///
/// Bug: Previously, external paste always went to the editor buffer, ignoring the open prompt
#[test]
fn test_external_paste_goes_to_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type some text in the buffer first
    harness.type_text("buffer content").unwrap();
    harness.assert_buffer_content("buffer content");

    // Open the command palette prompt
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains(">command");

    // Simulate external paste (bracketed paste) - this should go to the prompt, not the buffer
    harness.editor_mut().paste_text("pasted text".to_string());
    harness.render().unwrap();

    // The pasted text should appear in the prompt
    harness.assert_screen_contains(">pasted text");

    // The buffer should NOT be modified
    harness.assert_buffer_content("buffer content");
}

/// Test that external paste works in the Open File prompt
#[test]
fn test_external_paste_in_open_file_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Open the "Open File" prompt
    harness
        .send_key(KeyCode::Char('o'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Open file:");

    // Simulate external paste of a file path
    harness
        .editor_mut()
        .paste_text("/path/to/file.txt".to_string());
    harness.render().unwrap();

    // The path should appear in the prompt
    harness.assert_screen_contains("/path/to/file.txt");
}

/// Test that external paste appends to existing text in prompt
#[test]
fn test_external_paste_appends_to_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Open prompt and type some text
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("hello ").unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains(">hello ");

    // Paste more text
    harness.editor_mut().paste_text("world".to_string());
    harness.render().unwrap();

    // Should see both typed and pasted text
    harness.assert_screen_contains(">hello world");
}

/// Test that Ctrl+V paste works in prompt
#[test]
fn test_ctrl_v_paste_in_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Set clipboard content
    harness
        .editor_mut()
        .set_clipboard_for_test("clipboard content".to_string());

    // Open prompt
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains(">command");

    // Press Ctrl+V to paste
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should see pasted text in prompt
    harness.assert_screen_contains(">clipboard content");
}

/// Test copy and paste workflow within prompt
#[test]
fn test_prompt_copy_paste_workflow() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Enable internal-only clipboard to avoid system clipboard interference in parallel tests
    harness.editor_mut().set_clipboard_for_test("".to_string());

    // Open prompt and type some text (Quick Open starts with ">" prefix)
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("copy me").unwrap();
    harness.render().unwrap();

    // Copy all text with Ctrl+C (copies ">copy me" including prefix)
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Clear the prompt by selecting all and deleting
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::CONTROL)
        .unwrap();
    harness
        .send_key(KeyCode::Delete, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // The prompt should be empty now (just hints line visible)
    harness.assert_screen_contains(">command");

    // Paste the copied text back
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should see the copied text (may have double ">" from paste)
    harness.assert_screen_contains("copy me");
}

/// Test cut and paste workflow in prompt
#[test]
fn test_prompt_cut_paste_workflow() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Enable internal-only clipboard to avoid system clipboard interference in parallel tests
    harness.editor_mut().set_clipboard_for_test("".to_string());

    // Open prompt and type some text (Quick Open starts with ">" prefix)
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("cut me").unwrap();
    harness.process_async_and_render().unwrap();
    harness.assert_screen_contains(">cut me");

    // Cut all text with Ctrl+X (cuts ">cut me" including prefix)
    harness
        .send_key(KeyCode::Char('x'), KeyModifiers::CONTROL)
        .unwrap();
    harness.process_async_and_render().unwrap();

    // The prompt should be empty now (text was cut)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains(">command") && !screen.contains("cut me"),
        "Prompt should be empty after cut. Screen:\n{}",
        screen
    );

    // Paste the cut text back
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.process_async_and_render().unwrap();

    // Should see the cut text pasted back (may have double ">" from paste)
    harness.assert_screen_contains("cut me");
}

/// Test that copy with selection only copies selected text
#[test]
fn test_prompt_copy_selection() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Enable internal-only clipboard to avoid system clipboard interference in parallel tests
    harness.editor_mut().set_clipboard_for_test("".to_string());

    // Open prompt and type some text (Quick Open starts with ">" prefix)
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("hello world").unwrap();
    harness.render().unwrap();

    // Move to start, skip the ">" prefix, then select "hello" (5 characters)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap(); // Skip ">"
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }

    // Copy selection
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();

    // Cancel prompt and open a new one
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Paste - should only paste "hello", not "hello world"
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should see ">hello" (the ">" prefix plus pasted "hello")
    harness.assert_screen_contains(">hello");
    // Verify we didn't paste "world"
    let screen = harness.screen_to_string();
    // Find the prompt input line (starts with ">")
    let prompt_content = screen
        .lines()
        .find(|line| line.starts_with(">"))
        .unwrap_or("");
    assert!(
        !prompt_content.contains("world"),
        "Should only paste selected text 'hello', not 'world'. Line: {}",
        prompt_content
    );
}

/// Test that cut with selection only cuts selected text
#[test]
fn test_prompt_cut_selection() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Enable internal-only clipboard to avoid system clipboard interference in parallel tests
    harness.editor_mut().set_clipboard_for_test("".to_string());

    // Open prompt and type some text (Quick Open starts with ">" prefix)
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("hello world").unwrap();
    harness.render().unwrap();

    // Move to start, skip the ">" prefix, then select "hello " (6 characters)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap(); // Skip ">"
    for _ in 0..6 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }

    // Cut selection
    harness
        .send_key(KeyCode::Char('x'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should only have ">world" remaining (prefix + remaining text)
    harness.assert_screen_contains(">world");

    // Cancel and open new prompt to verify cut text
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should paste "hello " (the ">" prefix is already there from Quick Open)
    harness.assert_screen_contains(">hello ");
}

/// Test paste replaces selection in prompt
#[test]
fn test_prompt_paste_replaces_selection() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Set clipboard
    harness
        .editor_mut()
        .set_clipboard_for_test("replaced".to_string());

    // Open prompt and type some text (Quick Open starts with ">" prefix)
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("hello world").unwrap();
    harness.render().unwrap();

    // Select "world" (move to position 7 to skip ">hello ", then select 5 chars)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    for _ in 0..7 {
        // Skip ">hello " (7 characters)
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }

    // Paste - should replace "world" with "replaced"
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_screen_contains(">hello replaced");
}

/// Test external paste replaces selection in prompt
#[test]
fn test_external_paste_replaces_prompt_selection() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Open prompt and type some text
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("old text").unwrap();
    harness.render().unwrap();

    // Select all
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::CONTROL)
        .unwrap();

    // External paste should replace selection
    // Note: When select-all replaces everything including the ">" prefix,
    // the pasted text appears without the prefix
    harness.editor_mut().paste_text("new text".to_string());
    harness.render().unwrap();

    harness.assert_screen_contains("new text");
    let screen = harness.screen_to_string();
    // Find the input line (last line that's not the hints line)
    let lines: Vec<&str> = screen.lines().collect();
    let input_line = lines.last().unwrap_or(&"");
    assert!(
        !input_line.contains("old"),
        "Old text should be replaced. Line: {}",
        input_line
    );
}

/// Test that paste in search prompt works
#[test]
fn test_paste_in_search_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type some content to search in
    harness.type_text("find this text").unwrap();

    // Set clipboard
    harness
        .editor_mut()
        .set_clipboard_for_test("this".to_string());

    // Open search prompt
    harness
        .send_key(KeyCode::Char('f'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Search:");

    // Paste the search term
    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness.assert_screen_contains("Search: this");
}

// ============================================================================
// CRLF paste normalization tests (Issue #427)
// ============================================================================

/// Test that pasting CRLF text is normalized to the buffer's line ending format
/// Issue #427: On Windows, pasting multiline text collapsed into single line
#[test]
fn test_paste_crlf_text_normalized() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Start with empty buffer (default LF line endings)
    harness.assert_buffer_content("");

    // Paste text with Windows CRLF line endings
    harness
        .editor_mut()
        .paste_text("line1\r\nline2\r\nline3".to_string());
    harness.render().unwrap();

    // Should be normalized to LF (the buffer's default)
    harness.assert_buffer_content("line1\nline2\nline3");
}

/// Test that pasting CR-only text (old Mac) is normalized
#[test]
fn test_paste_cr_only_text_normalized() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Start with empty buffer
    harness.assert_buffer_content("");

    // Paste text with old Mac CR-only line endings
    harness
        .editor_mut()
        .paste_text("line1\rline2\rline3".to_string());
    harness.render().unwrap();

    // Should be normalized to LF
    harness.assert_buffer_content("line1\nline2\nline3");
}

/// Test that pasting into a CRLF buffer preserves CRLF format
#[test]
fn test_paste_into_crlf_buffer() {
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("crlf_paste_test.txt");

    // Create a file with CRLF line endings
    std::fs::write(&file_path, "existing\r\n").unwrap();

    let mut harness = EditorTestHarness::new(80, 24).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Move to end of buffer
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();

    // Paste text (even with LF, should convert to CRLF)
    harness.editor_mut().paste_text("new\nlines".to_string());
    harness.render().unwrap();

    // Buffer should now contain both original and pasted text with CRLF
    let content = harness.get_buffer_content().unwrap();
    assert!(
        content.contains("\r\n"),
        "Pasted text should use CRLF in CRLF buffer"
    );
    assert!(
        content.contains("existing\r\nnew\r\nlines"),
        "Content should be: {:?}",
        content
    );
}

/// Test that mixed line endings in paste are all normalized
#[test]
fn test_paste_mixed_line_endings() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Paste text with mixed line endings (CRLF, CR, LF)
    harness
        .editor_mut()
        .paste_text("crlf\r\ncr\rlf\n".to_string());
    harness.render().unwrap();

    // All should be normalized to LF
    harness.assert_buffer_content("crlf\ncr\nlf\n");
}

/// Test that pasting CRLF into prompt works correctly
#[test]
fn test_paste_crlf_into_prompt() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Open the command palette prompt
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains(">command");

    // Paste text with CRLF (should be normalized to LF for prompt)
    harness
        .editor_mut()
        .paste_text("line1\r\nline2".to_string());
    harness.render().unwrap();

    // Prompt should contain the text (newlines may be shown differently in prompt)
    harness.assert_screen_contains("line1");
}