fleetcom 0.9.0

A fleet-view supervisor for arbitrary shell commands.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
use super::*;
use crate::{
    supervisor::Supervisor,
    testutil::{temp, wait_until},
    transport::LocalTransport,
    ui::scroll_window,
};

impl App {
    /// A synchronous App: the supervisor ticks inline on `poll`, so `send`
    /// then `pump` is deterministic with no core-thread timing to race.
    /// Uses this process's launch context.
    fn new_local(rows: u16, cols: u16) -> App {
        App::assemble(rows, cols, |pr, c, _wait_tx| {
            let mut sup = Supervisor::new(pr, c, 2000);
            sup.set_launch_context(crate::protocol::LaunchContext::here());
            Box::new(LocalTransport::new(sup))
        })
    }

    /// `new_local` with an explicit launch context, for tests that must pin
    /// the core's session root instead of inheriting this process's env.
    fn new_local_with_ctx(rows: u16, cols: u16, ctx: crate::protocol::LaunchContext) -> App {
        App::assemble(rows, cols, move |pr, c, _wait_tx| {
            let mut sup = Supervisor::new(pr, c, 2000);
            sup.set_launch_context(ctx);
            Box::new(LocalTransport::new(sup))
        })
    }

    /// Drive one core sync so `views` reflects the latest spawns and reaps:
    /// the test-side equivalent of one run-loop tick.
    fn pump(&mut self) {
        self.sync();
    }

    fn spawn_in(&mut self, cmd: &str, cwd: PathBuf) {
        self.spawn_checked(cmd, cwd, None);
    }

    fn spawn_grouped(&mut self, cmd: &str, cwd: PathBuf, group: &str) {
        self.spawn_checked(cmd, cwd, Some(group));
    }

    /// Send a spawn and retry transient failures. Failed spawns leave
    /// `next_id` unchanged, so retries preserve task IDs.
    fn spawn_checked(&mut self, cmd: &str, cwd: PathBuf, group: Option<&str>) {
        self.pump();
        let want = self.views.len() + 1;
        for attempt in 0u64..5 {
            if attempt > 0 {
                std::thread::sleep(std::time::Duration::from_millis(25 << attempt));
            }
            self.transport.send(Command::Spawn {
                command: cmd.to_string(),
                cwd: cwd.clone(),
                group: group.map(str::to_string),
            });
            self.pump();
            if self.views.len() == want {
                // Drop the refusal notice a failed attempt left behind so
                // status assertions see only their own test's traffic.
                if attempt > 0 {
                    self.status = None;
                }
                return;
            }
        }
        panic!("spawn never landed: {:?}", self.status);
    }

    /// Return section labels with task ids instead of `views` indices.
    fn section_ids(&self) -> Vec<(String, Vec<u64>)> {
        self.sections()
            .into_iter()
            .map(|(l, idxs)| (l, idxs.into_iter().map(|i| self.views[i].id).collect()))
            .collect()
    }
}

/// Selection is bound to a task id, so a reorder (here: tagging a task into
/// the "In use" bucket) must not move the highlight to a different task.
#[test]
fn selection_follows_task_across_reorder() {
    let mut app = App::new_local(30, 100);
    let dir = app.invocation_dir.clone();
    app.spawn_in("sleep 5", dir.clone()); // id 1
    app.spawn_in("sleep 5", dir); // id 2
    app.pump();
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    // Tag id 2 -> it sorts into the "In use" bucket, ahead of id 1.
    app.transport.send(Command::Tag { id: 2, on: true });
    app.pump();

    let order = app.display_order();
    assert_eq!(app.views[order[0]].id, 2, "tagged task should sort first");

    // Still on id 1, even though it is now the second row.
    assert_eq!(app.selected_id, Some(1));
    assert_eq!(app.views[app.selected_task().unwrap()].id, 1);
}

/// Dir mode makes one section per distinct cwd (invocation dir first); state
/// mode collapses them back into the state buckets.
#[test]
fn dir_mode_groups_by_cwd() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv); // id 1, invocation dir
    app.spawn_in("sleep 5", PathBuf::from("/tmp")); // id 2, /tmp
    app.pump();

    app.group_mode = GroupMode::State;
    let s = app.sections();
    assert_eq!(s.len(), 1);
    assert_eq!(s[0].0, "Running");
    assert_eq!(s[0].1.len(), 2);

    app.group_mode = GroupMode::Dir;
    let s = app.sections();
    assert_eq!(s.len(), 2, "one section per distinct cwd");
    assert_eq!(s[0].0, app.invocation_label, "invocation dir sorts first");
    assert_eq!(s[1].0, "/tmp");
}

/// `s` cycles through all grouping modes.
#[test]
fn group_mode_cycles_state_dir_custom() {
    assert_eq!(GroupMode::State.next(), GroupMode::Dir);
    assert_eq!(GroupMode::Dir.next(), GroupMode::Custom);
    assert_eq!(GroupMode::Custom.next(), GroupMode::State);

    let mut app = App::new_local(30, 100);
    assert_eq!(app.group_mode, GroupMode::State);
    for expect in [GroupMode::Dir, GroupMode::Custom, GroupMode::State] {
        app.on_key_dashboard(KeyEvent::new(KeyCode::Char('s'), KeyModifiers::NONE));
        assert_eq!(app.group_mode, expect);
    }
}

/// Custom mode sorts named sections alphabetically and Unassigned last.
#[test]
fn custom_mode_groups_by_name_with_unassigned_last() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "beta"); // id 1
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 2
    app.spawn_in("sleep 5", inv); // id 3, no group
    app.pump();

    app.group_mode = GroupMode::Custom;
    assert_eq!(
        app.section_ids(),
        vec![
            ("alpha".to_string(), vec![2]),
            ("beta".to_string(), vec![1]),
            ("Unassigned".to_string(), vec![3]),
        ]
    );
}

/// Custom mode does not emit empty group sections.
#[test]
fn custom_mode_unassigned_tracks_membership() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.pump();
    app.group_mode = GroupMode::Custom;
    assert_eq!(app.section_ids(), vec![("alpha".to_string(), vec![1])]);

    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv); // id 1, no group
    app.pump();
    app.group_mode = GroupMode::Custom;
    assert_eq!(app.section_ids(), vec![("Unassigned".to_string(), vec![1])]);
}

/// In Custom mode, tagged tasks sort first within their existing group.
#[test]
fn custom_mode_tag_floats_within_group() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 2
    app.pump();
    app.group_mode = GroupMode::Custom;
    assert_eq!(app.section_ids(), vec![("alpha".to_string(), vec![1, 2])]);

    app.transport.send(Command::Tag { id: 2, on: true });
    app.pump();
    assert_eq!(
        app.section_ids(),
        vec![("alpha".to_string(), vec![2, 1])],
        "tag floats id 2 to the top of alpha, not into an In use section"
    );
}

