vig 0.9.0

Read-only TUI cockpit for busy repositories - git, GitHub PRs/CI/projects, containers and processes at a glance
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
//! Data shapes read from `gh project … --format json`.
//!
//! `gh project item-list` emits one object per item whose keys are the
//! project's field names with the first letter lowercased (`status`,
//! `priority`, `linked pull requests`, …). Only `id`, `title`, `status` and
//! `content` are modelled explicitly; every other field lands in
//! [`ProjectItem::fields`] so custom fields (single select, text, number,
//! date, iteration) show up without a schema change.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Column shown for items whose `Status` is unset.
pub const NO_STATUS: &str = "No status";

// === gh repo view ===

/// `gh repo view --json nameWithOwner,owner,projectsV2`: the repository
/// vig runs in, its owner and the projects linked to it. The linked
/// projects are the page's only data source.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct RepoInfo {
    /// `owner/repo`, to tell cards of other repositories apart.
    #[serde(rename = "nameWithOwner", default)]
    pub name_with_owner: String,
    pub owner: RepoOwner,
    #[serde(rename = "projectsV2", default)]
    pub projects_v2: LinkedProjects,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct RepoOwner {
    #[serde(default)]
    pub login: String,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct LinkedProjects {
    #[serde(rename = "Nodes", default)]
    pub nodes: Vec<LinkedProject>,
}

/// One node of `projectsV2`: `gh` emits `id`, `title`, `number`,
/// `resourcePath`, `closed` and `url`.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct LinkedProject {
    pub number: u64,
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub url: String,
    #[serde(rename = "resourcePath", default)]
    pub resource_path: String,
    #[serde(default)]
    pub closed: bool,
}

impl LinkedProject {
    /// The login owning the project, read off `resourcePath`
    /// (`/users/<login>/projects/<n>` or `/orgs/<login>/projects/<n>`):
    /// `gh project` commands take it as `--owner`.
    pub fn owner_login(&self) -> Option<(&str, &str)> {
        let mut parts = self.resource_path.trim_start_matches('/').split('/');
        let kind = match parts.next()? {
            "users" => "User",
            "orgs" => "Organization",
            _ => return None,
        };
        let login = parts.next().filter(|l| !l.is_empty())?;
        Some((login, kind))
    }

    /// The [`Project`] the page works with; `repo_owner` is the fallback
    /// owner when `resourcePath` is missing.
    pub fn to_project(&self, repo_owner: &str) -> Project {
        let (login, kind) = self
            .owner_login()
            .map(|(l, k)| (l.to_string(), k.to_string()))
            .unwrap_or_else(|| (repo_owner.to_string(), String::new()));
        Project {
            number: self.number,
            title: self.title.clone(),
            id: self.id.clone(),
            url: self.url.clone(),
            closed: self.closed,
            items: Count::default(),
            owner: ProjectOwner { login, kind },
            linked: true,
        }
    }
}

impl RepoInfo {
    /// The open projects linked to the repository, in GitHub's order.
    pub fn linked_projects(&self) -> Vec<Project> {
        self.projects_v2
            .nodes
            .iter()
            .filter(|p| !p.closed)
            .map(|p| p.to_project(&self.owner.login))
            .collect()
    }
}

// === Projects ===

/// A project the page can show: one of the repository's linked projects.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Project {
    pub number: u64,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub url: String,
    #[serde(default)]
    pub closed: bool,
    /// Item count, known once the board has been fetched (0 until then).
    #[serde(default)]
    pub items: Count,
    #[serde(default)]
    pub owner: ProjectOwner,
    /// Linked to the repository vig runs in (marked in the list pane).
    #[serde(default)]
    pub linked: bool,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Count {
    #[serde(rename = "totalCount", default)]
    pub total_count: u64,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ProjectOwner {
    #[serde(default)]
    pub login: String,
    #[serde(rename = "type", default)]
    pub kind: String,
}

/// What the page keeps on disk between runs: the linked projects, so a
/// board can show up before `gh repo view` answers.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ProjectListCache {
    /// `owner/repo` the list belongs to.
    #[serde(default)]
    pub repo: String,
    #[serde(default)]
    pub projects: Vec<Project>,
}

// === gh project field-list ===

