autom8-cli 0.3.0

CLI automation tool for orchestrating Claude-powered development
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
//! Status and project display.
//!
//! Output functions for displaying run status, project trees, and descriptions.

use crate::state::{MachineState, RunState, RunStatus, SessionStatus};
use chrono::Utc;

use super::colors::*;

const WARNING_PANEL_WIDTH: usize = 60;

/// Print current run status.
pub fn print_status(state: &RunState) {
    println!("{BLUE}Run ID:{RESET}    {}", state.run_id);
    println!("{BLUE}Status:{RESET}    {:?}", state.status);
    println!("{BLUE}Spec:{RESET}      {}", state.spec_json_path.display());
    println!("{BLUE}Branch:{RESET}    {}", state.branch);
    if let Some(story) = &state.current_story {
        println!("{BLUE}Current:{RESET}   {}", story);
    }
    println!("{BLUE}Task:{RESET}      {}", state.iteration);
    println!(
        "{BLUE}Started:{RESET}   {}",
        state.started_at.format("%Y-%m-%d %H:%M:%S")
    );
    println!("{BLUE}Tasks run:{RESET}  {}", state.iterations.len());
}

/// Print global status across all projects.
pub fn print_global_status(statuses: &[crate::config::ProjectStatus]) {
    if statuses.is_empty() {
        println!("{GRAY}No projects found.{RESET}");
        println!();
        println!("Run {CYAN}autom8{RESET} in a project directory to create a project.");
        return;
    }

    let (needs_attention, idle): (Vec<_>, Vec<_>) =
        statuses.iter().partition(|s| s.needs_attention());

    if !needs_attention.is_empty() {
        println!("{BOLD}Projects needing attention:{RESET}");
        println!();

        for status in &needs_attention {
            let status_indicator = match status.run_status {
                Some(RunStatus::Running) => format!("{YELLOW}[running]{RESET}"),
                Some(RunStatus::Failed) => format!("{RED}[failed]{RESET}"),
                Some(RunStatus::Interrupted) => format!("{YELLOW}[interrupted]{RESET}"),
                Some(RunStatus::Completed) => String::new(),
                None => String::new(),
            };

            let spec_info = if status.incomplete_spec_count > 0 {
                format!(
                    " {CYAN}{} incomplete spec{}{RESET}",
                    status.incomplete_spec_count,
                    if status.incomplete_spec_count == 1 {
                        ""
                    } else {
                        "s"
                    }
                )
            } else {
                String::new()
            };

            if status_indicator.is_empty() {
                println!("  {BOLD}{}{RESET}{}", status.name, spec_info);
            } else {
                println!(
                    "  {BOLD}{}{RESET} {}{}",
                    status.name, status_indicator, spec_info
                );
            }
        }
        println!();
    }

    if !idle.is_empty() {
        println!("{GRAY}Idle projects:{RESET}");
        for status in &idle {
            println!("{GRAY}  {}{RESET}", status.name);
        }
        println!();
    }

    let active_count = statuses
        .iter()
        .filter(|s| s.run_status == Some(RunStatus::Running))
        .count();
    let failed_count = statuses
        .iter()
        .filter(|s| s.run_status == Some(RunStatus::Failed))
        .count();
    let incomplete_spec_total: usize = statuses.iter().map(|s| s.incomplete_spec_count).sum();

    println!(
        "{GRAY}({} project{}, {} active, {} failed, {} incomplete spec{}){RESET}",
        statuses.len(),
        if statuses.len() == 1 { "" } else { "s" },
        active_count,
        failed_count,
        incomplete_spec_total,
        if incomplete_spec_total == 1 { "" } else { "s" }
    );
}

