pinto-cli 0.1.0

A lightweight, local-first, Git-friendly Scrum backlog and Kanban board for the CLI and TUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
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
//! Unit tests for the backlog-item use cases.

use super::*;
use crate::error::Error;
use crate::service::test_support::{add_with, init_temp, parent_edit};
use crate::service::{LabelMatch, create_sprint};
use crate::sprint::SprintId;
use crate::storage::{BacklogItemRepository, FileRepository};
use std::path::Path;
use tempfile::TempDir;
use tokio::fs;

async fn create_sprint_for_test(dir: &Path, id: &str) {
    let sprint_id = SprintId::new(id).expect("valid sprint id");
    create_sprint(dir, &sprint_id, id, None, None)
        .await
        .expect("create sprint");
}

#[tokio::test]
async fn add_creates_item_with_default_status_and_first_id() {
    let dir = init_temp().await;

    let item = add_item(dir.path(), "First task", NewItem::default())
        .await
        .expect("add succeeds");

    assert_eq!(item.id, ItemId::new("T", 1));
    assert_eq!(item.title, "First task");
    assert_eq!(item.status, Status::new("todo"));

    // It is made permanent.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let loaded = repo.load(&item.id).await.expect("load");
    assert_eq!(loaded, item);
}

#[tokio::test]
async fn add_assigns_unique_incrementing_ids() {
    let dir = init_temp().await;

    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();

    assert_eq!(a.id, ItemId::new("T", 1));
    assert_eq!(b.id, ItemId::new("T", 2));
}

#[tokio::test]
async fn add_sets_optional_fields() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;

    let new = NewItem {
        points: Some(5),
        labels: vec!["backend".to_string(), "urgent".to_string()],
        sprint: Some("S-1".to_string()),
        body: "Acceptance criteria".to_string(),
        parent: None,
        depends_on: Vec::new(),
    };
    let item = add_item(dir.path(), "Configured", new).await.unwrap();

    assert_eq!(item.points, Some(5));
    assert_eq!(item.labels, ["backend", "urgent"]);
    assert_eq!(item.sprint.as_deref(), Some("S-1"));
    assert_eq!(item.body, "Acceptance criteria");
}