/// Group reassignment can reorder sections without changing the selected id.
#[test]
fn custom_mode_selection_survives_group_move() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_grouped("sleep 5", inv, "beta"); // id 2
    app.pump();
    app.group_mode = GroupMode::Custom;
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    // Move id 1 from the first section to the last.
    app.transport.send(Command::SetGroup {
        id: 1,
        group: Some("zeta".to_string()),
    });
    app.pump();
    assert_eq!(
        app.section_ids(),
        vec![("beta".to_string(), vec![2]), ("zeta".to_string(), vec![1]),]
    );

    // Selection remains on id 1 in its new section.
    assert_eq!(app.selected_id, Some(1));
    assert_eq!(app.views[app.selected_task().unwrap()].id, 1);
}

/// Within a group, tasks cluster by directory and then by spawn order.
#[test]
fn custom_mode_clusters_by_dir_within_group() {
    let mut app = App::new_local(30, 100);
    let base = temp("app_cg");
    let (dir_a, dir_b) = (base.join("a"), base.join("b"));
    std::fs::create_dir_all(&dir_a).unwrap();
    std::fs::create_dir_all(&dir_b).unwrap();

    app.spawn_grouped("sleep 5", dir_b.clone(), "alpha"); // id 1, dir b
    app.spawn_grouped("sleep 5", dir_a.clone(), "alpha"); // id 2, dir a
    app.spawn_grouped("sleep 5", dir_a, "alpha"); // id 3, dir a
    app.pump();

    app.group_mode = GroupMode::Custom;
    assert_eq!(
        app.section_ids(),
        vec![("alpha".to_string(), vec![2, 3, 1])],
        "dir a's tasks cluster (in id order) ahead of dir b's"
    );
    let _ = std::fs::remove_dir_all(&base);
}

/// A manual tag must pull a task out of Completed into In use, even after it
/// has exited.
#[test]
fn tagging_a_finished_task_moves_it_to_in_use() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("true", inv); // exits ~immediately
    wait_until(Duration::from_secs(5), || {
        app.pump();
        app.views
            .first()
            .map(|v| matches!(v.lifecycle, Lifecycle::Ok | Lifecycle::Failed))
            .unwrap_or(false)
    });
    assert!(matches!(
        app.views[0].lifecycle,
        Lifecycle::Ok | Lifecycle::Failed
    ));
    assert_eq!(app.sections()[0].0, "Completed");

    let id = app.views[0].id;
    app.transport.send(Command::Tag { id, on: true });
    app.pump();
    assert_eq!(app.sections()[0].0, "In use");
}

/// A parked live task gets its own "Idle" section between "Running" and
/// "Completed". The test updates the local snapshot after the last pump so a
/// fresh core snapshot cannot overwrite it; this avoids waiting for the 10 s
/// quiet window.
#[test]
fn parked_task_lands_in_idle_between_running_and_completed() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv.clone()); // id 1: running
    app.spawn_in("sleep 5", inv.clone()); // id 2: parked below
    app.spawn_in("sleep 5", inv.clone()); // id 3: tagged below
    app.spawn_in("true", inv); // id 4: exits ~immediately
    wait_until(Duration::from_secs(5), || {
        app.pump();
        app.views
            .iter()
            .any(|v| v.id == 4 && matches!(v.lifecycle, Lifecycle::Ok | Lifecycle::Failed))
    });
    app.transport.send(Command::Tag { id: 3, on: true });
    app.pump();

    let i = app.views.iter().position(|v| v.id == 2).unwrap();
    app.views[i].parked = true;

    assert_eq!(
        app.section_ids(),
        vec![
            ("In use".to_string(), vec![3]),
            ("Running".to_string(), vec![1]),
            ("Idle".to_string(), vec![2]),
            ("Completed".to_string(), vec![4]),
        ]
    );
}

/// Tagged beats parked: a tagged task stays in "In use" even while parked.
#[test]
fn tagged_parked_task_stays_in_use() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv.clone()); // id 1: tagged + parked
    app.spawn_in("sleep 5", inv); // id 2: running
    app.pump();
    app.transport.send(Command::Tag { id: 1, on: true });
    app.pump();

    let i = app.views.iter().position(|v| v.id == 1).unwrap();
    app.views[i].parked = true;

    assert_eq!(
        app.section_ids(),
        vec![
            ("In use".to_string(), vec![1]),
            ("Running".to_string(), vec![2]),
        ]
    );
}

/// One window drives both signals, so a live core ships `Lifecycle::Idle`
/// and `parked` together: an idle-glyph task lands in the "Idle" section
/// under state grouping. Glyph and placement agree.
#[test]
fn idle_glyph_task_lands_in_idle_section() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv); // id 1
    app.pump();

    let i = app.views.iter().position(|v| v.id == 1).unwrap();
    app.views[i].lifecycle = Lifecycle::Idle;
    app.views[i].parked = true;

    assert_eq!(app.section_ids(), vec![("Idle".to_string(), vec![1])]);
}

/// Selection is bound to a task id, so a `parked` flip (re-bucketing the
/// row from "Running" into "Idle") must not move the highlight to a
/// different task.
#[test]
fn selection_follows_task_across_parked_rebucket() {
    let mut app = App::new_local(30, 100);
    let dir = app.invocation_dir.clone();
    app.spawn_in("sleep 5", dir.clone()); // id 1
    app.spawn_in("sleep 5", dir); // id 2
    app.pump();
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    // Park id 1 -> it sinks into "Idle", below id 2's "Running".
    let i = app.views.iter().position(|v| v.id == 1).unwrap();
    app.views[i].parked = true;

    let order = app.display_order();
    assert_eq!(app.views[order[0]].id, 2, "running task should sort first");

    // Still on id 1, even though it is now the second row.
    assert_eq!(app.selected_id, Some(1));
    assert_eq!(app.views[app.selected_task().unwrap()].id, 1);
}

/// `bucket` doubles as the within-group tiebreak, so a parked task sinks
/// below a running one inside a Custom group too, not only in State mode.
#[test]
fn custom_mode_parked_sinks_within_group() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 2
    app.pump();
    app.group_mode = GroupMode::Custom;
    assert_eq!(app.section_ids(), vec![("alpha".to_string(), vec![1, 2])]);

    let i = app.views.iter().position(|v| v.id == 1).unwrap();
    app.views[i].parked = true;
    assert_eq!(
        app.section_ids(),
        vec![("alpha".to_string(), vec![2, 1])],
        "parked id 1 sinks below running id 2 within alpha"
    );
}