/// Print a tree view of all projects in the config directory.
pub fn print_project_tree(projects: &[crate::config::ProjectTreeInfo]) {
    if projects.is_empty() {
        println!("{GRAY}No projects found in ~/.config/autom8/{RESET}");
        println!();
        println!("Run {CYAN}autom8{RESET} in a project directory to create a project.");
        return;
    }

    println!("{BOLD}~/.config/autom8/{RESET}");

    let total = projects.len();

    for (idx, project) in projects.iter().enumerate() {
        let is_last_project = idx == total - 1;
        let branch_char = if is_last_project { "" } else { "" };
        let cont_char = if is_last_project { " " } else { "" };

        let (status_indicator, status_color) = match project.run_status {
            Some(RunStatus::Running) => ("[running]", YELLOW),
            Some(RunStatus::Failed) => ("[failed]", RED),
            Some(RunStatus::Interrupted) => ("[interrupted]", YELLOW),
            Some(RunStatus::Completed) if project.incomplete_spec_count > 0 => {
                ("[incomplete]", CYAN)
            }
            Some(RunStatus::Completed) => ("[complete]", GREEN),
            None if project.incomplete_spec_count > 0 => ("[incomplete]", CYAN),
            None if project.has_content() => ("[idle]", GRAY),
            None => ("", GRAY),
        };

        if status_indicator.is_empty() {
            println!("{branch_char}── {BOLD}{}{RESET}", project.name);
        } else {
            println!(
                "{branch_char}── {BOLD}{}{RESET} {status_color}{status_indicator}{RESET}",
                project.name
            );
        }

        let subdirs = [
            ("spec", project.spec_md_count, "md"),
            ("spec", project.spec_count, "json"),
            ("runs", project.runs_count, "archived"),
        ];

        for (subidx, (name, count, unit)) in subdirs.iter().enumerate() {
            let is_last_subdir = subidx == subdirs.len() - 1;
            let sub_branch = if is_last_subdir { "" } else { "" };

            let count_str = if *count == 0 {
                format!("{GRAY}(empty){RESET}")
            } else if *count == 1 {
                format!("{GRAY}(1 {unit}){RESET}")
            } else {
                format!("{GRAY}({count} {unit}s){RESET}")
            };

            println!("{cont_char}   {sub_branch}── {name}/     {count_str}");
        }

        if !is_last_project {
            println!("{cont_char}");
        }
    }

    println!();
    let active_count = projects.iter().filter(|p| p.has_active_run).count();
    let failed_count = projects
        .iter()
        .filter(|p| p.run_status == Some(RunStatus::Failed))
        .count();
    let incomplete_total: usize = projects.iter().map(|p| p.incomplete_spec_count).sum();

    println!(
        "{GRAY}({} project{}, {} active, {} failed, {} incomplete spec{}){RESET}",
        total,
        if total == 1 { "" } else { "s" },
        active_count,
        failed_count,
        incomplete_total,
        if incomplete_total == 1 { "" } else { "s" }
    );
}

/// Print detailed description of a project.
pub fn print_project_description(desc: &crate::config::ProjectDescription) {
    println!("{BOLD}Project: {CYAN}{}{RESET}", desc.name);
    println!("{GRAY}Path: {}{RESET}", desc.path.display());
    println!();

    let status_indicator = match desc.run_status {
        Some(RunStatus::Running) => format!("{YELLOW}[running]{RESET}"),
        Some(RunStatus::Failed) => format!("{RED}[failed]{RESET}"),
        Some(RunStatus::Interrupted) => format!("{YELLOW}[interrupted]{RESET}"),
        Some(RunStatus::Completed) => format!("{GREEN}[completed]{RESET}"),
        None => format!("{GRAY}[idle]{RESET}"),
    };
    println!("{BOLD}Status:{RESET} {}", status_indicator);

    if let Some(branch) = &desc.current_branch {
        println!("{BLUE}Branch:{RESET} {}", branch);
    }

    if let Some(story) = &desc.current_story {
        println!("{BLUE}Current Story:{RESET} {}", story);
    }
    println!();

    if desc.specs.is_empty() {
        println!("{GRAY}No specs found.{RESET}");
    } else {
        println!("{BOLD}Specs:{RESET} ({} total)", desc.specs.len());
        println!();

        for spec in &desc.specs {
            print_spec_summary(spec);
        }
    }

    println!("{GRAY}─────────────────────────────────────────────────────────{RESET}");
    println!(
        "{GRAY}Files: {} spec md, {} spec json, {} archived runs{RESET}",
        desc.spec_md_count,
        desc.specs.len(),
        desc.runs_count
    );
}

