penknife 0.2.2

Git-style remotes for your documents: sync a folder of markdown to GitHub Gists, track drift per file, and copy anything as rich text to paste elsewhere
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
//! Gist sync operations: push/pull/diff/remote-check/hydration/delete, the
//! URL clipboard helpers, and the application of their async results.
//!
//! The store/disk mutation rules for async results live in
//! [`crate::sync_apply`]; `handle_async_event` only renders outcomes
//! (status bar, mode transitions, tree refreshes).

use std::path::PathBuf;

use super::{App, ConfirmAction, Mode};
use crate::event::AsyncEvent;
use crate::store::{FileEntry, Store};
use crate::sync;
use crate::sync_apply::{self, PullApply};
use crate::ui::input::LineEditor;

impl App {
    pub fn handle_async_event(&mut self, event: AsyncEvent) {
        match event {
            AsyncEvent::PushDone {
                root,
                rel_path,
                result,
            } => {
                self.pending_pushes.remove(&rel_path);
                match result {
                    Ok(entry) => {
                        let url = entry.url.clone();
                        self.store.insert(&root, rel_path.clone(), entry);
                        if let Err(e) = self.store.save() {
                            self.status_message =
                                format!("Pushed, but saving local state failed: {e}");
                            return;
                        }
                        self.refresh_status_for(&rel_path);
                        self.rebuild_tree();
                        // Follow up on a queued copy-url request, if it's for
                        // this file. Skip the intermediate "Pushed" message when
                        // a copy is about to supersede it - otherwise a second,
                        // later PushDone's "Pushed" line would clobber "Copied".
                        if self.pending_copy.as_ref().is_some_and(|p| *p == rel_path) {
                            self.pending_copy = None;
                            self.copy_to_clipboard(&url);
                        } else {
                            self.status_message = format!("Pushed {rel_path}{url}");
                        }
                    }
                    Err(e) => {
                        self.pending_copy = None;
                        self.status_message = format!("Push failed: {e}{}", scope_hint(&e));
                    }
                }
            }
            AsyncEvent::PullDone {
                root,
                rel_path,
                expected_local_sha256,
                result,
            } => match result {
                Ok((content, entry)) => {
                    match sync_apply::apply_pull(
                        &mut self.store,
                        &root,
                        &rel_path,
                        &expected_local_sha256,
                        &content,
                        entry,
                    ) {
                        Ok(PullApply::DriftRefused) => {
                            self.status_message = format!(
                                "{rel_path} changed on disk during pull - not overwriting. Pull again to retry."
                            );
                        }
                        Ok(PullApply::Applied) => {
                            if let Err(e) = self.store.save() {
                                self.status_message =
                                    format!("Pulled, but saving local state failed: {e}");
                                return;
                            }
                            self.refresh_status_for(&rel_path);
                            self.update_preview();
                            self.rebuild_tree();
                            self.status_message = format!("Pulled {rel_path}");
                        }
                        Err(e) => {
                            self.status_message = format!("Write failed: {e}");
                        }
                    }
                }
                Err(e) => {
                    self.status_message = format!("Pull failed: {e}");
                }
            },
            AsyncEvent::PushBlocked {
                root,
                rel_path,
                remote_sha256,
                remote_updated_at,
            } => {
                self.pending_pushes.remove(&rel_path);
                // Record the observed divergence so the tree shows the real
                // state (RemoteNewer/Conflict) even if the user declines.
                if sync_apply::record_divergence(
                    &mut self.store,
                    &root,
                    &rel_path,
                    remote_sha256,
                    remote_updated_at,
                ) {
                    if let Err(e) = self.store.save() {
                        self.status_message = format!("Saving local state failed: {e}");
                    }
                    self.refresh_status_for(&rel_path);
                    self.rebuild_tree();
                    self.update_status();
                }
                self.mode = Mode::Confirm {
                    message: format!(
                        "Remote gist for {rel_path} changed since last sync. Force push (overwrites remote - use D to diff first)?"
                    ),
                    action: ConfirmAction::ForcePush {
                        rel_path: rel_path.clone(),
                    },
                };
                self.status_message = format!("Push blocked: remote changed for {rel_path}");
            }
            AsyncEvent::RemoteCheckDone {
                root,
                started,
                result,
            } => {
                self.remote_check_inflight = false;
                self.last_remote_poll = Some(std::time::Instant::now());
                match result {
                    Ok(outcome) => {
                        self.remote_poll_failures = 0;
                        let applied = sync_apply::apply_remote_updates(
                            &mut self.store,
                            &root,
                            started,
                            outcome.updated,
                        );
                        for rel in &applied {
                            self.refresh_status_for(rel);
                        }
                        if !applied.is_empty()
                            && let Err(e) = self.store.save()
                        {
                            self.status_message =
                                format!("Remote check: saving local state failed: {e}");
                            return;
                        }
                        self.rebuild_tree();
                        self.update_status();
                        // Quiet when nothing moved; the icons and counts are
                        // the steady-state signal.
                        if outcome.divergent > 0 || !outcome.missing.is_empty() {
                            let mut msg = format!(
                                "Remote: {} of {} changed",
                                outcome.divergent, outcome.checked
                            );
                            if !outcome.missing.is_empty() {
                                msg.push_str(&format!(
                                    ", {} deleted ({})",
                                    outcome.missing.len(),
                                    outcome.missing.join(", ")
                                ));
                            }
                            self.status_message = msg;
                        }
                    }
                    Err(e) => {
                        self.remote_poll_failures = self.remote_poll_failures.saturating_add(1);
                        // Report the first failure; repeats just extend the
                        // backoff silently (offline shouldn't nag).
                        if self.remote_poll_failures == 1 {
                            self.status_message = format!("Remote check failed: {e}");
                        }
                    }
                }
            }
            AsyncEvent::HydrationUpdate(progress) => {
                // Hydration runs in the background; narrate inline. Phase
                // strings carry their own counts.
                self.status_message = if progress.matched > 0 {
                    format!(
                        "Hydrating: {} ({} matched)",
                        progress.phase, progress.matched
                    )
                } else {
                    format!("Hydrating: {}", progress.phase)
                };
            }
            AsyncEvent::HydrationDone(result) => match result {
                Ok(data) => {
                    // Merge hydration's discovered mappings into the live store
                    // rather than reloading from disk (which would clobber any
                    // concurrent push/pull that completed during hydration).
                    self.store.merge_from(&data.store);
                    // Advance this root's incremental cursor so the next
                    // hydrate only fetches gists changed after this walk began.
                    self.store.set_hydrated_cursor(&data.root, data.new_cursor);
                    if let Err(e) = self.store.save() {
                        self.status_message =
                            format!("Hydrated, but saving local state failed: {e}");
                        return;
                    }
                    self.pending_ambiguous = data.ambiguous;
                    if data.matched > 0 {
                        self.refresh_status_cache();
                        self.rebuild_tree();
                        self.update_status();
                    }
                    // Quiet unless hydration actually found something to say.
                    if !self.pending_ambiguous.is_empty() {
                        self.status_message = format!(
                            "Hydrated {} file(s); {} ambiguous match(es): press M to resolve",
                            data.matched,
                            self.pending_ambiguous.len()
                        );
                    } else if data.matched > 0 {
                        self.status_message = format!("Hydrated {} file(s)", data.matched);
                    } else if self.status_message.starts_with("Hydrating:") {
                        self.status_message.clear();
                        self.update_status();
                    }
                }
                Err(e) => {
                    self.status_message = format!("Hydration error: {e}");
                }
            },
            AsyncEvent::StatusCheck {
                root,
                rel_path,
                started,
                result,
            } => match result {
                Ok(full) => {
                    // A diff fetch is also a remote observation - persist it
                    // so the tree reflects any divergence it revealed. Skip
                    // if the entry synced while the fetch was in flight.
                    if sync_apply::record_observation(
                        &mut self.store,
                        &root,
                        &rel_path,
                        started,
                        full.remote_sha256.clone(),
                        full.remote_updated_at,
                    ) {
                        if let Err(e) = self.store.save() {
                            self.status_message = format!("Saving local state failed: {e}");
                        }
                        self.refresh_status_for(&rel_path);
                        self.rebuild_tree();
                    }
                    self.status_message = format!("{} {rel_path}", full.status.icon());
                    if let Mode::Diff { remote, .. } = &mut self.mode {
                        *remote = full.remote_content;
                    }
                }
                Err(e) => {
                    self.status_message = format!("Status check failed: {e}");
                }
            },
            AsyncEvent::DeleteDone {
                root,
                rel_path,
                result,
            } => match result {
                Ok(()) => {
                    self.store.remove(&root, &rel_path);
                    if let Err(e) = self.store.save() {
                        self.status_message =
                            format!("Deleted, but saving local state failed: {e}");
                        return;
                    }
                    self.status_message = format!("Deleted gist for {rel_path}");
                    self.rebuild_tree();
                    self.update_status();
                }
                Err(e) => {
                    self.status_message = format!("Delete failed: {e}");
                }
            },
            AsyncEvent::RenameRemoteDone { rel_path, result } => match result {
                Ok(()) => {
                    self.status_message = format!("Renamed (local + remote): {rel_path}");
                }
                Err(e) => {
                    self.status_message = format!("Renamed locally; remote rename failed: {e}");
                }
            },
            AsyncEvent::LinkDone {
                root,
                rel_path,
                result,
            } => match result {
                Ok(entry) => {
                    let url = entry.url.clone();
                    let synced = entry.local_sha256 == entry.remote_sha256;
                    self.store.insert(&root, rel_path.clone(), entry);
                    if let Err(e) = self.store.save() {
                        self.status_message = format!("Linked, but saving local state failed: {e}");
                        return;
                    }
                    self.refresh_status_for(&rel_path);
                    self.rebuild_tree();
                    self.update_status();
                    self.status_message = if synced {
                        format!("Linked {rel_path}{url} (in sync)")
                    } else {
                        format!("Linked {rel_path}{url} (differs - D to diff, u/d to reconcile)")
                    };
                }
                Err(e) => {
                    self.status_message = format!("Link failed: {e}");
                }
            },
            AsyncEvent::GdocFetched(result) => match result {
                Ok(content) => {
                    self.gdoc_content = Some(content);
                    self.input_editor = LineEditor::new();
                    self.mode = Mode::GdocFilename;
                }
                Err(e) => {
                    self.mode = Mode::Message(format!("Google Doc error: {e}"));
                }
            },
            AsyncEvent::GistImportFetched(result) => match result {
                Ok(data) => {
                    self.gdoc_content = Some(data.content);
                    self.pending_import_entry = Some(data.entry);
                    // Prefill the save-as prompt with the gist's own filename
                    // (the prompt appends .md).
                    self.input_editor = LineEditor::new();
                    self.input_editor.content = data.filename.trim_end_matches(".md").to_string();
                    self.input_editor.cursor = self.input_editor.content.len();
                    self.mode = Mode::GdocFilename;
                }
                Err(e) => {
                    self.mode = Mode::Message(format!("Gist import failed: {e}"));
                }
            },
        }
    }

