timebomb-cli 0.9.0

Scan source code for deadline-tagged fuses and fail when they detonate
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
//! Logic for the `timebomb plant` subcommand.
//!
//! This module implements the core logic for inserting a timebomb fuse into a
//! source file at a specific line. It is intentionally defined with primitive
//! parameters so that it compiles independently of any `PlantArgs` struct
//! changes in `cli.rs`.

use crate::error::{Error, Result};
use chrono::NaiveDate;
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Core logic for `timebomb plant`.
///
/// All parameters are primitives so this compiles independently of `cli.rs`
/// changes.
///
/// # Parameters
/// - `target`   — `"path/to/file.rs:42"` when search is None; plain file path when search is Some
/// - `tag`      — tag keyword, e.g. `"TODO"`
/// - `owner`    — optional owner name
/// - `date_str` — optional `"YYYY-MM-DD"` expiry date
/// - `in_days`  — optional number of days from `today` until expiry
/// - `yes`      — skip confirmation prompt when `true`
/// - `message`  — annotation message text
/// - `today`    — the current date (injected for testability)
/// - `search`   — optional pattern; when Some, `target` is a plain file path
#[allow(clippy::too_many_arguments)]
pub fn run_add(
    target: &str,
    tag: &str,
    owner: Option<&str>,
    date_str: Option<&str>,
    in_days: Option<u32>,
    yes: bool,
    message: &str,
    today: NaiveDate,
    search: Option<&str>,
) -> Result<i32> {
    // 1. Resolve file path and line number -----------------------------------
    let (file_path, line_number) = if let Some(pattern) = search {
        let path = PathBuf::from(target);
        let matches = find_matching_lines(&path, pattern)?;
        match matches.len() {
            0 => {
                return Err(Error::InvalidArgument(format!(
                    "no lines matching '{}' found in {}",
                    pattern, target
                )));
            }
            1 => {
                println!("matched line {}: {}", matches[0].0, matches[0].1.trim_end());
                (path, matches[0].0)
            }
            n => {
                let mut detail =
                    format!("pattern '{}' matched {} lines in {}:", pattern, n, target);
                for (ln, content) in &matches {
                    detail.push_str(&format!("\n  line {}: {}", ln, content.trim_end()));
                }
                detail.push_str("\nuse FILE:LINE to be specific");
                return Err(Error::InvalidArgument(detail));
            }
        }
    } else {
        parse_target(target)?
    };

    // 2. Resolve the expiry date ---------------------------------------------
    let expiry = resolve_date(date_str, in_days, today, yes)?;

    // Warn if the date is already in the past — the fuse will immediately detonate.
    if expiry < today {
        eprintln!(
            "warning: expiry date {} is already in the past — this fuse will detonate immediately",
            expiry.format("%Y-%m-%d")
        );
    }

    // 3. Detect comment style ------------------------------------------------
    let prefix = detect_comment_style(&file_path);

    // 4. Build the annotation string -----------------------------------------
    let annotation = build_annotation(prefix, tag, expiry, owner, message);

    // 5. Read the file -------------------------------------------------------
    let content = std::fs::read_to_string(&file_path).map_err(|e| Error::Io {
        source: e,
        path: Some(file_path.clone()),
    })?;
    let had_trailing_newline = content.ends_with('\n');

    // 6. Validate line number ------------------------------------------------
    let lines: Vec<&str> = content.lines().collect();
    let line_count = lines.len();

    // line_number is 1-based; allow inserting at line_count + 1 (append)
    if line_number < 1 || line_number > line_count + 1 {
        return Err(Error::InvalidArgument(format!(
            "line number {} is out of range for '{}' ({} lines); \
             must be between 1 and {}",
            line_number,
            file_path.display(),
            line_count,
            line_count + 1,
        )));
    }

    // 7. Build the new file content ------------------------------------------
    let mut new_content = insert_line(&lines, line_number, &annotation);
    // insert_line always appends a trailing newline; strip it if the original
    // file did not have one so we don't alter the file's newline convention.
    if !had_trailing_newline {
        new_content.pop();
    }

    // 8. Print a diff --------------------------------------------------------
    println!("+ {}:{}  {}", file_path.display(), line_number, annotation);

    // 9. Prompt for confirmation (unless --yes) ------------------------------
    if !yes {
        print!("Write change? [y/N]: ");
        // Flush so the prompt appears before we block on stdin.
        io::stdout().flush().map_err(|e| Error::Io {
            source: e,
            path: None,
        })?;

        let stdin = io::stdin();
        let mut line_buf = String::new();
        stdin
            .lock()
            .read_line(&mut line_buf)
            .map_err(|e| Error::Io {
                source: e,
                path: None,
            })?;

        let response = line_buf.trim();
        if response != "y" && response != "Y" {
            // User cancelled — not an error.
            return Ok(0);
        }
    }

    // 10. Write the file atomically ------------------------------------------
    // Write to a sibling temp file then rename so a mid-write crash never
    // leaves a partially-written source file.
    let tmp_path = file_path.with_extension(format!("tmp.{}", std::process::id()));
    std::fs::write(&tmp_path, &new_content).map_err(|e| Error::Io {
        source: e,
        path: Some(tmp_path.clone()),
    })?;
    std::fs::rename(&tmp_path, &file_path).map_err(|e| Error::Io {
        source: e,
        path: Some(file_path.clone()),
    })?;

    // 11. Print confirmation -------------------------------------------------
    println!("wrote {}:{}", file_path.display(), line_number);

    // 12. Return success -----------------------------------------------------
    Ok(0)
}