/// Print summary of a single spec.
///
/// Shows full details (with user stories) only for the active spec.
/// All other specs (including when no spec is active) show condensed view.
fn print_spec_summary(spec: &crate::config::SpecSummary) {
    // Show "(Active)" indicator for the active spec
    let active_label = if spec.is_active {
        format!(" {YELLOW}(Active){RESET}")
    } else {
        String::new()
    };

    println!(
        "{CYAN}━━━{RESET} {BOLD}{}{RESET}{}",
        spec.filename, active_label
    );

    // Only show full details for the active spec
    // All other specs (or when no spec is active) show condensed view
    if !spec.is_active {
        let desc_preview = if spec.description.len() > 80 {
            format!("{}...", &spec.description[..80])
        } else {
            spec.description.clone()
        };
        let first_line = desc_preview.lines().next().unwrap_or(&desc_preview);
        println!("{GRAY}{}{RESET}", first_line);
        println!(
            "{GRAY}({}/{} stories complete){RESET}",
            spec.completed_count, spec.total_count
        );
        println!();
        return;
    }

    // Full display for active spec only
    println!("{BLUE}Project:{RESET} {}", spec.project_name);
    println!("{BLUE}Branch:{RESET}  {}", spec.branch_name);

    let desc_preview = if spec.description.len() > 100 {
        format!("{}...", &spec.description[..100])
    } else {
        spec.description.clone()
    };
    let first_line = desc_preview.lines().next().unwrap_or(&desc_preview);
    println!("{BLUE}Description:{RESET} {}", first_line);
    println!();

    let progress_bar = make_progress_bar_simple(spec.completed_count, spec.total_count, 12);
    let progress_color = if spec.completed_count == spec.total_count {
        GREEN
    } else if spec.completed_count == 0 {
        GRAY
    } else {
        YELLOW
    };
    println!(
        "{BOLD}Progress:{RESET} [{}] {}{}/{} stories complete{}",
        progress_bar, progress_color, spec.completed_count, spec.total_count, RESET
    );
    println!();

    println!("{BOLD}User Stories:{RESET}");
    for story in &spec.stories {
        let status_icon = if story.passes {
            format!("{GREEN}{RESET}")
        } else {
            format!("{GRAY}{RESET}")
        };
        let title_color = if story.passes { GREEN } else { RESET };
        println!(
            "  {} {BOLD}{}{RESET}: {}{}{}",
            status_icon, story.id, title_color, story.title, RESET
        );
    }
    println!();
}

fn make_progress_bar_simple(completed: usize, total: usize, width: usize) -> String {
    if total == 0 {
        return " ".repeat(width);
    }
    let filled = (completed * width) / total;
    let empty = width - filled;
    format!(
        "{GREEN}{}{RESET}{GRAY}{}{RESET}",
        "".repeat(filled),
        "".repeat(empty)
    )
}

/// Print history entry.
pub fn print_history_entry(state: &RunState, index: usize) {
    let status_color = match state.status {
        RunStatus::Completed => GREEN,
        RunStatus::Failed => RED,
        _ => YELLOW,
    };
    println!(
        "{}. [{}{:?}{}] {} - {} ({} tasks)",
        index + 1,
        status_color,
        state.status,
        RESET,
        state.started_at.format("%Y-%m-%d %H:%M"),
        state.branch,
        state.iterations.len()
    );
}