#[tokio::test]
async fn add_rejects_invalid_or_missing_sprint_before_allocating_an_id() {
    let dir = init_temp().await;

    let invalid = add_item(
        dir.path(),
        "Malformed",
        NewItem {
            sprint: Some("S 1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(invalid, Error::InvalidSprintId("S 1".to_string()));

    let missing = add_item(
        dir.path(),
        "Missing",
        NewItem {
            sprint: Some("S-9".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(
        missing,
        Error::SprintNotFound(SprintId::new("S-9").unwrap())
    );
    assert!(
        list_items(dir.path(), &ListFilter::default())
            .await
            .unwrap()
            .is_empty()
    );

    create_sprint_for_test(dir.path(), "S-1").await;
    let item = add_item(
        dir.path(),
        "Valid",
        NewItem {
            sprint: Some("S-1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();
    assert_eq!(item.id, ItemId::new("T", 1));
}

#[tokio::test]
async fn add_sets_parent_and_multiple_dependencies() {
    let dir = init_temp().await;
    let parent = add_item(dir.path(), "Parent", NewItem::default())
        .await
        .unwrap();
    let dependency = add_item(dir.path(), "Dependency", NewItem::default())
        .await
        .unwrap();

    let item = add_item(
        dir.path(),
        "Configured relationships",
        NewItem {
            parent: Some(parent.id.clone()),
            depends_on: vec![dependency.id.clone(), parent.id.clone()],
            ..NewItem::default()
        },
    )
    .await
    .expect("add with relationships succeeds");

    assert_eq!(item.parent.as_ref(), Some(&parent.id));
    assert_eq!(item.depends_on, [dependency.id, parent.id]);
}

#[tokio::test]
async fn add_rejects_missing_relationship_targets_without_saving() {
    let dir = init_temp().await;

    let error = add_item(
        dir.path(),
        "Missing parent",
        NewItem {
            parent: Some("T-404".parse().unwrap()),
            ..NewItem::default()
        },
    )
    .await
    .expect_err("missing parent must be rejected");
    assert!(matches!(error, Error::NotFound(_)), "got {error:?}");

    let items = list_items(dir.path(), &ListFilter::default())
        .await
        .expect("list succeeds");
    assert!(items.is_empty(), "failed add must not leave an item");
}

#[tokio::test]
async fn add_rejects_a_self_parent_with_the_same_cycle_error_as_edit() {
    let dir = init_temp().await;

    let error = add_item(
        dir.path(),
        "Self parent",
        NewItem {
            parent: Some("T-1".parse().unwrap()),
            ..NewItem::default()
        },
    )
    .await
    .expect_err("self parent must be rejected");

    assert!(matches!(error, Error::ParentCycle { .. }), "got {error:?}");
}

#[tokio::test]
async fn add_allows_dependency_cycles_with_a_warning_outcome() {
    let dir = init_temp().await;
    let item_id: ItemId = "T-1".parse().unwrap();

    let outcome = add_item_with_outcome(
        dir.path(),
        "Self dependent",
        NewItem {
            depends_on: vec![item_id.clone()],
            ..NewItem::default()
        },
    )
    .await
    .expect("dependency cycles are warnings");

    assert!(outcome.cycle_warning);
    assert_eq!(outcome.item.depends_on, [item_id]);
}

#[tokio::test]
async fn add_appends_in_increasing_rank_order() {
    let dir = init_temp().await;

    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    // Rank increases monotonically in the order of addition.
    assert!(a.rank < b.rank, "{} < {}", a.rank, b.rank);
    assert!(b.rank < c.rank, "{} < {}", b.rank, c.rank);

    // list returns in rank ascending order = addition order.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let ids: Vec<_> = repo
        .list()
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.id)
        .collect();
    assert_eq!(ids, [a.id, b.id, c.id]);
}

#[tokio::test]
async fn add_rejects_empty_title() {
    let dir = init_temp().await;

    let err = add_item(dir.path(), "   ", NewItem::default())
        .await
        .unwrap_err();

    assert_eq!(err, Error::EmptyTitle);
}

#[tokio::test]
async fn add_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");

    let err = add_item(dir.path(), "Task", NewItem::default())
        .await
        .unwrap_err();

    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn list_returns_all_items_in_rank_order() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    let items = list_items(dir.path(), &ListFilter::default())
        .await
        .expect("list succeeds");

    let ids: Vec<_> = items.into_iter().map(|it| it.id).collect();
    assert_eq!(ids, [a.id, b.id, c.id]);
}

#[tokio::test]
async fn list_filters_by_label() {
    let dir = init_temp().await;
    add_with(dir.path(), "Backend task", &["backend"], None).await;
    add_with(dir.path(), "Frontend task", &["frontend"], None).await;
    add_with(dir.path(), "Both", &["backend", "frontend"], None).await;

    let filter = ListFilter {
        labels: vec!["backend".to_string()],
        ..Default::default()
    };
    let titles: Vec<_> = list_items(dir.path(), &filter)
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.title)
        .collect();

    assert_eq!(titles, ["Backend task", "Both"]);
}

#[tokio::test]
async fn list_filters_by_multiple_labels_with_any_or_all_matching() {
    let dir = init_temp().await;
    add_with(dir.path(), "Backend task", &["backend"], None).await;
    add_with(dir.path(), "Frontend task", &["frontend"], None).await;
    add_with(dir.path(), "Both", &["backend", "frontend"], None).await;

    let any = ListFilter {
        labels: vec!["backend".to_string(), "frontend".to_string()],
        ..Default::default()
    };
    let any_titles: Vec<_> = list_items(dir.path(), &any)
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.title)
        .collect();
    assert_eq!(any_titles, ["Backend task", "Frontend task", "Both"]);

    let all = ListFilter {
        labels: vec!["backend".to_string(), "frontend".to_string()],
        label_match: LabelMatch::All,
        ..Default::default()
    };
    let all_titles: Vec<_> = list_items(dir.path(), &all)
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.title)
        .collect();
    assert_eq!(all_titles, ["Both"]);
}

#[tokio::test]
async fn list_filters_by_sprint() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    create_sprint_for_test(dir.path(), "S-2").await;
    add_with(dir.path(), "In sprint", &[], Some("S-1")).await;
    add_with(dir.path(), "No sprint", &[], None).await;
    add_with(dir.path(), "Other sprint", &[], Some("S-2")).await;

    let filter = ListFilter {
        sprint: Some("S-1".to_string()),
        ..Default::default()
    };
    let titles: Vec<_> = list_items(dir.path(), &filter)
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.title)
        .collect();

    assert_eq!(titles, ["In sprint"]);
}

#[tokio::test]
async fn list_filters_by_status() {
    let dir = init_temp().await;
    // Immediately after addition, all columns are default columns (todo). All cases exist, and 0 cases do not.
    add_item(dir.path(), "One", NewItem::default())
        .await
        .unwrap();
    add_item(dir.path(), "Two", NewItem::default())
        .await
        .unwrap();

    let todo = ListFilter {
        status: vec!["todo".to_string()],
        ..Default::default()
    };
    assert_eq!(list_items(dir.path(), &todo).await.unwrap().len(), 2);

    let done = ListFilter {
        status: vec!["done".to_string()],
        ..Default::default()
    };
    assert!(list_items(dir.path(), &done).await.unwrap().is_empty());
}

#[tokio::test]
async fn list_filters_by_multiple_statuses() {
    let dir = init_temp().await;
    let todo = add_item(dir.path(), "Todo", NewItem::default())
        .await
        .unwrap();
    let progress = add_item(dir.path(), "In progress", NewItem::default())
        .await
        .unwrap();
    move_item(dir.path(), &progress.id, "in-progress")
        .await
        .unwrap();

    let filter = ListFilter {
        status: vec!["todo".to_string(), "in-progress".to_string()],
        ..Default::default()
    };
    let ids: Vec<_> = list_items(dir.path(), &filter)
        .await
        .unwrap()
        .into_iter()
        .map(|item| item.id)
        .collect();

    assert_eq!(ids, [todo.id, progress.id]);
}

#[tokio::test]
async fn list_rejects_unknown_status() {
    let dir = init_temp().await;
    let filter = ListFilter {
        status: vec!["unknown".to_string()],
        ..Default::default()
    };

    let error = list_items(dir.path(), &filter).await.unwrap_err();

    assert!(matches!(error, Error::UnknownStatus(status) if status == "unknown"));
}

#[tokio::test]
async fn list_combines_filters_conjunctively() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    create_sprint_for_test(dir.path(), "S-2").await;
    add_with(dir.path(), "Match", &["backend"], Some("S-1")).await;
    add_with(dir.path(), "Wrong label", &["frontend"], Some("S-1")).await;
    add_with(dir.path(), "Wrong sprint", &["backend"], Some("S-2")).await;

    let filter = ListFilter {
        labels: vec!["backend".to_string()],
        sprint: Some("S-1".to_string()),
        ..Default::default()
    };
    let titles: Vec<_> = list_items(dir.path(), &filter)
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.title)
        .collect();

    assert_eq!(titles, ["Match"]);
}

