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
use super::atomic_write;
use super::issued_ids::{max_number, record};
use super::markdown::{from_markdown, sprint_from_markdown, sprint_to_markdown, to_markdown};
use super::repository::{BacklogItemRepository, SprintRepository};
use crate::backlog::{BacklogItem, ItemId};
use crate::error::Error;
use crate::error::Result;
use crate::sprint::{Sprint, SprintId};
use rayon::prelude::*;
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use tokio::fs;
use tokio::task::JoinSet;

/// [`BacklogItemRepository`] and [`SprintRepository`] implementation backed by the `.pinto/` directory.
#[derive(Debug, Clone)]
pub struct FileRepository {
    root: PathBuf,
}

type ItemRecord = (PathBuf, BacklogItem);
type SprintRecord = (PathBuf, Sprint);

impl FileRepository {
    /// Build by specifying the board root (`.pinto/`). No file I/O is performed.
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into() }
    }

    /// Directory to place task files (`<root>/tasks`).
    #[must_use]
    pub fn tasks_dir(&self) -> PathBuf {
        self.root.join("tasks")
    }

    /// Directory to put sprint files (`<root>/sprints`).
    #[must_use]
    pub fn sprints_dir(&self) -> PathBuf {
        self.root.join("sprints")
    }

    /// The sprint file path for the specified ID (`<root>/sprints/<id>.md`).
    pub(crate) fn sprint_path_for(&self, id: &SprintId) -> PathBuf {
        self.sprints_dir().join(format!("{id}.md"))
    }

    /// Task file path for the specified ID (`<root>/tasks/<id>.md`).
    pub(crate) fn path_for(&self, id: &ItemId) -> Result<PathBuf> {
        self.safe_item_path(&self.tasks_dir(), id)
    }

    /// Destination directory (`<root>/archive`).
    fn archive_dir(&self) -> PathBuf {
        self.root.join("archive")
    }

    /// Archive file path for the specified ID (`<root>/archive/<id>.md`).
    fn archive_path_for(&self, id: &ItemId) -> Result<PathBuf> {
        self.safe_item_path(&self.archive_dir(), id)
    }

    /// Build one item path while checking that the ID contributes exactly one filename component.
    fn safe_item_path(&self, directory: &Path, id: &ItemId) -> Result<PathBuf> {
        let path = directory.join(format!("{id}.md"));
        if path.parent() != Some(directory) || !path.starts_with(directory) {
            return Err(Error::InvalidItemId(id.to_string()));
        }
        Ok(path)
    }
}

impl BacklogItemRepository for FileRepository {
    async fn save(&self, item: &BacklogItem) -> Result<()> {
        let (_, archived) = self.read_all_item_records().await?;
        let dir = self.tasks_dir();
        fs::create_dir_all(&dir)
            .await
            .map_err(|e| Error::io(&dir, &e))?;
        let path = self.path_for(&item.id)?;
        if let Some((archive_path, _)) =
            archived.iter().find(|(_, existing)| existing.id == item.id)
        {
            return Err(Error::parse(
                &path,
                format!(
                    "cannot save item `{}`: archive file {} already exists; remove the archived copy before restoring the item",
                    item.id,
                    archive_path.display()
                ),
            ));
        }
        record(&self.root, &item.id).await?;
        let text = to_markdown(item)?;
        atomic_write(&path, &text).await
    }

    async fn load(&self, id: &ItemId) -> Result<BacklogItem> {
        let (active, _) = self.read_all_item_records().await?;
        active
            .into_iter()
            .find_map(|(_, item)| (item.id == *id).then_some(item))
            .ok_or_else(|| Error::NotFound(id.clone()))
    }

    async fn list(&self) -> Result<Vec<BacklogItem>> {
        let (active, _) = self.read_all_item_records().await?;
        let mut items = active.into_iter().map(|(_, item)| item).collect::<Vec<_>>();

        // Canonical backlog order (rank asc, ID tie-break) shared with every view.
        items.sort_by(BacklogItem::backlog_cmp);
        Ok(items)
    }

    async fn delete(&self, id: &ItemId) -> Result<()> {
        self.read_all_item_records().await?;
        let path = self.path_for(id)?;
        match fs::remove_file(&path).await {
            Ok(()) => record(&self.root, id).await,
            Err(e) if e.kind() == io::ErrorKind::NotFound => Err(Error::NotFound(id.clone())),
            Err(e) => Err(Error::io(&path, &e)),
        }
    }

