knotra 0.34.0

knotra: multi-project VCS dashboard and bulk-operation tool.
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
//! Pure dashboard classification, filtering, grouping, and ordering.

use std::{cmp::Ordering, collections::HashSet};

use knotra_ui::widget::{Tone, status_symbol as symbol};
use knotra_vcs::{ProjectId, WorkspaceStatus, model::project::Project};

use crate::{
    config::{DashboardGrouping, DashboardSort},
    message::StatusFilter,
    state::FilterState,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DashboardTier {
    NeedsHelp,
    InProgress,
    AllSet,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DashboardCause {
    MissingPath,
    Conflict,
    ConflictDetectionUnavailable,
    ReadUnavailable,
    DetachedContext,
    StatusUnknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ProgressKind {
    Uncommitted,
    Untracked,
    Ahead,
    Behind,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RelevantCount {
    pub kind: ProgressKind,
    pub value: u32,
}

#[derive(Debug, Clone)]
pub struct DashboardEntry<'a> {
    pub project: &'a Project,
    pub status: Option<&'a knotra_vcs::ProjectStatus>,
    pub tier: DashboardTier,
    pub cause: Option<DashboardCause>,
    pub relevant_count: Option<RelevantCount>,
}

// --- RFC-071 D3 / Amendment A4: tone and symbol ----------------------------
//
// A badge goes on each row's status line and on each chip โ€” never on a
// group heading, which is structure, not a status (A4). Every function here
// is exhaustive over its own enum, with no `_` arm, so a new `StatusFilter`,
// `DashboardCause` or `ProgressKind` variant fails to compile without a
// tone and a symbol, rather than silently rendering with neither.

/// A chip's badge. Every `StatusFilter` variant is one of D3's five rows
/// (RFC-073 D5 removed the sixth, `Conflict` โ€” "Needs a choice" duplicated
/// `NeedsHelp`'s own meaning), each its own row.
#[must_use]
pub fn badge_for_filter(filter: &StatusFilter) -> (Tone, snora::Icon) {
    match filter {
        StatusFilter::NeedsHelp => (Tone::Danger, symbol::alert()),
        StatusFilter::Dirty => (Tone::Warning, symbol::unsaved()),
        StatusFilter::Behind => (Tone::Info, symbol::updates_available()),
        StatusFilter::Ahead => (Tone::Info, symbol::local_commits()),
        StatusFilter::AllSet => (Tone::Success, symbol::all_set()),
    }
}

/// A needs-help row's badge. Every `DashboardCause` variant is a reason a
/// project needs help โ€” none of them is "all set" or "in progress" โ€” so
/// every one shares D3's "Needs help, Needs a choice, unreadable" row:
/// `danger`, alert.
#[must_use]
pub fn badge_for_cause(cause: DashboardCause) -> (Tone, snora::Icon) {
    match cause {
        DashboardCause::MissingPath
        | DashboardCause::Conflict
        | DashboardCause::ConflictDetectionUnavailable
        | DashboardCause::ReadUnavailable
        | DashboardCause::DetachedContext
        | DashboardCause::StatusUnknown => (Tone::Danger, symbol::alert()),
    }
}

/// An in-progress row's badge. `Uncommitted`/`Untracked` are both "work not
/// yet saved to the repository" (Review 250 ยง3 confirms `Untracked` here) โ€”
/// D3's "Unsaved work" row, `warning`. `Ahead`/`Behind` are D3's "Local
/// commits"/"Updates available" rows, both `info` but with different
/// arrows.
#[must_use]
pub fn badge_for_progress(kind: ProgressKind) -> (Tone, snora::Icon) {
    match kind {
        ProgressKind::Uncommitted | ProgressKind::Untracked => (Tone::Warning, symbol::unsaved()),
        ProgressKind::Ahead => (Tone::Info, symbol::local_commits()),
        ProgressKind::Behind => (Tone::Info, symbol::updates_available()),
    }
}

/// An all-set row's badge. D3's "All set" row: `success`, check. A plain
/// function rather than a one-armed match over a unit case, since there is
/// no enum to be exhaustive over here.
#[must_use]
pub fn badge_for_all_set() -> (Tone, snora::Icon) {
    (Tone::Success, symbol::all_set())
}

/// RFC-073 D9: the one outcome-to-badge mapping used everywhere a
/// completed per-project operation shows its result outside the dashboard
/// itself โ€” the ready check (`freezer.rs`), Smart Pull's plan and results,
/// Change work area's results, and History's entry outcome and per-project
/// lines โ€” so "success"/"failure"/"skipped" always mean the same colour
/// and symbol, never a screen-specific glyph. Exhaustive over
/// `ProjectOperationOutcome`, the same discipline as this file's other
/// `badge_for_*` functions, even though the type it classifies is not
/// dashboard-specific.
#[must_use]
pub fn badge_for_operation_outcome(
    outcome: knotra_vcs::model::operation::ProjectOperationOutcome,
) -> (Tone, snora::Icon) {
    use knotra_vcs::model::operation::ProjectOperationOutcome;
    match outcome {
        ProjectOperationOutcome::Succeeded => (Tone::Success, symbol::all_set()),
        ProjectOperationOutcome::Failed => (Tone::Danger, symbol::alert()),
        ProjectOperationOutcome::Skipped => (Tone::Info, symbol::skipped()),
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DashboardSectionKey {
    Tier(DashboardTier),
    ProjectGroup(Option<String>),
    Flat,
}

#[derive(Debug, Clone)]
pub struct DashboardSection<'a> {
    pub key: DashboardSectionKey,
    pub collapsed: bool,
    pub entries: Vec<DashboardEntry<'a>>,
}

#[derive(Debug, Clone, Default)]
pub struct DashboardDisplay<'a> {
    pub sections: Vec<DashboardSection<'a>>,
    pub ordered_selectable_ids: Vec<ProjectId>,
}

#[derive(Debug, Clone, Copy)]
pub struct DashboardDisplayOptions {
    pub grouping: DashboardGrouping,
    pub sort: DashboardSort,
    pub in_progress_collapsed: bool,
    pub all_set_collapsed: bool,
}

/// Every project, classified, before `FilterState` (search text, active
/// group, status-filter toggles) narrows the list. Shared by
/// [`build_dashboard_display`] and [`count_for_filter`] โ€” RFC-071 D5's chip
/// counts read from this same unfiltered set, so toggling one chip never
/// moves another chip's own number.
fn classify_all<'a>(
    projects: &'a [Project],
    workspace_status: Option<&'a WorkspaceStatus>,
    missing_projects: &HashSet<ProjectId>,
) -> Vec<DashboardEntry<'a>> {
    let statuses = workspace_status
        .map(|status| status.projects.as_slice())
        .unwrap_or_default();
    projects
        .iter()
        .map(|project| {
            let status = statuses
                .iter()
                .find(|status| status.project_id == project.id);
            classify(project, status, missing_projects.contains(&project.id))
        })
        .collect()
}

/// RFC-071 D5: a chip's own count โ€” how many of the full, unfiltered
/// classified set match `filter`.
#[must_use]
pub fn count_for_filter(
    projects: &[Project],
    workspace_status: Option<&WorkspaceStatus>,
    missing_projects: &HashSet<ProjectId>,
    filter: &StatusFilter,
) -> usize {
    classify_all(projects, workspace_status, missing_projects)
        .iter()
        .filter(|entry| matches_status_filter(entry, filter))
        .count()
}

pub fn build_dashboard_display<'a>(
    projects: &'a [Project],
    workspace_status: Option<&'a WorkspaceStatus>,
    missing_projects: &HashSet<ProjectId>,
    filter: &FilterState,
    options: DashboardDisplayOptions,
) -> DashboardDisplay<'a> {
    let mut entries: Vec<_> = classify_all(projects, workspace_status, missing_projects)
        .into_iter()
        .filter(|entry| matches_filter(entry, filter))
        .collect();

    sort_entries(&mut entries, options.sort);

    let sections = match options.grouping {
        DashboardGrouping::Attention => attention_sections(
            entries,
            options.in_progress_collapsed,
            options.all_set_collapsed,
        ),
        DashboardGrouping::ProjectGroup => project_group_sections(entries),
        DashboardGrouping::None => (!entries.is_empty())
            .then_some(DashboardSection {
                key: DashboardSectionKey::Flat,
                collapsed: false,
                entries,
            })
            .into_iter()
            .collect(),
    };
    let ordered_selectable_ids = sections
        .iter()
        .filter(|section| !section.collapsed)
        .flat_map(|section| section.entries.iter())
        .map(|entry| entry.project.id.clone())
        .collect();

    DashboardDisplay {
        sections,
        ordered_selectable_ids,
    }
}

