sara-tasks 0.8.0

Sara — folder-aware task manager
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
use chrono::Local;

use crate::infrastructure::db;

use super::handler::{guide_is_stale, notes_of_kind, verification_rows};
use super::types::Detail;

/// Options controlling the readable digest renderers (`render_plain` /
/// `render_markdown`). Defaults to the agent-friendly view: History collapsed.
#[derive(Clone, Copy, Default)]
pub(super) struct RenderOpts {
    /// Include the full History log (collapsed to a one-line summary otherwise).
    pub(super) history: bool,
}

/// Best-effort terminal column width for wrapping long comment bodies; falls
/// back to a readable default when stdout isn't a real terminal (the common
/// case for this renderer — piped into an agent or `--plain`).
fn terminal_width() -> usize {
    crossterm::terminal::size()
        .map(|(cols, _)| cols as usize)
        .unwrap_or(96)
        .clamp(60, 100)
}

/// Word-wrap `text` into lines prefixed with `indent`, so a long comment body
/// reads as a short paragraph instead of one unbroken line. Preserves blank
/// lines already present in the source as paragraph breaks.
fn wrap_body(text: &str, indent: &str, width: usize) -> Vec<String> {
    let avail = width.saturating_sub(indent.len()).max(20);
    let mut lines = Vec::new();
    for para in text.split('\n') {
        if para.trim().is_empty() {
            lines.push(String::new());
            continue;
        }
        let mut current = String::new();
        for word in para.split_whitespace() {
            let extra = if current.is_empty() { 0 } else { 1 };
            if !current.is_empty() && current.len() + extra + word.len() > avail {
                lines.push(format!("{indent}{current}"));
                current.clear();
            }
            if !current.is_empty() {
                current.push(' ');
            }
            current.push_str(word);
        }
        if !current.is_empty() {
            lines.push(format!("{indent}{current}"));
        }
    }
    lines
}

