rivets 0.1.0

A Rust-based issue tracking system using JSONL storage
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
//! Output formatting for CLI commands.
//!
//! This module provides utilities for formatting command output in both
//! human-readable text format and JSON format for programmatic use.

use crate::domain::{Dependency, Issue, IssueStatus, IssueType};
use colored::Colorize;
use serde::Serialize;
use std::env;
use std::io::{self, Write};

// ============================================================================
// Terminal Width Detection
// ============================================================================

const DEFAULT_TERMINAL_WIDTH: u16 = 80;
const DEFAULT_MAX_CONTENT_WIDTH: usize = 80;

/// Get the current terminal width, falling back to default if detection fails.
fn get_terminal_width() -> usize {
    terminal_size::terminal_size()
        .map(|(w, _)| w.0 as usize)
        .unwrap_or(DEFAULT_TERMINAL_WIDTH as usize)
}

/// Get the maximum content width, respecting RIVETS_MAX_WIDTH environment variable.
fn get_max_content_width() -> usize {
    env::var("RIVETS_MAX_WIDTH")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(DEFAULT_MAX_CONTENT_WIDTH)
}

// ============================================================================
// ASCII Fallback Mode
// ============================================================================

/// Check if ASCII-only mode is enabled via RIVETS_ASCII=1 environment variable.
fn use_ascii_icons() -> bool {
    env::var("RIVETS_ASCII")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false)
}

// ============================================================================
// Color Helpers
// ============================================================================

/// Apply color to status text based on issue status.
fn colorize_status(status: IssueStatus) -> String {
    let text = format!("{status}");
    match status {
        IssueStatus::Open => text.white().to_string(),
        IssueStatus::InProgress => text.yellow().to_string(),
        IssueStatus::Blocked => text.red().to_string(),
        IssueStatus::Closed => text.green().to_string(),
    }
}

/// Apply color to priority text based on priority level.
fn colorize_priority(priority: u8) -> String {
    let text = format!("P{priority}");
    match priority {
        0 => text.red().bold().to_string(),
        1 => text.yellow().to_string(),
        _ => text.to_string(),
    }
}

/// Colorize an issue ID (cyan).
fn colorize_id(id: &str) -> String {
    id.cyan().to_string()
}

/// Colorize labels (magenta).
fn colorize_labels(labels: &[String]) -> String {
    if labels.is_empty() {
        String::new()
    } else {
        labels.join(", ").magenta().to_string()
    }
}

/// Get a colored status icon, with ASCII fallback support.
fn colored_status_icon(status: IssueStatus) -> String {
    if use_ascii_icons() {
        match status {
            IssueStatus::Open => "o".white().to_string(),
            IssueStatus::InProgress => ">".yellow().to_string(),
            IssueStatus::Blocked => "x".red().to_string(),
            IssueStatus::Closed => "+".green().to_string(),
        }
    } else {
        match status {
            IssueStatus::Open => "".white().to_string(),
            IssueStatus::InProgress => "".yellow().to_string(),
            IssueStatus::Blocked => "".red().to_string(),
            IssueStatus::Closed => "".green().to_string(),
        }
    }
}

/// Get a type icon for issue types, with ASCII fallback support.
fn type_icon(issue_type: IssueType) -> &'static str {
    if use_ascii_icons() {
        match issue_type {
            IssueType::Task => "-",
            IssueType::Bug => "*",
            IssueType::Feature => "+",
            IssueType::Epic => "#",
            IssueType::Chore => ".",
        }
    } else {
        match issue_type {
            IssueType::Task => "",
            IssueType::Bug => "",
            IssueType::Feature => "",
            IssueType::Epic => "",
            IssueType::Chore => "",
        }
    }
}

// ============================================================================
// Section Printing Helpers
// ============================================================================

/// Print a text section with a bold title and wrapped, indented content.
fn print_text_section<W: Write>(
    w: &mut W,
    title: &str,
    content: &str,
    width: usize,
) -> io::Result<()> {
    if content.is_empty() {
        return Ok(());
    }
    writeln!(w)?;
    writeln!(w, "{}:", title.bold())?;
    for line in wrap_text(content, width.saturating_sub(2)) {
        writeln!(w, "  {line}")?;
    }
    Ok(())
}