fn classify<'a>(
    project: &'a Project,
    status: Option<&'a knotra_vcs::ProjectStatus>,
    path_missing: bool,
) -> DashboardEntry<'a> {
    let (tier, cause, relevant_count) = if path_missing {
        (
            DashboardTier::NeedsHelp,
            Some(DashboardCause::MissingPath),
            None,
        )
    } else if let Some(status) = status {
        if status.conflict.has_conflict {
            (
                DashboardTier::NeedsHelp,
                Some(DashboardCause::Conflict),
                None,
            )
        } else if status.conflict.detection_unavailable {
            (
                DashboardTier::NeedsHelp,
                Some(DashboardCause::ConflictDetectionUnavailable),
                None,
            )
        } else if status.read_error.is_some() {
            (
                DashboardTier::NeedsHelp,
                Some(DashboardCause::ReadUnavailable),
                None,
            )
        } else if status
            .context
            .as_ref()
            .is_some_and(|context| context.is_detached)
        {
            (
                DashboardTier::NeedsHelp,
                Some(DashboardCause::DetachedContext),
                None,
            )
        } else if let Some(relevant_count) = relevant_count(status) {
            (DashboardTier::InProgress, None, Some(relevant_count))
        } else {
            (DashboardTier::AllSet, None, None)
        }
    } else {
        (
            DashboardTier::NeedsHelp,
            Some(DashboardCause::StatusUnknown),
            None,
        )
    };

    DashboardEntry {
        project,
        status,
        tier,
        cause,
        relevant_count,
    }
}