#[tokio::test]
async fn list_on_empty_board_returns_empty() {
    let dir = init_temp().await;
    assert!(
        list_items(dir.path(), &ListFilter::default())
            .await
            .unwrap()
            .is_empty()
    );
}

#[tokio::test]
async fn list_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");
    let err = list_items(dir.path(), &ListFilter::default())
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn show_returns_item_by_id() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Detailed", NewItem::default())
        .await
        .unwrap();

    let got = show_item(dir.path(), &added.id)
        .await
        .expect("show succeeds");

    assert_eq!(got, added);
}

#[tokio::test]
async fn show_missing_id_returns_not_found() {
    let dir = init_temp().await;

    let err = show_item(dir.path(), &ItemId::new("T", 99))
        .await
        .unwrap_err();

    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn show_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");

    let err = show_item(dir.path(), &ItemId::new("T", 1))
        .await
        .unwrap_err();

    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn item_use_cases_fail_fast_on_filename_frontmatter_id_mismatch() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Corruptible", NewItem::default())
        .await
        .expect("add");
    fs::rename(
        dir.path().join(".pinto/tasks/T-1.md"),
        dir.path().join(".pinto/tasks/T-2.md"),
    )
    .await
    .expect("rename corrupt fixture");

    let operations = [
        list_items(dir.path(), &ListFilter::default())
            .await
            .map(|_| ()),
        show_item(dir.path(), &added.id).await.map(|_| ()),
        move_item(dir.path(), &added.id, "in-progress")
            .await
            .map(|_| ()),
        edit_item(
            dir.path(),
            &added.id,
            ItemEdit {
                title: Some("Updated".to_string()),
                ..ItemEdit::default()
            },
        )
        .await
        .map(|_| ()),
        remove_item(dir.path(), &added.id, false).await.map(|_| ()),
        remove_item(dir.path(), &added.id, true).await.map(|_| ()),
    ];
    for result in operations {
        let error = result.expect_err("corrupt board must stop every item operation");
        assert!(matches!(error, Error::Parse { .. }), "got {error:?}");
        assert!(error.to_string().contains("filename"), "got {error}");
    }
}

#[tokio::test]
async fn move_transitions_to_valid_column_and_persists() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Movable", NewItem::default())
        .await
        .unwrap();
    assert_eq!(added.status, Status::new("todo"));

    let moved = move_item(dir.path(), &added.id, "in-progress")
        .await
        .expect("move succeeds");

    assert_eq!(moved.status, Status::new("in-progress"));
    assert!(moved.updated >= added.created, "updated advanced");

    // It is persisted (confirm by reloading).
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.status, Status::new("in-progress"));
    assert_eq!(reloaded.updated, moved.updated);
}

#[tokio::test]
async fn move_to_unknown_column_is_rejected_and_unchanged() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Stay", NewItem::default())
        .await
        .unwrap();

    let err = move_item(dir.path(), &added.id, "archived")
        .await
        .unwrap_err();

    assert_eq!(err, Error::UnknownStatus("archived".to_string()));
    // The state on the disk remains unchanged.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.status, Status::new("todo"));
}

#[tokio::test]
async fn move_missing_id_returns_not_found() {
    let dir = init_temp().await;

    let err = move_item(dir.path(), &ItemId::new("T", 99), "done")
        .await
        .unwrap_err();

    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn move_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");

    let err = move_item(dir.path(), &ItemId::new("T", 1), "done")
        .await
        .unwrap_err();

    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn edit_updates_title_and_persists() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Old title", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        title: Some("New title".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit)
        .await
        .expect("edit succeeds");

    assert_eq!(edited.title, "New title");
    assert!(edited.updated >= added.created, "updated advanced");

    // It is made permanent.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "New title");
    assert_eq!(reloaded.updated, edited.updated);
}