#[derive(Debug, Clone, Deserialize)]
pub struct FieldList {
    #[serde(default)]
    pub fields: Vec<ProjectField>,
}

/// One field of `gh project field-list --format json`. `kind` is
/// `ProjectV2Field`, `ProjectV2SingleSelectField` or
/// `ProjectV2IterationField`; only single selects carry `options`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ProjectField {
    #[serde(default)]
    pub id: String,
    pub name: String,
    #[serde(rename = "type", default)]
    pub kind: String,
    #[serde(default)]
    pub options: Vec<FieldOption>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FieldOption {
    #[serde(default)]
    pub id: String,
    pub name: String,
}

impl ProjectField {
    /// The key `gh project item-list` uses for this field: the name with
    /// its first letter lowercased (`Linked pull requests` →
    /// `linked pull requests`).
    pub fn item_key(&self) -> String {
        item_key(&self.name)
    }

    pub fn is_status(&self) -> bool {
        self.name == "Status" && self.kind == "ProjectV2SingleSelectField"
    }
}

pub fn item_key(name: &str) -> String {
    let mut chars = name.chars();
    match chars.next() {
        Some(first) => first.to_lowercase().chain(chars).collect(),
        None => String::new(),
    }
}

/// Fields GitHub manages itself; they are either on the item's content
/// (title, assignees, labels, …) or never emitted by `item-list`. The
/// table mode shows the remaining ones — `Status` and the custom fields.
const BUILTIN_FIELDS: &[&str] = &[
    "Title",
    "Assignees",
    "Labels",
    "Linked pull requests",
    "Milestone",
    "Repository",
    "Reviewers",
    "Parent issue",
    "Sub-issues progress",
    "Tracked by",
    "Tracks",
    "Created",
    "Updated",
    "Closed",
];

/// The fields shown as table columns, in the project's field order.
pub fn table_fields(fields: &[ProjectField]) -> Vec<&ProjectField> {
    fields
        .iter()
        .filter(|f| !BUILTIN_FIELDS.contains(&f.name.as_str()))
        .collect()
}

// === gh project item-list ===

#[derive(Debug, Clone, Deserialize)]
pub struct ItemList {
    #[serde(default)]
    pub items: Vec<ProjectItem>,
    #[serde(rename = "totalCount", default)]
    pub total_count: u64,
}

/// One item of `gh project item-list --format json`.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ProjectItem {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub title: String,
    /// The `Status` single select, `None` when unset.
    #[serde(default)]
    pub status: Option<String>,
    /// The issue / PR / draft behind the item (`null` for redacted items).
    #[serde(default)]
    pub content: Option<ItemContent>,
    /// Every other field of the item, keyed the way `gh` names them.
    #[serde(flatten, default)]
    pub fields: BTreeMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ItemContent {
    #[serde(rename = "type", default)]
    pub kind: String,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub number: Option<u64>,
    /// `owner/repo` for issues and PRs.
    #[serde(default)]
    pub repository: Option<String>,
    #[serde(default)]
    pub url: Option<String>,
    /// Draft issues carry their body here; issues and PRs too.
    #[serde(default)]
    pub body: Option<String>,
}

/// Coarse item type, for icons and the detail pane.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemKind {
    Issue,
    PullRequest,
    Draft,
    Other,
}

impl ItemKind {
    pub fn icon(self) -> &'static str {
        match self {
            Self::Issue => "",
            Self::PullRequest => "",
            Self::Draft => "",
            Self::Other => "?",
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Issue => "issue",
            Self::PullRequest => "pull request",
            Self::Draft => "draft",
            Self::Other => "item",
        }
    }
}

impl ProjectItem {
    pub fn kind(&self) -> ItemKind {
        match self.content.as_ref().map(|c| c.kind.as_str()) {
            Some("Issue") => ItemKind::Issue,
            Some("PullRequest") => ItemKind::PullRequest,
            Some("DraftIssue") => ItemKind::Draft,
            _ => ItemKind::Other,
        }
    }

    pub fn number(&self) -> Option<u64> {
        self.content.as_ref().and_then(|c| c.number)
    }

    pub fn repository(&self) -> Option<&str> {
        self.content.as_ref().and_then(|c| c.repository.as_deref())
    }