// ---------------------------------------------------------------------------
// Helper: parse_target
// ---------------------------------------------------------------------------

/// Parse `"path/to/file.rs:42"` into `(PathBuf, usize)`.
///
/// Accepts optional column and trailing editor context after the line number:
/// - `src/foo.rs:42`
/// - `src/foo.rs:42:7`
/// - `src/foo.rs:42:7: some editor context`
///
/// Splits on the *last* `:` so that Windows absolute paths (`C:\foo\bar.rs:5`)
/// are handled correctly as long as the user puts the colon-number at the end.
pub fn parse_target(target: &str) -> Result<(PathBuf, usize)> {
    // Find the last colon
    let last_colon = target.rfind(':').ok_or_else(|| {
        Error::InvalidArgument(format!(
            "target '{}' must be in the form 'file:LINE' (e.g. src/main.rs:42)",
            target
        ))
    })?;

    let last_segment = &target[last_colon + 1..];

    // Check if the last segment is purely digits (possibly with trailing spaces)
    // If it is, it might be a column number — look back further.
    if last_segment.trim().chars().all(|c| c.is_ascii_digit()) && !last_segment.trim().is_empty() {
        // Could be file:line or file:line:col
        // Check if there is another colon before this position
        let before_last = &target[..last_colon];
        if let Some(prev_colon) = before_last.rfind(':') {
            let prev_segment = &before_last[prev_colon + 1..];
            // If prev_segment is also purely digits, treat it as the line number
            // and last_segment as the column
            if prev_segment.trim().chars().all(|c| c.is_ascii_digit())
                && !prev_segment.trim().is_empty()
            {
                let file_part = &before_last[..prev_colon];
                let line_part = prev_segment.trim();

                if file_part.is_empty() {
                    return Err(Error::InvalidArgument(format!(
                        "target '{}': file path is empty",
                        target
                    )));
                }

                let line_number: usize = line_part.parse().map_err(|_| {
                    Error::InvalidArgument(format!(
                        "target '{}': '{}' is not a valid line number",
                        target, line_part
                    ))
                })?;

                if line_number == 0 {
                    return Err(Error::InvalidArgument(format!(
                        "target '{}': line number must be >= 1",
                        target
                    )));
                }

                return Ok((PathBuf::from(file_part), line_number));
            }
        }
        // Fall through: the last segment is a line number (no col present)
    } else if !last_segment.trim().is_empty() {
        // Last segment starts with digits but has trailing non-digit content
        // e.g. "42:7: some editor context" — walk back to find the line number
        // Actually handle: "file:42:7: some text" where last colon is before "some text"
        // but last_segment is not purely digits.
        // Try: strip trailing text after space/colon, find line number in second-to-last numeric segment
        let before_last = &target[..last_colon];
        if let Some(prev_colon) = before_last.rfind(':') {
            let prev_segment = &before_last[prev_colon + 1..];
            if prev_segment.trim().chars().all(|c| c.is_ascii_digit())
                && !prev_segment.trim().is_empty()
            {
                // prev_segment is purely digits — check if the segment before it is also digits (col)
                let before_prev = &before_last[..prev_colon];
                if let Some(pp_colon) = before_prev.rfind(':') {
                    let pp_segment = &before_prev[pp_colon + 1..];
                    if pp_segment.trim().chars().all(|c| c.is_ascii_digit())
                        && !pp_segment.trim().is_empty()
                    {
                        // file:line:col: trailing text
                        let file_part = &before_prev[..pp_colon];
                        let line_part = pp_segment.trim();

                        if !file_part.is_empty() {
                            let line_number: usize = line_part.parse().map_err(|_| {
                                Error::InvalidArgument(format!(
                                    "target '{}': '{}' is not a valid line number",
                                    target, line_part
                                ))
                            })?;
                            if line_number > 0 {
                                return Ok((PathBuf::from(file_part), line_number));
                            }
                        }
                    }
                }
                // prev_segment is line, last_segment is trailing text (not digits)
                let file_part = &before_prev;
                let line_part = prev_segment.trim();
                if !file_part.is_empty() {
                    let line_number: usize = line_part.parse().map_err(|_| {
                        Error::InvalidArgument(format!(
                            "target '{}': '{}' is not a valid line number",
                            target, line_part
                        ))
                    })?;
                    if line_number > 0 {
                        return Ok((PathBuf::from(*file_part), line_number));
                    }
                }
            }
        }
    }

    // Standard file:line parsing
    let file_part = &target[..last_colon];
    let line_part = last_segment.trim();

    if file_part.is_empty() {
        return Err(Error::InvalidArgument(format!(
            "target '{}': file path is empty",
            target
        )));
    }

    let line_number: usize = line_part.parse().map_err(|_| {
        Error::InvalidArgument(format!(
            "target '{}': '{}' is not a valid line number",
            target, line_part
        ))
    })?;

    if line_number == 0 {
        return Err(Error::InvalidArgument(format!(
            "target '{}': line number must be >= 1",
            target
        )));
    }

    Ok((PathBuf::from(file_part), line_number))
}