fn relevant_count(status: &knotra_vcs::ProjectStatus) -> Option<RelevantCount> {
    [
        (
            ProgressKind::Uncommitted,
            status.working_tree.uncommitted_count,
        ),
        (ProgressKind::Untracked, status.working_tree.untracked_count),
        (ProgressKind::Ahead, status.remote.ahead),
        (ProgressKind::Behind, status.remote.behind),
    ]
    .into_iter()
    .find(|(_, value)| *value > 0)
    .map(|(kind, value)| RelevantCount { kind, value })
}

fn matches_filter(entry: &DashboardEntry<'_>, filter: &FilterState) -> bool {
    if !filter.search_text.is_empty()
        && !entry
            .project
            .name
            .to_lowercase()
            .contains(&filter.search_text.to_lowercase())
    {
        return false;
    }
    if let Some(group) = &filter.active_group
        && entry.project.group.as_deref() != Some(group.as_str())
    {
        return false;
    }
    filter.status_filters.is_empty()
        || filter
            .status_filters
            .iter()
            .any(|status_filter| matches_status_filter(entry, status_filter))
}

fn matches_status_filter(entry: &DashboardEntry<'_>, filter: &StatusFilter) -> bool {
    let usable_status_facts = !matches!(
        entry.cause,
        Some(
            DashboardCause::MissingPath
                | DashboardCause::ReadUnavailable
                | DashboardCause::StatusUnknown
        )
    );
    match filter {
        StatusFilter::AllSet => entry.tier == DashboardTier::AllSet,
        StatusFilter::Behind => {
            usable_status_facts && entry.status.is_some_and(|status| status.remote.behind > 0)
        }
        StatusFilter::Ahead => {
            usable_status_facts && entry.status.is_some_and(|status| status.remote.ahead > 0)
        }
        StatusFilter::Dirty => {
            usable_status_facts
                && entry
                    .status
                    .is_some_and(|status| status.working_tree.is_dirty())
        }
        StatusFilter::NeedsHelp => entry.tier == DashboardTier::NeedsHelp,
    }
}