/// Print a prominent warning panel for missing spec file.
pub fn print_missing_spec_warning(branch_name: &str, spec_path: &str) {
    let top_border = format!("{}", "".repeat(WARNING_PANEL_WIDTH - 2));
    let bottom_border = format!("{}", "".repeat(WARNING_PANEL_WIDTH - 2));
    let separator = format!("{}", "".repeat(WARNING_PANEL_WIDTH - 2));

    println!();
    println!("{YELLOW}{BOLD}{}{RESET}", top_border);

    let header = " ⚠  NO SPEC FILE FOUND ";
    let header_padding = WARNING_PANEL_WIDTH.saturating_sub(header.len() + 2);
    let left_pad = header_padding / 2;
    let right_pad = header_padding - left_pad;
    println!(
        "{YELLOW}{BOLD}{}{}{}{RESET}",
        " ".repeat(left_pad),
        header,
        " ".repeat(right_pad)
    );

    println!("{YELLOW}{}{RESET}", separator);

    print_warning_panel_line("The PR review will proceed with reduced context.");
    print_warning_panel_line("");
    print_warning_panel_line(&format!("Branch: {}", branch_name));

    let max_path_len = WARNING_PANEL_WIDTH - 12;
    let display_path = if spec_path.len() > max_path_len {
        format!("...{}", &spec_path[spec_path.len() - max_path_len + 3..])
    } else {
        spec_path.to_string()
    };
    print_warning_panel_line(&format!("Expected: {}", display_path));

    println!("{YELLOW}{}{RESET}", separator);

    print_warning_panel_line("Create a spec file to provide full context:");
    print_warning_panel_line("  autom8 --spec <spec.md>");

    println!("{YELLOW}{BOLD}{}{RESET}", bottom_border);
    println!();
}

fn print_warning_panel_line(text: &str) {
    let max_width = WARNING_PANEL_WIDTH - 4;
    let display_text = if text.len() > max_width {
        &text[..max_width]
    } else {
        text
    };
    let padding = max_width.saturating_sub(display_text.len());
    println!(
        "{YELLOW}{RESET} {}{} {YELLOW}{RESET}",
        display_text,
        " ".repeat(padding)
    );
}

/// Print a summary of the branch context being used.
pub fn print_branch_context_summary(has_spec: bool, commit_count: usize, branch_name: &str) {
    println!();
    println!("{CYAN}Branch Context:{RESET} {}", branch_name);

    if has_spec {
        println!("{GREEN}  ✓ Spec file loaded{RESET}");
    } else {
        println!("{YELLOW}  ⚠ No spec file (reduced context){RESET}");
    }

    println!(
        "{BLUE}  {} commit{} on branch{RESET}",
        commit_count,
        if commit_count == 1 { "" } else { "s" }
    );
    println!();
}

/// Print a list of commits for display.
pub fn print_commit_list(commits: &[crate::git::CommitInfo], max_display: usize) {
    if commits.is_empty() {
        println!("{GRAY}No commits found on this branch.{RESET}");
        return;
    }

    let display_count = commits.len().min(max_display);
    println!("{BOLD}Recent Commits:{RESET}");

    for commit in commits.iter().take(display_count) {
        let max_msg_len = 50;
        let display_msg = if commit.message.len() > max_msg_len {
            format!("{}...", &commit.message[..max_msg_len - 3])
        } else {
            commit.message.clone()
        };

        println!("  {CYAN}{}{RESET} {}", commit.short_hash, display_msg);
    }

    if commits.len() > max_display {
        println!(
            "{GRAY}  ... and {} more commit{}{RESET}",
            commits.len() - max_display,
            if commits.len() - max_display == 1 {
                ""
            } else {
                "s"
            }
        );
    }
    println!();
}

/// Print status for all sessions in a project.
///
/// Sessions are displayed with the current session highlighted, including:
/// - Session ID and worktree path
/// - Branch name and current state
/// - Current story (if any)
/// - Duration since start
pub fn print_sessions_status(sessions: &[SessionStatus]) {
    println!("{BOLD}Sessions for this project:{RESET}");
    println!();

    for session in sessions {
        print_session_row(session);
    }

    // Summary line
    let running_count = sessions
        .iter()
        .filter(|s| s.metadata.is_running && !s.is_stale)
        .count();
    let stale_count = sessions.iter().filter(|s| s.is_stale).count();

    println!();
    print!(
        "{GRAY}({} session{}",
        sessions.len(),
        if sessions.len() == 1 { "" } else { "s" }
    );
    if running_count > 0 {
        print!(", {} running", running_count);
    }
    if stale_count > 0 {
        print!(", {} stale", stale_count);
    }
    println!("){RESET}");
}