    /// Route the `I` import prompt. Gist URLs and bare gist IDs go to the
    /// gist importer; everything else is treated as a Google Doc URL (whose
    /// own validation rejects junk).
    pub(crate) fn start_import_from_url(&mut self, raw: &str) {
        let t = raw.trim();
        let bare_id =
            !t.is_empty() && !t.contains('/') && t.chars().all(|c| c.is_ascii_alphanumeric());
        if (t.contains("gist.github.com") || bare_id)
            && let Some(id) = parse_gist_id(t)
        {
            self.start_gist_import(id);
            return;
        }
        self.start_gdoc_fetch(t);
    }

    /// Fetch a gist for import as a new local file. Single-file gists only:
    /// a multi-file gist has no unambiguous "the content". On success the
    /// save-as prompt opens with the mapping queued; the store write lands
    /// in `save_gdoc_import` once the file exists on disk.
    fn start_gist_import(&mut self, id: String) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            self.mode = Mode::Normal;
            return;
        };
        let tx = self.async_tx.clone();
        self.status_message = "Fetching gist...".into();
        self.mode = Mode::Normal; // switches to the save-as prompt on result

        self.spawn_tracked(async move {
            let result = async {
                let gist = client.get(&id).await.map_err(|e| e.to_string())?;
                if gist.files.len() != 1 {
                    return Err(format!(
                        "gist has {} files; only single-file gists can be imported (use L to link)",
                        gist.files.len()
                    ));
                }
                let filename = gist.files.keys().next().cloned().unwrap_or_default();
                let content = client
                    .file_content(&gist, &filename)
                    .await
                    .map_err(|e| e.to_string())?
                    .ok_or_else(|| "gist file has no content".to_string())?;
                let sha = sync::sha256_hex(&content);
                Ok(crate::event::GistImportData {
                    entry: FileEntry {
                        backend: crate::store::GIST_BACKEND.into(),
                        remote_id: gist.id.clone(),
                        url: gist.html_url.clone(),
                        local_sha256: sha.clone(),
                        remote_sha256: sha,
                        last_synced: chrono::Utc::now(),
                        remote_updated_at: Some(gist.updated_at),
                    },
                    content,
                    filename,
                })
            }
            .await;
            let _ = tx.send(AsyncEvent::GistImportFetched(result));
        });
    }

    pub(crate) fn do_sync_up(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        self.do_sync_up_for(rel, false);
    }

    /// Inner push implementation, parameterized by rel_path so bulk-push
    /// can call it once per dirty file. Unless `force` is set, updates are
    /// refused when the remote changed since the last sync; a PushBlocked
    /// event then records the divergence and prompts for a force-push.
    pub(crate) fn do_sync_up_for(&mut self, rel: String, force: bool) {
        // Debounce: don't stack a second push on a file that's already
        // pushing (e.g. a double `u` press). Two concurrent pushes of an
        // unmapped file would each create a separate gist. A force-push is a
        // deliberate override from the confirm dialog, so it always proceeds
        // (and PushBlocked has already cleared the guard by then anyway).
        if !force && self.pending_pushes.contains(&rel) {
            self.status_message = format!("Already pushing {rel}...");
            return;
        }
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let Some(root) = self.active_root_path() else {
            self.status_message = "No active root.".into();
            return;
        };
        let path = self.abs_path(&rel);
        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(e) => {
                self.status_message = format!("Read {rel}: {e}");
                return;
            }
        };
        let filename = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        let store_snapshot = self.store.get(&root, &rel).cloned();
        let tx = self.async_tx.clone();
        let rel_clone = rel.clone();
        let root_clone = root.clone();

        self.status_message = format!("Pushing {rel}...");
        self.pending_pushes.insert(rel.clone());

        self.spawn_tracked(async move {
            let result =
                sync::push(&client, store_snapshot.as_ref(), &filename, &content, force).await;
            let event = match result {
                Ok(sync::PushOutcome::Pushed(entry)) => AsyncEvent::PushDone {
                    root: root_clone,
                    rel_path: rel_clone,
                    result: Ok(entry),
                },
                Ok(sync::PushOutcome::RemoteChanged {
                    remote_sha256,
                    remote_updated_at,
                }) => AsyncEvent::PushBlocked {
                    root: root_clone,
                    rel_path: rel_clone,
                    remote_sha256,
                    remote_updated_at,
                },
                Err(e) => AsyncEvent::PushDone {
                    root: root_clone,
                    rel_path: rel_clone,
                    result: Err(e.to_string()),
                },
            };
            let _ = tx.send(event);
        });
    }

    pub(crate) fn confirm_sync_down(&mut self) {
        let Some(ref rel) = self.selected_file() else {
            return;
        };
        let has_entry = self
            .active_root_path()
            .map(|r| self.store.get(&r, rel).is_some())
            .unwrap_or(false);
        if has_entry {
            self.mode = Mode::Confirm {
                message: format!(
                    "Pull remote content for {rel}? Local changes will be overwritten."
                ),
                action: ConfirmAction::SyncDown,
            };
        } else {
            self.status_message = "No gist mapped for this file.".into();
        }
    }

    pub(crate) fn do_sync_down(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        self.do_sync_down_for(rel);
    }

    /// Inner pull implementation, parameterized by rel_path so bulk-pull
    /// can call it once per remote-newer file.
    pub(crate) fn do_sync_down_for(&mut self, rel: String) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let Some(root) = self.active_root_path() else {
            self.status_message = "No active root.".into();
            return;
        };
        let Some(entry) = self.store.get(&root, &rel).cloned() else {
            self.status_message = format!("No gist mapped for {rel}.");
            return;
        };
        let filename = self
            .abs_path(&rel)
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        // Snapshot the local content's hash now; if the file changes on disk
        // while the pull is in flight, the PullDone handler refuses to
        // overwrite those edits.
        let local_now = std::fs::read_to_string(self.abs_path(&rel)).unwrap_or_default();
        let expected_local_sha256 = sync::sha256_hex(&local_now);
        let tx = self.async_tx.clone();
        let rel_clone = rel.clone();
        let root_clone = root.clone();

        self.status_message = format!("Pulling {rel}...");

        self.spawn_tracked(async move {
            let result = sync::pull(&client, &entry, &filename).await;
            let _ = tx.send(AsyncEvent::PullDone {
                root: root_clone,
                rel_path: rel_clone,
                expected_local_sha256,
                result: result.map_err(|e| e.to_string()),
            });
        });
    }

    pub(crate) fn do_open_in_browser(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        let Some(entry) = self
            .active_root_path()
            .and_then(|r| self.store.get(&r, &rel).cloned())
        else {
            self.status_message = "No gist mapped for this file.".into();
            return;
        };
        match open::that(&entry.url) {
            Ok(()) => self.status_message = format!("Opened {}", entry.url),
            Err(e) => self.status_message = format!("Open failed: {e}"),
        }
    }

    /// Open the delete menu for the selected file. Options are contextual:
    /// remote choices only appear when the file has a gist mapping.
    pub(crate) fn open_delete_menu(&mut self) {
        if self.selected_file().is_none() {
            self.status_message = "No file selected.".into();
            return;
        }
        if self.delete_options().is_empty() {
            self.status_message = "Nothing to delete.".into();
            return;
        }
        self.mode = Mode::DeleteMenu { selected: 0 };
    }

    /// The delete choices valid for the selected file, in display order.
    pub(crate) fn delete_options(&self) -> Vec<crate::app::DeleteChoice> {
        use crate::app::DeleteChoice;
        let Some(rel) = self.selected_file() else {
            return Vec::new();
        };
        let has_gist = self
            .active_root_path()
            .is_some_and(|root| self.store.get(&root, &rel).is_some());
        if has_gist {
            vec![
                DeleteChoice::Remote,
                DeleteChoice::Local,
                DeleteChoice::Both,
            ]
        } else {
            vec![DeleteChoice::Local]
        }
    }

    pub(crate) fn confirm_delete_both(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        let Some(root) = self.active_root_path() else {
            return;
        };
        let Some(entry) = self.store.get(&root, &rel).cloned() else {
            self.status_message = "No gist to delete.".into();
            return;
        };
        self.mode = Mode::Confirm {
            message: format!("Delete the remote gist for {rel} AND move the local file to trash?"),
            action: ConfirmAction::DeleteBoth {
                rel_path: rel,
                root,
                remote_id: entry.remote_id,
            },
        };
    }

    pub(crate) fn confirm_delete_remote(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        let Some(root) = self.active_root_path() else {
            return;
        };
        let Some(entry) = self.store.get(&root, &rel).cloned() else {
            self.status_message = "No gist to delete.".into();
            return;
        };
        self.mode = Mode::Confirm {
            message: format!(
                "Delete remote gist for {rel}? Local file is kept; mapping is removed."
            ),
            action: ConfirmAction::DeleteRemote {
                rel_path: rel,
                root,
                remote_id: entry.remote_id,
            },
        };
    }

    pub(crate) fn do_delete_remote(&mut self, rel_path: String, root: PathBuf, remote_id: String) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let tx = self.async_tx.clone();
        self.status_message = format!("Deleting gist for {rel_path}...");
        self.spawn_tracked(async move {
            let result = client.delete(&remote_id).await.map_err(|e| e.to_string());
            let _ = tx.send(AsyncEvent::DeleteDone {
                root,
                rel_path,
                result,
            });
        });
    }

    pub(crate) fn do_copy_url(&mut self) {
        let Some(rel) = self.selected_file() else {
            return;
        };
        let entry = self
            .active_root_path()
            .and_then(|r| self.store.get(&r, &rel).cloned());
        if let Some(entry) = entry {
            self.copy_to_clipboard(&entry.url.clone());
        } else if self.pending_pushes.contains(&rel) {
            // A push for this file is already in flight (e.g. `c` right after
            // `u`). Just queue the copy onto it - spawning a second push would
            // race two "Pushed" messages and clobber "Copied".
            self.pending_copy = Some(rel.clone());
            self.status_message = format!("Push in progress for {rel}; will copy URL when done...");
        } else {
            // Queue the copy and trigger a push; PushDone will follow up.
            self.pending_copy = Some(rel.clone());
            self.do_sync_up();
            self.status_message = format!("Pushing {rel} first, then copy URL...");
        }
    }

    /// Copy a URL to the system clipboard, returning true on success.
    pub(crate) fn copy_to_clipboard(&mut self, url: &str) -> bool {
        match arboard::Clipboard::new() {
            Ok(mut clip) => match clip.set_text(url) {
                Ok(()) => {
                    self.status_message = format!("Copied: {url}");
                    true
                }
                Err(_) => {
                    self.status_message = "Failed to copy to clipboard.".into();
                    false
                }
            },
            Err(e) => {
                self.status_message = format!("Clipboard error: {e}");
                false
            }
        }
    }

    pub(crate) fn do_diff(&mut self) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let Some(rel) = self.selected_file() else {
            return;
        };
        let Some(root) = self.active_root_path() else {
            self.status_message = "No active root.".into();
            return;
        };
        let Some(entry) = self.store.get(&root, &rel).cloned() else {
            self.status_message = "No gist to diff against.".into();
            return;
        };
        let local_content = std::fs::read_to_string(self.abs_path(&rel)).unwrap_or_default();
        let filename = self
            .abs_path(&rel)
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        let tx = self.async_tx.clone();
        let remote_id = entry.remote_id.clone();
        let local_for_task = local_content.clone();
        let started = chrono::Utc::now();

        self.status_message = "Fetching remote for diff...".into();

        self.spawn_tracked(async move {
            let result = sync::full_status(&client, &local_for_task, &entry, &filename).await;
            let _ = tx.send(AsyncEvent::StatusCheck {
                root,
                rel_path: rel,
                started,
                result: result.map_err(|e| e.to_string()),
            });
        });

        // Start with local-only diff; remote side will be fetched async
        self.diff_scroll = 0;
        self.mode = Mode::Diff {
            local: local_content,
            remote: format!("(fetching remote content for {remote_id}...)"),
        };
    }

    /// Kick off a background remote check: list all gists once, fetch content
    /// for any whose `updated_at` moved since we last looked, and record the
    /// observed remote hashes. This is what makes remote edits show up as
    /// remote-newer/conflict in the tree without pulling anything. Driven by
    /// the poll timer in `tick()`; silent unless something actually changed.
    pub(crate) fn start_remote_check(&mut self) {
        if self.remote_check_inflight {
            return;
        }
        let Some(client) = self.gist_client.clone() else {
            return;
        };
        let Some(root) = self.active_root_path() else {
            return;
        };
        let entries = self.store.gist_entries_for_root(&root);
        if entries.is_empty() {
            return;
        }
        let started = chrono::Utc::now();
        let tx = self.async_tx.clone();

        self.remote_check_inflight = true;

        self.spawn_tracked(async move {
            let result = crate::remote::check_remote(&client, &entries, |_, _| {}).await;
            let _ = tx.send(AsyncEvent::RemoteCheckDone {
                root,
                started,
                result: result.map_err(|e| e.to_string()),
            });
        });
    }

    pub(crate) fn start_hydration(&mut self) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let Some(root) = self.active_root_path() else {
            self.status_message = "No active root.".into();
            return;
        };

        let tx = self.async_tx.clone();
        let files = self.files.clone();
        // Incremental cursor: only walk gists updated since this root's last
        // hydrate. None on a root's first walk → full. Capture the new cursor
        // *now*, before listing, so anything created during the walk is
        // re-examined next time rather than skipped.
        let since = self.store.hydrated_cursor(&root);
        let new_cursor = chrono::Utc::now();
        // Snapshot only this root's mappings; we'll merge results back when done.
        let mut store = Store::default();
        for (rel, entry) in self.store.gist_entries_for_root(&root) {
            store.insert(&root, rel, entry);
        }

        self.spawn_tracked(async move {
            let tx2 = tx.clone();
            let result = crate::hydrate::hydrate(
                &client,
                &mut store,
                &root,
                &files,
                since,
                move |progress| {
                    let _ = tx2.send(AsyncEvent::HydrationUpdate(progress));
                },
            )
            .await;
            let payload = result.map(|outcome| crate::event::HydrationDoneData {
                matched: outcome.matched,
                ambiguous: outcome.ambiguous,
                store: Box::new(store),
                root,
                new_cursor,
            });
            let _ = tx.send(AsyncEvent::HydrationDone(
                payload.map_err(|e| e.to_string()),
            ));
        });
    }

    /// Apply a user pick from the ambiguous resolver: write the chosen gist mapping
    /// into the store for the current root.
    pub(crate) fn apply_ambiguous_pick(&mut self, item: usize, candidate: usize) {
        let Some(root) = self.active_root_path() else {
            return;
        };
        let Some(am) = self.pending_ambiguous.get(item) else {
            return;
        };
        let Some(cand) = am.candidates.get(candidate) else {
            return;
        };
        let entry = FileEntry {
            backend: crate::store::GIST_BACKEND.into(),
            remote_id: cand.remote_id.clone(),
            url: cand.url.clone(),
            local_sha256: am.local_hash.clone(),
            remote_sha256: am.local_hash.clone(),
            last_synced: chrono::Utc::now(),
            remote_updated_at: None,
        };
        let rel = am.local_path.clone();
        self.store.insert(&root, rel.clone(), entry.clone());
        if let Err(e) = self.store.save() {
            self.status_message = format!("Resolved, but saving local state failed: {e}");
        } else {
            self.status_message = format!("Mapped {rel}{}", cand.url);
        }
        // The candidates here failed the content match, so the remote almost
        // certainly differs from local - fetch its real hash in the
        // background so the tree shows the divergence instead of a
        // fabricated "Synced".
        if let Some(client) = self.gist_client.clone() {
            let filename = rel.rsplit('/').next().unwrap_or(&rel).to_string();
            let local_content = std::fs::read_to_string(self.abs_path(&rel)).unwrap_or_default();
            let started = chrono::Utc::now();
            let tx = self.async_tx.clone();
            let rel_clone = rel.clone();
            self.spawn_tracked(async move {
                let result = sync::full_status(&client, &local_content, &entry, &filename).await;
                let _ = tx.send(AsyncEvent::StatusCheck {
                    root,
                    rel_path: rel_clone,
                    started,
                    result: result.map_err(|e| e.to_string()),
                });
            });
        }
    }

    /// `L`: begin manually linking the selected file to an existing gist.
    /// Opens an input prompt for a gist URL or bare ID.
    pub(crate) fn start_manual_link(&mut self) {
        let Some(rel_path) = self.selected_file() else {
            self.status_message = "Select a file to link first.".into();
            return;
        };
        self.input_editor = LineEditor::new();
        self.mode = Mode::LinkGist { rel_path };
    }

    /// Fetch `raw` (a gist URL or ID), reconcile it against the local file at
    /// `rel_path`, and record the mapping. The fetch happens off-thread; the
    /// store write lands in the `LinkDone` handler.
    pub(crate) fn link_gist_to(&mut self, rel_path: String, raw: &str) {
        let Some(client) = self.gist_client.clone() else {
            self.status_message = crate::app::NO_TOKEN_HINT.into();
            return;
        };
        let Some(root) = self.active_root_path() else {
            self.status_message = "No active root.".into();
            return;
        };
        let Some(remote_id) = parse_gist_id(raw) else {
            self.status_message = format!("Couldn't read a gist ID from '{raw}'.");
            return;
        };
        // Read local content now (main thread) so the task only does network.
        let local_content = std::fs::read_to_string(self.abs_path(&rel_path)).unwrap_or_default();
        let filename = rel_path.rsplit('/').next().unwrap_or(&rel_path).to_string();
        let tx = self.async_tx.clone();
        self.status_message = format!("Linking {rel_path} → gist {remote_id}...");
        self.spawn_tracked(async move {
            let result = build_link_entry(&client, &remote_id, &filename, &local_content).await;
            let _ = tx.send(AsyncEvent::LinkDone {
                root,
                rel_path,
                result: result.map_err(|e| e.to_string()),
            });
        });
    }
}