/// `r` sends `Restart` only for a finished selection. On a running task
/// the key is a client-side no-op: nothing crosses the transport, so no
/// supervisor complaint lands in the status line. On a finished one the
/// same row (same id) comes back to life and the command re-executes.
#[test]
fn rerun_key_is_gated_to_finished_tasks() {
    let mut app = App::new_local(30, 100);
    let dir = temp("app_rerun");
    let marker = dir.join("marker");
    app.spawn_in("sleep 30", dir.clone()); // id 1: stays running
    app.spawn_in(&format!("echo run >> {}", marker.display()), dir.clone()); // id 2
    wait_until(Duration::from_secs(5), || {
        app.pump();
        app.views
            .iter()
            .any(|v| v.id == 2 && matches!(v.lifecycle, Lifecycle::Ok))
    });

    // Running selection: `r` must send nothing (and thus kill nothing).
    app.selected_id = Some(1);
    app.on_key_dashboard(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::NONE));
    app.pump();
    assert!(app.status.is_none(), "no Restart should have been sent");
    assert!(
        app.views
            .iter()
            .any(|v| v.id == 1 && matches!(v.lifecycle, Lifecycle::Active | Lifecycle::Idle)),
        "the running task must be untouched"
    );

    // Finished selection: `r` reruns it under the same id.
    app.selected_id = Some(2);
    app.on_key_dashboard(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::NONE));
    wait_until(Duration::from_secs(5), || {
        app.pump();
        std::fs::read_to_string(&marker)
            .map(|s| s.lines().count() == 2)
            .unwrap_or(false)
    });
    assert_eq!(
        std::fs::read_to_string(&marker).unwrap().lines().count(),
        2,
        "rerun must re-execute the command"
    );
    assert!(
        app.views.iter().any(|v| v.id == 2),
        "rerun must keep the id"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// Scratch config dir with the given pre-written (empty) session recipes.
fn session_scratch(tag: &str, names: &[&str]) -> PathBuf {
    let dir = temp(&format!("app_{tag}"));
    std::fs::create_dir_all(dir.join("sessions")).unwrap();
    for n in names {
        std::fs::write(dir.join("sessions").join(format!("{n}.json")), "{}").unwrap();
    }
    dir
}

/// An App whose core's session root is pinned to `dir` via the launch
/// context, so these tests never read this process's real config dir.
fn app_with_config_dir(dir: &Path) -> App {
    App::new_local_with_ctx(
        30,
        100,
        crate::protocol::LaunchContext {
            env: vec![(
                "FLEETCOM_CONFIG_DIR".into(),
                dir.to_path_buf().into_os_string(),
            )],
            cwd: dir.to_path_buf(),
        },
    )
}

/// `o` never touches the local filesystem: it sends `ListSessions`, opens
/// the picker empty, and the core's `Sessions` reply fills it.
#[test]
fn o_key_round_trips_the_session_list_through_the_core() {
    let dir = session_scratch("sess_list", &["b", "a"]);
    let mut app = app_with_config_dir(&dir);
    app.session_sel = 3; // stale from a previous picker visit

    app.on_key_dashboard(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::NONE));
    assert!(matches!(app.mode, Mode::LoadSession));
    assert!(
        app.session_names.is_empty(),
        "the picker opens empty until the reply lands"
    );
    assert_eq!(
        app.session_sel, 0,
        "opening the picker resets the selection"
    );

    app.pump();
    assert_eq!(
        app.session_names,
        vec!["a".to_string(), "b".to_string()],
        "the Sessions reply populates the picker, sorted"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// A shorter list arriving while the picker is open clamps the selection so
/// Enter cannot index past the new end.
#[test]
fn session_selection_clamps_when_a_shorter_list_arrives() {
    let dir = session_scratch("sess_clamp", &["a", "b", "c"]);
    let mut app = app_with_config_dir(&dir);
    app.on_key_dashboard(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::NONE));
    app.pump();
    assert_eq!(app.session_names.len(), 3);
    app.session_sel = 2;

    // Two recipes vanish; a refresh lands while the picker is still open.
    std::fs::remove_file(dir.join("sessions").join("b.json")).unwrap();
    std::fs::remove_file(dir.join("sessions").join("c.json")).unwrap();
    app.transport.send(Command::ListSessions);
    app.pump();
    assert_eq!(app.session_names, vec!["a".to_string()]);
    assert_eq!(app.session_sel, 0, "selection must clamp to the new length");

    // The empty list parks the selection at 0 too.
    std::fs::remove_file(dir.join("sessions").join("a.json")).unwrap();
    app.transport.send(Command::ListSessions);
    app.pump();
    assert!(app.session_names.is_empty());
    assert_eq!(app.session_sel, 0);
    let _ = std::fs::remove_dir_all(&dir);
}

/// The `@` recent list is the distinct task cwds, newest first.
#[test]
fn recent_dirs_are_distinct_and_newest_first() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", PathBuf::from("/tmp")); // id 1  /tmp
    app.spawn_in("sleep 5", inv.clone()); // id 2  invocation
    app.spawn_in("sleep 5", PathBuf::from("/tmp")); // id 3  /tmp (dup)
    app.pump();

    let dirs = app.in_use_dirs();
    assert_eq!(dirs.len(), 2, "duplicate dirs collapse");
    assert_eq!(dirs[0], PathBuf::from("/tmp"), "newest first");
    assert_eq!(dirs[1], inv);
}

#[test]
fn picker_puts_current_dir_first_and_selected() {
    let mut app = App::new_local(30, 100);
    app.dir_input.clear();
    app.refresh_dir_candidates();
    assert_eq!(app.dir_sel, 0, "current dir selected by default");
    assert_eq!(app.dir_candidates[0].kind, DirKind::Use);
    assert_eq!(app.dir_candidates[0].path, app.invocation_dir);
}

/// Focus is by id, so it points at the same task even after the list shifts
/// (a lower-id task is removed) and reports gone once it's removed.
#[test]
fn focus_by_id_survives_index_shift() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv.clone()); // id 1
    app.spawn_in("sleep 5", inv); // id 2
    app.pump();
    app.focused_id = Some(2);
    assert_eq!(app.views[app.focused_task().unwrap()].id, 2);

    app.transport.send(Command::Remove { id: 1 }); // id 2 slides to index 0
    app.pump();
    assert_eq!(app.views[app.focused_task().unwrap()].id, 2);

    app.transport.send(Command::Remove { id: 2 });
    app.pump();
    assert!(app.focused_task().is_none());
}