    pub fn url(&self) -> Option<&str> {
        self.content.as_ref().and_then(|c| c.url.as_deref())
    }

    pub fn body(&self) -> &str {
        self.content
            .as_ref()
            .and_then(|c| c.body.as_deref())
            .unwrap_or("")
    }

    pub fn title(&self) -> &str {
        if self.title.is_empty() {
            self.content
                .as_ref()
                .map(|c| c.title.as_str())
                .unwrap_or("")
        } else {
            &self.title
        }
    }

    /// Assignee logins (the `assignees` field, absent when unassigned).
    pub fn assignees(&self) -> Vec<String> {
        match self.fields.get("assignees") {
            Some(serde_json::Value::Array(a)) => a
                .iter()
                .filter_map(|v| v.as_str().map(str::to_string))
                .collect(),
            _ => Vec::new(),
        }
    }

    /// The display text of a field by its item key (`status`, `priority`,
    /// …); `None` when the item has no value for it.
    pub fn field_text(&self, key: &str) -> Option<String> {
        if key == "status" {
            return self.status.clone();
        }
        if key == "title" {
            return Some(self.title().to_string());
        }
        self.fields.get(key).and_then(value_text)
    }

    /// Every field value of the item as `(key, text)`, `Status` first,
    /// then in `fields` order (falling back to alphabetical for keys the
    /// project does not list any more).
    pub fn field_values(&self, fields: &[ProjectField]) -> Vec<(String, String)> {
        let mut out = Vec::new();
        if let Some(s) = &self.status {
            out.push(("Status".to_string(), s.clone()));
        }
        let mut seen: Vec<String> = vec!["status".into(), "title".into()];
        for f in fields {
            let key = f.item_key();
            if seen.contains(&key) {
                continue;
            }
            seen.push(key.clone());
            if let Some(text) = self.field_text(&key) {
                out.push((f.name.clone(), text));
            }
        }
        for (key, value) in &self.fields {
            if seen.contains(key) {
                continue;
            }
            if let Some(text) = value_text(value) {
                out.push((capitalize(key), text));
            }
        }
        out
    }
}

/// Render a field value the way it reads best: iterations by title,
/// milestones by title, lists joined, numbers without trailing zeros.
pub fn value_text(v: &serde_json::Value) -> Option<String> {
    use serde_json::Value;
    match v {
        Value::Null => None,
        Value::String(s) => Some(s.clone()),
        Value::Bool(b) => Some(b.to_string()),
        Value::Number(n) => Some(n.to_string()),
        Value::Array(a) => {
            let parts: Vec<String> = a.iter().filter_map(value_text).collect();
            if parts.is_empty() {
                None
            } else {
                Some(parts.join(", "))
            }
        }
        Value::Object(o) => {
            let title = o.get("title").and_then(|t| t.as_str()).unwrap_or("");
            let start = o.get("startDate").and_then(|t| t.as_str());
            match (title.is_empty(), start) {
                (false, Some(start)) => Some(format!("{title} ({start})")),
                (false, None) => Some(title.to_string()),
                (true, _) => o
                    .values()
                    .filter_map(value_text)
                    .next()
                    .filter(|s| !s.is_empty()),
            }
        }
    }
}

fn capitalize(key: &str) -> String {
    let mut chars = key.chars();
    match chars.next() {
        Some(first) => first.to_uppercase().chain(chars).collect(),
        None => String::new(),
    }
}

// === Board ===

/// A project's board: its fields and items, with the truncation flag of
/// the `item-list --limit`.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Board {
    pub number: u64,
    #[serde(default)]
    pub fields: Vec<ProjectField>,
    #[serde(default)]
    pub items: Vec<ProjectItem>,
    #[serde(default)]
    pub total_count: u64,
}

impl Board {
    /// More items exist than `item-list --limit` returned.
    pub fn truncated(&self) -> bool {
        (self.items.len() as u64) < self.total_count
    }

    pub fn status_field(&self) -> Option<&ProjectField> {
        self.fields.iter().find(|f| f.is_status())
    }