/// Print an optional text section (only if Some and non-empty).
fn print_optional_section<W: Write>(
    w: &mut W,
    title: &str,
    content: &Option<String>,
    width: usize,
) -> io::Result<()> {
    if let Some(text) = content {
        print_text_section(w, title, text, width)?;
    }
    Ok(())
}

/// Output format mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputMode {
    /// Human-readable text format
    Text,
    /// JSON format for programmatic use
    Json,
}

/// Print an issue in the specified format
pub fn print_issue(issue: &Issue, mode: OutputMode) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();

    match mode {
        OutputMode::Text => print_issue_text(&mut handle, issue),
        OutputMode::Json => print_issue_json(&mut handle, issue),
    }
}

/// Print a list of issues in the specified format
pub fn print_issues(issues: &[Issue], mode: OutputMode) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();

    match mode {
        OutputMode::Text => print_issues_text(&mut handle, issues),
        OutputMode::Json => print_issues_json(&mut handle, issues),
    }
}

/// Print an issue with full details (for show command)
pub fn print_issue_details(
    issue: &Issue,
    deps: &[Dependency],
    dependents: &[Dependency],
    mode: OutputMode,
) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();

    match mode {
        OutputMode::Text => print_issue_details_text(&mut handle, issue, deps, dependents),
        OutputMode::Json => print_issue_details_json(&mut handle, issue, deps, dependents),
    }
}

/// Print blocked issues with their blockers
pub fn print_blocked_issues(blocked: &[(Issue, Vec<Issue>)], mode: OutputMode) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();

    match mode {
        OutputMode::Text => print_blocked_text(&mut handle, blocked),
        OutputMode::Json => print_blocked_json(&mut handle, blocked),
    }
}

/// Print a simple message
pub fn print_message(msg: &str) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();
    writeln!(handle, "{}", msg)
}

/// Print a JSON-formatted result for any serializable value
pub fn print_json<T: Serialize>(value: &T) -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();
    let json = serde_json::to_string_pretty(value)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    writeln!(handle, "{}", json)
}

// ============================================================================
// Text Formatting
// ============================================================================

fn print_issue_text<W: Write>(w: &mut W, issue: &Issue) -> io::Result<()> {
    writeln!(
        w,
        "{} {} {} {} {}",
        colored_status_icon(issue.status),
        colorize_id(issue.id.as_str()),
        type_icon(issue.issue_type),
        colorize_priority(issue.priority),
        issue.title
    )?;

    if let Some(ref assignee) = issue.assignee {
        writeln!(w, "  {} {}", "Assignee:".dimmed(), assignee)?;
    }

    if !issue.labels.is_empty() {
        writeln!(
            w,
            "  {} {}",
            "Labels:".dimmed(),
            colorize_labels(&issue.labels)
        )?;
    }

    Ok(())
}

fn print_issues_text<W: Write>(w: &mut W, issues: &[Issue]) -> io::Result<()> {
    if issues.is_empty() {
        writeln!(w, "No issues found.")?;
        return Ok(());
    }

    writeln!(w, "Found {} issue(s):", issues.len())?;
    writeln!(w)?;

    for issue in issues {
        writeln!(
            w,
            "{} {}  {}  {}  {}",
            colored_status_icon(issue.status),
            colorize_id(issue.id.as_str()),
            type_icon(issue.issue_type),
            colorize_priority(issue.priority),
            issue.title
        )?;
    }

    Ok(())
}