/// The flat row list interleaves each section header with its tasks, in
/// section order: what the dashboard's scroll window slides over.
#[test]
fn rows_interleave_headers_and_tasks() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("a", inv.clone()); // id 1, invocation dir
    app.spawn_in("b", PathBuf::from("/tmp")); // id 2, /tmp
    app.pump();

    app.group_mode = GroupMode::Dir;
    let rows = app.list_rows();
    assert_eq!(rows.len(), 4, "two sections, one task each");
    assert_eq!(rows[0], Row::Section(app.invocation_label.clone()));
    assert!(matches!(rows[1], Row::Task(i) if app.views[i].id == 1));
    assert_eq!(rows[2], Row::Section("/tmp".to_string()));
    assert!(matches!(rows[3], Row::Task(i) if app.views[i].id == 2));
}

/// A fleet taller than the list region must keep the selected row inside
/// the scroll window at every step: the dashboard equivalent of the picker
/// guarantee, over the composed header+task row list.
#[test]
fn dashboard_selection_stays_in_scroll_window() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    for _ in 0..8 {
        app.spawn_in("sleep 5", inv.clone());
    }
    app.pump();
    app.resolve_selection();

    // A 4-row window over 9 rows (1 header + 8 tasks): walking the whole
    // list down and back up must never let the selection leave the window.
    let height = 4;
    for step in 0..10 {
        app.select_down();
        let rows = app.list_rows();
        let sel = app.selected_row(&rows).expect("selection always resolves");
        let (start, count) = scroll_window(sel, rows.len(), height);
        assert!(
            sel >= start && sel < start + count,
            "step {step}: row {sel} outside window ({start}, {count})"
        );
    }
    for step in 0..10 {
        app.select_up();
        let rows = app.list_rows();
        let sel = app.selected_row(&rows).expect("selection always resolves");
        let (start, count) = scroll_window(sel, rows.len(), height);
        assert!(
            sel >= start && sel < start + count,
            "step {step}: row {sel} outside window ({start}, {count})"
        );
    }
}

/// Selection wraps at the list edges: up from the first task lands on the
/// last and down from the last lands on the first, across the section
/// boundary. `display_order` is flat, so headers never trap the cursor.
#[test]
fn selection_wraps_at_list_edges() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv.clone()); // id 1
    app.spawn_in("sleep 5", inv.clone()); // id 2
    app.spawn_in("sleep 5", inv); // id 3
    app.pump();

    // Tag id 2 -> it sorts into a leading "In use" section, so the wrap
    // below crosses a section boundary.
    app.transport.send(Command::Tag { id: 2, on: true });
    app.pump();
    app.resolve_selection();
    assert_eq!(app.section_ids().len(), 2, "tag splits the list in two");

    let order = app.display_order();
    let first = app.views[order[0]].id;
    let last = app.views[*order.last().unwrap()].id;
    assert_eq!(first, 2, "tagged task sorts first");
    assert_eq!(app.selected_id, Some(first));

    app.select_up();
    assert_eq!(
        app.selected_id,
        Some(last),
        "up from the first wraps to the last"
    );
    app.select_down();
    assert_eq!(
        app.selected_id,
        Some(first),
        "down from the last wraps to the first"
    );
}

/// With a single task, wrap degrades to a no-op in both directions.
#[test]
fn selection_wrap_is_noop_with_one_task() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.pump();
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    app.select_up();
    assert_eq!(app.selected_id, Some(1));
    app.select_down();
    assert_eq!(app.selected_id, Some(1));
}

/// Build two two-task sections: In use [3, 4] and Running [1, 2].
fn app_with_two_sections() -> App {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    for _ in 0..4 {
        app.spawn_in("sleep 5", inv.clone());
    }
    app.pump();
    app.transport.send(Command::Tag { id: 3, on: true });
    app.transport.send(Command::Tag { id: 4, on: true });
    app.pump();
    assert_eq!(
        app.section_ids(),
        vec![
            ("In use".to_string(), vec![3, 4]),
            ("Running".to_string(), vec![1, 2]),
        ],
        "tags split the list into two two-task sections"
    );
    app
}

/// Next-section navigation selects its first task and wraps forward.
#[test]
fn tab_jumps_to_next_section_first_task() {
    let mut app = app_with_two_sections();

    // From In use, select Running's first task.
    app.selected_id = Some(4);
    app.select_next_section();
    assert_eq!(
        app.selected_id,
        Some(1),
        "next section's first, not same offset"
    );

    // From the last section, wrap to the first task in In use.
    app.selected_id = Some(2);
    app.select_next_section();
    assert_eq!(app.selected_id, Some(3), "forward from last section wraps");
}

/// Previous-section navigation selects its first task and wraps backward.
#[test]
fn backtab_jumps_to_previous_section_first_task() {
    let mut app = app_with_two_sections();

    // From Running, select In use's first task.
    app.selected_id = Some(2);
    app.select_prev_section();
    assert_eq!(
        app.selected_id,
        Some(3),
        "previous section's first, not current's"
    );

    // From the first section, wrap to Running's first task.
    app.selected_id = Some(3);
    app.select_prev_section();
    assert_eq!(
        app.selected_id,
        Some(1),
        "backward from first section wraps"
    );
}

/// Section navigation keeps a single task selected.
#[test]
fn section_nav_is_noop_with_one_task() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.pump();
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    app.select_next_section();
    assert_eq!(app.selected_id, Some(1));
    app.select_prev_section();
    assert_eq!(app.selected_id, Some(1));
}

/// Without a selection, navigation chooses the boundary section.
#[test]
fn section_nav_defaults_without_selection() {
    let mut app = app_with_two_sections();

    app.selected_id = None;
    app.select_next_section();
    assert_eq!(
        app.selected_id,
        Some(3),
        "no selection: first section's first"
    );

    app.selected_id = None;
    app.select_prev_section();
    assert_eq!(
        app.selected_id,
        Some(1),
        "no selection: last section's first"
    );

    let mut empty = App::new_local(30, 100);
    empty.select_next_section();
    assert_eq!(empty.selected_id, None);
    empty.select_prev_section();
    assert_eq!(empty.selected_id, None);
}

/// Supported crossterm keys and modifiers map to their wire representation.
#[test]
fn key_event_maps_to_semantic_key() {
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE)),
        Some((Key::Char('a'), Mods::default()))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::F(5), KeyModifiers::NONE)),
        Some((Key::F(5), Mods::default()))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::SHIFT)),
        Some((
            Key::Char('a'),
            Mods {
                shift: true,
                ..Mods::default()
            }
        ))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL)),
        Some((
            Key::Char('a'),
            Mods {
                ctrl: true,
                ..Mods::default()
            }
        ))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::ALT)),
        Some((
            Key::Char('a'),
            Mods {
                alt: true,
                ..Mods::default()
            }
        ))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Left, KeyModifiers::ALT)),
        Some((
            Key::Left,
            Mods {
                alt: true,
                ..Mods::default()
            }
        ))
    );
}

