pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use tokio::sync::mpsc;

use crate::state::{AppState, PackageItem, QueryInput};
use crate::theme::KeyChord;

use super::super::utils::matches_any;
use super::handle_search_key;
use super::helpers::navigate_pane;

/// What: Provide a baseline `AppState` tailored for search-pane tests without repeating setup boilerplate.
///
/// Inputs:
/// - None (relies on `Default::default()` for deterministic initial state).
///
/// Output:
/// - Fresh `AppState` ready for mutation inside individual test cases.
///
/// Details:
/// - Keeps search tests concise by centralizing the default application setup in one helper.
fn new_app() -> AppState {
    AppState::default()
}

#[test]
/// What: Insert-mode typing should update the search input, caret, and emit query messages.
///
/// Inputs:
/// - Key events for `'r'`, `'g'`, and `Backspace` applied in sequence while in insert mode.
///
/// Output:
/// - `app.input` transitions `"" -> "r" -> "rg" -> "r"`, and at least one query arrives on `qrx`.
///
/// Details:
/// - Validates caret/anchor bookkeeping indirectly by observing the query channel after each keystroke.
fn search_insert_typing_and_backspace() {
    let mut app = new_app();
    let (qtx, mut qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('r'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('g'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Backspace, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert_eq!(app.input, "r");
    // At least one query should have been sent
    assert!(qrx.try_recv().ok().is_some());
}

#[test]
/// What: Normal-mode selection commands should set the anchor and adjust the caret within bounds.
///
/// Inputs:
/// - Escape key to enter normal mode, followed by `'l'` (select-right) and `'h'` (select-left).
///
/// Output:
/// - `search_select_anchor` becomes `Some` and the caret remains within the valid input character range.
///
/// Details:
/// - Confirms the keymap translation respects the default bindings for navigation-style selection.
fn search_normal_mode_selection() {
    let mut app = new_app();
    app.input = "rip".into();
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Toggle into normal mode (Esc by default per KeyMap)
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Esc, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    // Select right (default 'l')
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('l'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert!(app.search_select_anchor.is_some());
    // Select left (default 'h')
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('h'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert!(app.search_caret <= crate::events::utils::char_count(&app.input));
}

#[test]
/// What: Verify `matches_any` correctly handles Shift+char edge cases across different terminal behaviors.
///
/// Inputs:
/// - Key events with and without Shift modifier, matched against configured chords with Shift.
///
/// Output:
/// - Returns `true` when the key event matches the configured chord, handling terminal inconsistencies.
///
/// Details:
/// - Tests that uppercase chars match Shift+lowercase configs even if terminal doesn't report Shift.
/// - Tests that lowercase chars with Shift modifier match Shift+lowercase configs.
/// - Tests exact matches without Shift handling.
fn helpers_matches_any_shift_handling() {
    use crossterm::event::KeyCode;

    // Test exact match (no Shift involved)
    let ke1 = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL);
    let chord1 = vec![KeyChord {
        code: KeyCode::Char('r'),
        mods: KeyModifiers::CONTROL,
    }];
    assert!(matches_any(&ke1, &chord1));

    // Test Shift+char config matching uppercase char (terminal may not report Shift)
    let ke2 = KeyEvent::new(KeyCode::Char('R'), KeyModifiers::empty());
    let chord2 = vec![KeyChord {
        code: KeyCode::Char('r'),
        mods: KeyModifiers::SHIFT,
    }];
    assert!(matches_any(&ke2, &chord2));

    // Test Shift+char config matching lowercase char with Shift modifier
    let ke3 = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::SHIFT);
    let chord3 = vec![KeyChord {
        code: KeyCode::Char('r'),
        mods: KeyModifiers::SHIFT,
    }];
    assert!(matches_any(&ke3, &chord3));

    // Test non-match case
    let ke4 = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::empty());
    let chord4 = vec![KeyChord {
        code: KeyCode::Char('r'),
        mods: KeyModifiers::SHIFT,
    }];
    assert!(!matches_any(&ke4, &chord4));
}

#[test]
/// What: Normal mode deletion (default: 'd') should remove selected text range and trigger query.
///
/// Inputs:
/// - Enter normal mode, set selection anchor, move caret, then press 'd' to delete.
///
/// Output:
/// - Selected text is removed from input, caret moves to start of deleted range, query is sent.
///
/// Details:
/// - Validates that deletion respects anchor and caret positions, removing the range between them.
fn search_normal_mode_deletion() {
    let mut app = new_app();
    app.input = "hello world".into();
    app.search_caret = 6; // After "hello "
    let (qtx, mut qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Enter normal mode
    app.search_normal_mode = true;
    // Set anchor at start
    app.search_select_anchor = Some(0);
    // Move caret to position 5 (after "hello")
    app.search_caret = 5;

    // Delete selection (default 'd')
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('d'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    // Should have deleted "hello"
    assert_eq!(app.input, " world");
    assert_eq!(app.search_caret, 0);
    assert!(app.search_select_anchor.is_none());
    // Query should have been sent
    assert!(qrx.try_recv().ok().is_some());
}

#[test]
/// What: Normal mode clear (default: Shift+Del) should clear entire input and trigger query.
///
/// Inputs:
/// - Enter normal mode with non-empty input, then press Shift+Del.
///
/// Output:
/// - Input is cleared, caret reset to 0, selection anchor cleared, query is sent.
///
/// Details:
/// - Validates that clear operation resets all input-related state.
fn search_normal_mode_clear() {
    let mut app = new_app();
    app.input = "test query".into();
    app.search_caret = 5;
    app.search_select_anchor = Some(3);
    let (qtx, mut qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Enter normal mode
    app.search_normal_mode = true;
    // Configure clear keybinding
    app.keymap.search_normal_clear = vec![KeyChord {
        code: KeyCode::Delete,
        mods: KeyModifiers::SHIFT,
    }];

    // Clear input
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Delete, KeyModifiers::SHIFT),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(app.input, "");
    assert_eq!(app.search_caret, 0);
    assert!(app.search_select_anchor.is_none());
    // Query should have been sent
    assert!(qrx.try_recv().ok().is_some());
}

#[test]
/// What: Mode toggle should switch between insert and normal mode.
///
/// Inputs:
/// - Start in insert mode, press toggle key (default: Esc), then toggle again.
///
/// Output:
/// - Mode toggles between false (insert) and true (normal), selection anchor cleared on exit.
///
/// Details:
/// - Validates that mode toggle correctly updates `search_normal_mode` state.
fn search_mode_toggle() {
    let mut app = new_app();
    app.search_normal_mode = false;
    app.search_select_anchor = Some(5);
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Configure toggle key (default Esc)
    app.keymap.search_normal_toggle = vec![KeyChord {
        code: KeyCode::Esc,
        mods: KeyModifiers::empty(),
    }];

    // Toggle to normal mode
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Esc, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert!(app.search_normal_mode);

    // Toggle back to insert mode
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Esc, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert!(!app.search_normal_mode);
    // Note: Selection anchor is cleared when entering insert mode via search_normal_insert key,
    // but not when toggling mode. This is expected behavior.
}

#[test]
/// What: Pane navigation should correctly switch focus and update selection state.
///
/// Inputs:
/// - Call `navigate_pane` with "right" direction in different modes.
///
/// Output:
/// - Focus changes appropriately, selection state initialized if needed.
///
/// Details:
/// - Tests that right navigation targets Install/Downgrade based on `installed_only_mode`.
/// - Left navigation test is skipped as it requires Tokio runtime for preview trigger.
fn helpers_navigate_pane_directions() {
    let mut app = new_app();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();

    // Test right navigation in normal mode
    app.installed_only_mode = false;
    app.install_list.push(crate::state::PackageItem {
        name: "test-pkg".to_string(),
        version: "1.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Official {
            repo: "extra".to_string(),
            arch: "x86_64".to_string(),
        },
        popularity: None,
        out_of_date: None,
        orphaned: false,
    });
    navigate_pane(&mut app, "right", &dtx, &ptx);
    assert!(matches!(app.focus, crate::state::Focus::Install));

    // Test right navigation in installed-only mode
    app.installed_only_mode = true;
    app.downgrade_list.push(crate::state::PackageItem {
        name: "downgrade-pkg".to_string(),
        version: "1.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Official {
            repo: "extra".to_string(),
            arch: "x86_64".to_string(),
        },
        popularity: None,
        out_of_date: None,
        orphaned: false,
    });
    navigate_pane(&mut app, "right", &dtx, &ptx);
    assert!(matches!(app.focus, crate::state::Focus::Install));
    assert!(matches!(
        app.right_pane_focus,
        crate::state::RightPaneFocus::Downgrade
    ));
}

#[test]
/// What: Space key in insert mode should add items to appropriate lists based on mode.
///
/// Inputs:
/// - Press Space with a selected item in normal and installed-only modes.
///
/// Output:
/// - Items are sent to `add_tx` in normal mode, or added to `remove_list` in installed-only mode.
///
/// Details:
/// - Validates that Space correctly routes items based on `installed_only_mode` flag.
fn search_insert_mode_space_adds_items() {
    let mut app = new_app();
    let test_item = crate::state::PackageItem {
        name: "test-package".to_string(),
        version: "1.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Official {
            repo: "extra".to_string(),
            arch: "x86_64".to_string(),
        },
        popularity: None,
        out_of_date: None,
        orphaned: false,
    };
    app.results.push(test_item.clone());
    app.selected = 0;

    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, mut arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Test normal mode: should send to add_tx
    app.installed_only_mode = false;
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char(' '), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    let received = arx.try_recv().ok();
    assert!(received.is_some());
    assert_eq!(
        received
            .expect("received should be Some after is_some() check")
            .name,
        "test-package"
    );

    // Test installed-only mode: should add to remove_list
    app.installed_only_mode = true;
    app.results.push(test_item);
    app.selected = 0;
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char(' '), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert!(app.remove_list.iter().any(|p| p.name == "test-package"));
}

#[test]
/// What: Normal mode navigation keys (j/k) should move selection within bounds.
///
/// Inputs:
/// - Enter normal mode, press 'j' to move down, 'k' to move up.
///
/// Output:
/// - Selection index changes appropriately, staying within results bounds.
///
/// Details:
/// - Validates that j/k navigation respects result list boundaries.
fn search_normal_mode_navigation() {
    let mut app = new_app();
    app.results = vec![
        crate::state::PackageItem {
            name: "pkg1".to_string(),
            version: "1.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
        crate::state::PackageItem {
            name: "pkg2".to_string(),
            version: "1.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
        crate::state::PackageItem {
            name: "pkg3".to_string(),
            version: "1.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
    ];
    app.selected = 1;
    app.search_normal_mode = true;

    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    // Move down (j)
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('j'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert_eq!(app.selected, 2);

    // Move up (k)
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('k'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert_eq!(app.selected, 1);

    // Move up at top should stay at 0
    app.selected = 0;
    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('k'), KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );
    assert_eq!(app.selected, 0);
}

#[test]
/// What: Ensure AUR vote action is rejected for non-AUR packages.
///
/// Inputs:
/// - Search normal mode with selected official package and `Ctrl+e` key press.
///
/// Output:
/// - Vote request is not queued and a toast explains availability.
///
/// Details:
/// - Guards phase-3 vote action from running on non-AUR selections.
fn search_normal_mode_vote_rejects_non_aur_package() {
    let mut app = new_app();
    app.search_normal_mode = true;
    app.results = vec![crate::state::PackageItem {
        name: "ripgrep".to_string(),
        version: "14.1.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Official {
            repo: "extra".to_string(),
            arch: "x86_64".to_string(),
        },
        popularity: None,
        out_of_date: None,
        orphaned: false,
    }];
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert!(app.pending_aur_vote_intent.is_none());
    assert!(app.pending_aur_vote_request.is_none());
    let toast = app
        .toast_message
        .as_ref()
        .expect("non-AUR vote should produce toast");
    assert!(toast.contains("only available for AUR packages"));
}

#[test]
/// What: Ensure AUR vote action opens confirmation flow when feature is enabled.
///
/// Inputs:
/// - Search normal mode with selected AUR package and `Ctrl+e` key press.
///
/// Output:
/// - Vote intent is stored and confirm modal is shown.
///
/// Details:
/// - Default settings enable AUR voting, so action should enter confirmation flow.
fn search_normal_mode_vote_opens_confirmation_when_enabled() {
    let mut app = new_app();
    app.search_normal_mode = true;
    let pkgbase = "pkg-vote-default-normal";
    app.results = vec![crate::state::PackageItem {
        name: pkgbase.to_string(),
        version: "1.0.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Aur,
        popularity: None,
        out_of_date: None,
        orphaned: false,
    }];
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(
        app.pending_aur_vote_intent,
        Some((pkgbase.to_string(), crate::sources::VoteAction::Vote))
    );
    assert!(app.pending_aur_vote_request.is_none());
    match app.modal {
        crate::state::Modal::ConfirmAurVote {
            pkgbase, action, ..
        } => {
            assert_eq!(pkgbase, "pkg-vote-default-normal");
            assert_eq!(action, crate::sources::VoteAction::Vote);
        }
        other => panic!("expected confirm modal, got {other:?}"),
    }
}

#[test]
/// What: Ensure AUR vote key works in insert mode and opens confirmation.
///
/// Inputs:
/// - Search insert mode with selected AUR package and `Ctrl+e` key press.
///
/// Output:
/// - Vote intent is stored and confirm modal is shown.
///
/// Details:
/// - Guards against silent no-op behavior in insert mode.
fn search_insert_mode_vote_opens_confirmation_when_enabled() {
    let mut app = new_app();
    app.search_normal_mode = false;
    let pkgbase = "pkg-vote-default-insert";
    app.results = vec![crate::state::PackageItem {
        name: pkgbase.to_string(),
        version: "1.0.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Aur,
        popularity: None,
        out_of_date: None,
        orphaned: false,
    }];
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(
        app.pending_aur_vote_intent,
        Some((pkgbase.to_string(), crate::sources::VoteAction::Vote))
    );
    assert!(app.pending_aur_vote_request.is_none());
    match app.modal {
        crate::state::Modal::ConfirmAurVote {
            pkgbase, action, ..
        } => {
            assert_eq!(pkgbase, "pkg-vote-default-insert");
            assert_eq!(action, crate::sources::VoteAction::Vote);
        }
        other => panic!("expected confirm modal, got {other:?}"),
    }
}

#[test]
/// What: Ensure `Ctrl+e` toggles to unvote when cache says package is already voted.
///
/// Inputs:
/// - Search normal mode with selected AUR package and cached `Voted` state.
///
/// Output:
/// - Confirmation intent/modal uses `VoteAction::Unvote`.
///
/// Details:
/// - Verifies single-keybind toggle behavior from persisted vote-state.
fn search_normal_mode_vote_toggles_to_unvote_when_cached_voted() {
    let mut app = new_app();
    app.search_normal_mode = true;
    app.results = vec![crate::state::PackageItem {
        name: "pacsea-bin".to_string(),
        version: "1.0.0".to_string(),
        description: String::new(),
        source: crate::state::Source::Aur,
        popularity: None,
        out_of_date: None,
        orphaned: false,
    }];
    app.aur_vote_state_by_pkgbase.insert(
        "pacsea-bin".to_string(),
        crate::state::app_state::AurVoteStateUi::Voted,
    );
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(
        app.pending_aur_vote_intent,
        Some(("pacsea-bin".to_string(), crate::sources::VoteAction::Unvote))
    );
}

#[test]
/// What: Ensure selecting an AUR package queues live checks when feature is enabled.
///
/// Inputs:
/// - Insert mode with two AUR results and Down key navigation.
///
/// Output:
/// - `pending_aur_vote_state_request` is queued for the selected package.
///
/// Details:
/// - Default settings enable AUR voting, so live vote-state checks should activate.
fn search_selection_vote_state_check_queues_when_feature_enabled() {
    let mut app = new_app();
    app.results = vec![
        crate::state::PackageItem {
            name: "pkg-a".to_string(),
            version: "1.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
        crate::state::PackageItem {
            name: "pkg-b".to_string(),
            version: "1.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
    ];
    app.selected = 0;
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Down, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(app.selected, 1);
    assert_eq!(
        app.pending_aur_vote_state_request,
        Some("pkg-b".to_string())
    );
    assert!(matches!(
        app.aur_vote_state_by_pkgbase.get("pkg-b"),
        Some(crate::state::app_state::AurVoteStateUi::Loading)
    ));
}

#[test]
/// What: Ensure selecting a non-AUR package does not queue vote-state checks.
///
/// Inputs:
/// - Insert mode with official package selection and Down key navigation.
///
/// Output:
/// - `pending_aur_vote_state_request` remains `None`.
///
/// Details:
/// - Guards live check behavior so only AUR sources trigger SSH requests.
fn search_selection_does_not_queue_vote_state_check_for_official_package() {
    let mut app = new_app();
    app.results = vec![
        crate::state::PackageItem {
            name: "pkg-a".to_string(),
            version: "1.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Official {
                repo: "core".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
        crate::state::PackageItem {
            name: "pkg-b".to_string(),
            version: "1.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Official {
                repo: "extra".to_string(),
                arch: "x86_64".to_string(),
            },
            popularity: None,
            out_of_date: None,
            orphaned: false,
        },
    ];
    app.selected = 0;
    let (qtx, _qrx) = mpsc::unbounded_channel::<QueryInput>();
    let (dtx, _drx) = mpsc::unbounded_channel::<PackageItem>();
    let (atx, _arx) = mpsc::unbounded_channel::<PackageItem>();
    let (ptx, _prx) = mpsc::unbounded_channel::<PackageItem>();
    let (comments_tx, _comments_rx) = mpsc::unbounded_channel::<String>();

    let _ = handle_search_key(
        KeyEvent::new(KeyCode::Down, KeyModifiers::empty()),
        &mut app,
        &qtx,
        &dtx,
        &atx,
        &ptx,
        &comments_tx,
    );

    assert_eq!(app.selected, 1);
    assert!(app.pending_aur_vote_state_request.is_none());
}