// ---------------------------------------------------------------------------
// Helper: find_matching_lines
// ---------------------------------------------------------------------------

/// Scan a file line-by-line and return all (1-based line number, line content)
/// pairs where the line contains `pattern` as a substring (case-sensitive).
pub fn find_matching_lines(file: &Path, pattern: &str) -> Result<Vec<(usize, String)>> {
    let content = std::fs::read_to_string(file).map_err(|e| Error::Io {
        source: e,
        path: Some(file.to_path_buf()),
    })?;

    let matches = content
        .lines()
        .enumerate()
        .filter(|(_, line)| line.contains(pattern))
        .map(|(i, line)| (i + 1, line.to_string()))
        .collect();

    Ok(matches)
}

// ---------------------------------------------------------------------------
// Helper: resolve_date
// ---------------------------------------------------------------------------

/// Resolve the expiry `NaiveDate` from the `--date` or `--in-days` arguments.
///
/// When both are None:
/// - If `yes` is true: default to 90 days silently (prints a notice)
/// - If `yes` is false: prompt the user for a number of days (default 90)
pub fn resolve_date(
    date_str: Option<&str>,
    in_days: Option<u32>,
    today: NaiveDate,
    yes: bool,
) -> Result<NaiveDate> {
    match (date_str, in_days) {
        (Some(s), _) => NaiveDate::parse_from_str(s, "%Y-%m-%d").map_err(|_| {
            Error::InvalidArgument(format!("'{}' is not a valid date — expected YYYY-MM-DD", s))
        }),
        (None, Some(days)) => today
            .checked_add_signed(chrono::Duration::days(days as i64))
            .ok_or_else(|| {
                Error::InvalidArgument(format!("--in-days {} overflows the calendar", days))
            }),
        (None, None) => {
            let days: u32 = if yes {
                let default_date = today
                    .checked_add_signed(chrono::Duration::days(90))
                    .ok_or_else(|| {
                        Error::InvalidArgument("90-day default overflows the calendar".to_string())
                    })?;
                println!(
                    "No expiry specified; defaulting to 90 days from today ({})",
                    default_date.format("%Y-%m-%d")
                );
                90
            } else {
                print!("Expire in how many days? [90]: ");
                io::stdout().flush().map_err(|e| Error::Io {
                    source: e,
                    path: None,
                })?;
                let stdin = io::stdin();
                let mut buf = String::new();
                stdin.lock().read_line(&mut buf).map_err(|e| Error::Io {
                    source: e,
                    path: None,
                })?;
                let trimmed = buf.trim();
                if trimmed.is_empty() {
                    90
                } else {
                    trimmed.parse::<u32>().map_err(|_| {
                        Error::InvalidArgument(format!(
                            "'{}' is not a valid number of days",
                            trimmed
                        ))
                    })?
                }
            };
            today
                .checked_add_signed(chrono::Duration::days(days as i64))
                .ok_or_else(|| {
                    Error::InvalidArgument(format!("--in-days {} overflows the calendar", days))
                })
        }
    }
}