/// Unsupported modifier bits are ignored; unsupported key codes are dropped.
#[test]
fn unencodable_modifiers_and_keys_are_dropped() {
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::SUPER)),
        Some((Key::Char('a'), Mods::default()))
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::CapsLock, KeyModifiers::NONE)),
        None
    );
    assert_eq!(
        key_event_to_key(KeyEvent::new(KeyCode::Null, KeyModifiers::NONE)),
        None
    );
}

/// An oversized attached paste is refused before it closes the connection.
#[test]
fn oversized_paste_is_refused_with_a_notice() {
    let mut app = App::new_local(30, 100);
    app.mode = Mode::Attached;
    app.focused_id = Some(1);
    app.on_paste(&"x".repeat(MAX_PASTE + 1));
    let status = app.status.clone().unwrap_or_default();
    assert!(status.contains("paste dropped"), "status was {status:?}");
    // The boundary value is accepted.
    app.on_paste(&"x".repeat(MAX_PASTE));
    assert!(app.status.is_none(), "boundary paste must not be refused");
}

/// A paste into a text-entry mode lands as one string with control
/// characters stripped: a multi-line clipboard must not fake the Enter
/// press that would launch a half-pasted command.
#[test]
fn paste_into_text_entry_strips_controls() {
    let mut app = App::new_local(30, 100);
    app.mode = Mode::Spawn;
    app.on_paste("cargo\ttest\r\n --all");
    assert_eq!(app.input.as_str(), "cargotest --all");
    assert!(app.mode == Mode::Spawn, "paste must not submit");
}

/// Select mouse capture by screen type.
#[test]
fn input_modes_match_screen_type() {
    let screen = |wants_mouse, alt_screen, alt_scroll| ScreenView {
        id: 1,
        lines: Vec::new(),
        formatted: Vec::new(),
        cursor: (0, 0),
        hide_cursor: false,
        wants_mouse,
        alt_screen,
        alt_scroll,
        scrollback: 0,
    };
    // No attached screen: keep native selection available.
    assert!(!desired_mouse_capture(None, false));
    // Mouse-aware child: capture.
    assert!(desired_mouse_capture(
        Some(&screen(true, true, true)),
        false
    ));
    assert!(desired_mouse_capture(
        Some(&screen(true, false, false)),
        false
    ));
    // Full-screen child with alternate scroll enabled.
    assert!(!desired_mouse_capture(
        Some(&screen(false, true, true)),
        false
    ));
    // Full-screen child with alternate scroll disabled.
    assert!(desired_mouse_capture(
        Some(&screen(false, true, false)),
        false
    ));
    // Inline child: capture wheel-up to enter scrollback.
    assert!(desired_mouse_capture(
        Some(&screen(false, false, false)),
        false
    ));
    // The scroll view overrides everything: the wheel must scroll it.
    assert!(desired_mouse_capture(
        Some(&screen(false, false, false)),
        true
    ));
    assert!(desired_mouse_capture(None, true));
}

/// Wheel-up enters scrollback for inline children, but forwards for
/// mouse-aware children.
#[test]
fn wheel_up_enters_scroll_view_for_inline_children() {
    let mut app = App::new_local(30, 100);
    let dir = app.invocation_dir.clone();
    app.spawn_in("sleep 5", dir);
    app.pump();
    app.resolve_selection();
    app.attach();
    let id = app.focused_id.expect("attached");
    let screen = |wants_mouse| ScreenView {
        id,
        lines: Vec::new(),
        formatted: Vec::new(),
        cursor: (0, 0),
        hide_cursor: false,
        wants_mouse,
        alt_screen: false,
        alt_scroll: false,
        scrollback: 0,
    };
    let wheel_up = MouseEvent {
        kind: MouseEventKind::ScrollUp,
        column: 0,
        row: 0,
        modifiers: KeyModifiers::NONE,
    };

    // Inline child: wheel-up enters scrollback.
    app.focused_screen = Some(screen(false));
    app.on_mouse(wheel_up);
    assert!(app.view_scroll, "wheel-up must open the scroll view");

    // Mouse-aware child: wheel-up forwards instead.
    app.view_scroll = false;
    app.focused_screen = Some(screen(true));
    app.on_mouse(wheel_up);
    assert!(!app.view_scroll, "mouse-aware children keep their wheel");
}