#[tokio::test]
async fn edit_updates_each_optional_field() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-2").await;
    let added = add_item(dir.path(), "Task", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        points: Some(8),
        labels: Some(vec!["backend".to_string(), "urgent".to_string()]),
        assignee: Some("alice".to_string()),
        sprint: Some("S-2".to_string()),
        body: Some("Acceptance criteria".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit).await.unwrap();

    assert_eq!(edited.points, Some(8));
    assert_eq!(edited.labels, ["backend", "urgent"]);
    assert_eq!(edited.assignee.as_deref(), Some("alice"));
    assert_eq!(edited.sprint.as_deref(), Some("S-2"));
    assert_eq!(edited.body, "Acceptance criteria");
    // Title and status that are not specified will not change.
    assert_eq!(edited.title, "Task");
    assert_eq!(edited.status, added.status);
}

#[tokio::test]
async fn edit_rejects_invalid_or_missing_sprint_without_saving() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let added = add_item(
        dir.path(),
        "Task",
        NewItem {
            sprint: Some("S-1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();

    let invalid = edit_item(
        dir.path(),
        &added.id,
        ItemEdit {
            sprint: Some("S 2".to_string()),
            ..ItemEdit::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(invalid, Error::InvalidSprintId("S 2".to_string()));

    let missing = edit_item(
        dir.path(),
        &added.id,
        ItemEdit {
            sprint: Some("S-9".to_string()),
            ..ItemEdit::default()
        },
    )
    .await
    .unwrap_err();
    assert_eq!(
        missing,
        Error::SprintNotFound(SprintId::new("S-9").unwrap())
    );

    let stored = show_item(dir.path(), &added.id).await.unwrap();
    assert_eq!(stored.sprint.as_deref(), Some("S-1"));
}

#[tokio::test]
async fn edit_leaves_unspecified_fields_unchanged() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let new = NewItem {
        points: Some(3),
        labels: vec!["keep".to_string()],
        sprint: Some("S-1".to_string()),
        body: "original body".to_string(),
        parent: None,
        depends_on: Vec::new(),
    };
    let added = add_item(dir.path(), "Keep me", new).await.unwrap();

    // Change only the title.
    let edit = ItemEdit {
        title: Some("Renamed".to_string()),
        ..Default::default()
    };
    let edited = edit_item(dir.path(), &added.id, edit).await.unwrap();

    assert_eq!(edited.title, "Renamed");
    assert_eq!(edited.points, Some(3));
    assert_eq!(edited.labels, ["keep"]);
    assert_eq!(edited.sprint.as_deref(), Some("S-1"));
    assert_eq!(edited.body, "original body");
}

#[tokio::test]
async fn edit_with_no_fields_is_rejected() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Untouched", NewItem::default())
        .await
        .unwrap();

    let err = edit_item(dir.path(), &added.id, ItemEdit::default())
        .await
        .unwrap_err();

    assert_eq!(err, Error::NothingToUpdate);
}

#[tokio::test]
async fn edit_rejects_empty_title_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Original", NewItem::default())
        .await
        .unwrap();

    let edit = ItemEdit {
        title: Some("   ".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &added.id, edit).await.unwrap_err();

    assert_eq!(err, Error::EmptyTitle);
    // The title on the disc remains the same.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "Original");
}

#[tokio::test]
async fn edit_missing_id_returns_not_found() {
    let dir = init_temp().await;
    let edit = ItemEdit {
        title: Some("x".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &ItemId::new("T", 99), edit)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn edit_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");
    let edit = ItemEdit {
        title: Some("x".to_string()),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &ItemId::new("T", 1), edit)
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

#[tokio::test]
async fn remove_archives_by_default_and_hides_from_list() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "Keep", NewItem::default())
        .await
        .unwrap();
    let b = add_item(dir.path(), "Remove me", NewItem::default())
        .await
        .unwrap();

    let outcome = remove_item(dir.path(), &b.id, false)
        .await
        .expect("remove succeeds");

    match outcome {
        RemoveOutcome::Archived(path) => {
            assert!(path.ends_with("archive/T-2.md"), "archived path: {path:?}");
            assert!(path.is_file(), "archived file exists");
        }
        other => panic!("expected Archived, got {other:?}"),
    }

    // The archived item is absent from list, leaving only a.
    let ids: Vec<_> = list_items(dir.path(), &ListFilter::default())
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.id)
        .collect();
    assert_eq!(ids, [a.id]);
    // It is also absent from show.
    assert!(matches!(
        show_item(dir.path(), &b.id).await.unwrap_err(),
        Error::NotFound(_)
    ));
}

#[tokio::test]
async fn remove_with_force_deletes_permanently() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "Bye", NewItem::default())
        .await
        .unwrap();

    let outcome = remove_item(dir.path(), &a.id, true)
        .await
        .expect("remove succeeds");
    assert_eq!(outcome, RemoveOutcome::Deleted);

    // Physical deletion does not remain in the archive.
    let archived = dir.path().join(".pinto").join("archive").join("T-1.md");
    assert!(!archived.exists(), "force delete must not archive");
    assert!(matches!(
        show_item(dir.path(), &a.id).await.unwrap_err(),
        Error::NotFound(_)
    ));
}