// ---------------------------------------------------------------------------
// Helper: detect_comment_style
// ---------------------------------------------------------------------------

/// Return the comment prefix appropriate for the given file extension.
///
/// | Prefix | Extensions                                                            |
/// |--------|-----------------------------------------------------------------------|
/// | `//`   | rs, go, ts, js, jsx, tsx, java, swift, c, cpp, cc, cs, kt            |
/// | `#`    | py, rb, sh, bash, zsh, yaml, yml, tf, toml, r                        |
/// | `--`   | sql, lua, hs                                                          |
/// | `//`   | anything else (default)                                               |
pub fn detect_comment_style(path: &std::path::Path) -> &'static str {
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    match ext.as_str() {
        // C-style line comments
        "rs" | "go" | "ts" | "js" | "jsx" | "tsx" | "java" | "swift" | "c" | "cpp" | "cc"
        | "cs" | "kt" => "//",

        // Hash-style comments
        "py" | "rb" | "sh" | "bash" | "zsh" | "yaml" | "yml" | "tf" | "toml" | "r" => "#",

        // Double-dash comments
        "sql" | "lua" | "hs" => "--",

        // Default to C-style
        _ => "//",
    }
}

// ---------------------------------------------------------------------------
// Helper: build_annotation
// ---------------------------------------------------------------------------

/// Build the full annotation string.
///
/// Without owner: `{prefix} {TAG}[{YYYY-MM-DD}]: {message}`
/// With owner:    `{prefix} {TAG}[{YYYY-MM-DD}][{owner}]: {message}`
pub fn build_annotation(
    prefix: &str,
    tag: &str,
    expiry: NaiveDate,
    owner: Option<&str>,
    message: &str,
) -> String {
    let tag_upper = tag.to_uppercase();
    let date_str = expiry.format("%Y-%m-%d");
    match owner {
        None => format!("{} {}[{}]: {}", prefix, tag_upper, date_str, message),
        Some(o) => format!("{} {}[{}][{}]: {}", prefix, tag_upper, date_str, o, message),
    }
}