/// Attached wheel input follows the child's DECSET 1007 state.
#[test]
fn attached_wheel_honors_the_childs_1007_veto() {
    let dir = temp("app_1007");

    let wheel_up = MouseEvent {
        kind: MouseEventKind::ScrollUp,
        column: 0,
        row: 0,
        modifiers: KeyModifiers::NONE,
    };
    // Send one wheel notch and return the first `take` bytes read by the child.
    let run = |veto: bool, take: usize, out: PathBuf| -> Vec<u8> {
        let mut app = App::new_local(30, 100);
        let cwd = app.invocation_dir.clone();
        let modes = if veto {
            "\\033[?1049h\\033[?1007l"
        } else {
            "\\033[?1049h"
        };
        // Noncanonical input lets `head` read arrow sequences without a newline.
        let cmd = format!(
            "stty -icanon -echo min 1 time 0; printf '{modes}'; head -c {take} > {}",
            out.display()
        );
        app.spawn_in(&cmd, cwd);
        app.pump();
        app.resolve_selection();
        app.attach();
        let id = app.focused_id.expect("attached");
        app.set_watch(Some(id));
        // Wait for the child's terminal modes to reach the client.
        assert!(
            wait_until(Duration::from_secs(5), || {
                app.pump();
                matches!(
                    app.screen_for(id),
                    Some(s) if s.alt_screen && s.alt_scroll != veto
                )
            }),
            "gate state (veto: {veto}) never reached the client"
        );
        // Disabled alternate scroll captures the wheel; enabled does not.
        assert_eq!(desired_mouse_capture(app.screen_for(id), false), veto);
        app.on_mouse(wheel_up);
        if veto {
            // The sentinel follows the wheel on the writer queue.
            app.transport.send(Command::Input {
                id,
                bytes: b"zzz".to_vec(),
            });
        }
        let mut got = Vec::new();
        wait_until(Duration::from_secs(5), || {
            got = std::fs::read(&out).unwrap_or_default();
            got.len() >= take
        });
        got
    };

    // Disabled alternate scroll suppresses the wheel bytes.
    assert_eq!(run(true, 3, dir.join("veto")), b"zzz".to_vec());
    // Enabled alternate scroll emits three arrows per notch.
    assert_eq!(
        run(false, 9, dir.join("dflt")),
        b"\x1b[A\x1b[A\x1b[A".to_vec()
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// Scrollback opens with modified PageUp and closes on Esc or typing.
#[test]
fn scroll_view_entry_and_exit() {
    let mut app = App::new_local(30, 100);
    let dir = app.invocation_dir.clone();
    app.spawn_in("sleep 5", dir);
    app.pump();
    app.resolve_selection();
    app.attach();
    assert!(app.mode == Mode::Attached);
    let mut out = io::stdout();

    let key = |code| KeyEvent::new(code, KeyModifiers::NONE);
    app.on_key_attached(
        &mut out,
        KeyEvent::new(KeyCode::PageUp, KeyModifiers::SHIFT),
    )
    .unwrap();
    assert!(app.view_scroll, "Shift+PageUp must enter the scroll view");
    app.on_key_attached(&mut out, key(KeyCode::Esc)).unwrap();
    assert!(!app.view_scroll, "Esc must return to live");

    app.on_key_attached(
        &mut out,
        KeyEvent::new(KeyCode::PageUp, KeyModifiers::CONTROL),
    )
    .unwrap();
    assert!(app.view_scroll, "Ctrl+PageUp is an entry fallback");
    app.on_key_attached(&mut out, key(KeyCode::Char('x')))
        .unwrap();
    assert!(!app.view_scroll, "typing must snap back to live");

    // Plain PageUp is forwarded to the child.
    app.on_key_attached(&mut out, key(KeyCode::PageUp)).unwrap();
    assert!(!app.view_scroll);
}

/// A wheel notch on the dashboard moves the selection like an arrow key.
#[test]
fn wheel_moves_dashboard_selection() {
    let mut app = App::new_local(30, 100);
    let dir = app.invocation_dir.clone();
    app.spawn_in("sleep 5", dir.clone()); // id 1
    app.spawn_in("sleep 5", dir); // id 2
    app.pump();
    app.resolve_selection();
    assert_eq!(app.selected_id, Some(1));

    let wheel = |kind| MouseEvent {
        kind,
        column: 0,
        row: 0,
        modifiers: KeyModifiers::NONE,
    };
    app.on_mouse(wheel(MouseEventKind::ScrollDown));
    assert_eq!(app.selected_id, Some(2));
    app.on_mouse(wheel(MouseEventKind::ScrollUp));
    assert_eq!(app.selected_id, Some(1));
}

// --- `g` group picker -------------------------------------------------

fn key(code: KeyCode) -> KeyEvent {
    KeyEvent::new(code, KeyModifiers::NONE)
}

fn ctrl(code: KeyCode) -> KeyEvent {
    KeyEvent::new(code, KeyModifiers::CONTROL)
}

/// `g` opens the picker only when a task is selected, pinning the target
/// to that task's id.
#[test]
fn group_picker_opens_on_g_only_with_a_selection() {
    let mut app = App::new_local(30, 100);
    app.on_key_dashboard(key(KeyCode::Char('g')));
    assert!(app.mode == Mode::Dashboard, "no selection: g must no-op");

    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));
    assert!(app.mode == Mode::PickGroup);
    assert_eq!(app.group_target, Some(1));
}

/// Picker candidates are distinct byte-sorted groups after Unassigned, with
/// the target's assignment marked "(current)".
#[test]
fn group_candidates_are_distinct_sorted_and_marked() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "beta"); // id 1
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 2
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 3, dup group
    app.spawn_in("sleep 5", inv); // id 4, no group
    app.pump();

    app.selected_id = Some(1); // group "beta"
    app.on_key_dashboard(key(KeyCode::Char('g')));
    let labels: Vec<&str> = app
        .group_candidates
        .iter()
        .map(|c| c.label.as_str())
        .collect();
    assert_eq!(labels, vec!["Unassigned", "alpha", "beta (current)"]);
    let groups: Vec<Option<&str>> = app
        .group_candidates
        .iter()
        .map(|c| c.group.as_deref())
        .collect();
    assert_eq!(groups, vec![None, Some("alpha"), Some("beta")]);
    assert_eq!(app.group_sel, 0, "nothing typed keeps the clear row");

    // An ungrouped target marks the Unassigned row instead.
    app.on_key_pickgroup(key(KeyCode::Esc));
    app.selected_id = Some(4);
    app.on_key_dashboard(key(KeyCode::Char('g')));
    assert_eq!(app.group_candidates[0].label, "Unassigned (current)");
}

/// Typing applies a case-insensitive prefix filter and selects the first
/// match; Backspace expands the candidate set again.
#[test]
fn group_filter_narrows_and_preselects_the_first_match() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_grouped("sleep 5", inv, "beta"); // id 2
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));
    assert_eq!(app.group_candidates.len(), 3);

    app.on_key_pickgroup(key(KeyCode::Char('B')));
    let labels: Vec<&str> = app
        .group_candidates
        .iter()
        .map(|c| c.label.as_str())
        .collect();
    assert_eq!(
        labels,
        vec!["Unassigned", "beta"],
        "case-insensitive prefix"
    );
    assert_eq!(app.group_sel, 1, "filtering preselects the first match");

    app.on_key_pickgroup(key(KeyCode::Char('z')));
    assert_eq!(app.group_candidates.len(), 1, "\"Bz\" matches nothing");
    assert_eq!(app.group_sel, 0);

    app.on_key_pickgroup(key(KeyCode::Backspace));
    assert_eq!(app.group_candidates.len(), 2, "backspace re-widens");
}

/// Enter assigns the highlighted candidate to the target task.
#[test]
fn group_enter_on_a_candidate_assigns_it() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_in("sleep 5", inv); // id 2, no group
    app.pump();

    app.selected_id = Some(2);
    app.on_key_dashboard(key(KeyCode::Char('g')));
    app.on_key_pickgroup(key(KeyCode::Char('a'))); // highlights "alpha"
    app.on_key_pickgroup(key(KeyCode::Enter));
    assert!(app.mode == Mode::Dashboard);
    app.pump();
    let v = app.views.iter().find(|v| v.id == 2).unwrap();
    assert_eq!(v.group.as_deref(), Some("alpha"));
}

/// Enter creates the typed group when no candidate matches.
#[test]
fn group_enter_on_novel_text_creates_the_group() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));
    for c in "gamma".chars() {
        app.on_key_pickgroup(key(KeyCode::Char(c)));
    }
    assert_eq!(app.group_candidates.len(), 1, "nothing matches");
    app.on_key_pickgroup(key(KeyCode::Enter));
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.group.as_deref(), Some("gamma"));
}