/// Reconcile a gist (fetched by ID) against a local file: pick the matching
/// file within the gist, fetch its content, and build a store entry carrying
/// both the local and the *observed* remote hash so the resulting status is
/// honest (Synced only when they truly match).
async fn build_link_entry(
    client: &penknife_gist::GistClient,
    remote_id: &str,
    local_filename: &str,
    local_content: &str,
) -> color_eyre::Result<FileEntry> {
    let gist = client.get(remote_id).await?;
    // Prefer the gist file whose name matches the local basename; fall back to
    // the sole file in a single-file gist. A multi-file gist with no name
    // match is genuinely ambiguous - refuse rather than guess.
    let chosen = if gist.files.contains_key(local_filename) {
        local_filename.to_string()
    } else if gist.files.len() == 1 {
        gist.files.keys().next().cloned().expect("len==1 has a key")
    } else if gist.files.is_empty() {
        color_eyre::eyre::bail!("Gist {remote_id} has no files.");
    } else {
        let names: Vec<&str> = gist.files.keys().map(|s| s.as_str()).collect();
        color_eyre::eyre::bail!(
            "Gist {remote_id} has multiple files ({}); none named '{local_filename}'. \
             Rename the local file to match one, or link via a single-file gist.",
            names.join(", ")
        );
    };
    let remote_content = client
        .file_content(&gist, &chosen)
        .await?
        .unwrap_or_default();
    Ok(FileEntry {
        backend: crate::store::GIST_BACKEND.into(),
        remote_id: gist.id.clone(),
        url: gist.html_url.clone(),
        local_sha256: sync::sha256_hex(local_content),
        remote_sha256: sync::sha256_hex(&remote_content),
        last_synced: chrono::Utc::now(),
        remote_updated_at: Some(gist.updated_at),
    })
}