/// Print a single session row.
fn print_session_row(session: &SessionStatus) {
    let metadata = &session.metadata;

    // Determine row color based on state
    let (indicator, indicator_color) = if session.is_stale {
        ("", GRAY)
    } else if session.is_current {
        ("", GREEN)
    } else if metadata.is_running {
        ("", YELLOW)
    } else {
        ("", GRAY)
    };

    // Session ID and current marker
    let current_marker = if session.is_current { " (current)" } else { "" };
    let stale_marker = if session.is_stale { " [stale]" } else { "" };

    println!(
        "{indicator_color}{indicator}{RESET} {BOLD}{}{RESET}{GREEN}{}{RESET}{GRAY}{}{RESET}",
        metadata.session_id, current_marker, stale_marker
    );

    // Worktree path (truncated if too long)
    let path_str = metadata.worktree_path.display().to_string();
    let display_path = if path_str.len() > 60 {
        format!("...{}", &path_str[path_str.len() - 57..])
    } else {
        path_str
    };
    println!("  {GRAY}Path:{RESET}    {}", display_path);

    // Branch name
    println!("  {BLUE}Branch:{RESET}  {}", metadata.branch_name);

    // Current state
    if let Some(state) = &session.machine_state {
        let state_str = format_machine_state(state);
        let state_color = machine_state_color(state);
        println!("  {BLUE}State:{RESET}   {state_color}{}{RESET}", state_str);
    }

    // Current story (if any)
    if let Some(story) = &session.current_story {
        println!("  {BLUE}Story:{RESET}   {}", story);
    }

    // Duration
    let duration = format_duration(metadata.created_at, metadata.last_active_at);
    println!(
        "  {GRAY}Started:{RESET} {} {}",
        metadata.created_at.format("%Y-%m-%d %H:%M"),
        duration
    );

    println!();
}

/// Format machine state for display.
fn format_machine_state(state: &MachineState) -> &'static str {
    match state {
        MachineState::Idle => "Idle",
        MachineState::LoadingSpec => "Loading Spec",
        MachineState::GeneratingSpec => "Generating Spec",
        MachineState::Initializing => "Initializing",
        MachineState::PickingStory => "Picking Story",
        MachineState::RunningClaude => "Running Claude",
        MachineState::Reviewing => "Reviewing",
        MachineState::Correcting => "Correcting",
        MachineState::Committing => "Committing",
        MachineState::CreatingPR => "Creating PR",
        MachineState::Completed => "Completed",
        MachineState::Failed => "Failed",
    }
}

/// Get color for machine state.
fn machine_state_color(state: &MachineState) -> &'static str {
    match state {
        MachineState::Completed => GREEN,
        MachineState::Failed => RED,
        MachineState::RunningClaude | MachineState::Reviewing | MachineState::Correcting => YELLOW,
        _ => CYAN,
    }
}