/// Empty input selects Unassigned, so Enter clears the target's group.
#[test]
fn group_enter_on_empty_input_clears_to_unassigned() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));
    assert_eq!(app.group_sel, 0, "empty input highlights the clear row");
    app.on_key_pickgroup(key(KeyCode::Enter));
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.group, None);
}

/// Esc closes the picker without changing the target task.
#[test]
fn group_esc_cancels_without_sending() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));
    for c in "gamma".chars() {
        app.on_key_pickgroup(key(KeyCode::Char(c)));
    }
    app.on_key_pickgroup(key(KeyCode::Esc));
    assert!(app.mode == Mode::Dashboard);
    assert!(app.group_input.is_empty() && app.group_candidates.is_empty());
    assert_eq!(app.group_target, None);
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.group.as_deref(), Some("alpha"), "Esc must send nothing");
}

// --- `R` rename prompt --------------------------------------------------

/// The rename prompt captures the selected task ID and current name.
#[test]
fn rename_prompt_opens_on_shift_r_only_with_a_selection() {
    let mut app = App::new_local(30, 100);
    app.on_key_dashboard(key(KeyCode::Char('R')));
    assert!(app.mode == Mode::Dashboard, "no selection: R must no-op");
    assert_eq!(app.rename_target, None);

    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    assert!(app.mode == Mode::Rename);
    assert_eq!(app.rename_target, Some(1));
    assert_eq!(app.input.as_str(), "", "an unnamed task prefills empty");

    // A named task prefills its name.
    app.on_key_rename(key(KeyCode::Esc));
    app.transport.send(Command::SetName {
        id: 1,
        name: Some("api".to_string()),
    });
    app.pump();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    assert_eq!(app.input.as_str(), "api");
}

/// Enter sends the trimmed name and returns to the dashboard.
#[test]
fn rename_enter_sends_the_typed_name() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    for c in "api server".chars() {
        app.on_key_rename(key(KeyCode::Char(c)));
    }
    app.on_key_rename(key(KeyCode::Enter));
    assert!(app.mode == Mode::Dashboard);
    assert!(app.input.is_empty() && app.rename_target.is_none());
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.name.as_deref(), Some("api server"));
}

/// Enter on an empty input clears the name.
#[test]
fn rename_enter_on_empty_input_clears_the_name() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.transport.send(Command::SetName {
        id: 1,
        name: Some("api".to_string()),
    });
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    assert_eq!(app.input.as_str(), "api");
    for _ in 0.."api".len() {
        app.on_key_rename(key(KeyCode::Backspace));
    }
    app.on_key_rename(key(KeyCode::Enter));
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.name, None);
}

/// Esc closes the prompt without changing the target task.
#[test]
fn rename_esc_cancels_without_sending() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.transport.send(Command::SetName {
        id: 1,
        name: Some("api".to_string()),
    });
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    for c in "junk".chars() {
        app.on_key_rename(key(KeyCode::Char(c)));
    }
    app.on_key_rename(key(KeyCode::Esc));
    assert!(app.mode == Mode::Dashboard);
    assert!(app.input.is_empty());
    assert_eq!(app.rename_target, None);
    app.pump();
    let v = app.views.iter().find(|v| v.id == 1).unwrap();
    assert_eq!(v.name.as_deref(), Some("api"), "Esc must send nothing");
}

// --- prompt caret editing ----------------------------------------------

/// Left/Right, Ctrl-A/Ctrl-E, and Home/End reposition the caret in the
/// rename prompt, and edits land at it.
#[test]
fn rename_caret_keys_edit_mid_name() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_in("sleep 5", inv);
    app.transport.send(Command::SetName {
        id: 1,
        name: Some("api".to_string()),
    });
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('R')));
    assert_eq!(app.input.as_str(), "api");

    // Move after "a" and insert within the name.
    app.on_key_rename(key(KeyCode::Left));
    app.on_key_rename(key(KeyCode::Left));
    app.on_key_rename(key(KeyCode::Char('x')));
    assert_eq!(app.input.as_str(), "axpi");

    // Ctrl-A jumps to the start; typing prepends.
    app.on_key_rename(ctrl(KeyCode::Char('a')));
    app.on_key_rename(key(KeyCode::Char('z')));
    assert_eq!(app.input.as_str(), "zaxpi");

    // Ctrl-E returns to the end; typing appends.
    app.on_key_rename(ctrl(KeyCode::Char('e')));
    app.on_key_rename(key(KeyCode::Char('!')));
    assert_eq!(app.input.as_str(), "zaxpi!");

    // Backspace removes only the char before the caret.
    app.on_key_rename(key(KeyCode::Left));
    app.on_key_rename(key(KeyCode::Backspace));
    assert_eq!(app.input.as_str(), "zaxp!");

    // Home/End alias the chords.
    app.on_key_rename(key(KeyCode::Home));
    app.on_key_rename(key(KeyCode::Char('0')));
    app.on_key_rename(key(KeyCode::End));
    app.on_key_rename(key(KeyCode::Char('9')));
    assert_eq!(app.input.as_str(), "0zaxp!9");
}

/// Unbound Ctrl chords do not insert their literal letters.
#[test]
fn ctrl_chords_never_insert_their_letter() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.resolve_selection();

    app.on_key_dashboard(key(KeyCode::Char('w')));
    app.on_key_savesession(ctrl(KeyCode::Char('k')));
    assert!(app.input.is_empty(), "Ctrl-K must not insert into a prompt");
    app.on_key_savesession(key(KeyCode::Esc));

    app.on_key_dashboard(key(KeyCode::Char('@')));
    app.on_key_pickdir(ctrl(KeyCode::Char('k')));
    assert!(app.dir_input.is_empty(), "Ctrl-K must not filter the dirs");
    app.on_key_pickdir(key(KeyCode::Esc));

    app.on_key_dashboard(key(KeyCode::Char('g')));
    let before = app.group_candidates.len();
    app.on_key_pickgroup(ctrl(KeyCode::Char('k')));
    assert!(
        app.group_input.is_empty(),
        "Ctrl-K must not filter the groups"
    );
    assert_eq!(app.group_candidates.len(), before);
}