/// Extract a gist ID from a URL or bare ID. Accepts the GitHub web URL form
/// (`https://gist.github.com/user/<id>` or `.../<id>`), an optional `.git`
/// suffix, trailing slashes, and a `#file-…` fragment. A bare token with no
/// slash is taken as the ID itself. Returns `None` if nothing ID-shaped
/// (alphanumeric) remains.
/// If a gist API error looks like a missing-scope 403, suggest the fix. A
/// token can be present and valid yet lack the `gist` scope (common when
/// `gh auth login` ran before you cared about gists), which surfaces as a
/// bare 403 on the first push. Point the user straight at the one command
/// that fixes it instead of leaving them to guess.
fn scope_hint(err: &str) -> &'static str {
    if err.contains("(403)") {
        " (your token may lack the 'gist' scope; run: gh auth refresh -s gist)"
    } else {
        ""
    }
}

fn parse_gist_id(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    // Drop any URL fragment / query, then trailing slashes.
    let no_frag = trimmed.split(['#', '?']).next().unwrap_or(trimmed);
    let no_slash = no_frag.trim_end_matches('/');
    // The ID is the last path segment, minus an optional `.git`.
    let last = no_slash.rsplit('/').next().unwrap_or(no_slash);
    let id = last.strip_suffix(".git").unwrap_or(last);
    if !id.is_empty() && id.chars().all(|c| c.is_ascii_alphanumeric()) {
        Some(id.to_string())
    } else {
        None
    }
}