/// Render the readable plain-text digest of a task — the single source of truth
/// shared by the non-TTY fallback, `sara info --plain`, and (later) the MCP
/// server. History is collapsed by default to keep agent token usage low.
pub(super) fn render_plain(d: &Detail, opts: RenderOpts) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    macro_rules! w {
        () => {{ let _ = writeln!(out); }};
        ($($arg:tt)*) => {{ let _ = writeln!(out, $($arg)*); }};
    }
    let t = &d.task;
    w!("Task {}", t.id.unwrap_or(0));
    w!();
    w!("{:<14}{}", "Description", t.description);
    w!("{:<14}{}", "Project", t.project);
    w!("{:<14}{}", "Status", t.status);
    w!(
        "{:<14}{}",
        "Priority",
        t.priority.as_ref().map(|p| p.label()).unwrap_or("-")
    );
    w!(
        "{:<14}{}",
        "Due",
        t.due
            .map(|dd| dd
                .with_timezone(&Local)
                .format("%Y-%m-%d %H:%M")
                .to_string())
            .unwrap_or_else(|| "-".to_string())
    );
    w!(
        "{:<14}{}",
        "Tags",
        if t.tags.is_empty() {
            "-".to_string()
        } else {
            t.tags.join(", ")
        }
    );
    w!(
        "{:<14}{}",
        "Time spent",
        crate::infrastructure::model::format_duration(t.total_time_spent())
    );
    w!("{:<14}{:.1}", "Urgency", t.urgency);
    w!("{:<14}{}", "UUID", t.uuid);

    // ── Guide ───────────────────────────────────────────────────────
    if let Some(a) = &d.guide.assignment {
        w!("{:<14}{}", "Assignment", a);
    }
    if let Some(r) = &d.guide.rationale {
        w!("{:<14}{}", "Rationale", r);
    }
    if guide_is_stale(d) {
        w!(
            "{:<14}guide validated @ {} but HEAD is {} — may be stale (run `sara validate`)",
            "Freshness",
            d.guide.validated_commit.as_deref().unwrap_or("-"),
            d.head_commit.as_deref().unwrap_or("-"),
        );
    } else if let Some(v) = &d.guide.validated_commit {
        w!("{:<14}validated @ {}", "Freshness", v);
    }

    // Steps (with intent + result).
    let steps: Vec<&crate::infrastructure::db::ChecklistItem> = d
        .checklist
        .iter()
        .filter(|c| c.kind != db::STEP_KIND_ACCEPTANCE)
        .collect();
    if !steps.is_empty() {
        w!("\nSteps:");
        for (i, s) in steps.iter().enumerate() {
            let mark = if s.done { "x" } else { " " };
            let badge = if s.source == "ai" { " (ai)" } else { "" };
            w!("  [{}] {}. {}{}", mark, i + 1, s.text, badge);
            if let Some(intent) = &s.intent {
                w!("        intent: {intent}");
            }
            if let Some(v) = &s.verify_cmd {
                w!("        verify: {v}");
            }
            if let Some(r) = &s.result {
                w!("        result: {r}");
            }
            if s.done && (s.done_commit.is_some() || s.done_at.is_some()) {
                let commit = s
                    .done_commit
                    .as_deref()
                    .map(|c| {
                        let short: String = c.chars().take(8).collect();
                        format!("@ {short} ")
                    })
                    .unwrap_or_default();
                let when = s.done_at.as_deref().unwrap_or("");
                w!("        done:   {commit}{when}");
            }
        }
    }

    // Acceptance criteria.
    let acceptance: Vec<&crate::infrastructure::db::ChecklistItem> = d
        .checklist
        .iter()
        .filter(|c| c.kind == db::STEP_KIND_ACCEPTANCE)
        .collect();
    if !acceptance.is_empty() {
        w!("\nAcceptance criteria:");
        for (i, a) in acceptance.iter().enumerate() {
            let mark = if a.done { "x" } else { " " };
            w!("  [{}] {}. {}", mark, i + 1, a.text);
        }
    }

    // Verification commands (project + task-level).
    let verif = verification_rows(d);
    if !verif.is_empty() {
        w!("\nVerification:");
        for (scope, label, cmd) in &verif {
            w!("  {label:<7} {cmd}  ({scope})");
        }
    }

    // Typed AI/human notes grouped by kind.
    for (label, kind) in [
        ("Findings", "finding"),
        ("Constraints", "constraint"),
        ("Assumptions", "assumption"),
        ("Open questions", "open_question"),
        ("Non-goals", "non_goal"),
        ("Decisions", "decision"),
        ("Risks", "risk"),
        ("Patterns", "pattern"),
    ] {
        let notes = notes_of_kind(d, kind);
        if !notes.is_empty() {
            w!("\n{label}:");
            for n in notes {
                let badge = if n.author == "ai" { " (ai)" } else { "" };
                w!("  - {}{}", n.text, badge);
            }
        }
    }

    // Code anchors (relevant files with reasons).
    let suggested: Vec<&crate::infrastructure::db::Anchor> = d
        .anchors
        .iter()
        .filter(|a| a.source == db::SOURCE_SUGGESTED)
        .collect();
    if !suggested.is_empty() {
        w!("\nRelevant code anchors (suggested by AI):");
        for a in suggested {
            w!("  {}{}", a.path, a.location());
            if let Some(r) = &a.reason {
                w!("      {r}");
            }
        }
    }

    for b in &d.blocked_by {
        w!("{:<14}{}", "Blocked by", b);
    }
    for b in &d.blocking {
        w!("{:<14}{}", "Blocking", b);
    }
    for link in &d.links {
        w!(
            "{:<14}[{}] {}  {}",
            "Link",
            link.id,
            link.display(),
            link.url
        );
    }
    for file in &d.manual_files {
        w!("{:<14}{}", "File", file);
    }
    // Comments (human feedback), with anchor + reconsider markers. Each
    // comment gets its own header line (id/target/flags/date) followed by
    // the wrapped body on indented lines, so long text stays scannable.
    let comments = notes_of_kind(d, "comment");
    if !comments.is_empty() {
        w!("\nComments:");
        let width = terminal_width();
        for (i, a) in comments.into_iter().enumerate() {
            if i > 0 {
                w!();
            }
            let date = a.entry.with_timezone(&Local).format("%Y-%m-%d %H:%M");
            let target = match (&a.target_kind, &a.target_id) {
                (Some(k), Some(idv)) => format!(" [{k}:{idv}]"),
                _ => String::new(),
            };
            let flag = if a.request_revision {
                " (reconsider)"
            } else {
                ""
            };
            let resolved = if a.status == "resolved" {
                " (resolved)"
            } else {
                ""
            };
            w!("  #{}{}{}{}  {}", a.id, target, flag, resolved, date);
            for line in wrap_body(&a.text, "      ", width) {
                w!("{}", line);
            }
        }
    }
    // AI activity footer.
    if !d.ai_runs.is_empty() {
        w!("\nAI activity:");
        for r in &d.ai_runs {
            let date = r.created_at.with_timezone(&Local).format("%Y-%m-%d %H:%M");
            w!(
                "  {} via {} [{}] @ {}",
                r.kind,
                r.model.as_deref().unwrap_or("?"),
                r.provider.as_deref().unwrap_or("?"),
                date
            );
        }
    }
    // History — collapsed to a one-line summary unless explicitly requested.
    if opts.history {
        for h in &d.history {
            w!(
                "{:<14}{} {}",
                "History",
                history_changed_at(h),
                history_change(h)
            );
        }
    } else if !d.history.is_empty() {
        w!(
            "{:<14}{} entries (use --history to show)",
            "History",
            d.history.len()
        );
    }
    out
}