/// In the `@` picker, Right descends only when the caret sits at the end
/// of the typed path; off the end it is caret motion.
#[test]
fn pickdir_right_descends_only_from_the_end() {
    let dir = temp("caret_pickdir");
    std::fs::create_dir_all(dir.join("alpha")).unwrap();
    let mut app = App::new_local(30, 100);
    app.invocation_dir = dir.clone();

    app.on_key_dashboard(key(KeyCode::Char('@')));
    for c in "al".chars() {
        app.on_key_pickdir(key(KeyCode::Char(c)));
    }
    assert_eq!(app.dir_sel, 1, "the fragment preselects alpha");

    // Off the end (one left), Right moves the caret without descending.
    app.on_key_pickdir(key(KeyCode::Left));
    app.on_key_pickdir(key(KeyCode::Right));
    assert_eq!(
        app.dir_input.as_str(),
        "al",
        "Right off-end must not descend"
    );

    // That Right returned the caret to the end, so the next one descends.
    app.on_key_pickdir(key(KeyCode::Right));
    assert!(
        app.dir_input.ends_with("alpha/"),
        "Right at end descends: {:?}",
        app.dir_input.as_str()
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// Typing after caret motion still refreshes the `@` candidates; the
/// motion itself does not.
#[test]
fn pickdir_refreshes_on_edits_not_caret_motion() {
    let dir = temp("caret_pickdir_refresh");
    std::fs::create_dir_all(dir.join("alpha")).unwrap();
    let mut app = App::new_local(30, 100);
    app.invocation_dir = dir.clone();

    app.on_key_dashboard(key(KeyCode::Char('@')));
    app.on_key_pickdir(key(KeyCode::Char('l')));
    assert_eq!(app.dir_candidates.len(), 1, "\"l\" matches nothing");

    app.on_key_pickdir(key(KeyCode::Home));
    assert_eq!(app.dir_candidates.len(), 1, "caret motion must not refresh");

    // "a" typed at the start makes the buffer "al": a match again.
    app.on_key_pickdir(key(KeyCode::Char('a')));
    assert_eq!(app.dir_input.as_str(), "al");
    assert!(
        app.dir_candidates.iter().any(|c| c.label == "alpha"),
        "an edit at the caret refreshes the candidates"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// Caret-positioned edits refresh the group filter like end-of-line ones.
#[test]
fn pickgroup_caret_edits_refresh_the_filter() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv.clone(), "alpha"); // id 1
    app.spawn_grouped("sleep 5", inv, "beta"); // id 2
    app.pump();
    app.resolve_selection();
    app.on_key_dashboard(key(KeyCode::Char('g')));

    app.on_key_pickgroup(key(KeyCode::Char('b')));
    assert_eq!(app.group_candidates.len(), 2, "\"b\" matches beta");

    app.on_key_pickgroup(key(KeyCode::Left));
    assert_eq!(
        app.group_candidates.len(),
        2,
        "caret motion must not refresh"
    );

    // "a" before the 'b' makes the filter "ab": nothing matches now.
    app.on_key_pickgroup(key(KeyCode::Char('a')));
    assert_eq!(app.group_input.as_str(), "ab");
    assert_eq!(app.group_candidates.len(), 1, "the caret edit re-filtered");
}

/// A paste inserts at the caret and leaves the caret after the pasted text.
#[test]
fn paste_lands_at_the_caret() {
    let mut app = App::new_local(30, 100);
    app.mode = Mode::Spawn;
    for c in "cargo t".chars() {
        app.on_key_spawn(key(KeyCode::Char(c)));
    }
    app.on_key_spawn(key(KeyCode::Left)); // caret before 't'
    app.on_paste("x\ny"); // control chars still stripped
    assert_eq!(app.input.as_str(), "cargo xyt");
    app.on_key_spawn(key(KeyCode::Char('z')));
    assert_eq!(
        app.input.as_str(),
        "cargo xyzt",
        "caret sits after the paste"
    );
}

/// Multibyte characters move, insert, and delete as whole units.
#[test]
fn multibyte_chars_edit_cleanly_in_a_prompt() {
    let mut app = App::new_local(30, 100);
    app.on_key_dashboard(key(KeyCode::Char('w')));
    app.on_key_savesession(key(KeyCode::Char('é')));
    app.on_key_savesession(key(KeyCode::Left));
    app.on_key_savesession(key(KeyCode::Char('')));
    assert_eq!(app.input.as_str(), "日é");
    app.on_key_savesession(key(KeyCode::Backspace));
    assert_eq!(app.input.as_str(), "é");
    app.on_key_savesession(key(KeyCode::Right));
    app.on_key_savesession(key(KeyCode::Backspace));
    assert!(app.input.is_empty());
}

// --- spawn group inheritance -------------------------------------------

/// In Custom mode, `n` assigns the selected task's group to the spawn.
#[test]
fn custom_mode_spawn_inherits_the_selected_group() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.group_mode = GroupMode::Custom;
    app.resolve_selection();

    app.on_key_dashboard(key(KeyCode::Char('n')));
    assert!(app.mode == Mode::Spawn);
    assert_eq!(app.spawn_group.as_deref(), Some("alpha"));

    for c in "sleep 5".chars() {
        app.on_key_spawn(key(KeyCode::Char(c)));
    }
    app.on_key_spawn(key(KeyCode::Enter));
    app.pump();
    let v = app.views.iter().find(|v| v.id == 2).unwrap();
    assert_eq!(
        v.group.as_deref(),
        Some("alpha"),
        "the spawn must carry the inherited group"
    );
}

/// In Custom mode, the `@` flow snapshots the group after directory selection.
#[test]
fn dir_picker_handoff_inherits_the_selected_group_in_custom_mode() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.group_mode = GroupMode::Custom;
    app.resolve_selection();

    app.on_key_dashboard(key(KeyCode::Char('@')));
    assert!(app.mode == Mode::PickDir);
    // Row 0 is the current dir (DirKind::Use): Enter hands off to Spawn.
    app.on_key_pickdir(key(KeyCode::Enter));
    assert!(app.mode == Mode::Spawn);
    assert_eq!(app.spawn_group.as_deref(), Some("alpha"));
}

/// State and Dir mode spawns are unassigned.
#[test]
fn state_and_dir_mode_spawns_stay_unassigned() {
    let mut app = App::new_local(30, 100);
    let inv = app.invocation_dir.clone();
    app.spawn_grouped("sleep 5", inv, "alpha"); // id 1
    app.pump();
    app.resolve_selection();

    for mode in [GroupMode::State, GroupMode::Dir] {
        app.group_mode = mode;
        app.spawn_group = Some("stale".to_string());
        app.on_key_dashboard(key(KeyCode::Char('n')));
        assert_eq!(app.spawn_group, None, "{mode:?} must not inherit");
        app.on_key_spawn(key(KeyCode::Esc));
    }

    app.on_key_dashboard(key(KeyCode::Char('n')));
    for c in "sleep 5".chars() {
        app.on_key_spawn(key(KeyCode::Char(c)));
    }
    app.on_key_spawn(key(KeyCode::Enter));
    app.pump();
    let v = app.views.iter().find(|v| v.id == 2).unwrap();
    assert_eq!(v.group, None);
}