/// RFC-073 D3: whether the active workspace holds both Git and Jujutsu
/// projects โ€” the only time a row's VCS-kind label ("Git" / "jj") tells
/// the viewer something a single-kind workspace wouldn't already imply.
/// `None` (no status snapshot yet) counts as not-mixed, the same as an
/// empty workspace: there is nothing yet to call "mixed".
#[must_use]
pub fn workspace_has_mixed_vcs_kinds(workspace_status: Option<&WorkspaceStatus>) -> bool {
    let projects = workspace_status
        .map(|status| status.projects.as_slice())
        .unwrap_or(&[]);
    let has_git = projects
        .iter()
        .any(|p| p.identity.vcs_kind == knotra_vcs::VcsKind::Git);
    let has_jujutsu = projects
        .iter()
        .any(|p| p.identity.vcs_kind == knotra_vcs::VcsKind::Jujutsu);
    has_git && has_jujutsu
}

fn attention_sections<'a>(
    entries: Vec<DashboardEntry<'a>>,
    in_progress_collapsed: bool,
    all_set_collapsed: bool,
) -> Vec<DashboardSection<'a>> {
    [
        (DashboardTier::NeedsHelp, false),
        (DashboardTier::InProgress, in_progress_collapsed),
        (DashboardTier::AllSet, all_set_collapsed),
    ]
    .into_iter()
    .filter_map(|(tier, collapsed)| {
        let tier_entries: Vec<_> = entries
            .iter()
            .filter(|entry| entry.tier == tier)
            .cloned()
            .collect();
        (!tier_entries.is_empty()).then_some(DashboardSection {
            key: DashboardSectionKey::Tier(tier),
            collapsed,
            entries: tier_entries,
        })
    })
    .collect()
}

fn project_group_sections<'a>(entries: Vec<DashboardEntry<'a>>) -> Vec<DashboardSection<'a>> {
    let mut names: Vec<String> = entries
        .iter()
        .filter_map(|entry| entry.project.group.clone())
        .collect();
    names.sort_by(|left, right| {
        left.to_lowercase()
            .cmp(&right.to_lowercase())
            .then_with(|| left.cmp(right))
    });
    names.dedup();

    let mut sections: Vec<_> = names
        .into_iter()
        .map(|name| DashboardSection {
            key: DashboardSectionKey::ProjectGroup(Some(name.clone())),
            collapsed: false,
            entries: entries
                .iter()
                .filter(|entry| entry.project.group.as_deref() == Some(name.as_str()))
                .cloned()
                .collect(),
        })
        .collect();
    let ungrouped: Vec<_> = entries
        .into_iter()
        .filter(|entry| entry.project.group.is_none())
        .collect();
    if !ungrouped.is_empty() {
        sections.push(DashboardSection {
            key: DashboardSectionKey::ProjectGroup(None),
            collapsed: false,
            entries: ungrouped,
        });
    }
    sections
}