fn print_issue_details_text<W: Write>(
    w: &mut W,
    issue: &Issue,
    deps: &[Dependency],
    dependents: &[Dependency],
) -> io::Result<()> {
    let terminal_width = get_terminal_width();
    let max_width = get_max_content_width();
    let content_width = terminal_width.min(max_width);

    // Header: status icon, ID, and title
    writeln!(
        w,
        "{} {}: {}",
        colored_status_icon(issue.status),
        colorize_id(issue.id.as_str()),
        issue.title
    )?;

    // Metadata line
    let type_display = format!("{} {}", type_icon(issue.issue_type), issue.issue_type);
    writeln!(
        w,
        "{}  {}    {}  {}    {}  {}",
        "Type:".dimmed(),
        type_display,
        "Status:".dimmed(),
        colorize_status(issue.status),
        "Priority:".dimmed(),
        colorize_priority(issue.priority)
    )?;

    // Optional fields
    if let Some(ref assignee) = issue.assignee {
        writeln!(w, "{} {}", "Assignee:".dimmed(), assignee)?;
    }

    if !issue.labels.is_empty() {
        writeln!(
            w,
            "{} {}",
            "Labels:".dimmed(),
            colorize_labels(&issue.labels)
        )?;
    }

    if let Some(ref ext_ref) = issue.external_ref {
        writeln!(w, "{} {}", "Ref:".dimmed(), ext_ref)?;
    }

    // Timestamps
    writeln!(
        w,
        "{} {}    {} {}",
        "Created:".dimmed(),
        issue.created_at.format("%Y-%m-%d %H:%M"),
        "Updated:".dimmed(),
        issue.updated_at.format("%Y-%m-%d %H:%M")
    )?;

    if let Some(closed_at) = issue.closed_at {
        writeln!(
            w,
            "{} {}",
            "Closed:".dimmed(),
            closed_at.format("%Y-%m-%d %H:%M")
        )?;
    }

    // Long-form content sections
    print_text_section(w, "Description", &issue.description, content_width)?;
    print_optional_section(w, "Design Notes", &issue.design, content_width)?;
    print_optional_section(
        w,
        "Acceptance Criteria",
        &issue.acceptance_criteria,
        content_width,
    )?;
    print_optional_section(w, "Notes", &issue.notes, content_width)?;

    // Dependencies section
    if !deps.is_empty() {
        writeln!(w)?;
        writeln!(w, "{} ({}):", "Dependencies".bold(), deps.len())?;
        for dep in deps {
            writeln!(
                w,
                "  {} {} ({})",
                "".cyan(),
                colorize_id(dep.depends_on_id.as_str()),
                dep.dep_type
            )?;
        }
    }

    // Dependents section
    if !dependents.is_empty() {
        writeln!(w)?;
        writeln!(w, "{} ({}):", "Dependents".bold(), dependents.len())?;
        for dep in dependents {
            writeln!(
                w,
                "  {} {} ({})",
                "".yellow(),
                colorize_id(dep.depends_on_id.as_str()),
                dep.dep_type
            )?;
        }
    }

    Ok(())
}

/// Wrap text to fit within a given width, preserving existing line breaks.
/// Uses textwrap to handle edge cases like long words (URLs, file paths).
fn wrap_text(text: &str, max_width: usize) -> Vec<String> {
    text.lines()
        .flat_map(|line| {
            if line.trim().is_empty() {
                vec![String::new()]
            } else {
                textwrap::wrap(line, max_width)
                    .into_iter()
                    .map(|s| s.into_owned())
                    .collect()
            }
        })
        .collect()
}

fn print_blocked_text<W: Write>(w: &mut W, blocked: &[(Issue, Vec<Issue>)]) -> io::Result<()> {
    if blocked.is_empty() {
        writeln!(w, "No blocked issues found.")?;
        return Ok(());
    }

    writeln!(w, "Found {} blocked issue(s):", blocked.len())?;
    writeln!(w)?;

    for (issue, blockers) in blocked {
        writeln!(
            w,
            "{} {}  {}  {}  {}",
            colored_status_icon(issue.status),
            colorize_id(issue.id.as_str()),
            type_icon(issue.issue_type),
            colorize_priority(issue.priority),
            issue.title
        )?;

        let blocked_by: Vec<String> = blockers
            .iter()
            .map(|b| {
                format!(
                    "{} ({})",
                    colorize_id(b.id.as_str()),
                    colorize_status(b.status)
                )
            })
            .collect();
        writeln!(w, "  {} {}", "Blocked by:".dimmed(), blocked_by.join(", "))?;
    }

    Ok(())
}