#[tokio::test]
async fn remove_with_force_rejects_parent_reference() {
    let dir = init_temp().await;
    let parent = add_item(dir.path(), "Parent", NewItem::default())
        .await
        .expect("parent");
    let child = add_item(
        dir.path(),
        "Child",
        NewItem {
            parent: Some(parent.id.clone()),
            ..NewItem::default()
        },
    )
    .await
    .expect("child");

    let err = remove_item(dir.path(), &parent.id, true)
        .await
        .expect_err("a referenced parent must not be deleted");
    assert_eq!(
        err,
        Error::ReferencedItem {
            item: parent.id.clone(),
            references: child.id.to_string(),
        }
    );
    assert!(show_item(dir.path(), &parent.id).await.is_ok());
    assert!(show_item(dir.path(), &child.id).await.is_ok());
}

#[tokio::test]
async fn remove_with_force_rejects_dependency_reference() {
    let dir = init_temp().await;
    let dependency = add_item(dir.path(), "Dependency", NewItem::default())
        .await
        .expect("dependency");
    let dependent = add_item(
        dir.path(),
        "Dependent",
        NewItem {
            depends_on: vec![dependency.id.clone()],
            ..NewItem::default()
        },
    )
    .await
    .expect("dependent");

    let err = remove_item(dir.path(), &dependency.id, true)
        .await
        .expect_err("a dependency target must not be deleted");
    assert_eq!(
        err,
        Error::ReferencedItem {
            item: dependency.id.clone(),
            references: dependent.id.to_string(),
        }
    );
}

#[tokio::test]
async fn remove_with_force_does_not_reuse_the_deleted_id() {
    let dir = init_temp().await;
    let deleted = add_item(dir.path(), "Deleted", NewItem::default())
        .await
        .expect("first item");
    remove_item(dir.path(), &deleted.id, true)
        .await
        .expect("force delete");

    let replacement = add_item(dir.path(), "Replacement", NewItem::default())
        .await
        .expect("replacement");
    assert_eq!(replacement.id, ItemId::new("T", 2));
}

#[tokio::test]
async fn archive_allows_a_referenced_item_to_remain_addressable() {
    let dir = init_temp().await;
    let parent = add_item(dir.path(), "Parent", NewItem::default())
        .await
        .expect("parent");
    add_item(
        dir.path(),
        "Child",
        NewItem {
            parent: Some(parent.id.clone()),
            ..NewItem::default()
        },
    )
    .await
    .expect("child");

    assert!(matches!(
        remove_item(dir.path(), &parent.id, false).await,
        Ok(RemoveOutcome::Archived(_))
    ));
}

#[tokio::test]
async fn remove_missing_id_returns_not_found() {
    let dir = init_temp().await;

    let err = remove_item(dir.path(), &ItemId::new("T", 99), false)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "archive: {err:?}");

    let err = remove_item(dir.path(), &ItemId::new("T", 99), true)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "force: {err:?}");
}

#[tokio::test]
async fn remove_on_uninitialized_dir_prompts_init() {
    let dir = TempDir::new().expect("temp dir");
    let err = remove_item(dir.path(), &ItemId::new("T", 1), false)
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::NotInitialized { .. }),
        "expected NotInitialized, got {err:?}"
    );
}

// --- set_parent ---

#[tokio::test]
async fn edit_sets_parent_and_persists() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();

    let updated = edit_item(dir.path(), &story.id, parent_edit(Some(epic.id.clone())))
        .await
        .expect("set parent succeeds");
    assert_eq!(updated.parent.as_ref(), Some(&epic.id));

    // It is made permanent.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(
        repo.load(&story.id).await.unwrap().parent.as_ref(),
        Some(&epic.id)
    );
}

#[tokio::test]
async fn edit_no_parent_clears_existing_parent() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();
    edit_item(dir.path(), &story.id, parent_edit(Some(epic.id)))
        .await
        .unwrap();

    let cleared = edit_item(dir.path(), &story.id, parent_edit(None))
        .await
        .expect("clear parent succeeds");
    assert_eq!(cleared.parent, None);
}

#[tokio::test]
async fn edit_parent_rejects_cycle_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    // a ← b (parent of b is a). If the parent of a is set to b, it becomes a cycle.
    edit_item(dir.path(), &b.id, parent_edit(Some(a.id.clone())))
        .await
        .unwrap();

    let err = edit_item(dir.path(), &a.id, parent_edit(Some(b.id)))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::ParentCycle { .. }), "got {err:?}");

    // The parent of a remains unset.
    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(repo.load(&a.id).await.unwrap().parent, None);
}