    async fn archive(&self, id: &ItemId) -> Result<PathBuf> {
        let src = self.path_for(id)?;
        let source_exists = fs::try_exists(&src)
            .await
            .map_err(|e| Error::io(&src, &e))?;
        let dest = self.archive_path_for(id)?;
        let destination_exists = fs::try_exists(&dest)
            .await
            .map_err(|e| Error::io(&dest, &e))?;
        if source_exists && destination_exists {
            return Err(Error::parse(
                &dest,
                format!(
                    "cannot archive `{id}`: destination already exists at {}; remove the archived copy before retrying",
                    dest.display()
                ),
            ));
        }
        self.read_all_item_records().await?;
        if !source_exists {
            return Err(Error::NotFound(id.clone()));
        }
        let archive_dir = self.archive_dir();
        fs::create_dir_all(&archive_dir)
            .await
            .map_err(|e| Error::io(&archive_dir, &e))?;

        // Rename after validation; map a source that disappears between validation and the move
        // to `NotFound` without allowing an existing destination to be replaced.
        match fs::rename(&src, &dest).await {
            Ok(()) => {
                record(&self.root, id).await?;
                Ok(dest)
            }
            Err(e) if e.kind() == io::ErrorKind::NotFound => Err(Error::NotFound(id.clone())),
            Err(e) => Err(Error::io(&src, &e)),
        }
    }

    async fn next_id(&self, prefix: &str) -> Result<ItemId> {
        // IDs are never reused after issuance. The history file also covers physically deleted IDs
        // and backend migrations; scanning both directories keeps older boards safe as well.
        let mut max = max_number(&self.root, prefix).await?;
        let (active, archived) = self.read_all_item_records().await?;
        let existing_max = active
            .iter()
            .chain(archived.iter())
            .map(|(_, item)| item.id.clone())
            .filter(|id| id.prefix() == prefix)
            .map(|id| id.number())
            .max()
            .unwrap_or(0);
        max = max.max(existing_max);
        let next = max
            .checked_add(1)
            .ok_or_else(|| Error::InvalidItemId(format!("{prefix}-{max}")))?;
        ItemId::try_new(prefix, next)
    }
}

impl SprintRepository for FileRepository {
    async fn save(&self, sprint: &Sprint) -> Result<()> {
        self.read_sprint_records().await?;
        let dir = self.sprints_dir();
        fs::create_dir_all(&dir)
            .await
            .map_err(|e| Error::io(&dir, &e))?;
        let path = self.sprint_path_for(&sprint.id);
        let text = sprint_to_markdown(sprint)?;
        atomic_write(&path, &text).await
    }

    async fn load(&self, id: &SprintId) -> Result<Sprint> {
        self.read_sprint_records()
            .await?
            .into_iter()
            .find_map(|(_, sprint)| (sprint.id == *id).then_some(sprint))
            .ok_or_else(|| Error::SprintNotFound(id.clone()))
    }

    async fn list(&self) -> Result<Vec<Sprint>> {
        let mut sprints = self
            .read_sprint_records()
            .await?
            .into_iter()
            .map(|(_, sprint)| sprint)
            .collect::<Vec<_>>();

        // Sort by creation time, using the ID as a deterministic tie-breaker.
        sprints.sort_by(|a, b| {
            a.created
                .cmp(&b.created)
                .then_with(|| a.id.as_str().cmp(b.id.as_str()))
        });
        Ok(sprints)
    }

    async fn delete(&self, id: &SprintId) -> Result<()> {
        self.read_sprint_records().await?;
        let path = self.sprint_path_for(id);
        match fs::remove_file(&path).await {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == io::ErrorKind::NotFound => Err(Error::SprintNotFound(id.clone())),
            Err(e) => Err(Error::io(&path, &e)),
        }
    }
}

impl FileRepository {
    /// Read and validate every active and archived item before exposing the active set.
    async fn read_all_item_records(&self) -> Result<(Vec<ItemRecord>, Vec<ItemRecord>)> {
        let active = self.read_item_records(&self.tasks_dir()).await?;
        let archived = self.read_item_records(&self.archive_dir()).await?;
        Self::ensure_unique_item_ids(active.iter().chain(archived.iter()))?;
        Ok((active, archived))
    }

    /// Read item files from one directory, validate their logical IDs, and retain their paths for
    /// actionable corruption diagnostics and cross-directory collision checks.
    async fn read_item_records(&self, dir: &Path) -> Result<Vec<ItemRecord>> {
        let Some(paths) = self.markdown_paths(dir).await? else {
            return Ok(Vec::new());
        };

        // Read all files concurrently; this is I/O-bound work.
        let mut reads = JoinSet::new();
        for path in paths {
            reads.spawn(async move {
                fs::read_to_string(&path)
                    .await
                    .map_err(|e| Error::io(&path, &e))
                    .map(|text| (path, text))
            });
        }
        let mut contents = Vec::new();
        while let Some(joined) = reads.join_next().await {
            contents.push(joined.map_err(Error::task)??);
        }

        // Parse frontmatter in parallel; this is CPU-bound work.
        let records = contents
            .into_par_iter()
            .map(|(path, text)| from_markdown(&text, &path).map(|item| (path, item)))
            .collect::<Result<Vec<_>>>()?;

        // Report duplicate logical IDs before filename mismatches so the error names both records
        // when a copied file creates an ambiguous lookup key.
        Self::ensure_unique_item_ids(records.iter())?;
        for (path, item) in &records {
            Self::validate_item_filename(path, item)?;
        }
        Ok(records)
    }