#[cfg(test)]
mod handler_tests {
    use super::super::App;
    use super::scope_hint;
    use crate::app::test_support::{guard, new_for_test, select, write_file};
    use crate::app::{ConfirmAction, DeleteChoice, Mode};
    use crate::event::{AsyncEvent, GistImportData, HydrationDoneData, async_channel};
    use crate::hydrate::{AmbiguousMatch, GistCandidate};
    use crate::remote::RemoteCheckOutcome;
    use crate::store::{FileEntry, GIST_BACKEND, Store};
    use crate::sync::{self, FullStatus, SyncStatus};
    use std::path::PathBuf;
    use tempfile::TempDir;

    fn app3() -> (TempDir, App, PathBuf) {
        let dir = tempfile::tempdir().unwrap();
        write_file(dir.path(), "a.md", "alpha");
        write_file(dir.path(), "b.md", "beta");
        write_file(dir.path(), "sub/c.md", "gamma");
        let (tx, rx) = async_channel();
        std::mem::forget(rx);
        let app = new_for_test(dir.path(), tx);
        let root = app.active_root_path().unwrap();
        (dir, app, root)
    }

    fn entry(id: &str, local: &str, remote: &str) -> FileEntry {
        FileEntry {
            backend: GIST_BACKEND.into(),
            remote_id: id.into(),
            url: format!("https://gist.github.com/u/{id}"),
            local_sha256: local.into(),
            remote_sha256: remote.into(),
            last_synced: chrono::Utc::now(),
            remote_updated_at: None,
        }
    }