#[tokio::test]
async fn edit_parent_with_other_field_is_atomic_on_failure() {
    let dir = init_temp().await;
    let epic = add_item(dir.path(), "Epic", NewItem::default())
        .await
        .unwrap();
    let story = add_item(dir.path(), "Story", NewItem::default())
        .await
        .unwrap();

    // Parent is valid but title is empty → EmptyTitle. Parent changes are also not saved (atomicity).
    let edit = ItemEdit {
        title: Some("  ".to_string()),
        parent: Some(Some(epic.id.clone())),
        ..Default::default()
    };
    let err = edit_item(dir.path(), &story.id, edit).await.unwrap_err();
    assert!(matches!(err, Error::EmptyTitle), "got {err:?}");

    let repo = FileRepository::new(dir.path().join(".pinto"));
    assert_eq!(
        repo.load(&story.id).await.unwrap().parent,
        None,
        "parent must not be persisted when the edit fails"
    );
}

#[tokio::test]
async fn edit_parent_to_missing_parent_returns_not_found() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let err = edit_item(dir.path(), &a.id, parent_edit(Some(ItemId::new("T", 99))))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

// --- reorder ---

/// ID column in current backlog order (ascending rank).
async fn order(dir: &std::path::Path) -> Vec<ItemId> {
    list_items(dir, &ListFilter::default())
        .await
        .unwrap()
        .into_iter()
        .map(|it| it.id)
        .collect()
}

#[tokio::test]
async fn reorder_before_places_item_immediately_before_reference() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    let moved = reorder_item(dir.path(), &c.id, ReorderTarget::Before(b.id.clone()))
        .await
        .expect("reorder succeeds");

    assert_eq!(order(dir.path()).await, [a.id, c.id, b.id]);
    assert_eq!(moved.status, Status::new("todo"), "status unchanged");
}

#[tokio::test]
async fn reorder_after_places_item_immediately_after_reference() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    reorder_item(dir.path(), &a.id, ReorderTarget::After(b.id.clone()))
        .await
        .expect("reorder succeeds");

    assert_eq!(order(dir.path()).await, [b.id, a.id, c.id]);
}

#[tokio::test]
async fn reorder_top_moves_within_same_column_to_head() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    reorder_item(dir.path(), &c.id, ReorderTarget::Top)
        .await
        .expect("reorder succeeds");

    assert_eq!(order(dir.path()).await, [c.id, a.id, b.id]);
}

#[tokio::test]
async fn reorder_lone_item_is_noop_and_does_not_bump_updated() {
    let dir = init_temp().await;
    // Only you in the same state: Top / Bottom The position does not change.
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();

    let moved = reorder_item(dir.path(), &a.id, ReorderTarget::Top)
        .await
        .expect("reorder succeeds");

    assert_eq!(moved.rank, a.rank, "rank unchanged");
    assert_eq!(
        moved.updated, a.updated,
        "no-op reorder must not bump updated (no save)"
    );
}

#[tokio::test]
async fn reorder_bottom_moves_within_same_column_to_tail() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();

    reorder_item(dir.path(), &a.id, ReorderTarget::Bottom)
        .await
        .expect("reorder succeeds");

    assert_eq!(order(dir.path()).await, [b.id, c.id, a.id]);
}

#[tokio::test]
async fn reorder_top_is_scoped_to_same_status() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();
    // B to done. Todo columns are A and C.
    move_item(dir.path(), &b.id, "done").await.unwrap();

    // Move C to the top of todo column → C, A within todo. It does not affect the position of B(done).
    let moved = reorder_item(dir.path(), &c.id, ReorderTarget::Top)
        .await
        .expect("reorder succeeds");
    assert_eq!(moved.status, Status::new("todo"), "status unchanged");

    let todo: Vec<_> = list_items(
        dir.path(),
        &ListFilter {
            status: vec!["todo".to_string()],
            ..Default::default()
        },
    )
    .await
    .unwrap()
    .into_iter()
    .map(|it| it.id)
    .collect();
    assert_eq!(todo, [c.id, a.id]);
}

#[tokio::test]
async fn reorder_relative_to_self_is_rejected() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();

    let err = reorder_item(dir.path(), &a.id, ReorderTarget::Before(a.id.clone()))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::SelfReference(_)), "got {err:?}");
}

#[tokio::test]
async fn reorder_missing_reference_returns_not_found() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();

    let err = reorder_item(
        dir.path(),
        &a.id,
        ReorderTarget::After(ItemId::new("T", 99)),
    )
    .await
    .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

#[tokio::test]
async fn reorder_missing_target_returns_not_found() {
    let dir = init_temp().await;
    let err = reorder_item(dir.path(), &ItemId::new("T", 99), ReorderTarget::Top)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}

// --- rebalance ---

/// Overwrite and save the rank of `id` to any (normal) value (for reproducing the bloated state).
async fn set_rank(dir: &Path, id: &ItemId, rank: &str) {
    use crate::storage::FileRepository;
    let repo = FileRepository::new(dir.join(".pinto"));
    let mut item = repo.load(id).await.expect("load");
    item.rank = rank.parse().expect("valid rank");
    repo.save(&item).await.expect("save");
}

