stax 0.85.0

Fast stacked Git branches and PRs
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
use crate::cache::CiCache;
use crate::ci::CheckRunInfo;
use crate::config::Config;
use crate::engine::{BranchMetadata, PrInfo, Stack};
use crate::forge::{forge_token, ForgeClient};
use crate::git::GitRepo;
use crate::github::pr::{CiStatus, PrMergeStatus};
use crate::remote::RemoteInfo;
use anyhow::{Context, Result};
use chrono::Local;
use console::{measure_text_width, truncate_str};
use futures_util::{stream, StreamExt};
use serde::Serialize;
use std::collections::HashMap;
use std::io::IsTerminal;
use std::path::PathBuf;

pub(crate) const READY_FETCH_CONCURRENCY: usize = 6;

const ACTION_WIDTH: usize = 7;
const PR_WIDTH: usize = 7;
const REVIEWS_MIN_WIDTH: usize = 7;
const CI_MIN_WIDTH: usize = 6;
const BRANCH_MIN_WIDTH: usize = 18;
const BRANCH_MAX_WIDTH: usize = 52;
const TITLE_MIN_WIDTH: usize = 16;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadyAction {
    Fix,
    Merge,
    Ping,
    Wait,
    Draft,
}

impl ReadyAction {
    fn label(self) -> &'static str {
        match self {
            ReadyAction::Fix => "fix",
            ReadyAction::Merge => "merge",
            ReadyAction::Ping => "ping",
            ReadyAction::Wait => "wait",
            ReadyAction::Draft => "draft",
        }
    }

    fn symbol(self) -> &'static str {
        match self {
            ReadyAction::Fix => "",
            ReadyAction::Merge => "",
            ReadyAction::Ping => "",
            ReadyAction::Wait => "",
            ReadyAction::Draft => "",
        }
    }

    pub fn display(self) -> String {
        format!("{} {}", self.symbol(), self.label())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadyReason {
    Ready,
    ReviewRequired,
    CiFailed,
    ChangesRequested,
    NotMergeable,
    CiPending,
    MergeablePending,
    Draft,
    Closed,
    Unknown,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CiSummary {
    status: CiStatus,
    text: String,
}

impl CiSummary {
    fn from_checks(status: CiStatus, checks: &[CheckRunInfo]) -> Self {
        match status {
            CiStatus::Failure => {
                let failed = checks
                    .iter()
                    .filter(|check| {
                        check.status == "completed"
                            && matches!(
                                check.conclusion.as_deref(),
                                Some("failure") | Some("timed_out") | Some("action_required")
                            )
                    })
                    .count()
                    .max(1);
                Self::failed(failed)
            }
            CiStatus::Pending => Self::running(),
            CiStatus::Success => Self::passed(),
            CiStatus::NoCi => Self::no_ci(),
        }
    }

    fn passed() -> Self {
        Self {
            status: CiStatus::Success,
            text: "passed".to_string(),
        }
    }

    fn failed(count: usize) -> Self {
        Self {
            status: CiStatus::Failure,
            text: format!("{} {}", count, if count == 1 { "failed" } else { "failed" }),
        }
    }

    fn running() -> Self {
        Self {
            status: CiStatus::Pending,
            text: "running".to_string(),
        }
    }

    fn no_ci() -> Self {
        Self {
            status: CiStatus::NoCi,
            text: "no CI".to_string(),
        }
    }

    #[cfg(test)]
    fn not_run() -> Self {
        Self {
            status: CiStatus::NoCi,
            text: "not run".to_string(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct PrReadinessRow {
    pub branch: String,
    pub pr_number: u64,
    pub title: String,
    pub updated_at: Option<String>,
    pub action: ReadyAction,
    pub reason: ReadyReason,
    pub review_decision: Option<String>,
    pub approvals: usize,
    pub changes_requested: bool,
    pub ci_status: String,
    pub ci_summary: String,
    pub is_draft: bool,
    pub mergeable: Option<bool>,
    pub mergeable_state: String,
    pub pr_url: Option<String>,
    #[serde(skip)]
    pub review_summary: String,
    #[serde(skip)]
    pub pr_state: String,
}

impl PrReadinessRow {
    pub fn from_status(branch: &str, status: PrMergeStatus, ci_summary: CiSummary) -> Self {
        let review_summary = review_summary(&status);
        let (action, reason) = classify_status(&status, &ci_summary);
        let ci_status = match ci_summary.status {
            CiStatus::Success => "success",
            CiStatus::Pending => "pending",
            CiStatus::Failure => "failure",
            CiStatus::NoCi => "no_ci",
        }
        .to_string();

        Self {
            branch: branch.to_string(),
            pr_number: status.number,
            title: status.title,
            updated_at: status.updated_at,
            action,
            reason,
            review_decision: status.review_decision,
            approvals: status.approvals,
            changes_requested: status.changes_requested,
            ci_status,
            ci_summary: ci_summary.text,
            is_draft: status.is_draft,
            mergeable: status.mergeable,
            mergeable_state: status.mergeable_state,
            pr_url: None,
            review_summary,
            pr_state: status.state,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadyBranch {
    pub name: String,
    pub pr_number: Option<u64>,
}

#[derive(Debug, Clone)]
pub struct ReadyScope {
    pub git_dir: PathBuf,
    pub repo_label: String,
    pub scope_label: String,
    pub branches: Vec<ReadyBranch>,
    pub remote: RemoteInfo,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReadyRowState {
    Loading {
        branch: ReadyBranch,
    },
    Loaded(PrReadinessRow),
    Unavailable {
        branch: ReadyBranch,
        message: String,
    },
}

impl ReadyRowState {
    pub fn branch(&self) -> &str {
        match self {
            ReadyRowState::Loading { branch } => &branch.name,
            ReadyRowState::Loaded(row) => &row.branch,
            ReadyRowState::Unavailable { branch, .. } => &branch.name,
        }
    }

    pub fn pr_number(&self) -> Option<u64> {
        match self {
            ReadyRowState::Loading { branch } => branch.pr_number,
            ReadyRowState::Loaded(row) => Some(row.pr_number),
            ReadyRowState::Unavailable { branch, .. } => branch.pr_number,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadyScopeMode {
    AllTracked,
    CurrentStack,
}

impl ReadyScopeMode {
    pub fn from_flags(_all: bool, current: bool, stack: bool) -> Self {
        if current || stack {
            Self::CurrentStack
        } else {
            Self::AllTracked
        }
    }

    fn include_all(self) -> bool {
        matches!(self, Self::AllTracked)
    }

    fn label(self) -> &'static str {
        match self {
            Self::AllTracked => "all tracked PRs",
            Self::CurrentStack => "current stack",
        }
    }
}

pub fn run(scope_mode: ReadyScopeMode, json: bool, plain: bool) -> Result<()> {
    if json {
        return run_static(scope_mode, true);
    }

    if !plain && std::io::stdin().is_terminal() && std::io::stdout().is_terminal() {
        return crate::tui::ready::run(scope_mode);
    }

    run_static(scope_mode, false)
}

fn run_static(scope_mode: ReadyScopeMode, json: bool) -> Result<()> {
    let repo = GitRepo::open()?;
    let current = repo.current_branch()?;
    let stack = Stack::load(&repo)?;
    let config = Config::load()?;
    let remote = RemoteInfo::from_repo(&repo, &config)?;

    if forge_token(remote.forge).is_none() {
        anyhow::bail!(
            "{} auth not configured; live PR readiness cannot be fetched.",
            remote.forge
        );
    }

    let rt = tokio::runtime::Runtime::new()?;
    let _enter = rt.enter();
    let client = ForgeClient::new(&remote)?;
    let (mut rows, skipped) = rt.block_on(async {
        fetch_readiness_rows(&repo, &client, &remote, &stack, &current, scope_mode).await
    })?;
    let branch_order = branch_scope(&stack, &current, scope_mode);
    sort_ready_rows(
        &mut rows,
        &branch_order
            .iter()
            .map(|branch| branch.name.as_str())
            .collect::<Vec<_>>(),
    );

    if json {
        println!("{}", serde_json::to_string_pretty(&rows)?);
        return Ok(());
    }

    let repo_label = format!("{}/{}", remote.namespace, remote.repo);
    let summary = readiness_summary(scope_mode.label(), rows.len(), skipped);
    let width = crate::commands::github_list::terminal_width().max(80);
    println!(
        "{}",
        render_ready_table(&repo_label, &summary, &rows, width)
    );
    Ok(())
}

pub(crate) fn load_ready_scope(scope_mode: ReadyScopeMode) -> Result<ReadyScope> {
    let repo = GitRepo::open()?;
    let current = repo.current_branch()?;
    let stack = Stack::load(&repo)?;
    let config = Config::load()?;
    let remote = RemoteInfo::from_repo(&repo, &config)?;

    if forge_token(remote.forge).is_none() {
        anyhow::bail!(
            "{} auth not configured; live PR readiness cannot be fetched.",
            remote.forge
        );
    }

    let branches = branch_scope(&stack, &current, scope_mode);
    Ok(ReadyScope {
        git_dir: repo.git_dir()?.to_path_buf(),
        repo_label: format!("{}/{}", remote.namespace, remote.repo),
        scope_label: scope_mode.label().to_string(),
        branches,
        remote,
    })
}

pub(crate) async fn fetch_row_for_branch(
    repo: &GitRepo,
    client: &ForgeClient,
    remote: &RemoteInfo,
    stack: &Stack,
    branch: &ReadyBranch,
) -> Result<Option<PrReadinessRow>> {
    let Some(pr_number) = resolve_branch_pr(client, stack, branch).await? else {
        return Ok(None);
    };

    let status = client
        .get_pr_merge_status(pr_number)
        .await
        .with_context(|| format!("Failed to fetch live readiness for PR #{}", pr_number))?;

    let ci_summary = CiSummary::from_checks(status.ci_status.clone(), &[]);
    let mut row = PrReadinessRow::from_status(&branch.name, status, ci_summary);
    row.pr_url = Some(remote.pr_url(pr_number));
    let _ = warm_caches_for_ready_row(repo, &row);
    Ok(Some(row))
}

pub(crate) fn warm_caches_for_ready_row(repo: &GitRepo, row: &PrReadinessRow) -> Result<()> {
    let git_dir = repo.git_dir()?;
    let mut cache = CiCache::load(git_dir);
    cache.update(
        &row.branch,
        Some(row.ci_status.clone()),
        Some(ready_row_pr_cache_state(row)),
    );
    cache.mark_refreshed();
    cache.save(git_dir)?;

    if let Some(mut meta) = BranchMetadata::read(repo.inner(), &row.branch)? {
        meta.pr_info = Some(PrInfo {
            number: row.pr_number,
            state: row.pr_state.clone(),
            is_draft: Some(row.is_draft),
        });
        meta.write(repo.inner(), &row.branch)?;
    }

    Ok(())
}

fn ready_row_pr_cache_state(row: &PrReadinessRow) -> String {
    if row.is_draft {
        "draft".to_string()
    } else {
        row.pr_state.clone()
    }
}

async fn fetch_readiness_rows(
    repo: &GitRepo,
    client: &ForgeClient,
    remote: &RemoteInfo,
    stack: &Stack,
    current: &str,
    scope_mode: ReadyScopeMode,
) -> Result<(Vec<PrReadinessRow>, usize)> {
    let branches = branch_scope(stack, current, scope_mode);
    let mut rows = Vec::new();
    let mut skipped = 0usize;

    let mut pending = stream::iter(
        branches
            .iter()
            .map(|branch| fetch_row_for_branch(repo, client, remote, stack, branch)),
    )
    .buffer_unordered(READY_FETCH_CONCURRENCY);

    while let Some(result) = pending.next().await {
        match result? {
            Some(row) => rows.push(row),
            None => skipped += 1,
        }
    }

    Ok((rows, skipped))
}

fn branch_scope(stack: &Stack, current: &str, scope_mode: ReadyScopeMode) -> Vec<ReadyBranch> {
    if scope_mode.include_all() {
        let mut branches = stack
            .branches
            .keys()
            .filter(|branch| *branch != &stack.trunk)
            .cloned()
            .collect::<Vec<_>>();
        branches.sort();
        branches
            .into_iter()
            .map(|name| ReadyBranch {
                pr_number: stack.branches.get(&name).and_then(|info| info.pr_number),
                name,
            })
            .collect()
    } else {
        stack
            .current_stack(current)
            .into_iter()
            .filter(|branch| branch != &stack.trunk)
            .map(|name| ReadyBranch {
                pr_number: stack.branches.get(&name).and_then(|info| info.pr_number),
                name,
            })
            .collect()
    }
}

async fn resolve_branch_pr(
    client: &ForgeClient,
    stack: &Stack,
    branch: &ReadyBranch,
) -> Result<Option<u64>> {
    if let Some(number) = branch.pr_number {
        return Ok(Some(number));
    }

    if let Some(number) = stack
        .branches
        .get(&branch.name)
        .and_then(|info| info.pr_number)
    {
        return Ok(Some(number));
    }

    Ok(client.find_pr(&branch.name).await?.map(|info| info.number))
}

fn classify_status(status: &PrMergeStatus, ci_summary: &CiSummary) -> (ReadyAction, ReadyReason) {
    if status.is_draft {
        return (ReadyAction::Draft, ReadyReason::Draft);
    }
    if !status.state.eq_ignore_ascii_case("open") {
        return (ReadyAction::Fix, ReadyReason::Closed);
    }
    if status.changes_requested || status.review_decision.as_deref() == Some("CHANGES_REQUESTED") {
        return (ReadyAction::Fix, ReadyReason::ChangesRequested);
    }
    if status.ci_status.is_failure() || ci_summary.status.is_failure() {
        return (ReadyAction::Fix, ReadyReason::CiFailed);
    }
    if status.mergeable == Some(false) {
        return (ReadyAction::Fix, ReadyReason::NotMergeable);
    }
    if status.ci_status.is_pending() || ci_summary.status.is_pending() {
        return (ReadyAction::Wait, ReadyReason::CiPending);
    }
    if status.mergeable.is_none() {
        return (ReadyAction::Wait, ReadyReason::MergeablePending);
    }
    if status.mergeable == Some(true)
        && status.ci_status.is_success()
        && matches!(status.review_decision.as_deref(), Some("APPROVED") | None)
    {
        return (ReadyAction::Merge, ReadyReason::Ready);
    }
    if status.review_decision.as_deref() == Some("REVIEW_REQUIRED") {
        return (ReadyAction::Ping, ReadyReason::ReviewRequired);
    }

    if status.mergeable == Some(true) && status.ci_status.is_success() {
        return (ReadyAction::Merge, ReadyReason::Ready);
    }

    (ReadyAction::Wait, ReadyReason::Unknown)
}

fn review_summary(status: &PrMergeStatus) -> String {
    if status.is_draft {
        return "draft".to_string();
    }
    if status.changes_requested || status.review_decision.as_deref() == Some("CHANGES_REQUESTED") {
        return "changes requested".to_string();
    }
    if status.review_decision.as_deref() == Some("REVIEW_REQUIRED") {
        return "missing review".to_string();
    }
    if status.approvals == 1 {
        return "1 approval".to_string();
    }
    if status.approvals > 1 {
        return format!("{} approvals", status.approvals);
    }
    if status.review_decision.is_none() {
        return "not required".to_string();
    }
    "unknown".to_string()
}

fn sort_ready_rows(rows: &mut [PrReadinessRow], branch_order: &[&str]) {
    let order = branch_order
        .iter()
        .enumerate()
        .map(|(idx, branch)| (*branch, idx))
        .collect::<HashMap<_, _>>();

    rows.sort_by(|a, b| {
        b.updated_at.cmp(&a.updated_at).then_with(|| {
            (
                order.get(a.branch.as_str()).copied().unwrap_or(usize::MAX),
                a.branch.as_str(),
            )
                .cmp(&(
                    order.get(b.branch.as_str()).copied().unwrap_or(usize::MAX),
                    b.branch.as_str(),
                ))
        })
    });
}

fn readiness_summary(scope: &str, row_count: usize, skipped: usize) -> String {
    let now = Local::now().format("%H:%M:%S");
    let pr_word = if row_count == 1 { "PR" } else { "PRs" };
    let skipped_suffix = if skipped > 0 {
        format!(" · {} skipped", skipped)
    } else {
        String::new()
    };
    format!("{scope} · fresh {now} · {row_count} {pr_word}{skipped_suffix}")
}

fn render_ready_table(
    repo_label: &str,
    summary: &str,
    rows: &[PrReadinessRow],
    width: usize,
) -> String {
    let mut output = String::new();
    output.push_str(&format!("{repo_label}  {summary}\n"));

    if rows.is_empty() {
        output.push_str("No pull requests in scope.\n");
        return output;
    }

    let review_width = rows
        .iter()
        .map(|row| measure_text_width(&row.review_summary))
        .max()
        .unwrap_or(REVIEWS_MIN_WIDTH)
        .max("REVIEWS".len())
        .max(REVIEWS_MIN_WIDTH);
    let ci_width = rows
        .iter()
        .map(|row| measure_text_width(&row.ci_summary))
        .max()
        .unwrap_or(CI_MIN_WIDTH)
        .max("CI".len())
        .max(CI_MIN_WIDTH);
    let branch_pref = rows
        .iter()
        .map(|row| measure_text_width(&row.branch))
        .max()
        .unwrap_or(BRANCH_MIN_WIDTH)
        .clamp(BRANCH_MIN_WIDTH, BRANCH_MAX_WIDTH);

    let fixed = ACTION_WIDTH + PR_WIDTH + review_width + ci_width + 10;
    let flexible = width.saturating_sub(fixed);
    let title_pref = rows
        .iter()
        .map(|row| measure_text_width(&row.title))
        .max()
        .unwrap_or(TITLE_MIN_WIDTH)
        .max(TITLE_MIN_WIDTH);
    let title_width = title_pref
        .min(
            flexible
                .saturating_sub(BRANCH_MIN_WIDTH)
                .max(TITLE_MIN_WIDTH),
        )
        .max(TITLE_MIN_WIDTH);
    let branch_width = flexible
        .saturating_sub(title_width)
        .clamp(BRANCH_MIN_WIDTH, branch_pref);

    output.push('\n');
    output.push_str(&format!(
        "{:<ACTION_WIDTH$}  {:<PR_WIDTH$}  {:<branch_width$}  {:<review_width$}  {:<ci_width$}  {}\n",
        "ACTION",
        "PR",
        "BRANCH",
        "REVIEWS",
        "CI",
        "TITLE"
    ));
    let divider_width =
        ACTION_WIDTH + PR_WIDTH + branch_width + review_width + ci_width + title_width + 10;
    output.push_str(&format!("{}\n", "".repeat(divider_width.min(width))));

    for row in rows {
        output.push_str(&format!(
            "{:<ACTION_WIDTH$}  {:<PR_WIDTH$}  {:<branch_width$}  {:<review_width$}  {:<ci_width$}  {}\n",
            row.action.display(),
            format!("#{}", row.pr_number),
            fit_middle(&row.branch, branch_width),
            fit_end(&row.review_summary, review_width),
            fit_end(&row.ci_summary, ci_width),
            fit_end(&row.title, title_width),
        ));
    }

    output
}

fn fit_end(text: &str, width: usize) -> String {
    truncate_str(text, width, "...").into_owned()
}

fn fit_middle(text: &str, width: usize) -> String {
    if measure_text_width(text) <= width {
        return text.to_string();
    }
    if width <= 3 {
        return ".".repeat(width);
    }
    let chars = text.chars().collect::<Vec<_>>();
    let keep = width.saturating_sub(3);
    let front = keep / 2 + keep % 2;
    let back = keep / 2;
    let prefix = chars.iter().take(front).collect::<String>();
    let suffix = chars
        .iter()
        .rev()
        .take(back)
        .copied()
        .collect::<Vec<_>>()
        .into_iter()
        .rev()
        .collect::<String>();
    format!("{prefix}...{suffix}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::CiCache;
    use crate::engine::BranchMetadata;
    use crate::git::GitRepo;
    use crate::github::pr::{CiStatus, PrMergeStatus};
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    fn status(overrides: impl FnOnce(&mut PrMergeStatus)) -> PrMergeStatus {
        let mut status = PrMergeStatus {
            number: 42,
            title: "Ready PR".to_string(),
            state: "open".to_string(),
            updated_at: None,
            is_draft: false,
            mergeable: Some(true),
            mergeable_state: "clean".to_string(),
            ci_status: CiStatus::Success,
            review_decision: Some("APPROVED".to_string()),
            approvals: 1,
            changes_requested: false,
            head_sha: "abc123".to_string(),
        };
        overrides(&mut status);
        status
    }

    #[test]
    fn classifies_ready_pr_as_merge() {
        let row = PrReadinessRow::from_status("feature", status(|_| {}), CiSummary::passed());

        assert_eq!(row.action, ReadyAction::Merge);
        assert_eq!(row.reason, ReadyReason::Ready);
        assert_eq!(row.review_summary, "1 approval");
        assert_eq!(row.ci_summary, "passed");
    }

    #[test]
    fn classifies_review_required_pr_as_ping() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| {
                s.review_decision = Some("REVIEW_REQUIRED".to_string());
                s.approvals = 0;
            }),
            CiSummary::passed(),
        );

        assert_eq!(row.action, ReadyAction::Ping);
        assert_eq!(row.reason, ReadyReason::ReviewRequired);
        assert_eq!(row.review_summary, "missing review");
    }

    #[test]
    fn classifies_no_review_requirement_as_ready() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| {
                s.review_decision = None;
                s.approvals = 0;
            }),
            CiSummary::passed(),
        );

        assert_eq!(row.action, ReadyAction::Merge);
        assert_eq!(row.reason, ReadyReason::Ready);
        assert_eq!(row.review_summary, "not required");
    }

    #[test]
    fn classifies_failed_ci_as_fix() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| s.ci_status = CiStatus::Failure),
            CiSummary::failed(2),
        );

        assert_eq!(row.action, ReadyAction::Fix);
        assert_eq!(row.reason, ReadyReason::CiFailed);
        assert_eq!(row.ci_summary, "2 failed");
    }

    #[test]
    fn classifies_changes_requested_as_fix() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| {
                s.review_decision = Some("CHANGES_REQUESTED".to_string());
                s.changes_requested = true;
            }),
            CiSummary::passed(),
        );

        assert_eq!(row.action, ReadyAction::Fix);
        assert_eq!(row.reason, ReadyReason::ChangesRequested);
        assert_eq!(row.review_summary, "changes requested");
    }

    #[test]
    fn classifies_pending_ci_as_wait() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| s.ci_status = CiStatus::Pending),
            CiSummary::running(),
        );

        assert_eq!(row.action, ReadyAction::Wait);
        assert_eq!(row.reason, ReadyReason::CiPending);
        assert_eq!(row.ci_summary, "running");
    }

    #[test]
    fn classifies_draft_before_failed_ci() {
        let row = PrReadinessRow::from_status(
            "feature",
            status(|s| {
                s.is_draft = true;
                s.ci_status = CiStatus::Failure;
            }),
            CiSummary::failed(1),
        );

        assert_eq!(row.action, ReadyAction::Draft);
        assert_eq!(row.reason, ReadyReason::Draft);
        assert_eq!(row.review_summary, "draft");
    }

    #[test]
    fn sorts_by_pr_updated_at_newest_first() {
        let mut rows = vec![
            PrReadinessRow::from_status(
                "old-fix",
                status(|s| {
                    s.ci_status = CiStatus::Failure;
                    s.updated_at = Some("2026-06-01T10:00:00Z".to_string());
                }),
                CiSummary::failed(1),
            ),
            PrReadinessRow::from_status(
                "new-ping",
                status(|s| {
                    s.review_decision = Some("REVIEW_REQUIRED".to_string());
                    s.approvals = 0;
                    s.updated_at = Some("2026-06-02T10:00:00Z".to_string());
                }),
                CiSummary::passed(),
            ),
            PrReadinessRow::from_status(
                "middle-merge",
                status(|s| {
                    s.updated_at = Some("2026-06-01T18:00:00Z".to_string());
                }),
                CiSummary::passed(),
            ),
            PrReadinessRow::from_status(
                "unknown-draft",
                status(|s| {
                    s.is_draft = true;
                    s.updated_at = None;
                }),
                CiSummary::not_run(),
            ),
        ];

        sort_ready_rows(
            &mut rows,
            &["old-fix", "new-ping", "middle-merge", "unknown-draft"],
        );

        let branches = rows
            .iter()
            .map(|row| row.branch.as_str())
            .collect::<Vec<_>>();
        assert_eq!(
            branches,
            vec!["new-ping", "middle-merge", "old-fix", "unknown-draft"]
        );
    }

    #[test]
    fn renders_table_with_titles_and_action_labels() {
        let rows = vec![PrReadinessRow::from_status(
            "feature",
            status(|_| {}),
            CiSummary::passed(),
        )];

        let rendered = render_ready_table("owner/repo", "current stack", &rows, 100);

        assert!(rendered.contains("ACTION"));
        assert!(rendered.contains("REVIEWS"));
        assert!(rendered.contains("CI"));
        assert!(rendered.contains("TITLE"));
        assert!(rendered.contains("merge"));
    }

    #[test]
    fn readiness_scope_defaults_to_all_tracked_prs() {
        assert_eq!(
            ReadyScopeMode::from_flags(false, false, false),
            ReadyScopeMode::AllTracked
        );
    }

    #[test]
    fn readiness_scope_current_or_stack_selects_current_stack() {
        assert_eq!(
            ReadyScopeMode::from_flags(false, true, false),
            ReadyScopeMode::CurrentStack
        );
        assert_eq!(
            ReadyScopeMode::from_flags(false, false, true),
            ReadyScopeMode::CurrentStack
        );
    }

    #[test]
    fn readiness_fetches_multiple_rows_per_batch() {
        assert!(READY_FETCH_CONCURRENCY > 1);
    }

    fn run_git(path: &std::path::Path, args: &[&str]) {
        let output = Command::new("git")
            .args(args)
            .current_dir(path)
            .output()
            .expect("git command");
        assert!(
            output.status.success(),
            "git {:?} failed\nstdout: {}\nstderr: {}",
            args,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }

    fn temp_repo() -> (TempDir, GitRepo) {
        let temp = TempDir::new().expect("temp repo");
        let path = temp.path();
        run_git(path, &["init", "-b", "main"]);
        run_git(path, &["config", "user.email", "test@example.com"]);
        run_git(path, &["config", "user.name", "Test User"]);
        fs::write(path.join("README.md"), "base\n").expect("write readme");
        run_git(path, &["add", "README.md"]);
        run_git(path, &["commit", "-m", "Initial commit"]);
        run_git(path, &["checkout", "-b", "feature/cache-ready"]);

        let repo = GitRepo::open_from_path(path).expect("open repo");
        (temp, repo)
    }

    #[test]
    fn ready_row_warms_ci_cache_and_branch_pr_metadata() {
        let (_temp, repo) = temp_repo();
        let branch = "feature/cache-ready";
        let meta = BranchMetadata {
            pr_info: None,
            ..BranchMetadata::new("main", "abc123")
        };
        meta.write(repo.inner(), branch).expect("write metadata");

        let row = PrReadinessRow::from_status(
            branch,
            status(|s| {
                s.number = 123;
                s.state = "open".to_string();
                s.is_draft = true;
                s.ci_status = CiStatus::Success;
            }),
            CiSummary::passed(),
        );

        warm_caches_for_ready_row(&repo, &row).expect("warm cache");

        let cache = CiCache::load(repo.git_dir().expect("git dir"));
        let entry = cache.branches.get(branch).expect("cache entry");
        assert_eq!(entry.ci_state.as_deref(), Some("success"));
        assert_eq!(entry.pr_state.as_deref(), Some("draft"));

        let updated = BranchMetadata::read(repo.inner(), branch)
            .expect("read metadata")
            .expect("metadata");
        let pr = updated.pr_info.expect("pr info");
        assert_eq!(pr.number, 123);
        assert_eq!(pr.state, "open");
        assert_eq!(pr.is_draft, Some(true));
    }
}