    // ── PushDone ────────────────────────────────────────────────────────

    #[test]
    fn push_done_ok_inserts_entry_and_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let h = sync::sha256_hex("alpha");
        app.handle_async_event(AsyncEvent::PushDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            result: Ok(entry("g1", &h, &h)),
        });
        assert_eq!(app.store.get(&root, "a.md").unwrap().remote_id, "g1");
        assert!(app.status_message.starts_with("Pushed a.md"));
        assert_eq!(app.cached_status("a.md"), SyncStatus::Synced);
    }

    #[test]
    fn push_done_err_reports_and_clears_pending_copy() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.pending_copy = Some("a.md".into());
        app.handle_async_event(AsyncEvent::PushDone {
            root,
            rel_path: "a.md".into(),
            result: Err("boom".into()),
        });
        assert!(app.status_message.starts_with("Push failed: boom"));
        assert!(app.pending_copy.is_none());
    }

    #[test]
    fn push_done_ok_with_pending_copy_clears_it() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let h = sync::sha256_hex("alpha");
        app.pending_copy = Some("a.md".into());
        app.handle_async_event(AsyncEvent::PushDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            result: Ok(entry("g1", &h, &h)),
        });
        // The queued copy fires (clipboard may be unavailable, but the
        // request is consumed either way).
        assert!(app.pending_copy.is_none());
        assert!(app.store.get(&root, "a.md").is_some());
    }

    #[test]
    fn push_done_clears_pending_push_and_skips_pushed_when_copying() {
        // Belt-and-suspenders (fix #2): when a PushDone both lands the push
        // and fulfils a queued copy, the intermediate "Pushed" line is never
        // shown - so a later, redundant push's "Pushed" echo can't clobber
        // "Copied". Also confirms the in-flight guard is cleared.
        let _g = guard();
        let (_d, mut app, root) = app3();
        let h = sync::sha256_hex("alpha");
        app.pending_copy = Some("a.md".into());
        app.pending_pushes.insert("a.md".into());
        app.handle_async_event(AsyncEvent::PushDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            result: Ok(entry("g1", &h, &h)),
        });
        assert!(app.pending_pushes.is_empty());
        assert!(app.pending_copy.is_none());
        // Either "Copied: …" or a clipboard error (headless CI), but never the
        // intermediate "Pushed …" that the second push would race against.
        assert!(!app.status_message.starts_with("Pushed"));
    }

    #[test]
    fn copy_url_queues_onto_inflight_push_without_respawning() {
        // Fix #1: pressing `c` while a push for the same fresh file is already
        // in flight must not spawn a second push - it just queues the copy.
        let _g = guard();
        let (_d, mut app, _root) = app3();
        select(&mut app, "a.md");
        // Simulate a push already running for a.md (as `u` would have set).
        app.pending_pushes.insert("a.md".into());
        app.do_copy_url();
        assert_eq!(app.pending_copy.as_deref(), Some("a.md"));
        assert!(app.status_message.contains("Push in progress"));
        // No second push was spawned (do_sync_up was skipped).
        assert!(app.tasks.is_empty());
    }

    #[test]
    fn do_sync_up_for_debounces_second_push_but_not_force() {
        // A non-force push for a file already in flight is dropped (guards the
        // double-`u` duplicate-gist race); a force push always proceeds.
        let _g = guard();
        let (_d, mut app, _root) = app3();
        app.pending_pushes.insert("a.md".into());
        app.do_sync_up_for("a.md".into(), false);
        assert!(app.status_message.contains("Already pushing"));
        // No task spawned - the guard returned before spawn_tracked.
        assert!(app.tasks.is_empty());
        // Force pushes bypass the guard; without a token it stops at the token
        // check rather than the debounce, proving it got past the guard.
        app.do_sync_up_for("a.md".into(), true);
        assert!(!app.status_message.contains("Already pushing"));
    }

    // ── PullDone ────────────────────────────────────────────────────────

    #[test]
    fn pull_done_applies_when_local_unchanged() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let expected = sync::sha256_hex("alpha");
        app.handle_async_event(AsyncEvent::PullDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            expected_local_sha256: expected,
            result: Ok(("remote body".into(), entry("g1", "x", "x"))),
        });
        assert_eq!(app.status_message, "Pulled a.md");
        let on_disk = std::fs::read_to_string(root.join("a.md")).unwrap();
        assert_eq!(on_disk, "remote body");
    }

    #[test]
    fn pull_done_refuses_on_drift() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::PullDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            // Stale hash: does not match the "alpha" on disk.
            expected_local_sha256: sync::sha256_hex("something else"),
            result: Ok(("remote body".into(), entry("g1", "x", "x"))),
        });
        assert!(app.status_message.contains("changed on disk"));
        // Disk content preserved.
        assert_eq!(std::fs::read_to_string(root.join("a.md")).unwrap(), "alpha");
    }

    #[test]
    fn pull_done_err_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::PullDone {
            root,
            rel_path: "a.md".into(),
            expected_local_sha256: "x".into(),
            result: Err("net down".into()),
        });
        assert!(app.status_message.starts_with("Pull failed: net down"));
    }

    // ── PushBlocked ─────────────────────────────────────────────────────

    #[test]
    fn push_blocked_records_divergence_and_prompts_force() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        // Seed a mapping so record_divergence has something to update.
        let h = sync::sha256_hex("alpha");
        app.store.insert(&root, "a.md".into(), entry("g1", &h, &h));
        app.refresh_status_cache();
        // A blocked push must also release the in-flight guard.
        app.pending_pushes.insert("a.md".into());
        app.handle_async_event(AsyncEvent::PushBlocked {
            root: root.clone(),
            rel_path: "a.md".into(),
            remote_sha256: "different".into(),
            remote_updated_at: chrono::Utc::now(),
        });
        assert!(app.pending_pushes.is_empty());
        assert!(matches!(
            app.mode,
            Mode::Confirm {
                action: ConfirmAction::ForcePush { .. },
                ..
            }
        ));
        assert!(app.status_message.contains("Push blocked"));
        // Divergence recorded => now RemoteNewer.
        assert_eq!(app.cached_status("a.md"), SyncStatus::RemoteNewer);
    }

    // ── RemoteCheckDone ─────────────────────────────────────────────────

    #[test]
    fn remote_check_ok_applies_updates_and_resets_failures() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let started = chrono::Utc::now();
        // Existing mapping older than the check.
        let mut cur = entry("g1", "l", "r");
        cur.last_synced = started - chrono::Duration::seconds(60);
        app.store.insert(&root, "a.md".into(), cur);
        app.remote_poll_failures = 3;
        app.remote_check_inflight = true;

        let refreshed = entry("g1", "l", "new-remote");
        app.handle_async_event(AsyncEvent::RemoteCheckDone {
            root: root.clone(),
            started,
            result: Ok(RemoteCheckOutcome {
                updated: vec![("a.md".into(), refreshed)],
                divergent: 1,
                missing: vec![],
                checked: 1,
            }),
        });
        assert!(!app.remote_check_inflight);
        assert_eq!(app.remote_poll_failures, 0);
        assert_eq!(
            app.store.get(&root, "a.md").unwrap().remote_sha256,
            "new-remote"
        );
        assert!(app.status_message.contains("1 of 1 changed"));
    }

    #[test]
    fn remote_check_err_increments_failures() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.remote_check_inflight = true;
        app.handle_async_event(AsyncEvent::RemoteCheckDone {
            root,
            started: chrono::Utc::now(),
            result: Err("offline".into()),
        });
        assert_eq!(app.remote_poll_failures, 1);
        assert!(app.status_message.contains("Remote check failed"));
        // A second failure backs off silently (no status change).
        let (_d2, mut app2, root2) = app3();
        app2.remote_poll_failures = 1;
        app2.status_message = "unchanged".into();
        app2.handle_async_event(AsyncEvent::RemoteCheckDone {
            root: root2,
            started: chrono::Utc::now(),
            result: Err("still offline".into()),
        });
        assert_eq!(app2.remote_poll_failures, 2);
        assert_eq!(app2.status_message, "unchanged");
    }

    // ── HydrationDone ───────────────────────────────────────────────────

    #[test]
    fn hydration_done_ok_merges_and_sets_cursor() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let mut discovered = Store::default();
        let h = sync::sha256_hex("alpha");
        discovered.insert(&root, "a.md".into(), entry("g1", &h, &h));
        let cursor = chrono::Utc::now();
        app.handle_async_event(AsyncEvent::HydrationDone(Ok(HydrationDoneData {
            matched: 1,
            ambiguous: vec![],
            store: Box::new(discovered),
            root: root.clone(),
            new_cursor: cursor,
        })));
        assert!(app.store.get(&root, "a.md").is_some());
        assert_eq!(app.store.hydrated_cursor(&root), Some(cursor));
        assert!(app.status_message.contains("Hydrated 1 file"));
    }

    #[test]
    fn hydration_done_with_ambiguous_sets_pending() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let am = AmbiguousMatch {
            local_path: "a.md".into(),
            local_hash: "h".into(),
            candidates: vec![GistCandidate {
                remote_id: "g1".into(),
                url: "u".into(),
                description: None,
                size: 1,
            }],
        };
        app.handle_async_event(AsyncEvent::HydrationDone(Ok(HydrationDoneData {
            matched: 0,
            ambiguous: vec![am],
            store: Box::new(Store::default()),
            root,
            new_cursor: chrono::Utc::now(),
        })));
        assert_eq!(app.pending_ambiguous.len(), 1);
        assert!(app.status_message.contains("press M to resolve"));
    }

    #[test]
    fn hydration_done_err_reports() {
        let _g = guard();
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::HydrationDone(Err("bad token".into())));
        assert!(app.status_message.starts_with("Hydration error: bad token"));
    }

    // ── DeleteDone ──────────────────────────────────────────────────────

    #[test]
    fn delete_done_ok_removes_mapping() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.store
            .insert(&root, "a.md".into(), entry("g1", "x", "x"));
        app.handle_async_event(AsyncEvent::DeleteDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            result: Ok(()),
        });
        // The mapping is gone; the transient "Deleted" message is immediately
        // replaced by the refreshed status dashboard (update_status runs last).
        assert!(app.store.get(&root, "a.md").is_none());
    }

    #[test]
    fn delete_done_err_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::DeleteDone {
            root,
            rel_path: "a.md".into(),
            result: Err("403".into()),
        });
        assert!(app.status_message.starts_with("Delete failed: 403"));
    }

    // ── LinkDone ────────────────────────────────────────────────────────

    #[test]
    fn link_done_ok_in_sync_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        let h = sync::sha256_hex("alpha");
        app.handle_async_event(AsyncEvent::LinkDone {
            root: root.clone(),
            rel_path: "a.md".into(),
            result: Ok(entry("g1", &h, &h)),
        });
        assert!(app.store.get(&root, "a.md").is_some());
        assert!(app.status_message.contains("in sync"));
    }

    #[test]
    fn link_done_ok_differs_reports_diff_hint() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::LinkDone {
            root,
            rel_path: "a.md".into(),
            result: Ok(entry("g1", "local", "remote")),
        });
        assert!(app.status_message.contains("differs"));
    }

    #[test]
    fn link_done_err_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::LinkDone {
            root,
            rel_path: "a.md".into(),
            result: Err("not found".into()),
        });
        assert!(app.status_message.starts_with("Link failed: not found"));
    }

    // ── RenameRemoteDone ────────────────────────────────────────────────

    #[test]
    fn rename_remote_done_ok_and_err() {
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::RenameRemoteDone {
            rel_path: "new.md".into(),
            result: Ok(()),
        });
        assert!(app.status_message.contains("Renamed (local + remote)"));
        app.handle_async_event(AsyncEvent::RenameRemoteDone {
            rel_path: "new.md".into(),
            result: Err("nope".into()),
        });
        assert!(app.status_message.contains("remote rename failed"));
    }

    // ── StatusCheck ─────────────────────────────────────────────────────

    #[test]
    fn status_check_ok_updates_diff_remote() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.mode = Mode::Diff {
            local: "alpha".into(),
            remote: "(fetching...)".into(),
        };
        app.handle_async_event(AsyncEvent::StatusCheck {
            root,
            rel_path: "a.md".into(),
            started: chrono::Utc::now(),
            result: Ok(FullStatus {
                status: SyncStatus::Synced,
                remote_content: "remote text".into(),
                remote_sha256: sync::sha256_hex("remote text"),
                remote_updated_at: chrono::Utc::now(),
            }),
        });
        if let Mode::Diff { remote, .. } = &app.mode {
            assert_eq!(remote, "remote text");
        } else {
            panic!("expected Diff mode");
        }
        assert!(app.status_message.contains("a.md"));
    }

    #[test]
    fn status_check_err_reports() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.handle_async_event(AsyncEvent::StatusCheck {
            root,
            rel_path: "a.md".into(),
            started: chrono::Utc::now(),
            result: Err("timeout".into()),
        });
        assert!(
            app.status_message
                .starts_with("Status check failed: timeout")
        );
    }

    // ── GdocFetched / GistImportFetched ─────────────────────────────────

    #[test]
    fn gdoc_fetched_ok_opens_filename_prompt() {
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::GdocFetched(Ok("# Doc".into())));
        assert!(matches!(app.mode, Mode::GdocFilename));
        assert_eq!(app.gdoc_content.as_deref(), Some("# Doc"));
    }

    #[test]
    fn gdoc_fetched_err_shows_message() {
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::GdocFetched(Err("404".into())));
        assert!(matches!(app.mode, Mode::Message(_)));
    }

    #[test]
    fn gist_import_fetched_prefills_filename() {
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::GistImportFetched(Ok(GistImportData {
            content: "body".into(),
            filename: "notes.md".into(),
            entry: entry("g1", "x", "x"),
        })));
        assert!(matches!(app.mode, Mode::GdocFilename));
        assert_eq!(app.gdoc_content.as_deref(), Some("body"));
        assert!(app.pending_import_entry.is_some());
        // The .md suffix is stripped from the prefilled name.
        assert_eq!(app.input_editor.content, "notes");
    }

    #[test]
    fn gist_import_fetched_err_shows_message() {
        let (_d, mut app, _root) = app3();
        app.handle_async_event(AsyncEvent::GistImportFetched(Err("multi-file".into())));
        assert!(matches!(app.mode, Mode::Message(_)));
    }

    // ── delete_options / apply_ambiguous_pick / scope_hint ──────────────

    #[test]
    fn delete_options_local_only_without_gist() {
        let (_d, mut app, _root) = app3();
        select(&mut app, "a.md");
        assert_eq!(app.delete_options(), vec![DeleteChoice::Local]);
    }

    #[test]
    fn delete_options_all_when_gisted() {
        let (_d, mut app, root) = app3();
        app.store
            .insert(&root, "a.md".into(), entry("g1", "x", "x"));
        select(&mut app, "a.md");
        assert_eq!(
            app.delete_options(),
            vec![
                DeleteChoice::Remote,
                DeleteChoice::Local,
                DeleteChoice::Both
            ]
        );
    }

    #[test]
    fn delete_options_empty_without_selection() {
        let (_d, app, _root) = app3();
        assert!(app.delete_options().is_empty());
    }

    #[test]
    fn apply_ambiguous_pick_records_mapping() {
        let _g = guard();
        let (_d, mut app, root) = app3();
        app.pending_ambiguous.push(AmbiguousMatch {
            local_path: "a.md".into(),
            local_hash: sync::sha256_hex("alpha"),
            candidates: vec![GistCandidate {
                remote_id: "g1".into(),
                url: "https://gist.github.com/u/g1".into(),
                description: None,
                size: 1,
            }],
        });
        app.apply_ambiguous_pick(0, 0);
        let mapped = app.store.get(&root, "a.md").unwrap();
        assert_eq!(mapped.remote_id, "g1");
        assert!(app.status_message.contains("Mapped a.md"));
    }

    #[test]
    fn scope_hint_flags_403() {
        assert!(scope_hint("boom (403)").contains("scope"));
        assert_eq!(scope_hint("some other error"), "");
    }
}