/// rank Returns the ID column in ascending order (for order verification).
async fn ids_in_rank_order(dir: &Path) -> Vec<ItemId> {
    list_items(dir, &ListFilter::default())
        .await
        .expect("list")
        .into_iter()
        .map(|it| it.id)
        .collect()
}

#[tokio::test]
async fn rebalance_shortens_ranks_and_preserves_order() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    let b = add_item(dir.path(), "B", NewItem::default()).await.unwrap();
    let c = add_item(dir.path(), "C", NewItem::default()).await.unwrap();
    // Expand the rank to a longer rank than the default while maintaining the sort order (a < b < c).
    set_rank(dir.path(), &a.id, "0i").await;
    set_rank(dir.path(), &b.id, "0j").await;
    set_rank(dir.path(), &c.id, "0k").await;

    let order_before = ids_in_rank_order(dir.path()).await;
    let outcome = rebalance(dir.path(), false).await.expect("rebalance");

    assert_eq!(outcome.total, 3);
    assert_eq!(outcome.changed, 3, "all three ranks shortened");
    assert!(
        outcome.after.max_len < outcome.before.max_len,
        "max rank length must shrink ({} -> {})",
        outcome.before.max_len,
        outcome.after.max_len
    );
    assert_eq!(
        ids_in_rank_order(dir.path()).await,
        order_before,
        "rebalance must preserve order"
    );
}

#[tokio::test]
async fn rebalance_is_scoped_to_status_and_parent_siblings() {
    let dir = init_temp().await;
    let root_a = add_item(dir.path(), "Root A", NewItem::default())
        .await
        .unwrap();
    let root_b = add_item(dir.path(), "Root B", NewItem::default())
        .await
        .unwrap();
    let child_a = add_item(
        dir.path(),
        "Child A",
        NewItem {
            parent: Some(root_a.id.clone()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();
    let child_b = add_item(
        dir.path(),
        "Child B",
        NewItem {
            parent: Some(root_a.id.clone()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();
    let other_status = add_item(dir.path(), "Other status", NewItem::default())
        .await
        .unwrap();
    move_item(dir.path(), &other_status.id, "in-progress")
        .await
        .unwrap();

    set_rank(dir.path(), &root_a.id, "0i").await;
    set_rank(dir.path(), &root_b.id, "0j").await;
    set_rank(dir.path(), &child_a.id, "0k").await;
    set_rank(dir.path(), &child_b.id, "0l").await;
    set_rank(dir.path(), &other_status.id, "i").await;

    let todo = ListFilter {
        status: vec!["todo".to_string()],
        ..ListFilter::default()
    };
    let todo_order_before: Vec<_> = list_items(dir.path(), &todo)
        .await
        .unwrap()
        .into_iter()
        .map(|item| item.id)
        .collect();
    let other_before = show_item(dir.path(), &other_status.id).await.unwrap();

    let outcome = rebalance(dir.path(), false).await.unwrap();

    assert_eq!(outcome.total, 5);
    assert_eq!(
        outcome.changed, 4,
        "only the two todo sibling groups change"
    );
    assert_eq!(
        list_items(dir.path(), &todo)
            .await
            .unwrap()
            .into_iter()
            .map(|item| item.id)
            .collect::<Vec<_>>(),
        todo_order_before,
        "each sibling group keeps its relative order"
    );

    for id in [&root_a.id, &root_b.id, &child_a.id, &child_b.id] {
        let item = show_item(dir.path(), id).await.unwrap();
        assert_eq!(item.rank.as_str().len(), 1);
    }
    let other_after = show_item(dir.path(), &other_status.id).await.unwrap();
    assert_eq!(other_after.rank, other_before.rank);
    assert_eq!(other_after.updated, other_before.updated);
}

#[tokio::test]
async fn rebalance_dry_run_reports_but_does_not_write() {
    let dir = init_temp().await;
    let a = add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    set_rank(dir.path(), &a.id, "0i").await;
    let before = show_item(dir.path(), &a.id).await.unwrap();

    let outcome = rebalance(dir.path(), true).await.expect("rebalance");

    assert_eq!(outcome.changed, 1, "would change one item");
    let after = show_item(dir.path(), &a.id).await.unwrap();
    assert_eq!(after.rank, before.rank, "dry-run must not touch rank");
    assert_eq!(
        after.updated, before.updated,
        "dry-run must not bump updated"
    );
}

#[tokio::test]
async fn rebalance_on_already_short_ranks_changes_nothing() {
    let dir = init_temp().await;
    // Newly added items already fit the shortest width for their sibling scope.
    add_item(dir.path(), "A", NewItem::default()).await.unwrap();
    add_item(dir.path(), "B", NewItem::default()).await.unwrap();

    let outcome = rebalance(dir.path(), false).await.expect("rebalance");
    assert_eq!(outcome.total, 2);
    assert_eq!(outcome.changed, 0, "already-balanced ranks are untouched");
}

// --- $EDITOR Edit (item_edit_template / apply_item_edit)---

#[tokio::test]
async fn editor_template_has_frontmatter_and_guidance() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Template me", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id)
        .await
        .expect("template");

    assert!(
        tpl.starts_with("+++\n"),
        "starts with frontmatter delimiter"
    );
    assert!(tpl.contains("# pinto:"), "includes guidance comment");
    assert!(tpl.contains("title = \"Template me\""));
    // It can be parsed even with guidance comments, and editable fields match (no changes) in a round trip.
    let outcome = apply_item_edit(dir.path(), &added.id, &tpl).await.unwrap();
    assert_eq!(outcome, EditOutcome::Unchanged);
}

#[tokio::test]
async fn editor_apply_updates_title_and_body_and_persists() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Before", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Before\"", "title = \"After\"");
    let edited = format!("{edited}\nRewritten body");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => {
            assert_eq!(item.title, "After");
            assert!(item.body.contains("Rewritten body"), "body applied");
            assert!(item.updated >= added.updated, "updated advanced");
        }
        other => panic!("expected Updated, got {other:?}"),
    }

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.title, "After");
    assert!(reloaded.body.contains("Rewritten body"));
}

#[tokio::test]
async fn editor_apply_without_changes_returns_unchanged_and_keeps_updated() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Same", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let outcome = apply_item_edit(dir.path(), &added.id, &tpl)
        .await
        .expect("apply");
    assert_eq!(outcome, EditOutcome::Unchanged);

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded.updated, added.updated, "updated not bumped");
}