    /// Kanban columns: one per `Status` option in GitHub's order, then any
    /// status the options do not list, then [`NO_STATUS`] when some item
    /// has none. Each column holds indices into `items`.
    pub fn columns(&self) -> Vec<Column> {
        let mut columns: Vec<Column> = self
            .status_field()
            .map(|f| f.options.iter().map(|o| Column::new(&o.name)).collect())
            .unwrap_or_default();
        let mut no_status = Column::new(NO_STATUS);
        for (idx, item) in self.items.iter().enumerate() {
            match item.status.as_deref().filter(|s| !s.is_empty()) {
                Some(status) => {
                    if let Some(col) = columns.iter_mut().find(|c| c.name == status) {
                        col.items.push(idx);
                    } else {
                        let mut col = Column::new(status);
                        col.items.push(idx);
                        columns.push(col);
                    }
                }
                None => no_status.items.push(idx),
            }
        }
        if !no_status.items.is_empty() {
            columns.push(no_status);
        }
        columns
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Column {
    pub name: String,
    pub items: Vec<usize>,
}

impl Column {
    fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            items: Vec::new(),
        }
    }
}

// === Table ===

/// A table column: the header and how to read a cell off an item.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TableColumn {
    Number,
    Title,
    Assignees,
    Field { name: String, key: String },
}

impl TableColumn {
    pub fn header(&self) -> &str {
        match self {
            Self::Number => "#",
            Self::Title => "Title",
            Self::Assignees => "Assignees",
            Self::Field { name, .. } => name,
        }
    }

    pub fn cell(&self, item: &ProjectItem) -> String {
        match self {
            Self::Number => item.number().map(|n| format!("#{n}")).unwrap_or_default(),
            Self::Title => item.title().to_string(),
            Self::Assignees => item.assignees().join(", "),
            Self::Field { key, .. } => item.field_text(key).unwrap_or_default(),
        }
    }
}

/// `#`, `Title`, `Assignees`, then the project's own fields.
pub fn table_columns(fields: &[ProjectField]) -> Vec<TableColumn> {
    let mut cols = vec![
        TableColumn::Number,
        TableColumn::Title,
        TableColumn::Assignees,
    ];
    cols.extend(
        table_fields(fields)
            .into_iter()
            .map(|f| TableColumn::Field {
                name: f.name.clone(),
                key: f.item_key(),
            }),
    );
    cols
}