// ============================================================================
// JSON Formatting
// ============================================================================

fn print_issue_json<W: Write>(w: &mut W, issue: &Issue) -> io::Result<()> {
    let json = serde_json::to_string_pretty(issue)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    writeln!(w, "{}", json)
}

fn print_issues_json<W: Write>(w: &mut W, issues: &[Issue]) -> io::Result<()> {
    let json = serde_json::to_string_pretty(issues)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    writeln!(w, "{}", json)
}

#[derive(Serialize)]
struct IssueDetails<'a> {
    #[serde(flatten)]
    issue: &'a Issue,
    dependency_details: Vec<&'a Dependency>,
    dependent_details: Vec<&'a Dependency>,
}

fn print_issue_details_json<W: Write>(
    w: &mut W,
    issue: &Issue,
    deps: &[Dependency],
    dependents: &[Dependency],
) -> io::Result<()> {
    let details = IssueDetails {
        issue,
        dependency_details: deps.iter().collect(),
        dependent_details: dependents.iter().collect(),
    };

    let json = serde_json::to_string_pretty(&details)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    writeln!(w, "{}", json)
}

#[derive(Serialize)]
struct BlockedIssue<'a> {
    issue: &'a Issue,
    blocked_by: Vec<&'a Issue>,
}

fn print_blocked_json<W: Write>(w: &mut W, blocked: &[(Issue, Vec<Issue>)]) -> io::Result<()> {
    let items: Vec<BlockedIssue> = blocked
        .iter()
        .map(|(issue, blockers)| BlockedIssue {
            issue,
            blocked_by: blockers.iter().collect(),
        })
        .collect();

    let json = serde_json::to_string_pretty(&items)
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
    writeln!(w, "{}", json)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::{DependencyType, IssueId, IssueType};
    use chrono::Utc;
    use colored::control::set_override;
    use std::env;
    use std::sync::Mutex;

    // Mutex to prevent color tests from running in parallel (set_override is global state)
    static COLOR_TEST_MUTEX: Mutex<()> = Mutex::new(());

    fn test_issue() -> Issue {
        Issue {
            id: IssueId::new("test-abc"),
            title: "Test Issue".to_string(),
            description: "A test description".to_string(),
            status: IssueStatus::Open,
            priority: 1,
            issue_type: IssueType::Task,
            assignee: Some("alice".to_string()),
            labels: vec!["urgent".to_string()],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
            created_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        }
    }

    #[test]
    fn test_wrap_text() {
        let text = "This is a test of text wrapping functionality";
        let wrapped = wrap_text(text, 20);
        assert!(!wrapped.is_empty());
        for line in &wrapped {
            assert!(
                line.len() <= 20,
                "Line too long: '{}' ({} chars)",
                line,
                line.len()
            );
        }
    }

    #[test]
    fn test_wrap_text_preserves_newlines() {
        let text = "Line one\nLine two\nLine three";
        let wrapped = wrap_text(text, 50);
        assert_eq!(wrapped.len(), 3);
    }

    #[test]
    fn test_wrap_text_handles_long_words() {
        // Long URL that exceeds max_width
        let text = "Check out https://example.com/very/long/path/to/resource for details";
        let wrapped = wrap_text(text, 30);
        assert!(!wrapped.is_empty());
        // textwrap will break long words to fit
        for line in &wrapped {
            assert!(
                line.len() <= 30,
                "Line too long: '{}' ({} chars)",
                line,
                line.len()
            );
        }
    }

    #[test]
    fn test_colorize_status_contains_ansi_codes() {
        // Lock mutex to prevent race conditions with other color tests
        let _guard = COLOR_TEST_MUTEX.lock().unwrap();
        set_override(true);

        let open = colorize_status(IssueStatus::Open);
        let in_progress = colorize_status(IssueStatus::InProgress);
        let blocked = colorize_status(IssueStatus::Blocked);
        let closed = colorize_status(IssueStatus::Closed);

        // All should contain the status text
        assert!(open.contains("open"));
        assert!(in_progress.contains("in_progress"));
        assert!(blocked.contains("blocked"));
        assert!(closed.contains("closed"));

        // All should contain ANSI escape codes (\x1b[)
        assert!(open.contains("\x1b["), "Open status should have ANSI codes");
        assert!(
            in_progress.contains("\x1b["),
            "InProgress status should have ANSI codes"
        );
        assert!(
            blocked.contains("\x1b["),
            "Blocked status should have ANSI codes"
        );
        assert!(
            closed.contains("\x1b["),
            "Closed status should have ANSI codes"
        );

        set_override(false);
    }

    #[test]
    fn test_colorize_priority_contains_ansi_codes() {
        let _guard = COLOR_TEST_MUTEX.lock().unwrap();
        set_override(true);

        let p0 = colorize_priority(0);
        let p1 = colorize_priority(1);
        let p2 = colorize_priority(2);

        // Verify priority text is present
        assert!(p0.contains("P0"));
        assert!(p1.contains("P1"));
        assert!(p2.contains("P2"));

        // P0 (bold+red) and P1 (yellow) should have ANSI codes
        assert!(p0.contains("\x1b["), "P0 should have ANSI codes");
        assert!(p1.contains("\x1b["), "P1 should have ANSI codes");
        // P2 and higher have no color styling
        assert!(!p2.contains("\x1b["), "P2 should not have ANSI codes");

        set_override(false);
    }

    #[test]
    fn test_colorize_id_contains_ansi_codes() {
        let _guard = COLOR_TEST_MUTEX.lock().unwrap();
        set_override(true);

        let id = colorize_id("test-123");
        assert!(id.contains("test-123"));
        // Cyan color adds ANSI codes
        assert!(id.contains("\x1b["), "ID should have ANSI codes");

        set_override(false);
    }

    #[test]
    fn test_type_icon() {
        // Test all issue types including Chore
        assert_eq!(type_icon(IssueType::Task), "");
        assert_eq!(type_icon(IssueType::Bug), "");
        assert_eq!(type_icon(IssueType::Feature), "");
        assert_eq!(type_icon(IssueType::Epic), "");
        assert_eq!(type_icon(IssueType::Chore), "");
    }

    #[test]
    fn test_ascii_fallback_icons() {
        // Lock mutex since we modify global env state
        let _guard = COLOR_TEST_MUTEX.lock().unwrap();

        // Test ASCII mode by temporarily setting env var
        env::set_var("RIVETS_ASCII", "1");

        assert_eq!(type_icon(IssueType::Task), "-");
        assert_eq!(type_icon(IssueType::Bug), "*");
        assert_eq!(type_icon(IssueType::Feature), "+");
        assert_eq!(type_icon(IssueType::Epic), "#");
        assert_eq!(type_icon(IssueType::Chore), ".");

        // Status icons should also be ASCII
        let open = colored_status_icon(IssueStatus::Open);
        let closed = colored_status_icon(IssueStatus::Closed);
        assert!(open.contains("o"));
        assert!(closed.contains("+"));

        // Clean up
        env::remove_var("RIVETS_ASCII");
    }

    #[test]
    fn test_max_width_env_var() {
        // Lock mutex since we modify global env state
        let _guard = COLOR_TEST_MUTEX.lock().unwrap();

        // Test that get_max_content_width respects env var
        env::set_var("RIVETS_MAX_WIDTH", "120");
        assert_eq!(get_max_content_width(), 120);

        env::set_var("RIVETS_MAX_WIDTH", "invalid");
        assert_eq!(get_max_content_width(), DEFAULT_MAX_CONTENT_WIDTH);

        env::remove_var("RIVETS_MAX_WIDTH");
        assert_eq!(get_max_content_width(), DEFAULT_MAX_CONTENT_WIDTH);
    }

    #[test]
    fn test_print_issue_text() {
        let issue = test_issue();
        let mut buffer = Vec::new();

        print_issue_text(&mut buffer, &issue).unwrap();

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("test-abc"));
        assert!(output.contains("Test Issue"));
        assert!(output.contains("P1"));
        assert!(output.contains("alice"));
    }

    #[test]
    fn test_print_issue_json() {
        let issue = test_issue();
        let mut buffer = Vec::new();

        print_issue_json(&mut buffer, &issue).unwrap();

        let output = String::from_utf8(buffer).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
        assert_eq!(parsed["id"], "test-abc");
        assert_eq!(parsed["title"], "Test Issue");
    }

    #[test]
    fn test_print_issue_details_text() {
        let issue = test_issue();
        let deps = vec![Dependency {
            depends_on_id: IssueId::new("test-xyz"),
            dep_type: DependencyType::Blocks,
        }];
        let dependents = vec![];

        let mut buffer = Vec::new();
        print_issue_details_text(&mut buffer, &issue, &deps, &dependents).unwrap();

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("test-abc"));
        assert!(output.contains("Dependencies"));
        assert!(output.contains("test-xyz"));
        assert!(output.contains("blocks"));
    }

    #[test]
    fn test_print_issues_list_format() {
        let issues = vec![test_issue()];
        let mut buffer = Vec::new();

        print_issues_text(&mut buffer, &issues).unwrap();

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("Found 1 issue"));
        assert!(output.contains("test-abc"));
    }

    #[test]
    fn test_print_text_section_skips_empty_content() {
        let mut buffer = Vec::new();

        // Empty content should produce no output
        print_text_section(&mut buffer, "Description", "", 80).unwrap();
        assert!(buffer.is_empty(), "Empty content should produce no output");

        // Non-empty content should produce output
        print_text_section(&mut buffer, "Description", "Some text", 80).unwrap();
        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("Description:"));
        assert!(output.contains("Some text"));
    }

    #[test]
    fn test_print_optional_section_handles_none() {
        let mut buffer = Vec::new();

        // None should produce no output
        print_optional_section(&mut buffer, "Notes", &None, 80).unwrap();
        assert!(buffer.is_empty(), "None should produce no output");

        // Some with empty string should also produce no output
        let empty: Option<String> = Some(String::new());
        print_optional_section(&mut buffer, "Notes", &empty, 80).unwrap();
        assert!(buffer.is_empty(), "Empty Some should produce no output");

        // Some with content should produce output
        let content: Option<String> = Some("Important note".to_string());
        print_optional_section(&mut buffer, "Notes", &content, 80).unwrap();
        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("Notes:"));
        assert!(output.contains("Important note"));
    }

    #[test]
    fn test_issue_with_empty_description() {
        let mut issue = test_issue();
        issue.description = String::new();

        let mut buffer = Vec::new();
        print_issue_details_text(&mut buffer, &issue, &[], &[]).unwrap();

        let output = String::from_utf8(buffer).unwrap();
        // Should not contain "Description:" section when empty
        assert!(
            !output.contains("Description:"),
            "Empty description should not show Description section"
        );
    }

    #[test]
    fn test_wrap_text_with_narrow_width() {
        // Edge case: very narrow width
        let text = "Hello world";
        let wrapped = wrap_text(text, 5);
        assert!(!wrapped.is_empty());
        for line in &wrapped {
            assert!(line.len() <= 5, "Line '{}' exceeds width 5", line);
        }
    }

    #[test]
    fn test_wrap_text_with_wide_width() {
        // Edge case: width wider than content
        let text = "Short";
        let wrapped = wrap_text(text, 100);
        assert_eq!(wrapped.len(), 1);
        assert_eq!(wrapped[0], "Short");
    }

    #[test]
    fn test_wrap_text_empty_input() {
        let wrapped = wrap_text("", 80);
        // Empty string has no lines, so result is empty
        assert!(wrapped.is_empty() || (wrapped.len() == 1 && wrapped[0].is_empty()));
    }
}