fn sort_entries(entries: &mut [DashboardEntry<'_>], sort: DashboardSort) {
    entries.sort_by(|left, right| match sort {
        DashboardSort::NameAscending => compare_identity(left, right),
        DashboardSort::Recommended => left
            .tier
            .cmp(&right.tier)
            .then_with(|| match left.tier {
                DashboardTier::NeedsHelp => cause_rank(left.cause).cmp(&cause_rank(right.cause)),
                DashboardTier::InProgress => compare_progress(left, right),
                DashboardTier::AllSet => Ordering::Equal,
            })
            .then_with(|| compare_identity(left, right)),
    });
}

fn compare_progress(left: &DashboardEntry<'_>, right: &DashboardEntry<'_>) -> Ordering {
    match (left.relevant_count, right.relevant_count) {
        (Some(left), Some(right)) => left
            .kind
            .cmp(&right.kind)
            .then_with(|| right.value.cmp(&left.value)),
        (Some(_), None) => Ordering::Less,
        (None, Some(_)) => Ordering::Greater,
        (None, None) => Ordering::Equal,
    }
}

fn cause_rank(cause: Option<DashboardCause>) -> u8 {
    match cause {
        Some(DashboardCause::Conflict) => 0,
        Some(DashboardCause::MissingPath) => 1,
        Some(DashboardCause::ConflictDetectionUnavailable) => 2,
        Some(DashboardCause::ReadUnavailable) => 3,
        Some(DashboardCause::DetachedContext) => 4,
        Some(DashboardCause::StatusUnknown) | None => 5,
    }
}

fn compare_identity(left: &DashboardEntry<'_>, right: &DashboardEntry<'_>) -> Ordering {
    left.project
        .name
        .to_lowercase()
        .cmp(&right.project.name.to_lowercase())
        .then_with(|| {
            left.project
                .id
                .to_string()
                .cmp(&right.project.id.to_string())
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use jiff::Timestamp;
    use knotra_vcs::model::status::{
        ConflictStatus, RemoteStatus, RepositoryIdentity, VcsContext, VcsKind, WorkingTreeStatus,
    };
    use uuid::Uuid;

    fn project(number: u128, name: &str, group: Option<&str>) -> Project {
        let mut project = Project::new(name, format!("/project/{number}"));
        project.id = ProjectId(Uuid::from_u128(number));
        project.group = group.map(str::to_owned);
        project
    }

    fn status(project: &Project) -> knotra_vcs::ProjectStatus {
        knotra_vcs::ProjectStatus {
            project_id: project.id.clone(),
            identity: RepositoryIdentity {
                path: project.path.clone(),
                vcs_kind: VcsKind::Git,
            },
            context: Some(VcsContext {
                label: "feature/work".to_owned(),
                branch: Some("feature/work".to_owned()),
                jj_change_id: None,
                jj_bookmark: None,
                is_detached: false,
                unborn: false,
            }),
            remote: RemoteStatus::default(),
            working_tree: WorkingTreeStatus::default(),
            conflict: ConflictStatus::default(),
            refreshed_at: Timestamp::now(),
            read_error: None,
        }
    }

    fn entry_for<'a>(
        project: &'a Project,
        status: Option<&'a knotra_vcs::ProjectStatus>,
        missing: bool,
    ) -> DashboardEntry<'a> {
        classify(project, status, missing)
    }

    #[test]
    fn classification_uses_the_specified_priority_and_typed_detached_fact() {
        let project = project(1, "api", None);
        let mut status = status(&project);
        status.conflict.has_conflict = true;
        status.conflict.detection_unavailable = true;
        status.read_error = Some("read failed".to_owned());
        status.context.as_mut().expect("context").is_detached = true;
        status.working_tree.uncommitted_count = 2;
        status.remote.ahead = 3;
        status.remote.behind = 4;

        assert_eq!(
            entry_for(&project, Some(&status), true).cause,
            Some(DashboardCause::MissingPath)
        );
        assert_eq!(
            entry_for(&project, Some(&status), false).cause,
            Some(DashboardCause::Conflict)
        );
        status.conflict.has_conflict = false;
        assert_eq!(
            entry_for(&project, Some(&status), false).cause,
            Some(DashboardCause::ConflictDetectionUnavailable)
        );
        status.conflict.detection_unavailable = false;
        assert_eq!(
            entry_for(&project, Some(&status), false).cause,
            Some(DashboardCause::ReadUnavailable)
        );
        status.read_error = None;
        assert_eq!(
            entry_for(&project, Some(&status), false).cause,
            Some(DashboardCause::DetachedContext)
        );

        status.identity.vcs_kind = VcsKind::Jujutsu;
        status.context.as_mut().expect("context").branch = None;
        status.context.as_mut().expect("context").jj_change_id = Some("abc123".to_owned());
        assert_eq!(
            entry_for(&project, Some(&status), false).cause,
            Some(DashboardCause::DetachedContext)
        );
    }

    #[test]
    fn display_labels_do_not_create_in_progress_or_help_tiers() {
        let project = project(1, "api", None);
        for label in ["main", "master", "trunk", "feature/work", "bookmark"] {
            let mut status = status(&project);
            status.context.as_mut().expect("context").label = label.to_owned();
            status.context.as_mut().expect("context").branch = Some(label.to_owned());
            assert_eq!(
                entry_for(&project, Some(&status), false).tier,
                DashboardTier::AllSet
            );
        }
        assert_eq!(
            entry_for(&project, None, false).cause,
            Some(DashboardCause::StatusUnknown)
        );
    }

    #[test]
    fn fact_filters_follow_the_complete_typed_truth_table() {
        let project = project(1, "api", None);
        let mut observed = status(&project);
        observed.remote.behind = 1;
        observed.remote.ahead = 1;
        observed.working_tree.uncommitted_count = 1;

        let mut cases = Vec::new();
        cases.push((
            entry_for(&project, Some(&observed), true),
            [false, false, false, false, true],
        ));

        let mut missing_with_stale_conflict = observed.clone();
        missing_with_stale_conflict.conflict.has_conflict = true;
        let missing_conflict_entry = entry_for(&project, Some(&missing_with_stale_conflict), true);
        assert_eq!(missing_conflict_entry.tier, DashboardTier::NeedsHelp);
        assert_eq!(
            missing_conflict_entry.cause,
            Some(DashboardCause::MissingPath)
        );
        cases.push((missing_conflict_entry, [false, false, false, false, true]));

        let mut conflict = observed.clone();
        conflict.conflict.has_conflict = true;
        cases.push((
            entry_for(&project, Some(&conflict), false),
            [false, true, true, true, true],
        ));

        let mut detection = observed.clone();
        detection.conflict.detection_unavailable = true;
        cases.push((
            entry_for(&project, Some(&detection), false),
            [false, true, true, true, true],
        ));

        let mut read = observed.clone();
        read.read_error = Some("failed".to_owned());
        cases.push((
            entry_for(&project, Some(&read), false),
            [false, false, false, false, true],
        ));

        let mut detached = observed.clone();
        detached.context.as_mut().expect("context").is_detached = true;
        cases.push((
            entry_for(&project, Some(&detached), false),
            [false, true, true, true, true],
        ));
        cases.push((
            entry_for(&project, None, false),
            [false, false, false, false, true],
        ));
        cases.push((
            entry_for(&project, Some(&observed), false),
            [false, true, true, true, false],
        ));

        let clean = status(&project);
        cases.push((
            entry_for(&project, Some(&clean), false),
            [true, false, false, false, false],
        ));

        let filters = [
            StatusFilter::AllSet,
            StatusFilter::Behind,
            StatusFilter::Ahead,
            StatusFilter::Dirty,
            StatusFilter::NeedsHelp,
        ];
        for (entry, expected) in cases {
            for (filter, expected) in filters.iter().zip(expected) {
                assert_eq!(
                    matches_status_filter(&entry, filter),
                    expected,
                    "{filter:?}"
                );
            }
        }
    }

    #[test]
    fn relevant_count_and_recommended_order_use_kind_before_value() {
        let projects = vec![
            project(1, "one changed", None),
            project(2, "many behind", None),
            project(3, "five changed", None),
            project(4, "ahead", None),
        ];
        let mut statuses: Vec<_> = projects.iter().map(status).collect();
        statuses[0].working_tree.uncommitted_count = 1;
        statuses[0].remote.behind = 200;
        statuses[1].remote.behind = 100;
        statuses[2].working_tree.uncommitted_count = 5;
        statuses[3].remote.ahead = 2;
        let workspace_status = WorkspaceStatus {
            projects: statuses,
            last_refresh: None,
        };
        let display = build_dashboard_display(
            &projects,
            Some(&workspace_status),
            &HashSet::new(),
            &FilterState::default(),
            DashboardDisplayOptions {
                grouping: DashboardGrouping::None,
                sort: DashboardSort::Recommended,
                in_progress_collapsed: false,
                all_set_collapsed: false,
            },
        );
        assert_eq!(
            display.ordered_selectable_ids,
            vec![
                projects[2].id.clone(),
                projects[0].id.clone(),
                projects[3].id.clone(),
                projects[1].id.clone(),
            ]
        );
        assert_eq!(
            display.sections[0].entries[1].relevant_count,
            Some(RelevantCount {
                kind: ProgressKind::Uncommitted,
                value: 1,
            })
        );
    }

    #[test]
    fn project_groups_are_case_stable_with_ungrouped_last() {
        let projects = vec![
            project(1, "z", None),
            project(2, "b", Some("alpha")),
            project(3, "a", Some("Alpha")),
            project(4, "c", Some("Beta")),
        ];
        let statuses = WorkspaceStatus {
            projects: projects.iter().map(status).collect(),
            last_refresh: None,
        };
        let display = build_dashboard_display(
            &projects,
            Some(&statuses),
            &HashSet::new(),
            &FilterState::default(),
            DashboardDisplayOptions {
                grouping: DashboardGrouping::ProjectGroup,
                sort: DashboardSort::NameAscending,
                in_progress_collapsed: false,
                all_set_collapsed: true,
            },
        );
        let keys: Vec<_> = display
            .sections
            .iter()
            .map(|section| section.key.clone())
            .collect();
        assert_eq!(
            keys,
            vec![
                DashboardSectionKey::ProjectGroup(Some("Alpha".to_owned())),
                DashboardSectionKey::ProjectGroup(Some("alpha".to_owned())),
                DashboardSectionKey::ProjectGroup(Some("Beta".to_owned())),
                DashboardSectionKey::ProjectGroup(None),
            ]
        );
    }

    #[test]
    fn selectable_ids_equal_rendered_rows_for_all_display_combinations() {
        let projects = vec![
            project(1, "help", Some("A")),
            project(2, "work", Some("A")),
            project(3, "set", None),
        ];
        let mut statuses: Vec<_> = projects.iter().map(status).collect();
        statuses[0].conflict.has_conflict = true;
        statuses[1].working_tree.untracked_count = 2;
        let workspace_status = WorkspaceStatus {
            projects: statuses,
            last_refresh: None,
        };

        for grouping in [
            DashboardGrouping::Attention,
            DashboardGrouping::ProjectGroup,
            DashboardGrouping::None,
        ] {
            for sort in [DashboardSort::Recommended, DashboardSort::NameAscending] {
                for in_progress_collapsed in [false, true] {
                    for all_set_collapsed in [false, true] {
                        let display = build_dashboard_display(
                            &projects,
                            Some(&workspace_status),
                            &HashSet::new(),
                            &FilterState::default(),
                            DashboardDisplayOptions {
                                grouping,
                                sort,
                                in_progress_collapsed,
                                all_set_collapsed,
                            },
                        );
                        let rendered: Vec<_> = display
                            .sections
                            .iter()
                            .filter(|section| !section.collapsed)
                            .flat_map(|section| section.entries.iter())
                            .map(|entry| entry.project.id.clone())
                            .collect();
                        assert_eq!(display.ordered_selectable_ids, rendered);
                        let unique: HashSet<_> = display
                            .sections
                            .iter()
                            .flat_map(|section| section.entries.iter())
                            .map(|entry| entry.project.id.clone())
                            .collect();
                        assert_eq!(unique.len(), projects.len());
                        assert_eq!(
                            display
                                .sections
                                .iter()
                                .map(|section| section.entries.len())
                                .sum::<usize>(),
                            projects.len()
                        );
                        if grouping != DashboardGrouping::Attention {
                            assert_eq!(display.ordered_selectable_ids.len(), projects.len());
                        }
                    }
                }
            }
        }
    }

    /// RFC-071 D3/Amendment A4: every chip's tone, matching the table's
    /// five rows (RFC-073 D5 removed `Conflict`, the sixth).
    #[test]
    fn badge_for_filter_matches_d3s_table() {
        assert_eq!(badge_for_filter(&StatusFilter::NeedsHelp).0, Tone::Danger);
        assert_eq!(badge_for_filter(&StatusFilter::Dirty).0, Tone::Warning);
        assert_eq!(badge_for_filter(&StatusFilter::Behind).0, Tone::Info);
        assert_eq!(badge_for_filter(&StatusFilter::Ahead).0, Tone::Info);
        assert_eq!(badge_for_filter(&StatusFilter::AllSet).0, Tone::Success);
    }

    /// RFC-071 D3: every `DashboardCause` is `danger` โ€” none of them is
    /// "all set" or "in progress".
    #[test]
    fn badge_for_cause_is_always_danger() {
        for cause in [
            DashboardCause::MissingPath,
            DashboardCause::Conflict,
            DashboardCause::ConflictDetectionUnavailable,
            DashboardCause::ReadUnavailable,
            DashboardCause::DetachedContext,
            DashboardCause::StatusUnknown,
        ] {
            assert_eq!(badge_for_cause(cause).0, Tone::Danger, "{cause:?}");
        }
    }

    /// RFC-073 D3: the kind label tells something only when the workspace
    /// holds both Git and Jujutsu โ€” not for no status at all, not for one
    /// kind alone, not for the same kind twice.
    #[test]
    fn workspace_has_mixed_vcs_kinds_requires_both_kinds_present() {
        let git_project = project(1, "git-repo", None);
        let mut git_status = status(&git_project);
        git_status.identity.vcs_kind = VcsKind::Git;

        let other_git_project = project(2, "other-git-repo", None);
        let mut other_git_status = status(&other_git_project);
        other_git_status.identity.vcs_kind = VcsKind::Git;

        let jj_project = project(3, "jj-repo", None);
        let mut jj_status = status(&jj_project);
        jj_status.identity.vcs_kind = VcsKind::Jujutsu;

        assert!(!workspace_has_mixed_vcs_kinds(None));

        let git_only = WorkspaceStatus {
            projects: vec![git_status.clone(), other_git_status],
            last_refresh: None,
        };
        assert!(!workspace_has_mixed_vcs_kinds(Some(&git_only)));

        let mixed = WorkspaceStatus {
            projects: vec![git_status, jj_status],
            last_refresh: None,
        };
        assert!(workspace_has_mixed_vcs_kinds(Some(&mixed)));
    }

    /// RFC-071 D3 / Review 250 ยง3: `Uncommitted`/`Untracked` are both
    /// "Unsaved work" (`warning`); `Ahead`/`Behind` are both `info`.
    #[test]
    fn badge_for_progress_matches_d3s_table() {
        assert_eq!(
            badge_for_progress(ProgressKind::Uncommitted).0,
            Tone::Warning
        );
        assert_eq!(badge_for_progress(ProgressKind::Untracked).0, Tone::Warning);
        assert_eq!(badge_for_progress(ProgressKind::Ahead).0, Tone::Info);
        assert_eq!(badge_for_progress(ProgressKind::Behind).0, Tone::Info);
    }

    #[test]
    fn badge_for_all_set_is_success() {
        assert_eq!(badge_for_all_set().0, Tone::Success);
    }

    /// RFC-071 D5: a chip's count reads from the full classified set,
    /// independent of the active search text or other status-filter
    /// toggles โ€” toggling `NeedsHelp` must not move `AllSet`'s own number.
    #[test]
    fn count_for_filter_is_independent_of_active_filters() {
        let needs_help = project(1, "needs-help", None);
        let mut needs_help_status = status(&needs_help);
        needs_help_status.conflict.has_conflict = true;

        let all_set = project(2, "all-set", None);
        let all_set_status = status(&all_set);

        let projects = [needs_help.clone(), all_set.clone()];
        let ws_status = knotra_vcs::WorkspaceStatus {
            projects: vec![needs_help_status, all_set_status],
            ..Default::default()
        };
        let missing = HashSet::new();

        assert_eq!(
            count_for_filter(
                &projects,
                Some(&ws_status),
                &missing,
                &StatusFilter::NeedsHelp
            ),
            1
        );
        assert_eq!(
            count_for_filter(&projects, Some(&ws_status), &missing, &StatusFilter::AllSet),
            1
        );
        assert_eq!(
            count_for_filter(&projects, Some(&ws_status), &missing, &StatusFilter::Dirty),
            0
        );
    }
}