// ---------------------------------------------------------------------------
// Helper: insert_line
// ---------------------------------------------------------------------------

/// Insert `new_line` *before* the 1-based `line_number` in `lines`, returning
/// the complete new file content as a `String`.
///
/// Inserting at `line_number == lines.len() + 1` appends after the last line.
///
/// The returned string always ends with a newline.
pub fn insert_line(lines: &[&str], line_number: usize, new_line: &str) -> String {
    // Convert to owned strings so we can splice freely.
    let mut owned: Vec<String> = lines.iter().map(|l| l.to_string()).collect();

    // line_number is 1-based; insert at index (line_number - 1).
    let insert_at = line_number - 1;
    owned.insert(insert_at, new_line.to_string());

    // Join with newlines and ensure trailing newline.
    let mut result = owned.join("\n");
    result.push('\n');
    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::NaiveDate;

    fn date(s: &str) -> NaiveDate {
        NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap()
    }

    fn today() -> NaiveDate {
        NaiveDate::from_ymd_opt(2026, 3, 22).unwrap()
    }

    // -- detect_comment_style ------------------------------------------------

    #[test]
    fn test_detect_comment_style_rs() {
        assert_eq!(
            detect_comment_style(std::path::Path::new("src/main.rs")),
            "//"
        );
    }

    #[test]
    fn test_detect_comment_style_py() {
        assert_eq!(detect_comment_style(std::path::Path::new("script.py")), "#");
    }

    #[test]
    fn test_detect_comment_style_sql() {
        assert_eq!(
            detect_comment_style(std::path::Path::new("schema.sql")),
            "--"
        );
    }

    #[test]
    fn test_detect_comment_style_unknown() {
        assert_eq!(detect_comment_style(std::path::Path::new("file.xyz")), "//");
    }

    #[test]
    fn test_detect_comment_style_no_extension() {
        assert_eq!(detect_comment_style(std::path::Path::new("Makefile")), "//");
    }

    #[test]
    fn test_detect_comment_style_go() {
        assert_eq!(detect_comment_style(std::path::Path::new("main.go")), "//");
    }

    #[test]
    fn test_detect_comment_style_yaml() {
        assert_eq!(
            detect_comment_style(std::path::Path::new("config.yaml")),
            "#"
        );
    }

    #[test]
    fn test_detect_comment_style_lua() {
        assert_eq!(detect_comment_style(std::path::Path::new("init.lua")), "--");
    }

    #[test]
    fn test_detect_comment_style_toml() {
        assert_eq!(
            detect_comment_style(std::path::Path::new("Cargo.toml")),
            "#"
        );
    }

    // -- build_annotation ----------------------------------------------------

    #[test]
    fn test_build_annotation_no_owner() {
        let expiry = date("2026-09-01");
        let result = build_annotation("//", "todo", expiry, None, "remove legacy oauth flow");
        assert_eq!(result, "// TODO[2026-09-01]: remove legacy oauth flow");
    }

    #[test]
    fn test_build_annotation_with_owner() {
        let expiry = date("2026-09-01");
        let result = build_annotation(
            "//",
            "TODO",
            expiry,
            Some("alice"),
            "remove legacy oauth flow",
        );
        assert_eq!(
            result,
            "// TODO[2026-09-01][alice]: remove legacy oauth flow"
        );
    }

    #[test]
    fn test_build_annotation_tag_uppercased() {
        let expiry = date("2027-01-15");
        let result = build_annotation("#", "fixme", expiry, None, "cleanup");
        assert_eq!(result, "# FIXME[2027-01-15]: cleanup");
    }

    #[test]
    fn test_build_annotation_sql_prefix() {
        let expiry = date("2025-12-31");
        let result = build_annotation("--", "HACK", expiry, Some("bob"), "temp workaround");
        assert_eq!(result, "-- HACK[2025-12-31][bob]: temp workaround");
    }

    // -- parse_target --------------------------------------------------------

    #[test]
    fn test_parse_target_valid() {
        let (path, line) = parse_target("src/foo.rs:42").unwrap();
        assert_eq!(path, PathBuf::from("src/foo.rs"));
        assert_eq!(line, 42);
    }

    #[test]
    fn test_parse_target_valid_nested() {
        let (path, line) = parse_target("a/b/c/main.go:1").unwrap();
        assert_eq!(path, PathBuf::from("a/b/c/main.go"));
        assert_eq!(line, 1);
    }

    #[test]
    fn test_parse_target_invalid_no_colon() {
        let result = parse_target("src/foo.rs");
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("FILE:LINE") || msg.contains("file:LINE") || msg.contains("form"));
    }

    #[test]
    fn test_parse_target_invalid_line_zero() {
        let result = parse_target("src/foo.rs:0");
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("1") || msg.contains("zero") || msg.contains(">="));
    }

    #[test]
    fn test_parse_target_invalid_non_numeric_line() {
        let result = parse_target("src/foo.rs:abc");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_target_empty_file() {
        let result = parse_target(":42");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_target_accepts_col() {
        // file:line:col — should return line=42
        let (path, line) = parse_target("src/foo.rs:42:7").unwrap();
        assert_eq!(path, PathBuf::from("src/foo.rs"));
        assert_eq!(line, 42);
    }

    #[test]
    fn test_parse_target_accepts_col_and_message() {
        // file:line:col: trailing message — should return line=42
        let (path, line) = parse_target("src/foo.rs:42:7: some editor context").unwrap();
        assert_eq!(path, PathBuf::from("src/foo.rs"));
        assert_eq!(line, 42);
    }

    // -- resolve_date --------------------------------------------------------

    #[test]
    fn test_resolve_date_from_date_str() {
        let t = date("2025-06-01");
        let result = resolve_date(Some("2026-09-01"), None, t, true).unwrap();
        assert_eq!(result, date("2026-09-01"));
    }

    #[test]
    fn test_resolve_date_from_in_days() {
        let t = date("2025-06-01");
        let result = resolve_date(None, Some(90), t, true).unwrap();
        assert_eq!(result, date("2025-08-30"));
    }

    #[test]
    fn test_resolve_date_in_days_zero() {
        let t = date("2025-06-01");
        let result = resolve_date(None, Some(0), t, true).unwrap();
        assert_eq!(result, t);
    }

    #[test]
    fn test_resolve_date_neither_yes_defaults_90() {
        // When neither --date nor --in-days and yes=true, defaults to 90 days
        let t = today();
        let result = resolve_date(None, None, t, true).unwrap();
        let expected = t.checked_add_signed(chrono::Duration::days(90)).unwrap();
        assert_eq!(result, expected);
    }

    #[test]
    fn test_resolve_date_prefers_date_str_over_in_days() {
        let t = date("2025-06-01");
        let result = resolve_date(Some("2099-01-01"), Some(5), t, true).unwrap();
        assert_eq!(result, date("2099-01-01"));
    }

    #[test]
    fn test_resolve_date_invalid_format() {
        let t = date("2025-06-01");
        let result = resolve_date(Some("01-09-2026"), None, t, true);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_add_default_days_yes() {
        // Neither --date nor --in-days, yes=true → defaults to 90 days
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn main() {}\n").unwrap();
        let target = format!("{}:1", file.display());

        let t = today();
        let result = run_add(&target, "TODO", None, None, None, true, "msg", t, None);
        assert!(result.is_ok());

        let written = std::fs::read_to_string(&file).unwrap();
        let expected_date = t
            .checked_add_signed(chrono::Duration::days(90))
            .unwrap()
            .format("%Y-%m-%d")
            .to_string();
        assert!(written.contains(&expected_date));
    }

    // -- insert_line ---------------------------------------------------------

    #[test]
    fn test_insert_line_middle() {
        // 3-line file, insert at line 2 → new line becomes line 2, old line 2 → line 3
        let lines = vec!["line one", "line two", "line three"];
        let result = insert_line(&lines, 2, "// TODO[2026-01-01]: new annotation");
        let result_lines: Vec<&str> = result.lines().collect();
        assert_eq!(result_lines.len(), 4);
        assert_eq!(result_lines[0], "line one");
        assert_eq!(result_lines[1], "// TODO[2026-01-01]: new annotation");
        assert_eq!(result_lines[2], "line two");
        assert_eq!(result_lines[3], "line three");
    }

    #[test]
    fn test_insert_line_first() {
        let lines = vec!["first", "second", "third"];
        let result = insert_line(&lines, 1, "// annotation");
        let result_lines: Vec<&str> = result.lines().collect();
        assert_eq!(result_lines.len(), 4);
        assert_eq!(result_lines[0], "// annotation");
        assert_eq!(result_lines[1], "first");
        assert_eq!(result_lines[2], "second");
        assert_eq!(result_lines[3], "third");
    }

    #[test]
    fn test_insert_line_after_last() {
        // Inserting at line N+1 appends
        let lines = vec!["alpha", "beta", "gamma"];
        let result = insert_line(&lines, 4, "// appended");
        let result_lines: Vec<&str> = result.lines().collect();
        assert_eq!(result_lines.len(), 4);
        assert_eq!(result_lines[3], "// appended");
    }

    #[test]
    fn test_insert_line_single_line_file() {
        let lines = vec!["only line"];
        let result = insert_line(&lines, 1, "// before");
        let result_lines: Vec<&str> = result.lines().collect();
        assert_eq!(result_lines.len(), 2);
        assert_eq!(result_lines[0], "// before");
        assert_eq!(result_lines[1], "only line");
    }

    #[test]
    fn test_insert_line_trailing_newline() {
        let lines = vec!["a", "b"];
        let result = insert_line(&lines, 1, "x");
        assert!(result.ends_with('\n'), "result should end with a newline");
    }

    // -- find_matching_lines -------------------------------------------------

    #[test]
    fn test_find_matching_lines_found() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "line one\ncontains_pattern here\nline three\n").unwrap();
        let matches = find_matching_lines(&file, "contains_pattern").unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].0, 2);
        assert!(matches[0].1.contains("contains_pattern"));
    }

    #[test]
    fn test_find_matching_lines_multiple() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "foo bar\nfoo baz\nno match\n").unwrap();
        let matches = find_matching_lines(&file, "foo").unwrap();
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].0, 1);
        assert_eq!(matches[1].0, 2);
    }

    #[test]
    fn test_find_matching_lines_none() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "line one\nline two\n").unwrap();
        let matches = find_matching_lines(&file, "zzz_no_match").unwrap();
        assert_eq!(matches.len(), 0);
    }

    // -- run_add (integration tests using tempfile) --------------------------

    #[test]
    fn test_run_add_invalid_target_no_colon() {
        let t = date("2025-06-01");
        let result = run_add(
            "src/nocoton",
            "TODO",
            None,
            Some("2026-01-01"),
            None,
            true,
            "msg",
            t,
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_add_missing_date_and_in_days_yes_defaults() {
        // With yes=true and no date/in_days, should default to 90 days (not error)
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn main() {}\n").unwrap();
        let target = format!("{}:1", file.display());

        let t = date("2025-06-01");
        let result = run_add(&target, "TODO", None, None, None, true, "msg", t, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_add_line_out_of_range() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn main() {}\n").unwrap();
        let target = format!("{}:999", file.display());

        let t = date("2025-06-01");
        let result = run_add(
            &target,
            "TODO",
            None,
            Some("2026-01-01"),
            None,
            true,
            "msg",
            t,
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_run_add_inserts_annotation_with_yes() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn foo() {}\nfn bar() {}\n").unwrap();
        let target = format!("{}:1", file.display());

        let t = date("2025-06-01");
        let result = run_add(
            &target,
            "TODO",
            None,
            Some("2026-09-01"),
            None,
            true, // --yes: skip prompt
            "remove foo after migration",
            t,
            None,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 0);

        let written = std::fs::read_to_string(&file).unwrap();
        let lines: Vec<&str> = written.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "// TODO[2026-09-01]: remove foo after migration");
        assert_eq!(lines[1], "fn foo() {}");
        assert_eq!(lines[2], "fn bar() {}");
    }

    #[test]
    fn test_run_add_inserts_annotation_with_owner() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.py");
        std::fs::write(&file, "def foo():\n    pass\n").unwrap();
        let target = format!("{}:2", file.display());

        let t = date("2025-06-01");
        let result = run_add(
            &target,
            "FIXME",
            Some("alice"),
            None,
            Some(30),
            true,
            "clean this up",
            t,
            None,
        );
        assert!(result.is_ok());

        let written = std::fs::read_to_string(&file).unwrap();
        let lines: Vec<&str> = written.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "def foo():");
        // date should be today + 30 days = 2025-07-01
        assert_eq!(lines[1], "# FIXME[2025-07-01][alice]: clean this up");
        assert_eq!(lines[2], "    pass");
    }

    #[test]
    fn test_run_add_append_after_last_line() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "line1\nline2\n").unwrap();
        // line count is 2; line 3 = append
        let target = format!("{}:3", file.display());

        let t = date("2025-06-01");
        let result = run_add(
            &target,
            "TODO",
            None,
            Some("2027-01-01"),
            None,
            true,
            "appended",
            t,
            None,
        );
        assert!(result.is_ok());

        let written = std::fs::read_to_string(&file).unwrap();
        let lines: Vec<&str> = written.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[2], "// TODO[2027-01-01]: appended");
    }

    #[test]
    fn test_run_add_nonexistent_file_returns_io_error() {
        let t = date("2025-06-01");
        let result = run_add(
            "/nonexistent/path/file.rs:1",
            "TODO",
            None,
            Some("2026-01-01"),
            None,
            true,
            "msg",
            t,
            None,
        );
        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Io { .. } => {}
            other => panic!("expected Io error, got: {:?}", other),
        }
    }

    #[test]
    fn test_run_add_with_search_single_match() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn alpha() {}\nfn legacy_auth() {}\nfn gamma() {}\n").unwrap();

        let t = today();
        let result = run_add(
            file.to_str().unwrap(),
            "TODO",
            None,
            Some("2027-01-01"),
            None,
            true,
            "remove legacy auth",
            t,
            Some("legacy_auth"),
        );
        assert!(result.is_ok());

        let written = std::fs::read_to_string(&file).unwrap();
        assert!(written.contains("TODO[2027-01-01]: remove legacy auth"));
    }

    #[test]
    fn test_run_add_with_search_no_match() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn alpha() {}\nfn beta() {}\n").unwrap();

        let t = today();
        let result = run_add(
            file.to_str().unwrap(),
            "TODO",
            None,
            Some("2027-01-01"),
            None,
            true,
            "msg",
            t,
            Some("zzz_no_match"),
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("no lines matching"));
    }

    #[test]
    fn test_run_add_with_search_multiple_matches() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.rs");
        std::fs::write(&file, "fn foo_a() {}\nfn foo_b() {}\nfn bar() {}\n").unwrap();

        let t = today();
        let result = run_add(
            file.to_str().unwrap(),
            "TODO",
            None,
            Some("2027-01-01"),
            None,
            true,
            "msg",
            t,
            Some("foo"),
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("matched") || msg.contains("2 lines"));
    }
}