/// Format duration since session start.
fn format_duration(
    created_at: chrono::DateTime<chrono::Utc>,
    last_active_at: chrono::DateTime<chrono::Utc>,
) -> String {
    let now = Utc::now();
    let duration = now.signed_duration_since(created_at);

    // Calculate active duration
    let active_duration = last_active_at.signed_duration_since(created_at);

    let days = duration.num_days();
    let hours = duration.num_hours() % 24;
    let minutes = duration.num_minutes() % 60;

    let age_str = if days > 0 {
        format!("{}d {}h ago", days, hours)
    } else if hours > 0 {
        format!("{}h {}m ago", hours, minutes)
    } else if minutes > 0 {
        format!("{}m ago", minutes)
    } else {
        "just now".to_string()
    };

    // Show active duration if significantly different from total
    let active_hours = active_duration.num_hours();
    let active_mins = active_duration.num_minutes() % 60;
    if active_hours > 0 {
        format!("{} (active {}h {}m)", age_str, active_hours, active_mins)
    } else if active_mins > 5 {
        format!("{} (active {}m)", age_str, active_mins)
    } else {
        age_str
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::SessionMetadata;
    use std::path::PathBuf;

    fn make_session_status(
        session_id: &str,
        branch: &str,
        is_current: bool,
        is_stale: bool,
        is_running: bool,
        machine_state: Option<MachineState>,
        current_story: Option<&str>,
    ) -> SessionStatus {
        SessionStatus {
            metadata: SessionMetadata {
                session_id: session_id.to_string(),
                worktree_path: PathBuf::from(format!("/projects/test-wt-{}", session_id)),
                branch_name: branch.to_string(),
                created_at: Utc::now(),
                last_active_at: Utc::now(),
                is_running,
                spec_json_path: None,
            },
            machine_state,
            current_story: current_story.map(|s| s.to_string()),
            is_current,
            is_stale,
        }
    }

    // ======================================================================
    // Tests for US-006: Verify status --all command output
    // ======================================================================

    #[test]
    fn test_us006_format_machine_state_all_variants() {
        // Verify all machine states have display names
        assert_eq!(format_machine_state(&MachineState::Idle), "Idle");
        assert_eq!(
            format_machine_state(&MachineState::LoadingSpec),
            "Loading Spec"
        );
        assert_eq!(
            format_machine_state(&MachineState::GeneratingSpec),
            "Generating Spec"
        );
        assert_eq!(
            format_machine_state(&MachineState::Initializing),
            "Initializing"
        );
        assert_eq!(
            format_machine_state(&MachineState::PickingStory),
            "Picking Story"
        );
        assert_eq!(
            format_machine_state(&MachineState::RunningClaude),
            "Running Claude"
        );
        assert_eq!(format_machine_state(&MachineState::Reviewing), "Reviewing");
        assert_eq!(
            format_machine_state(&MachineState::Correcting),
            "Correcting"
        );
        assert_eq!(
            format_machine_state(&MachineState::Committing),
            "Committing"
        );
        assert_eq!(
            format_machine_state(&MachineState::CreatingPR),
            "Creating PR"
        );
        assert_eq!(format_machine_state(&MachineState::Completed), "Completed");
        assert_eq!(format_machine_state(&MachineState::Failed), "Failed");
    }

    #[test]
    fn test_us006_machine_state_colors() {
        // Verify appropriate colors for different states
        assert_eq!(machine_state_color(&MachineState::Completed), GREEN);
        assert_eq!(machine_state_color(&MachineState::Failed), RED);
        assert_eq!(machine_state_color(&MachineState::RunningClaude), YELLOW);
        assert_eq!(machine_state_color(&MachineState::Reviewing), YELLOW);
        assert_eq!(machine_state_color(&MachineState::Correcting), YELLOW);
        // Other states use CYAN
        assert_eq!(machine_state_color(&MachineState::Idle), CYAN);
        assert_eq!(machine_state_color(&MachineState::Initializing), CYAN);
    }

    #[test]
    fn test_us006_session_row_current_marker() {
        // Test that current session gets → indicator and (current) marker
        let session = make_session_status(
            "main",
            "feature/test",
            true,  // is_current
            false, // is_stale
            true,  // is_running
            Some(MachineState::RunningClaude),
            Some("US-001"),
        );

        // Verify the logic for current marker
        let current_marker = if session.is_current { " (current)" } else { "" };
        assert_eq!(current_marker, " (current)");

        // Verify indicator for current session
        let (indicator, _) = if session.is_stale {
            ("", GRAY)
        } else if session.is_current {
            ("", GREEN)
        } else if session.metadata.is_running {
            ("", YELLOW)
        } else {
            ("", GRAY)
        };
        assert_eq!(indicator, "");
    }

    #[test]
    fn test_us006_session_row_stale_marker() {
        // Test that stale sessions get [stale] marker
        let session = make_session_status(
            "abc12345",
            "feature/old",
            false, // is_current
            true,  // is_stale
            true,  // is_running
            Some(MachineState::RunningClaude),
            None,
        );

        let stale_marker = if session.is_stale { " [stale]" } else { "" };
        assert_eq!(stale_marker, " [stale]");

        // Stale sessions get ✗ indicator regardless of other status
        let (indicator, indicator_color) = if session.is_stale {
            ("", GRAY)
        } else if session.is_current {
            ("", GREEN)
        } else if session.metadata.is_running {
            ("", YELLOW)
        } else {
            ("", GRAY)
        };
        assert_eq!(indicator, "");
        assert_eq!(indicator_color, GRAY);
    }

    #[test]
    fn test_us006_session_row_running_indicator() {
        // Test that running (but not current) sessions get ● indicator
        let session = make_session_status(
            "session1",
            "feature/parallel",
            false, // is_current
            false, // is_stale
            true,  // is_running
            Some(MachineState::Reviewing),
            Some("US-002"),
        );

        let (indicator, indicator_color) = if session.is_stale {
            ("", GRAY)
        } else if session.is_current {
            ("", GREEN)
        } else if session.metadata.is_running {
            ("", YELLOW)
        } else {
            ("", GRAY)
        };
        assert_eq!(indicator, "");
        assert_eq!(indicator_color, YELLOW);
    }

    #[test]
    fn test_us006_session_row_idle_indicator() {
        // Test that idle sessions get ○ indicator
        let session = make_session_status(
            "session2",
            "feature/done",
            false, // is_current
            false, // is_stale
            false, // is_running
            Some(MachineState::Completed),
            None,
        );

        let (indicator, indicator_color) = if session.is_stale {
            ("", GRAY)
        } else if session.is_current {
            ("", GREEN)
        } else if session.metadata.is_running {
            ("", YELLOW)
        } else {
            ("", GRAY)
        };
        assert_eq!(indicator, "");
        assert_eq!(indicator_color, GRAY);
    }

    #[test]
    fn test_us006_summary_counts() {
        // Test that summary correctly counts running and stale sessions
        let sessions = vec![
            make_session_status(
                "main",
                "main",
                true,
                false,
                true,
                Some(MachineState::RunningClaude),
                Some("US-001"),
            ),
            make_session_status(
                "session1",
                "feat-1",
                false,
                false,
                true,
                Some(MachineState::Reviewing),
                Some("US-002"),
            ),
            make_session_status("session2", "feat-2", false, true, false, None, None), // stale
            make_session_status(
                "session3",
                "feat-3",
                false,
                false,
                false,
                Some(MachineState::Completed),
                None,
            ), // idle
        ];

        // Running count: sessions that are running AND not stale
        let running_count = sessions
            .iter()
            .filter(|s| s.metadata.is_running && !s.is_stale)
            .count();
        assert_eq!(running_count, 2);

        // Stale count
        let stale_count = sessions.iter().filter(|s| s.is_stale).count();
        assert_eq!(stale_count, 1);

        // Total count
        assert_eq!(sessions.len(), 4);
    }

    #[test]
    fn test_us006_worktree_path_truncation() {
        // Test that long worktree paths are truncated properly
        let long_path =
            "/very/long/path/that/exceeds/sixty/characters/for/display/purposes/test-worktree";
        assert!(long_path.len() > 60);

        let display_path = if long_path.len() > 60 {
            format!("...{}", &long_path[long_path.len() - 57..])
        } else {
            long_path.to_string()
        };

        assert!(display_path.starts_with("..."));
        assert!(display_path.len() <= 60);
    }

    #[test]
    fn test_us006_session_status_displays_all_fields() {
        // Verify SessionStatus contains all required display fields per acceptance criteria:
        // - session ID, worktree path, branch, state, current story
        let session = make_session_status(
            "abc12345",                        // session_id
            "feature/test",                    // branch
            true,                              // is_current (for highlighting)
            false,                             // is_stale
            true,                              // is_running
            Some(MachineState::RunningClaude), // state
            Some("US-001"),                    // current story
        );

        // Session ID
        assert_eq!(session.metadata.session_id, "abc12345");

        // Worktree path
        assert!(session
            .metadata
            .worktree_path
            .to_string_lossy()
            .contains("abc12345"));

        // Branch
        assert_eq!(session.metadata.branch_name, "feature/test");

        // State
        assert_eq!(session.machine_state, Some(MachineState::RunningClaude));

        // Current story
        assert_eq!(session.current_story, Some("US-001".to_string()));

        // Current session highlighting
        assert!(session.is_current);
    }
}