    /// Reject two files that resolve to the same logical item ID.
    fn ensure_unique_item_ids<'a>(records: impl IntoIterator<Item = &'a ItemRecord>) -> Result<()> {
        let mut seen = HashMap::new();
        for (path, item) in records {
            if let Some(previous) = seen.insert(item.id.clone(), path.clone()) {
                return Err(Error::parse(
                    path,
                    format!(
                        "duplicate item ID `{}` in {} and {}; fix one frontmatter ID or rename one file",
                        item.id,
                        previous.display(),
                        path.display()
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Ensure an item's filename stem and frontmatter ID describe the same record.
    fn validate_item_filename(path: &Path, item: &BacklogItem) -> Result<()> {
        let stem = path
            .file_stem()
            .and_then(|value| value.to_str())
            .ok_or_else(|| {
                Error::parse(
                    path,
                    "item filename must be a UTF-8 `<PREFIX>-<NUMBER>.md`; rename the file",
                )
            })?;
        let filename_id = stem.parse::<ItemId>().map_err(|error| {
            Error::parse(
                path,
                format!("invalid item filename `{stem}.md`: {error}; rename the file to `<ID>.md`"),
            )
        })?;
        if filename_id != item.id {
            return Err(Error::parse(
                path,
                format!(
                    "filename ID `{filename_id}` does not match frontmatter ID `{}`; rename the file or fix its frontmatter",
                    item.id
                ),
            ));
        }
        Ok(())
    }

    /// Read and validate every sprint file, retaining paths for collision diagnostics.
    async fn read_sprint_records(&self) -> Result<Vec<SprintRecord>> {
        let dir = self.sprints_dir();
        let Some(paths) = self.markdown_paths(&dir).await? else {
            return Ok(Vec::new());
        };

        let mut reads = JoinSet::new();
        for path in paths {
            reads.spawn(async move {
                fs::read_to_string(&path)
                    .await
                    .map_err(|e| Error::io(&path, &e))
                    .map(|text| (path, text))
            });
        }
        let mut contents = Vec::new();
        while let Some(joined) = reads.join_next().await {
            contents.push(joined.map_err(Error::task)??);
        }

        let records = contents
            .into_iter()
            .map(|(path, text)| sprint_from_markdown(&text, &path).map(|sprint| (path, sprint)))
            .collect::<Result<Vec<_>>>()?;
        Self::ensure_unique_sprint_ids(&records)?;
        for (path, sprint) in &records {
            Self::validate_sprint_filename(path, sprint)?;
        }
        Ok(records)
    }

    /// Reject two sprint files that resolve to the same logical sprint ID.
    fn ensure_unique_sprint_ids(records: &[SprintRecord]) -> Result<()> {
        let mut seen = HashMap::new();
        for (path, sprint) in records {
            if let Some(previous) = seen.insert(sprint.id.clone(), path.clone()) {
                return Err(Error::parse(
                    path,
                    format!(
                        "duplicate sprint ID `{}` in {} and {}; fix one frontmatter ID or rename one file",
                        sprint.id,
                        previous.display(),
                        path.display()
                    ),
                ));
            }
        }
        Ok(())
    }

    /// Ensure a sprint filename stem and frontmatter ID describe the same record.
    fn validate_sprint_filename(path: &Path, sprint: &Sprint) -> Result<()> {
        let stem = path
            .file_stem()
            .and_then(|value| value.to_str())
            .ok_or_else(|| {
                Error::parse(
                    path,
                    "sprint filename must be a UTF-8 `<ID>.md`; rename the file",
                )
            })?;
        let filename_id = stem.parse::<SprintId>().map_err(|error| {
            Error::parse(
                path,
                format!(
                    "invalid sprint filename `{stem}.md`: {error}; rename the file to `<ID>.md`"
                ),
            )
        })?;
        if filename_id != sprint.id {
            return Err(Error::parse(
                path,
                format!(
                    "filename ID `{filename_id}` does not match frontmatter ID `{}`; rename the file or fix its frontmatter",
                    sprint.id
                ),
            ));
        }
        Ok(())
    }

    /// Collect the `.md` file paths directly under `tasks/` (directory scanning is asynchronous).
    ///
    /// If the directory does not exist, return `None` to indicate that it has no items yet.
    async fn markdown_paths(&self, dir: &Path) -> Result<Option<Vec<PathBuf>>> {
        let mut read_dir = match fs::read_dir(dir).await {
            Ok(rd) => rd,
            Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(Error::io(dir, &e)),
        };

        let mut paths = Vec::new();
        while let Some(entry) = read_dir
            .next_entry()
            .await
            .map_err(|e| Error::io(dir, &e))?
        {
            let path = entry.path();
            if Self::is_markdown(&path) {
                paths.push(path);
            }
        }
        Ok(Some(paths))
    }

    /// Return whether `path` has the `.md` extension.
    fn is_markdown(path: &Path) -> bool {
        path.extension().and_then(|s| s.to_str()) == Some("md")
    }
}

#[cfg(test)]
mod tests {
    //! Persistence tests for `FileRepository`.
    //!
    //! Each test uses a `tempfile` directory so it cannot modify the real file system. The I/O
    //! layer is asynchronous (`tokio`), so tests use `#[tokio::test]`.

    use super::{BacklogItemRepository, FileRepository, SprintRepository};
    use crate::backlog::{BacklogItem, ItemId, Status};
    use crate::error::Error;
    use crate::rank::Rank;
    use crate::sprint::{Sprint, SprintId};
    use chrono::{DateTime, TimeZone, Utc};
    use tempfile::TempDir;
    use tokio::fs;
    fn ts(secs: i64) -> DateTime<Utc> {
        Utc.timestamp_opt(secs, 0)
            .single()
            .expect("valid timestamp")
    }

    /// Create a temporary directory and a repository rooted at `.pinto` inside it.
    fn repo() -> (TempDir, FileRepository) {
        let dir = TempDir::new().expect("create temp dir");
        let repo = FileRepository::new(dir.path().join(".pinto"));
        (dir, repo)
    }

    /// Generate `count` monotonically increasing ranks in the order of insertion.
    fn ranks(count: usize) -> Vec<Rank> {
        let mut out = Vec::with_capacity(count);
        let mut prev: Option<Rank> = None;
        for _ in 0..count {
            let next = Rank::after(prev.as_ref());
            prev = Some(next.clone());
            out.push(next);
        }
        out
    }

    /// Create a minimal item with the specified ID and rank.
    fn item(n: u32, rank: Rank) -> BacklogItem {
        BacklogItem::new(
            ItemId::new("T", n),
            format!("Item {n}"),
            Status::new("todo"),
            rank,
            ts(1_000),
        )
        .expect("valid item")
    }

    fn sample_item() -> BacklogItem {
        let mut item = BacklogItem::new(
            ItemId::new("T", 1),
            "Implement storage layer",
            Status::new("todo"),
            Rank::after(None),
            ts(1_000),
        )
        .expect("valid item");
        item.points = Some(5);
        item.labels = vec!["storage".to_string(), "cli".to_string()];
        item.assignee = Some("alice".to_string());
        item.sprint = Some("S-1".to_string());
        item.parent = Some(ItemId::new("T", 0));
        item.depends_on = vec![ItemId::new("T", 2), ItemId::new("T", 3)];
        item.updated = ts(2_000);
        item.body = "## 説明\n本文の Markdown。\n\n- [ ] 受け入れ条件".to_string();
        item
    }

    #[tokio::test]
    async fn save_then_load_roundtrips_all_fields() {
        let (_dir, repo) = repo();
        let item = sample_item();

        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save succeeds");
        let loaded = BacklogItemRepository::load(&repo, &item.id)
            .await
            .expect("load succeeds");

        assert_eq!(loaded, item);
    }

    #[tokio::test]
    async fn save_writes_toml_frontmatter_file() {
        let (_dir, repo) = repo();
        let item = sample_item();

        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save succeeds");

        let path = repo.tasks_dir().join("T-1.md");
        let text = fs::read_to_string(&path).await.expect("file exists");
        assert!(text.starts_with("+++\n"), "should open with TOML delimiter");
        assert!(text.contains("id = \"T-1\""), "frontmatter carries id");
        assert!(
            text.contains("status = \"todo\""),
            "frontmatter carries status"
        );
        assert!(text.contains("rank = "), "frontmatter carries rank");
        assert!(
            text.contains("depends_on = [\"T-2\", \"T-3\"]"),
            "frontmatter carries dependencies"
        );
        assert!(
            text.contains("## 説明"),
            "body is preserved after frontmatter"
        );
    }

    #[tokio::test]
    async fn load_missing_item_returns_not_found() {
        let (_dir, repo) = repo();

        let err = BacklogItemRepository::load(&repo, &ItemId::new("T", 99))
            .await
            .expect_err("should be missing");
        assert_eq!(err, Error::NotFound(ItemId::new("T", 99)));
    }

    #[tokio::test]
    async fn list_returns_all_items_sorted_by_rank() {
        let (_dir, repo) = repo();
        // Give items ranks that differ from their ID order and verify that rank determines the result.
        let order = [3u32, 1, 10, 2];
        let rs = ranks(order.len());
        for (n, rank) in order.iter().zip(rs) {
            BacklogItemRepository::save(&repo, &item(*n, rank))
                .await
                .expect("save succeeds");
        }

        let items = BacklogItemRepository::list(&repo)
            .await
            .expect("list succeeds");
        let ids: Vec<u32> = items.iter().map(|i| i.id.number()).collect();
        assert_eq!(ids, order.to_vec(), "rank 昇順(= 割当順)で返る");
    }

    #[tokio::test]
    async fn list_rejects_filename_frontmatter_id_mismatch() {
        let (_dir, repo) = repo();
        BacklogItemRepository::save(&repo, &item(2, Rank::after(None)))
            .await
            .expect("save fixture");
        fs::rename(
            repo.tasks_dir().join("T-2.md"),
            repo.tasks_dir().join("T-1.md"),
        )
        .await
        .expect("rename corrupt fixture");

        let err = BacklogItemRepository::list(&repo)
            .await
            .expect_err("filename/frontmatter mismatch must fail fast");
        let message = err.to_string();
        assert!(message.contains("filename"), "got {message}");
        assert!(
            message.contains("T-1") && message.contains("T-2"),
            "got {message}"
        );
    }

    #[tokio::test]
    async fn list_rejects_duplicate_logical_ids() {
        let (_dir, repo) = repo();
        let ranks = ranks(2);
        BacklogItemRepository::save(&repo, &item(1, ranks[0].clone()))
            .await
            .expect("save first fixture");
        BacklogItemRepository::save(&repo, &item(2, ranks[1].clone()))
            .await
            .expect("save second fixture");
        fs::copy(
            repo.tasks_dir().join("T-1.md"),
            repo.tasks_dir().join("T-2.md"),
        )
        .await
        .expect("copy duplicate fixture");

        let err = BacklogItemRepository::list(&repo)
            .await
            .expect_err("duplicate logical IDs must fail fast");
        let message = err.to_string();
        assert!(
            message.contains("duplicate") && message.contains("T-1"),
            "got {message}"
        );
    }

    #[tokio::test]
    async fn list_parallelizes_many_items_and_keeps_order() {
        // Verify that concurrent reads and rayon parsing preserve the results for many files.
        let (_dir, repo) = repo();
        let n = 200usize;
        let rs = ranks(n);
        for (i, rank) in (1..=n).zip(rs) {
            BacklogItemRepository::save(&repo, &item(i as u32, rank))
                .await
                .expect("save succeeds");
        }

        let items = BacklogItemRepository::list(&repo)
            .await
            .expect("list succeeds");
        let ids: Vec<u32> = items.iter().map(|i| i.id.number()).collect();
        let expected: Vec<u32> = (1..=n as u32).collect();
        assert_eq!(ids, expected, "並列読込・パースでも rank 昇順を保つ");
    }

    #[tokio::test]
    async fn list_on_uninitialized_dir_is_empty_not_error() {
        let (_dir, repo) = repo();
        let items = BacklogItemRepository::list(&repo)
            .await
            .expect("list must not error on missing dir");
        assert!(items.is_empty());
    }

    #[tokio::test]
    async fn delete_removes_file_and_missing_delete_errors() {
        let (_dir, repo) = repo();
        let item = sample_item();
        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save succeeds");

        BacklogItemRepository::delete(&repo, &item.id)
            .await
            .expect("delete succeeds");
        assert_eq!(
            BacklogItemRepository::load(&repo, &item.id)
                .await
                .expect_err("gone"),
            Error::NotFound(item.id.clone())
        );
        assert_eq!(
            BacklogItemRepository::delete(&repo, &item.id)
                .await
                .expect_err("already gone"),
            Error::NotFound(item.id)
        );
    }

    #[tokio::test]
    async fn next_id_does_not_reuse_a_physically_deleted_id() {
        let (_dir, repo) = repo();
        let item = sample_item();
        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save succeeds");
        BacklogItemRepository::delete(&repo, &item.id)
            .await
            .expect("delete succeeds");

        assert_eq!(
            repo.next_id("T").await.expect("next id"),
            ItemId::new("T", 2),
            "a physically deleted ID must remain reserved"
        );
    }

    #[tokio::test]
    async fn archive_moves_file_out_of_tasks_and_missing_archive_errors() {
        let (_dir, repo) = repo();
        let item = sample_item();
        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save succeeds");

        let dest = repo.archive(&item.id).await.expect("archive succeeds");

        // The item is no longer present in `tasks/`.
        assert_eq!(
            BacklogItemRepository::load(&repo, &item.id)
                .await
                .expect_err("gone from tasks"),
            Error::NotFound(item.id.clone())
        );
        // The archived file exists at the destination.
        assert!(dest.is_file(), "archived file exists at {dest:?}");
        assert!(
            dest.ends_with("archive/T-1.md"),
            "archived under archive dir: {dest:?}"
        );
        // Archiving the same item again returns `NotFound`.
        assert_eq!(
            BacklogItemRepository::archive(&repo, &item.id)
                .await
                .expect_err("already archived"),
            Error::NotFound(item.id)
        );
    }

    #[tokio::test]
    async fn next_id_increments_from_max_and_defaults_to_one() {
        let (_dir, repo) = repo();

        assert_eq!(repo.next_id("T").await.expect("empty"), ItemId::new("T", 1));

        for (n, rank) in [1u32, 2, 7].into_iter().zip(ranks(3)) {
            BacklogItemRepository::save(&repo, &item(n, rank))
                .await
                .expect("save succeeds");
        }
        assert_eq!(repo.next_id("T").await.expect("next"), ItemId::new("T", 8));
        // Different prefixes are numbered independently.
        assert_eq!(
            repo.next_id("BUG").await.expect("next"),
            ItemId::new("BUG", 1)
        );
    }

    #[tokio::test]
    async fn next_id_does_not_reuse_archived_ids() {
        // Archived IDs must not be reused, so `next_id` scans both `tasks/` and `archive/`.
        let (_dir, repo) = repo();

        // Create T-1, then archive it; it leaves `tasks/`.
        BacklogItemRepository::save(&repo, &item(1, ranks(1).remove(0)))
            .await
            .expect("save succeeds");
        repo.archive(&ItemId::new("T", 1))
            .await
            .expect("archive succeeds");

        // The next ID is T-2 because `archive/T-1.md` remains reserved.
        assert_eq!(
            repo.next_id("T").await.expect("next"),
            ItemId::new("T", 2),
            "archived id must not be reused"
        );
    }

    #[tokio::test]
    async fn archive_rejects_an_existing_destination_without_overwriting_it() {
        let (_dir, repo) = repo();
        let item = item(1, Rank::after(None));
        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save fixture");
        fs::create_dir_all(repo.archive_dir())
            .await
            .expect("create archive dir");
        fs::copy(
            repo.tasks_dir().join("T-1.md"),
            repo.archive_dir().join("T-1.md"),
        )
        .await
        .expect("create archive collision");

        let err = BacklogItemRepository::archive(&repo, &item.id)
            .await
            .expect_err("archive collision must fail fast");
        assert!(err.to_string().contains("already exists"), "got {err}");
        assert!(repo.tasks_dir().join("T-1.md").is_file());
        assert!(repo.archive_dir().join("T-1.md").is_file());
    }

    #[tokio::test]
    async fn save_rejects_an_archived_duplicate_before_creating_an_active_file() {
        let (_dir, repo) = repo();
        let item = item(1, Rank::after(None));
        BacklogItemRepository::save(&repo, &item)
            .await
            .expect("save fixture");
        BacklogItemRepository::archive(&repo, &item.id)
            .await
            .expect("archive fixture");

        let err = BacklogItemRepository::save(&repo, &item)
            .await
            .expect_err("saving an archived duplicate must fail fast");
        assert!(err.to_string().contains("already exists"), "got {err}");
        assert!(!repo.tasks_dir().join("T-1.md").exists());
        assert!(repo.archive_dir().join("T-1.md").is_file());
    }

    #[tokio::test]
    async fn next_id_rejects_filename_frontmatter_id_mismatch() {
        let (_dir, repo) = repo();
        BacklogItemRepository::save(&repo, &item(2, Rank::after(None)))
            .await
            .expect("save fixture");
        fs::rename(
            repo.tasks_dir().join("T-2.md"),
            repo.tasks_dir().join("T-1.md"),
        )
        .await
        .expect("rename corrupt fixture");

        let err = repo
            .next_id("T")
            .await
            .expect_err("next_id must validate existing records before allocating");
        assert!(err.to_string().contains("filename"), "got {err}");
    }

    #[tokio::test]
    async fn next_id_rejects_number_overflow_in_existing_files() {
        let (_dir, repo) = repo();
        let tasks = repo.tasks_dir();
        fs::create_dir_all(&tasks).await.expect("create tasks dir");
        let maximum = item(u32::MAX, Rank::after(None));
        let text = crate::storage::markdown::to_markdown(&maximum).expect("serialize maximum ID");
        fs::write(tasks.join("T-4294967295.md"), text)
            .await
            .expect("write maximum id fixture");

        let err = repo
            .next_id("T")
            .await
            .expect_err("the next id must not wrap around");
        assert!(err.to_string().contains("T-4294967295"));
    }

    #[tokio::test]
    async fn load_tolerates_crlf_delimiters() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        // Files edited on Windows with CRLF line endings remain readable.
        fs::write(
            dir.join("T-1.md"),
            "+++\r\nid = \"T-1\"\r\ntitle = \"CRLF\"\r\nstatus = \"todo\"\r\nrank = \"i\"\r\ncreated = \"1970-01-01T00:00:00Z\"\r\nupdated = \"1970-01-01T00:00:00Z\"\r\n+++\r\n",
        )
        .await
        .expect("write");

        let item = BacklogItemRepository::load(&repo, &ItemId::new("T", 1))
            .await
            .expect("load succeeds");
        assert_eq!(item.title, "CRLF");
    }

    #[tokio::test]
    async fn corrupt_frontmatter_returns_parse_error_without_panic() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        // The closing delimiter is present, but the frontmatter is invalid TOML.
        fs::write(dir.join("T-1.md"), "+++\nid = \nbroken\n+++\n\nbody")
            .await
            .expect("write");

        let err = BacklogItemRepository::load(&repo, &ItemId::new("T", 1))
            .await
            .expect_err("should fail to parse");
        assert!(matches!(err, Error::Parse { .. }), "got {err:?}");
    }

    #[tokio::test]
    async fn unsafe_frontmatter_id_is_rejected_before_file_backend_uses_it() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        fs::write(
            dir.join("T-1.md"),
            r#"+++
id = "../outside-1"
title = "Unsafe"
status = "todo"
rank = "i"
created = "1970-01-01T00:00:00Z"
updated = "1970-01-01T00:00:00Z"
+++
"#,
        )
        .await
        .expect("write unsafe frontmatter");

        let err = BacklogItemRepository::list(&repo)
            .await
            .expect_err("unsafe frontmatter ID must be rejected");
        assert!(err.to_string().contains("invalid item id"), "got {err:?}");
    }

    #[tokio::test]
    async fn missing_frontmatter_delimiter_returns_error_without_panic() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        fs::write(dir.join("T-1.md"), "no frontmatter here\njust text")
            .await
            .expect("write");

        let err = BacklogItemRepository::load(&repo, &ItemId::new("T", 1))
            .await
            .expect_err("should fail");
        assert!(
            matches!(err, Error::MissingFrontmatter { .. }),
            "got {err:?}"
        );
    }

    #[tokio::test]
    async fn empty_title_on_load_returns_parse_error() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        // All required fields are present, but an empty title violates the model invariant.
        fs::write(
            dir.join("T-1.md"),
            "+++\nid = \"T-1\"\ntitle = \"\"\nstatus = \"todo\"\nrank = \"i\"\ncreated = \"1970-01-01T00:00:00Z\"\nupdated = \"1970-01-01T00:00:00Z\"\n+++\n",
        )
        .await
        .expect("write");

        let err = BacklogItemRepository::load(&repo, &ItemId::new("T", 1))
            .await
            .expect_err("should fail");
        assert!(matches!(err, Error::Parse { .. }), "got {err:?}");
    }

    #[tokio::test]
    async fn missing_required_field_returns_parse_error() {
        let (_dir, repo) = repo();
        let dir = repo.tasks_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        // Required fields such as `title` are missing.
        fs::write(dir.join("T-1.md"), "+++\nid = \"T-1\"\n+++\n\nbody")
            .await
            .expect("write");

        let err = BacklogItemRepository::load(&repo, &ItemId::new("T", 1))
            .await
            .expect_err("should fail");
        assert!(matches!(err, Error::Parse { .. }), "got {err:?}");
    }

    // --- Sprint persistence ---

    /// A sample sprint with goals, dates, and status.
    fn sample_sprint() -> Sprint {
        let mut s = Sprint::new(SprintId::new("S-1").unwrap(), "Sprint 1", ts(1_000)).unwrap();
        s.goal = "## ゴール\n\nログイン機能を完成させる".to_string();
        s.start = Some(ts(2_000));
        s.end = Some(ts(9_000));
        s.start(ts(2_000)).expect("planned -> active");
        s
    }

    #[tokio::test]
    async fn save_then_load_roundtrips_sprint() {
        let (_dir, repo) = repo();
        let sprint = sample_sprint();

        SprintRepository::save(&repo, &sprint)
            .await
            .expect("save succeeds");
        let loaded = SprintRepository::load(&repo, &sprint.id)
            .await
            .expect("load succeeds");

        assert_eq!(loaded, sprint);
    }

    #[tokio::test]
    async fn save_then_load_roundtrips_default_sprint() {
        // The default sprint (no schedule or goal, still planned) also round-trips without data loss.
        let (_dir, repo) = repo();
        let sprint = Sprint::new(SprintId::new("S-1").unwrap(), "Sprint 1", ts(1_000)).unwrap();

        SprintRepository::save(&repo, &sprint)
            .await
            .expect("save succeeds");
        let loaded = SprintRepository::load(&repo, &sprint.id)
            .await
            .expect("load succeeds");

        assert_eq!(loaded, sprint);
        assert_eq!(loaded.start, None);
        assert_eq!(loaded.end, None);
        assert_eq!(loaded.goal, "");
    }

    #[tokio::test]
    async fn sprint_frontmatter_carries_fields_and_goal_body() {
        let (_dir, repo) = repo();
        let sprint = sample_sprint();
        SprintRepository::save(&repo, &sprint)
            .await
            .expect("save succeeds");

        let text = fs::read_to_string(repo.sprints_dir().join("S-1.md"))
            .await
            .expect("read file");
        assert!(text.contains("id = \"S-1\""), "frontmatter carries id");
        assert!(
            text.contains("title = \"Sprint 1\""),
            "frontmatter carries title"
        );
        assert!(
            text.contains("state = \"active\""),
            "frontmatter carries state"
        );
        let frontmatter = text.split("+++\n").nth(1).expect("frontmatter exists");
        assert!(
            !frontmatter.contains("sprint_goal"),
            "goal is not a frontmatter field"
        );
        assert!(text.contains("## ゴール"), "goal is stored as body");
    }

    #[tokio::test]
    async fn load_missing_sprint_returns_not_found() {
        let (_dir, repo) = repo();
        let id = SprintId::new("S-99").unwrap();

        let err = SprintRepository::load(&repo, &id)
            .await
            .expect_err("should be missing");
        assert_eq!(err, Error::SprintNotFound(id));
    }

    #[tokio::test]
    async fn delete_sprint_removes_and_missing_is_not_found() {
        let (_dir, repo) = repo();
        let s = Sprint::new(SprintId::new("S-1").unwrap(), "S1", ts(1_000)).unwrap();
        SprintRepository::save(&repo, &s).await.expect("save");
        SprintRepository::delete(&repo, &s.id)
            .await
            .expect("delete");
        assert!(matches!(
            SprintRepository::load(&repo, &s.id).await,
            Err(Error::SprintNotFound(_))
        ));
        assert!(matches!(
            SprintRepository::delete(&repo, &s.id).await,
            Err(Error::SprintNotFound(_))
        ));
    }

    #[tokio::test]
    async fn list_sprints_returns_all_in_creation_order() {
        let (_dir, repo) = repo();
        // The list is oldest-first even when files are saved in a different order.
        for (id, secs) in [("S-3", 3_000i64), ("S-1", 1_000), ("S-2", 2_000)] {
            let s = Sprint::new(SprintId::new(id).unwrap(), id, ts(secs)).unwrap();
            SprintRepository::save(&repo, &s)
                .await
                .expect("save succeeds");
        }

        let ids: Vec<String> = SprintRepository::list(&repo)
            .await
            .expect("list succeeds")
            .into_iter()
            .map(|s| s.id.as_str().to_string())
            .collect();
        assert_eq!(ids, ["S-1", "S-2", "S-3"]);
    }

    #[tokio::test]
    async fn list_sprints_rejects_filename_frontmatter_id_mismatch() {
        let (_dir, repo) = repo();
        let sprint =
            Sprint::new(SprintId::new("S-1").unwrap(), "Sprint 1", ts(1_000)).expect("sprint");
        SprintRepository::save(&repo, &sprint)
            .await
            .expect("save fixture");
        fs::rename(
            repo.sprints_dir().join("S-1.md"),
            repo.sprints_dir().join("S-2.md"),
        )
        .await
        .expect("rename corrupt fixture");

        let err = SprintRepository::list(&repo)
            .await
            .expect_err("sprint filename/frontmatter mismatch must fail fast");
        let message = err.to_string();
        assert!(message.contains("filename"), "got {message}");
        assert!(
            message.contains("S-1") && message.contains("S-2"),
            "got {message}"
        );
    }

    #[tokio::test]
    async fn list_sprints_rejects_duplicate_logical_ids() {
        let (_dir, repo) = repo();
        for (id, title) in [("S-1", "First"), ("S-2", "Second")] {
            let sprint = Sprint::new(SprintId::new(id).unwrap(), title, ts(1_000)).expect("sprint");
            SprintRepository::save(&repo, &sprint)
                .await
                .expect("save fixture");
        }
        fs::copy(
            repo.sprints_dir().join("S-1.md"),
            repo.sprints_dir().join("S-2.md"),
        )
        .await
        .expect("copy duplicate fixture");

        let err = SprintRepository::list(&repo)
            .await
            .expect_err("duplicate sprint IDs must fail fast");
        let message = err.to_string();
        assert!(
            message.contains("duplicate") && message.contains("S-1"),
            "got {message}"
        );
    }

    #[tokio::test]
    async fn list_sprints_on_empty_board_returns_empty() {
        let (_dir, repo) = repo();
        assert!(
            BacklogItemRepository::list(&repo)
                .await
                .expect("list succeeds")
                .is_empty()
        );
    }

    #[tokio::test]
    async fn corrupt_sprint_state_returns_parse_error() {
        let (_dir, repo) = repo();
        let dir = repo.sprints_dir();
        fs::create_dir_all(&dir).await.expect("mkdir");
        // An unknown state is reported as a parse error without panicking.
        fs::write(
            dir.join("S-1.md"),
            "+++\nid = \"S-1\"\ntitle = \"Sprint 1\"\nstate = \"archived\"\ncreated = \"1970-01-01T00:00:00Z\"\nupdated = \"1970-01-01T00:00:00Z\"\n+++\n",
        )
        .await
        .expect("write");

        let err = SprintRepository::load(&repo, &SprintId::new("S-1").unwrap())
            .await
            .expect_err("should fail");
        assert!(matches!(err, Error::Parse { .. }), "got {err:?}");
    }
}