#[cfg(test)]
mod tests {
    use super::parse_gist_id;

    #[test]
    fn parses_full_web_url() {
        assert_eq!(
            parse_gist_id("https://gist.github.com/jhheider/0828a8e1bbdb66e7e082b054c9e975e3"),
            Some("0828a8e1bbdb66e7e082b054c9e975e3".to_string())
        );
    }

    #[test]
    fn parses_bare_id() {
        assert_eq!(
            parse_gist_id("0828a8e1bbdb66e7e082b054c9e975e3"),
            Some("0828a8e1bbdb66e7e082b054c9e975e3".to_string())
        );
    }

    #[test]
    fn strips_git_suffix_trailing_slash_and_fragment() {
        assert_eq!(
            parse_gist_id("https://gist.github.com/u/abc123.git"),
            Some("abc123".to_string())
        );
        assert_eq!(
            parse_gist_id("https://gist.github.com/u/abc123/"),
            Some("abc123".to_string())
        );
        assert_eq!(
            parse_gist_id("https://gist.github.com/u/abc123#file-foo-md"),
            Some("abc123".to_string())
        );
    }

    #[test]
    fn rejects_empty_or_nonalphanumeric() {
        // Nothing ID-shaped left → None. A merely-wrong-but-alphanumeric
        // segment is accepted and left to 404 on the actual fetch.
        assert_eq!(parse_gist_id(""), None);
        assert_eq!(parse_gist_id("   "), None);
        assert_eq!(parse_gist_id("not a/url!"), None);
    }
}