#[tokio::test]
async fn editor_apply_rejects_invalid_content_and_leaves_item_unchanged() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Intact", NewItem::default())
        .await
        .unwrap();

    let err = apply_item_edit(dir.path(), &added.id, "not valid frontmatter\n")
        .await
        .unwrap_err();
    assert!(matches!(err, Error::EditorInvalid { .. }), "got {err:?}");

    let repo = FileRepository::new(dir.path().join(".pinto"));
    let reloaded = repo.load(&added.id).await.unwrap();
    assert_eq!(reloaded, added, "data untouched on invalid edit");
}

#[tokio::test]
async fn editor_apply_rejects_empty_title() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Has title", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Has title\"", "title = \"\"");
    let err = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::EditorInvalid { .. }), "got {err:?}");
}

#[tokio::test]
async fn editor_apply_ignores_managed_fields() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Managed", NewItem::default())
        .await
        .unwrap();

    // Even if you rewrite status / rank / id, it will not be reflected, only the editable title will be reflected.
    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl
        .replace("status = \"todo\"", "status = \"done\"")
        .replace(
            &format!("id = \"{}\"", added.id),
            &format!("id = \"{}-999\"", added.id.prefix()),
        )
        .replace("title = \"Managed\"", "title = \"Managed v2\"");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => {
            assert_eq!(item.id, added.id, "id preserved");
            assert_eq!(item.status, added.status, "status preserved");
            assert_eq!(item.rank, added.rank, "rank preserved");
            assert_eq!(item.title, "Managed v2", "title applied");
        }
        other => panic!("expected Updated, got {other:?}"),
    }
}

#[tokio::test]
async fn editor_apply_can_clear_optional_field() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let new = NewItem {
        sprint: Some("S-1".to_string()),
        ..NewItem::default()
    };
    let added = add_item(dir.path(), "Assigned", new).await.unwrap();

    // Delete sprint line = return to unset (editing not possible with field specification CLI).
    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    assert!(tpl.contains("sprint = \"S-1\""));
    let edited: String = tpl
        .lines()
        .filter(|l| !l.starts_with("sprint = "))
        .collect::<Vec<_>>()
        .join("\n");
    let edited = format!("{edited}\n");

    let outcome = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .expect("apply");
    match outcome {
        EditOutcome::Updated(item) => assert_eq!(item.sprint, None, "sprint cleared"),
        other => panic!("expected Updated, got {other:?}"),
    }
}

#[tokio::test]
async fn editor_apply_rejects_missing_sprint_without_saving() {
    let dir = init_temp().await;
    create_sprint_for_test(dir.path(), "S-1").await;
    let added = add_item(
        dir.path(),
        "Assigned",
        NewItem {
            sprint: Some("S-1".to_string()),
            ..NewItem::default()
        },
    )
    .await
    .unwrap();

    let template = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = template.replace("sprint = \"S-1\"", "sprint = \"S-9\"");
    let error = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(
        error,
        Error::EditorInvalid { message } if message.contains("sprint not found")
    ));

    let stored = show_item(dir.path(), &added.id).await.unwrap();
    assert_eq!(stored.sprint.as_deref(), Some("S-1"));
}

#[tokio::test]
async fn editor_apply_rejects_missing_parent() {
    let dir = init_temp().await;
    let added = add_item(dir.path(), "Child", NewItem::default())
        .await
        .unwrap();

    let tpl = item_edit_template(dir.path(), &added.id).await.unwrap();
    let edited = tpl.replace("title = \"Child\"", "title = \"Child\"\nparent = \"T-404\"");
    let err = apply_item_edit(dir.path(), &added.id, &edited)
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "got {err:?}");
}