/// Item indices sorted by `column`: numerically when every non-empty cell
/// is a number (`#12`, `3.5`), case-insensitively otherwise; empty cells
/// last; ties keep the board order. `Status` sorts by option order.
pub fn sort_items(items: &[ProjectItem], column: &TableColumn, board: &Board) -> Vec<usize> {
    let option_rank = |s: &str| -> Option<usize> {
        board
            .status_field()
            .and_then(|f| f.options.iter().position(|o| o.name == s))
    };
    let cells: Vec<String> = items.iter().map(|i| column.cell(i)).collect();
    let numeric = cells
        .iter()
        .filter(|c| !c.is_empty())
        .all(|c| c.trim_start_matches('#').parse::<f64>().is_ok());
    let mut order: Vec<usize> = (0..items.len()).collect();
    order.sort_by(|&a, &b| {
        let (ca, cb) = (&cells[a], &cells[b]);
        match (ca.is_empty(), cb.is_empty()) {
            (true, true) => return std::cmp::Ordering::Equal,
            (true, false) => return std::cmp::Ordering::Greater,
            (false, true) => return std::cmp::Ordering::Less,
            _ => {}
        }
        if matches!(column, TableColumn::Field { key, .. } if key == "status") {
            // Options in GitHub order, statuses the field no longer lists after them.
            let (ra, rb) = (
                option_rank(ca).unwrap_or(usize::MAX),
                option_rank(cb).unwrap_or(usize::MAX),
            );
            if ra != rb {
                return ra.cmp(&rb);
            }
        }
        if numeric {
            let na: f64 = ca.trim_start_matches('#').parse().unwrap_or(0.0);
            let nb: f64 = cb.trim_start_matches('#').parse().unwrap_or(0.0);
            na.partial_cmp(&nb).unwrap_or(std::cmp::Ordering::Equal)
        } else {
            // Compare lowercased char streams without allocating per comparison.
            ca.chars()
                .flat_map(char::to_lowercase)
                .cmp(cb.chars().flat_map(char::to_lowercase))
        }
    });
    order
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    pub(crate) const FIELDS_JSON: &str = r#"{"fields":[
      {"id":"F1","name":"Title","type":"ProjectV2Field"},
      {"id":"F2","name":"Assignees","type":"ProjectV2Field"},
      {"id":"F3","name":"Status","options":[{"id":"a","name":"Todo"},{"id":"b","name":"In Progress"},{"id":"c","name":"Done"}],"type":"ProjectV2SingleSelectField"},
      {"id":"F4","name":"Labels","type":"ProjectV2Field"},
      {"id":"F5","name":"Linked pull requests","type":"ProjectV2Field"},
      {"id":"F6","name":"Priority","options":[{"id":"p1","name":"P1"},{"id":"p2","name":"P2"}],"type":"ProjectV2SingleSelectField"},
      {"id":"F7","name":"Estimate","type":"ProjectV2Field"},
      {"id":"F8","name":"Iteration","type":"ProjectV2IterationField"},
      {"id":"F9","name":"Start date","type":"ProjectV2Field"}
    ],"totalCount":9}"#;

    pub(crate) const ITEMS_JSON: &str = r###"{"items":[
      {"content":{"body":"## Problem\n\nText","number":114,"repository":"td72/vig","title":"Config: explicit page slots","type":"Issue","url":"https://github.com/td72/vig/issues/114"},
       "id":"I1","labels":["enhancement"],"linked pull requests":["https://github.com/td72/vig/pull/117"],"repository":"https://github.com/td72/vig","status":"Done","title":"Config: explicit page slots","assignees":["td72"],"estimate":3,"priority":"P1"},
      {"content":{"body":"","number":124,"repository":"td72/vig","title":"Fold the Actions page","type":"PullRequest","url":"https://github.com/td72/vig/pull/124"},
       "id":"I2","repository":"https://github.com/td72/vig","status":"In Progress","title":"Fold the Actions page","estimate":1.5,"iteration":{"title":"Sprint 3","startDate":"2026-08-24","duration":14,"iterationId":"it3"},"start date":"2026-08-25"},
      {"content":{"body":"Draft item used by the vig demo.","id":"DI_1","title":"Record the Projects demo tape","type":"DraftIssue"},
       "id":"I3","title":"Record the Projects demo tape"},
      {"content":{"body":"","number":119,"repository":"td72/vig","title":"Projects view","type":"Issue","url":"https://github.com/td72/vig/issues/119"},
       "id":"I4","status":"Todo","title":"Projects view","priority":"P2","estimate":2},
      {"content":null,"id":"I5","status":"Blocked","title":"redacted"}
    ],"totalCount":7}"###;

    pub(crate) fn board() -> Board {
        let fields: FieldList = serde_json::from_str(FIELDS_JSON).unwrap();
        let items: ItemList = serde_json::from_str(ITEMS_JSON).unwrap();
        Board {
            number: 2,
            fields: fields.fields,
            items: items.items,
            total_count: items.total_count,
        }
    }

    /// `gh repo view --json nameWithOwner,owner,projectsV2` for a repository
    /// with a user project, an organization project and a closed one linked.
    pub(crate) const REPO_JSON: &str = r#"{"nameWithOwner":"td72/vig","owner":{"id":"X","login":"td72"},"projectsV2":{"Nodes":[
      {"id":"PVT_1","title":"vig demo board","number":2,"resourcePath":"/users/td72/projects/2","closed":false,"url":"https://github.com/users/td72/projects/2"},
      {"id":"PVT_2","title":"Roadmap","number":7,"resourcePath":"/orgs/acme/projects/7","closed":false,"url":"https://github.com/orgs/acme/projects/7"},
      {"id":"PVT_3","title":"Old","number":1,"resourcePath":"/users/td72/projects/1","closed":true,"url":"https://github.com/users/td72/projects/1"}
    ]}}"#;

    pub(crate) fn repo_info() -> RepoInfo {
        serde_json::from_str(REPO_JSON).unwrap()
    }

    #[test]
    fn parses_repo_info_into_the_linked_projects() {
        let info = repo_info();
        assert_eq!(info.name_with_owner, "td72/vig");
        assert_eq!(info.owner.login, "td72");
        let numbers: Vec<u64> = info.projects_v2.nodes.iter().map(|p| p.number).collect();
        assert_eq!(numbers, [2, 7, 1]);
        // Closed projects are dropped; the owner comes from resourcePath.
        let linked = info.linked_projects();
        assert_eq!(linked.len(), 2);
        assert_eq!(linked[0].number, 2);
        assert_eq!(linked[0].title, "vig demo board");
        assert_eq!(linked[0].url, "https://github.com/users/td72/projects/2");
        assert_eq!(linked[0].owner.login, "td72");
        assert_eq!(linked[0].owner.kind, "User");
        assert!(linked[0].linked);
        assert_eq!(
            linked[0].items.total_count, 0,
            "unknown until the board loads"
        );
        assert_eq!(linked[1].number, 7);
        assert_eq!(linked[1].owner.login, "acme");
        assert_eq!(linked[1].owner.kind, "Organization");
        // No resourcePath: fall back to the repository owner.
        let bare = LinkedProject {
            number: 3,
            ..Default::default()
        };
        assert_eq!(bare.owner_login(), None);
        assert_eq!(bare.to_project("td72").owner.login, "td72");
        // Older gh output without the field, and a repository without links.
        let info: RepoInfo = serde_json::from_str(r#"{"owner":{"login":"o"}}"#).unwrap();
        assert!(info.projects_v2.nodes.is_empty());
        assert!(info.linked_projects().is_empty());
        let info: RepoInfo =
            serde_json::from_str(r#"{"owner":{"login":"o"},"projectsV2":{"Nodes":[]}}"#).unwrap();
        assert!(info.linked_projects().is_empty());
    }

    #[test]
    fn parses_field_list_and_derives_item_keys() {
        let list: FieldList = serde_json::from_str(FIELDS_JSON).unwrap();
        assert_eq!(list.fields.len(), 9);
        let status = list.fields.iter().find(|f| f.is_status()).unwrap();
        let names: Vec<&str> = status.options.iter().map(|o| o.name.as_str()).collect();
        assert_eq!(names, ["Todo", "In Progress", "Done"]);
        assert_eq!(list.fields[4].item_key(), "linked pull requests");
        assert_eq!(list.fields[8].item_key(), "start date");
        assert_eq!(item_key("Priority"), "priority");
        assert_eq!(item_key(""), "");
        let table: Vec<&str> = table_fields(&list.fields)
            .iter()
            .map(|f| f.name.as_str())
            .collect();
        assert_eq!(
            table,
            ["Status", "Priority", "Estimate", "Iteration", "Start date"]
        );
    }

    #[test]
    fn parses_item_list_with_issue_pr_draft_and_redacted_items() {
        let list: ItemList = serde_json::from_str(ITEMS_JSON).unwrap();
        assert_eq!(list.total_count, 7);
        assert_eq!(list.items.len(), 5);
        let issue = &list.items[0];
        assert_eq!(issue.kind(), ItemKind::Issue);
        assert_eq!(issue.number(), Some(114));
        assert_eq!(issue.repository(), Some("td72/vig"));
        assert_eq!(issue.status.as_deref(), Some("Done"));
        assert_eq!(issue.assignees(), ["td72"]);
        assert_eq!(issue.field_text("estimate").as_deref(), Some("3"));
        assert_eq!(issue.field_text("priority").as_deref(), Some("P1"));
        assert_eq!(issue.field_text("labels").as_deref(), Some("enhancement"));
        assert!(issue.field_text("iteration").is_none());
        let pr = &list.items[1];
        assert_eq!(pr.kind(), ItemKind::PullRequest);
        assert_eq!(pr.field_text("estimate").as_deref(), Some("1.5"));
        assert_eq!(
            pr.field_text("iteration").as_deref(),
            Some("Sprint 3 (2026-08-24)")
        );
        assert_eq!(pr.field_text("start date").as_deref(), Some("2026-08-25"));
        assert!(pr.assignees().is_empty());
        let draft = &list.items[2];
        assert_eq!(draft.kind(), ItemKind::Draft);
        assert_eq!(draft.number(), None);
        assert!(draft.status.is_none());
        assert_eq!(draft.body(), "Draft item used by the vig demo.");
        assert_eq!(draft.title(), "Record the Projects demo tape");
        let redacted = &list.items[4];
        assert_eq!(redacted.kind(), ItemKind::Other);
        assert!(redacted.url().is_none());
    }

    #[test]
    fn field_values_follow_field_order_with_status_first() {
        let board = board();
        let values = board.items[1].field_values(&board.fields);
        let keys: Vec<&str> = values.iter().map(|(k, _)| k.as_str()).collect();
        assert_eq!(
            keys,
            [
                "Status",
                "Estimate",
                "Iteration",
                "Start date",
                "Repository"
            ]
        );
        assert_eq!(values[0].1, "In Progress");
        // A field the project no longer lists is still shown (capitalized key).
        let mut item = board.items[0].clone();
        item.fields
            .insert("legacy".into(), serde_json::Value::String("x".into()));
        let values = item.field_values(&board.fields);
        assert!(values.iter().any(|(k, v)| k == "Legacy" && v == "x"));
    }

    #[test]
    fn columns_follow_option_order_then_unknown_then_no_status() {
        let board = board();
        let cols = board.columns();
        let names: Vec<&str> = cols.iter().map(|c| c.name.as_str()).collect();
        assert_eq!(names, ["Todo", "In Progress", "Done", "Blocked", NO_STATUS]);
        assert_eq!(cols[0].items, [3]);
        assert_eq!(cols[1].items, [1]);
        assert_eq!(cols[2].items, [0]);
        assert_eq!(cols[3].items, [4]);
        assert_eq!(cols[4].items, [2]);
        assert!(board.truncated());
        // Without a Status field every item is "No status".
        let mut plain = board.clone();
        plain.fields.retain(|f| !f.is_status());
        for item in &mut plain.items {
            item.status = None;
        }
        let cols = plain.columns();
        assert_eq!(cols.len(), 1);
        assert_eq!(cols[0].name, NO_STATUS);
        assert_eq!(cols[0].items.len(), 5);
        // No column for "No status" when every item has one.
        let mut full = board.clone();
        full.items.retain(|i| i.status.is_some());
        assert!(full.columns().iter().all(|c| c.name != NO_STATUS));
    }

    #[test]
    fn table_columns_and_sorting() {
        let board = board();
        let cols = table_columns(&board.fields);
        let headers: Vec<&str> = cols.iter().map(TableColumn::header).collect();
        assert_eq!(
            headers,
            [
                "#",
                "Title",
                "Assignees",
                "Status",
                "Priority",
                "Estimate",
                "Iteration",
                "Start date"
            ]
        );
        // Numbers sort numerically, drafts (no number) last.
        let by_number = sort_items(&board.items, &cols[0], &board);
        assert_eq!(by_number, [0, 3, 1, 2, 4]);
        // Estimate: 1.5 < 2 < 3, then the empty cells.
        let est = cols.iter().find(|c| c.header() == "Estimate").unwrap();
        let by_estimate = sort_items(&board.items, est, &board);
        assert_eq!(&by_estimate[..3], &[1, 3, 0][..]);
        // Status: option order, unknown statuses after, empty last.
        let status = cols.iter().find(|c| c.header() == "Status").unwrap();
        let by_status = sort_items(&board.items, status, &board);
        assert_eq!(by_status, [3, 1, 0, 4, 2]);
        // Titles: case-insensitive.
        let by_title = sort_items(&board.items, &cols[1], &board);
        assert_eq!(by_title[0], 0, "Config… sorts first");
    }

    #[test]
    fn value_text_covers_every_gh_shape() {
        use serde_json::json;
        assert_eq!(value_text(&json!("Todo")).as_deref(), Some("Todo"));
        assert_eq!(value_text(&json!(3)).as_deref(), Some("3"));
        assert_eq!(value_text(&json!(2.5)).as_deref(), Some("2.5"));
        assert_eq!(value_text(&json!(null)), None);
        assert_eq!(value_text(&json!([])), None);
        assert_eq!(value_text(&json!(["a", "b"])).as_deref(), Some("a, b"));
        assert_eq!(
            value_text(&json!({"title":"v1.0","description":"","dueOn":"2026-09-01"})).as_deref(),
            Some("v1.0")
        );
        assert_eq!(
            value_text(&json!({"title":"Sprint 1","startDate":"2026-08-01","duration":7}))
                .as_deref(),
            Some("Sprint 1 (2026-08-01)")
        );
    }
}