/// Format a single history entry's timestamp for the readable digest.
pub(super) fn history_changed_at(h: &crate::infrastructure::db::HistoryEntry) -> String {
    h.changed_at
        .with_timezone(&Local)
        .format("%Y-%m-%d %H:%M")
        .to_string()
}

/// Describe a single history entry as a one-line change summary.
pub(super) fn history_change(h: &crate::infrastructure::db::HistoryEntry) -> String {
    if h.field == "created" {
        h.new_value.clone().unwrap_or_default()
    } else if h.field == "annotation" {
        match (&h.new_value, &h.old_value) {
            (Some(text), _) => format!("comment added: {text}"),
            (None, Some(text)) => format!("comment removed: {text}"),
            _ => "comment".to_string(),
        }
    } else {
        format!(
            "{}: {} -> {}",
            h.field,
            h.old_value.as_deref().unwrap_or("-"),
            h.new_value.as_deref().unwrap_or("-"),
        )
    }
}

/// Render a Markdown digest of a task — description, steps and acceptance
/// criteria as checkboxes, plus the key context sections. Suitable for embedding
/// in agent context or a PR body. Shares `RenderOpts` with `render_plain`.
pub(super) fn render_markdown(d: &Detail, opts: RenderOpts) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    macro_rules! w {
        () => {{ let _ = writeln!(out); }};
        ($($arg:tt)*) => {{ let _ = writeln!(out, $($arg)*); }};
    }
    let t = &d.task;

    w!("# Task {} — {}", t.id.unwrap_or(0), t.status);
    w!();
    w!("- **Project:** {}", t.project);
    w!(
        "- **Priority:** {}",
        t.priority.as_ref().map(|p| p.label()).unwrap_or("-")
    );
    if let Some(due) = t.due {
        w!(
            "- **Due:** {}",
            due.with_timezone(&Local).format("%Y-%m-%d %H:%M")
        );
    }
    if !t.tags.is_empty() {
        w!("- **Tags:** {}", t.tags.join(", "));
    }
    w!("- **Urgency:** {:.1}", t.urgency);
    w!("- **UUID:** `{}`", t.uuid);

    if guide_is_stale(d) {
        w!();
        w!(
            "> ⚠️ Guide validated @ {} but project HEAD is {} — may be stale (run `sara validate`).",
            d.guide.validated_commit.as_deref().unwrap_or("-"),
            d.head_commit.as_deref().unwrap_or("-"),
        );
    }

    w!();
    w!("## Description");
    w!();
    w!("{}", t.description);

    if let Some(a) = &d.guide.assignment {
        w!();
        w!("## Assignment");
        w!();
        w!("{a}");
    }
    if let Some(r) = &d.guide.rationale {
        w!();
        w!("## Rationale");
        w!();
        w!("{r}");
    }

    let steps: Vec<&crate::infrastructure::db::ChecklistItem> = d
        .checklist
        .iter()
        .filter(|c| c.kind != db::STEP_KIND_ACCEPTANCE)
        .collect();
    if !steps.is_empty() {
        w!();
        w!("## Steps");
        w!();
        for s in &steps {
            let mark = if s.done { "x" } else { " " };
            w!("- [{}] {}", mark, s.text);
        }
    }

    let acceptance: Vec<&crate::infrastructure::db::ChecklistItem> = d
        .checklist
        .iter()
        .filter(|c| c.kind == db::STEP_KIND_ACCEPTANCE)
        .collect();
    if !acceptance.is_empty() {
        w!();
        w!("## Acceptance criteria");
        w!();
        for a in &acceptance {
            let mark = if a.done { "x" } else { " " };
            w!("- [{}] {}", mark, a.text);
        }
    }

    // Typed AI/human notes grouped by kind.
    for (label, kind) in [
        ("Findings", "finding"),
        ("Constraints", "constraint"),
        ("Assumptions", "assumption"),
        ("Open questions", "open_question"),
        ("Non-goals", "non_goal"),
        ("Decisions", "decision"),
        ("Risks", "risk"),
        ("Patterns", "pattern"),
    ] {
        let notes = notes_of_kind(d, kind);
        if !notes.is_empty() {
            w!();
            w!("## {label}");
            w!();
            for n in notes {
                w!("- {}", n.text);
            }
        }
    }

    let anchors: Vec<&crate::infrastructure::db::Anchor> = d
        .anchors
        .iter()
        .filter(|a| a.source == db::SOURCE_SUGGESTED)
        .collect();
    if !anchors.is_empty() {
        w!();
        w!("## Relevant code anchors");
        w!();
        for a in &anchors {
            match &a.reason {
                Some(r) => w!("- `{}{}` — {}", a.path, a.location(), r),
                None => w!("- `{}{}`", a.path, a.location()),
            }
        }
    }

    if !d.links.is_empty() {
        w!();
        w!("## Links");
        w!();
        for link in &d.links {
            w!("- [{}]({})", link.display(), link.url);
        }
    }

    if !d.blocked_by.is_empty() {
        w!();
        w!("## Blocked by");
        w!();
        for b in &d.blocked_by {
            w!("- {b}");
        }
    }
    if !d.blocking.is_empty() {
        w!();
        w!("## Blocking");
        w!();
        for b in &d.blocking {
            w!("- {b}");
        }
    }

    // Human comments — high-signal direction for an agent; flag reconsider/open.
    let comments = notes_of_kind(d, "comment");
    if !comments.is_empty() {
        w!();
        w!("## Comments");
        w!();
        for a in comments {
            let flag = if a.request_revision {
                " **(reconsider)**"
            } else {
                ""
            };
            let resolved = if a.status == "resolved" {
                " _(resolved)_"
            } else {
                ""
            };
            w!("- #{}{}{} {}", a.id, flag, resolved, a.text);
        }
    }

    if opts.history && !d.history.is_empty() {
        w!();
        w!("## History");
        w!();
        for h in &d.history {
            w!("- {} {}", history_changed_at(h), history_change(h));
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::super::edit::{apply_field, current_value, cycle_priority};
    use super::super::types::{DependencyGraph, Detail, EditField};
    use super::{RenderOpts, render_markdown, render_plain};
    use crate::infrastructure::config::Config;
    use crate::infrastructure::db;
    use crate::infrastructure::model::{Priority, Task};
    use chrono::Utc;

    fn task() -> Task {
        Task::new("original".into(), "tk".into())
    }

    #[test]
    fn editing_description_updates_value() {
        let mut t = task();
        let cfg = Config::default();
        apply_field(&mut t, EditField::Description, "new description", &cfg);
        assert_eq!(t.description, "new description");
    }

    #[test]
    fn empty_description_is_ignored() {
        let mut t = task();
        let cfg = Config::default();
        apply_field(&mut t, EditField::Description, "   ", &cfg);
        assert_eq!(t.description, "original");
    }

    #[test]
    fn editing_tags_splits_and_trims() {
        let mut t = task();
        let cfg = Config::default();
        apply_field(&mut t, EditField::Tags, " rust , cli ,", &cfg);
        assert_eq!(t.tags, vec!["rust".to_string(), "cli".to_string()]);
    }

    #[test]
    fn editing_due_empty_clears_it() {
        let mut t = task();
        let cfg = Config::default();
        t.due = Some(Utc::now());
        apply_field(&mut t, EditField::Due, "", &cfg);
        assert!(t.due.is_none());
    }

    #[test]
    fn editing_due_parses_relative() {
        let mut t = task();
        let cfg = Config::default();
        apply_field(&mut t, EditField::Due, "+3d", &cfg);
        assert!(t.due.is_some());
    }

    #[test]
    fn priority_cycles_forward_and_back() {
        let mut t = task();
        assert!(t.priority.is_none());
        cycle_priority(&mut t, true);
        assert_eq!(t.priority, Some(Priority::L));
        cycle_priority(&mut t, true);
        assert_eq!(t.priority, Some(Priority::M));
        cycle_priority(&mut t, true);
        assert_eq!(t.priority, Some(Priority::H));
        cycle_priority(&mut t, true);
        assert!(t.priority.is_none());
        cycle_priority(&mut t, false);
        assert_eq!(t.priority, Some(Priority::H));
    }

    #[test]
    fn current_value_round_trips_with_apply() {
        let mut t = task();
        let cfg = Config::default();
        apply_field(&mut t, EditField::Project, "myproj", &cfg);
        assert_eq!(current_value(&t, EditField::Project), "myproj");
        apply_field(&mut t, EditField::Tags, "a, b", &cfg);
        assert_eq!(current_value(&t, EditField::Tags), "a, b");
    }

    fn step(
        id: i64,
        text: &str,
        kind: &str,
        done: bool,
    ) -> crate::infrastructure::db::ChecklistItem {
        crate::infrastructure::db::ChecklistItem {
            id,
            text: text.into(),
            done,
            position: id,
            intent: None,
            kind: kind.into(),
            source: "human".into(),
            verify_cmd: None,
            result: None,
            done_commit: None,
            done_at: None,
        }
    }

    fn history(
        field: &str,
        old: Option<&str>,
        new: Option<&str>,
    ) -> crate::infrastructure::db::HistoryEntry {
        crate::infrastructure::db::HistoryEntry {
            field: field.into(),
            old_value: old.map(Into::into),
            new_value: new.map(Into::into),
            changed_at: chrono::Utc::now(),
        }
    }

    fn detail(
        checklist: Vec<crate::infrastructure::db::ChecklistItem>,
        hist: Vec<crate::infrastructure::db::HistoryEntry>,
    ) -> Detail {
        Detail {
            task: task(),
            blocked_by: vec![],
            blocking: vec![],
            depends_on_ids: vec![],
            manual_files: vec![],
            suggested_files: vec![],
            links: vec![],
            annotations: vec![],
            history: hist,
            project_root: None,
            branch: None,
            overlaps: vec![],
            similar: vec![],
            checklist,
            urgency_breakdown: None,
            activity: std::collections::HashMap::new(),
            stats: None,
            guide: crate::infrastructure::db::TaskGuideFields::default(),
            anchors: vec![],
            ai_runs: vec![],
            head_commit: None,
            project_commands: crate::infrastructure::db::ProjectCommands::default(),
            chain: vec![],
            graph: DependencyGraph::default(),
        }
    }

    fn comment(
        id: i64,
        text: &str,
        request_revision: bool,
    ) -> crate::infrastructure::db::Annotation {
        crate::infrastructure::db::Annotation {
            id,
            text: text.into(),
            entry: chrono::Utc::now(),
            kind: "comment".into(),
            author: "human".into(),
            target_kind: None,
            target_id: None,
            status: "open".into(),
            request_revision,
            resolved_by_run: None,
        }
    }

    #[test]
    fn render_plain_wraps_long_comments_onto_indented_lines() {
        let long_text = "word ".repeat(40).trim().to_string();
        let d = Detail {
            annotations: vec![comment(1, &long_text, true)],
            ..detail(vec![], vec![])
        };
        let out = render_plain(&d, RenderOpts::default());
        assert!(out.contains("Comments:"));
        assert!(out.contains("#1"));
        assert!(out.contains("(reconsider)"));
        // The header line carries no body text — the comment id line is short.
        let header = out.lines().find(|l| l.contains("#1")).unwrap();
        assert!(!header.contains("word word word word word word word word"));
        // The body is wrapped onto its own indented line(s).
        assert!(out.lines().any(|l| l.starts_with("      word")));
    }

    #[test]
    fn render_plain_separates_multiple_comments_with_blank_line() {
        let d = Detail {
            annotations: vec![comment(1, "first", false), comment(2, "second", false)],
            ..detail(vec![], vec![])
        };
        let out = render_plain(&d, RenderOpts::default());
        let idx1 = out.find("#1").unwrap();
        let idx2 = out.find("#2").unwrap();
        let between = &out[idx1..idx2];
        assert!(
            between.contains("\n\n"),
            "expected a blank line between comments, got: {between:?}"
        );
    }

    #[test]
    fn render_plain_collapses_history_by_default() {
        let d = detail(
            vec![],
            vec![history("status", Some("pending"), Some("done"))],
        );
        let collapsed = render_plain(&d, RenderOpts { history: false });
        assert!(collapsed.contains("1 entries (use --history to show)"));
        assert!(!collapsed.contains("status: pending -> done"));

        let full = render_plain(&d, RenderOpts { history: true });
        assert!(full.contains("status: pending -> done"));
    }

    #[test]
    fn render_plain_lists_steps_and_acceptance() {
        let d = detail(
            vec![
                step(1, "do the thing", db::STEP_KIND_STEP, true),
                step(2, "ship it", db::STEP_KIND_ACCEPTANCE, false),
            ],
            vec![],
        );
        let out = render_plain(&d, RenderOpts::default());
        assert!(out.contains("Steps:"));
        assert!(out.contains("[x] 1. do the thing"));
        assert!(out.contains("Acceptance criteria:"));
        assert!(out.contains("[ ] 1. ship it"));
    }

    #[test]
    fn render_markdown_has_description_steps_and_acceptance() {
        let d = detail(
            vec![
                step(1, "first step", db::STEP_KIND_STEP, false),
                step(2, "second step", db::STEP_KIND_STEP, true),
                step(3, "definition of done", db::STEP_KIND_ACCEPTANCE, false),
            ],
            vec![],
        );
        let md = render_markdown(&d, RenderOpts::default());
        // Description present.
        assert!(md.contains("## Description"));
        assert!(md.contains("original"));
        // Steps rendered as GitHub-style checkboxes.
        assert!(md.contains("## Steps"));
        assert!(md.contains("- [ ] first step"));
        assert!(md.contains("- [x] second step"));
        // Acceptance criteria rendered with checkboxes.
        assert!(md.contains("## Acceptance criteria"));
        assert!(md.contains("- [ ] definition of done"));
    }

    #[test]
    fn render_markdown_omits_history_unless_requested() {
        let d = detail(
            vec![],
            vec![history("status", Some("pending"), Some("done"))],
        );
        let lean = render_markdown(&d, RenderOpts { history: false });
        assert!(!lean.contains("## History"));

        let full = render_markdown(&d, RenderOpts { history: true });
        assert!(full.contains("## History"));
        assert!(full.contains("status: pending -> done"));
    }
}