beads_viewer_rust 0.2.1

Spec-first Rust port of beads_viewer (bv) — graph-aware triage for beads issue trackers (CLI binary: bvr)
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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::{BvrError, Result};

const KNOWN_STATUSES: &[&str] = &[
    "open",
    "in_progress",
    "blocked",
    "deferred",
    "pinned",
    "hooked",
    "review",
    "closed",
    "tombstone",
];

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Issue {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub design: String,
    #[serde(default)]
    pub acceptance_criteria: String,
    #[serde(default)]
    pub notes: String,
    #[serde(default)]
    pub status: String,
    #[serde(default = "default_priority")]
    pub priority: i32,
    #[serde(default)]
    pub issue_type: String,
    #[serde(default)]
    pub assignee: String,
    #[serde(default)]
    pub estimated_minutes: Option<i32>,
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    #[serde(default)]
    pub due_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub closed_at: Option<DateTime<Utc>>,
    #[serde(default)]
    pub labels: Vec<String>,
    #[serde(default)]
    pub comments: Vec<Comment>,
    #[serde(default)]
    pub dependencies: Vec<Dependency>,
    #[serde(default)]
    pub source_repo: String,
    /// Internal workspace prefix used to recover raw IDs from namespaced
    /// workspace issues. Computed during workspace loading and never emitted.
    #[serde(skip)]
    pub workspace_prefix: Option<String>,
    /// Internal content hash for dedup — computed, not serialized to JSON output.
    #[serde(default, skip_serializing)]
    pub content_hash: Option<String>,
    /// Optional link to external issue tracker (e.g., GitHub issue URL).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub external_ref: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Dependency {
    #[serde(default)]
    pub issue_id: String,
    #[serde(default)]
    pub depends_on_id: String,
    #[serde(default, rename = "type")]
    pub dep_type: String,
    #[serde(default)]
    pub created_by: String,
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Comment {
    #[serde(default)]
    pub id: i64,
    #[serde(default)]
    pub issue_id: String,
    #[serde(default)]
    pub author: String,
    #[serde(default)]
    pub text: String,
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
}

impl Dependency {
    #[must_use]
    pub fn is_blocking(&self) -> bool {
        // Mirrors beads_rust::model::DependencyType::is_blocking: the four
        // edge kinds that gate readiness. `parent-child` is listed on the
        // authoritative side for propagation-only purposes and is handled
        // separately in `actionable_ids`; it must NOT be treated as a direct
        // blocker here or blocker-graph edges would double up.
        let t = self.dep_type.trim().to_ascii_lowercase();
        matches!(
            t.as_str(),
            "" | "blocks" | "waits-for" | "conditional-blocks"
        )
    }

    #[must_use]
    pub fn is_parent_child(&self) -> bool {
        let t = self.dep_type.trim().to_ascii_lowercase();
        t == "parent-child"
    }
}

/// Parse an RFC 3339 timestamp string into `DateTime<Utc>`.
///
/// Accepts both `"2025-01-10T10:00:00Z"` and `"2025-01-10T10:00:00+00:00"`.
pub fn parse_timestamp(s: &str) -> Option<DateTime<Utc>> {
    DateTime::parse_from_rfc3339(s)
        .ok()
        .map(|dt| dt.with_timezone(&Utc))
}

/// Shorthand to create `Some(DateTime<Utc>)` from an RFC 3339 string.
/// Panics if the string is invalid — intended for test fixtures.
pub fn ts(s: &str) -> Option<DateTime<Utc>> {
    Some(
        DateTime::parse_from_rfc3339(s)
            .unwrap_or_else(|e| panic!("invalid timestamp {s:?}: {e}"))
            .with_timezone(&Utc),
    )
}

/// Create a timestamp N days before now. Useful for test fixtures that need
/// staleness-relative dates instead of hard-coded absolute timestamps.
pub fn days_ago(n: i64) -> Option<DateTime<Utc>> {
    Some(Utc::now() - chrono::Duration::days(n))
}

impl Issue {
    #[must_use]
    pub fn normalized_status(&self) -> String {
        self.status.trim().to_ascii_lowercase()
    }

    /// Returns true for any terminal status (closed or tombstone).
    #[must_use]
    pub fn is_closed_like(&self) -> bool {
        matches!(self.normalized_status().as_str(), "closed" | "tombstone")
    }

    /// Returns true only for the "closed" status (not tombstone).
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.normalized_status() == "closed"
    }

    /// Returns true only for the "tombstone" status (permanently removed).
    #[must_use]
    pub fn is_tombstone(&self) -> bool {
        self.normalized_status() == "tombstone"
    }

    /// Returns true when the issue is already being worked on.
    #[must_use]
    pub fn is_in_progress(&self) -> bool {
        self.normalized_status() == "in_progress"
    }

    #[must_use]
    pub fn is_open_like(&self) -> bool {
        !self.is_closed_like()
    }

    #[must_use]
    pub fn priority_normalized(&self) -> f64 {
        let p = self.priority.clamp(0, 4);
        // Priority 0 => 1.0, Priority 4 => 0.2
        (5_i32.saturating_sub(p)) as f64 / 5.0
    }

    pub fn validate(&self) -> Result<()> {
        if self.id.trim().is_empty() {
            return Err(BvrError::InvalidIssue(
                "issue id cannot be empty".to_string(),
            ));
        }
        if self.title.trim().is_empty() {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} title cannot be empty",
                self.id
            )));
        }
        if self.issue_type.trim().is_empty() {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} issue_type cannot be empty",
                self.id
            )));
        }

        let status = self.normalized_status();
        if status.is_empty() {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} status cannot be empty",
                self.id
            )));
        }
        if !KNOWN_STATUSES.contains(&status.as_str()) {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} has unknown status: {}",
                self.id, self.status
            )));
        }

        if let (Some(created_at), Some(updated_at)) = (self.created_at, self.updated_at)
            && updated_at < created_at
        {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} updated_at cannot be earlier than created_at",
                self.id
            )));
        }

        if let (Some(created_at), Some(closed_at)) = (self.created_at, self.closed_at)
            && closed_at < created_at
        {
            return Err(BvrError::InvalidIssue(format!(
                "issue {} closed_at cannot be earlier than created_at",
                self.id
            )));
        }

        Ok(())
    }
}

const fn default_priority() -> i32 {
    3
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Sprint {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub start_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub end_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub bead_ids: Vec<String>,
}

impl Sprint {
    #[must_use]
    pub fn is_active_at(&self, now: DateTime<Utc>) -> bool {
        let Some(start_date) = self.start_date else {
            return false;
        };
        let Some(end_date) = self.end_date else {
            return false;
        };

        now >= start_date && now <= end_date
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BurndownPoint {
    pub date: DateTime<Utc>,
    pub remaining: i32,
    pub completed: i32,
}

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

    // -- Dependency tests --

    #[test]
    fn dependency_is_blocking_for_empty_type() {
        let dep = Dependency {
            dep_type: String::new(),
            ..Default::default()
        };
        assert!(dep.is_blocking());
    }

    #[test]
    fn dependency_is_blocking_for_blocks_type() {
        let dep = Dependency {
            dep_type: "blocks".to_string(),
            ..Default::default()
        };
        assert!(dep.is_blocking());
        // case insensitive + trim
        let dep2 = Dependency {
            dep_type: "  Blocks  ".to_string(),
            ..Default::default()
        };
        assert!(dep2.is_blocking());
    }

    #[test]
    fn dependency_is_blocking_for_waits_for_and_conditional_blocks() {
        // Regression for bvr #14: waits-for and conditional-blocks were silently
        // ignored, letting dependent issues surface as actionable/top-picks while
        // `br ready` (the authoritative view) correctly excluded them.
        for t in ["waits-for", "conditional-blocks"] {
            let dep = Dependency {
                dep_type: t.to_string(),
                ..Default::default()
            };
            assert!(dep.is_blocking(), "{t} should be blocking");
        }
        // Case-insensitive + trim applies uniformly
        let dep = Dependency {
            dep_type: "  Waits-For  ".to_string(),
            ..Default::default()
        };
        assert!(dep.is_blocking());
        let dep = Dependency {
            dep_type: "Conditional-Blocks".to_string(),
            ..Default::default()
        };
        assert!(dep.is_blocking());
    }

    #[test]
    fn dependency_not_blocking_for_other_types() {
        // `parent-child` is excluded deliberately: it's a propagation edge in
        // beads_rust's blocked-cache rebuild (src/storage/sqlite.rs:3109-3115),
        // not a direct blocker. `actionable_ids` handles parent-child
        // transitively via epic-blocked propagation.
        for t in [
            "parent-child",
            "related",
            "mentions",
            "discovered-from",
            "unknown",
        ] {
            let dep = Dependency {
                dep_type: t.to_string(),
                ..Default::default()
            };
            assert!(!dep.is_blocking(), "{t} should not be blocking");
        }
    }

    #[test]
    fn dependency_is_parent_child() {
        let dep = Dependency {
            dep_type: "parent-child".to_string(),
            ..Default::default()
        };
        assert!(dep.is_parent_child());
        // case insensitive
        let dep2 = Dependency {
            dep_type: " Parent-Child ".to_string(),
            ..Default::default()
        };
        assert!(dep2.is_parent_child());
        // Not parent-child
        let dep3 = Dependency {
            dep_type: "blocks".to_string(),
            ..Default::default()
        };
        assert!(!dep3.is_parent_child());
    }

    // -- Issue status tests --

    #[test]
    fn normalized_status_lowercases_and_trims() {
        let issue = Issue {
            status: "  OPEN  ".to_string(),
            ..Default::default()
        };
        assert_eq!(issue.normalized_status(), "open");
    }

    #[test]
    fn is_closed_like_detects_closed_and_tombstone() {
        for status in ["closed", "Closed", "CLOSED", "tombstone", "Tombstone"] {
            let issue = Issue {
                status: status.to_string(),
                ..Default::default()
            };
            assert!(issue.is_closed_like(), "{status} should be closed-like");
            assert!(!issue.is_open_like(), "{status} should not be open-like");
        }
    }

    #[test]
    fn is_closed_vs_tombstone_distinction() {
        let closed = Issue {
            status: "closed".to_string(),
            ..Default::default()
        };
        assert!(closed.is_closed());
        assert!(!closed.is_tombstone());
        assert!(closed.is_closed_like());

        let tombstone = Issue {
            status: "tombstone".to_string(),
            ..Default::default()
        };
        assert!(!tombstone.is_closed());
        assert!(tombstone.is_tombstone());
        assert!(tombstone.is_closed_like());

        let open = Issue {
            status: "open".to_string(),
            ..Default::default()
        };
        assert!(!open.is_closed());
        assert!(!open.is_tombstone());
        assert!(!open.is_closed_like());
    }

    #[test]
    fn is_open_like_for_all_open_statuses() {
        for status in [
            "open",
            "in_progress",
            "blocked",
            "deferred",
            "pinned",
            "hooked",
            "review",
        ] {
            let issue = Issue {
                status: status.to_string(),
                ..Default::default()
            };
            assert!(issue.is_open_like(), "{status} should be open-like");
            assert!(
                !issue.is_closed_like(),
                "{status} should not be closed-like"
            );
        }
    }

    #[test]
    fn content_hash_and_external_ref_defaults() {
        let issue = Issue::default();
        assert!(issue.workspace_prefix.is_none());
        assert!(issue.content_hash.is_none());
        assert!(issue.external_ref.is_none());

        let issue_with_ref = Issue {
            external_ref: Some("https://github.com/org/repo/issues/42".to_string()),
            ..Default::default()
        };
        assert_eq!(
            issue_with_ref.external_ref.as_deref(),
            Some("https://github.com/org/repo/issues/42")
        );
    }

    #[test]
    fn workspace_prefix_is_never_serialized_or_deserialized() {
        let issue = Issue {
            id: "api-1".to_string(),
            workspace_prefix: Some("api-".to_string()),
            ..Default::default()
        };
        let json = serde_json::to_value(&issue).unwrap();
        assert!(
            json.get("workspace_prefix").is_none(),
            "internal workspace_prefix must not be serialized"
        );

        let parsed: Issue = serde_json::from_str(
            r#"{"id":"api-1","title":"T","status":"open","issue_type":"task","workspace_prefix":"evil-"}"#,
        )
        .unwrap();
        assert!(
            parsed.workspace_prefix.is_none(),
            "internal workspace_prefix must not be accepted from external JSON"
        );
    }

    // -- Priority normalization --

    #[test]
    fn priority_normalized_maps_p0_to_highest_and_p4_to_lowest() {
        let p0 = Issue {
            priority: 0,
            ..Default::default()
        };
        assert!((p0.priority_normalized() - 1.0).abs() < f64::EPSILON);

        let p4 = Issue {
            priority: 4,
            ..Default::default()
        };
        assert!((p4.priority_normalized() - 0.2).abs() < f64::EPSILON);
    }

    #[test]
    fn priority_normalized_distinguishes_p0_from_p1() {
        let p0 = Issue {
            priority: 0,
            ..Default::default()
        };
        let p1 = Issue {
            priority: 1,
            ..Default::default()
        };

        assert!(p0.priority_normalized() > p1.priority_normalized());
        assert!((p1.priority_normalized() - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn priority_normalized_clamps_out_of_range() {
        let too_low = Issue {
            priority: -10,
            ..Default::default()
        };
        // clamp(0, 4) => 0 => (5-0)/5 = 1.0
        assert!((too_low.priority_normalized() - 1.0).abs() < f64::EPSILON);

        let too_high = Issue {
            priority: 100,
            ..Default::default()
        };
        // clamp(0, 4) => 4 => (5-4)/5 = 0.2
        assert!((too_high.priority_normalized() - 0.2).abs() < f64::EPSILON);
    }

    #[test]
    fn priority_normalized_default_treats_zero_as_p0() {
        // Issue::default() has priority=0 (Rust default), which is also the valid P0 value.
        let issue = Issue::default();
        assert_eq!(issue.priority, 0);
        assert!((issue.priority_normalized() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn priority_normalized_serde_default_is_3() {
        let json = r#"{"id":"X","title":"T"}"#;
        let issue: Issue = serde_json::from_str(json).unwrap();
        assert_eq!(issue.priority, 3);
        // (5-3)/5 = 0.4
        assert!((issue.priority_normalized() - 0.4).abs() < f64::EPSILON);
    }

    // -- Validation --

    #[test]
    fn validate_rejects_empty_id() {
        let issue = Issue {
            id: "  ".to_string(),
            title: "T".to_string(),
            issue_type: "task".to_string(),
            status: "open".to_string(),
            ..Default::default()
        };
        let err = issue.validate().unwrap_err();
        assert!(err.to_string().contains("id cannot be empty"));
    }

    #[test]
    fn validate_rejects_empty_title() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: String::new(),
            issue_type: "task".to_string(),
            status: "open".to_string(),
            ..Default::default()
        };
        let err = issue.validate().unwrap_err();
        assert!(err.to_string().contains("title cannot be empty"));
    }

    #[test]
    fn validate_rejects_empty_type() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: String::new(),
            status: "open".to_string(),
            ..Default::default()
        };
        let err = issue.validate().unwrap_err();
        assert!(err.to_string().contains("issue_type cannot be empty"));
    }

    #[test]
    fn validate_rejects_empty_status() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: String::new(),
            ..Default::default()
        };
        let err = issue.validate().unwrap_err();
        assert!(err.to_string().contains("status cannot be empty"));
    }

    #[test]
    fn validate_rejects_unknown_status() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: "banana".to_string(),
            ..Default::default()
        };
        let err = issue.validate().unwrap_err();
        assert!(err.to_string().contains("unknown status"));
    }

    #[test]
    fn validate_accepts_all_known_statuses() {
        for status in KNOWN_STATUSES {
            let issue = Issue {
                id: "X-1".to_string(),
                title: "Test".to_string(),
                issue_type: "task".to_string(),
                status: status.to_string(),
                ..Default::default()
            };
            assert!(issue.validate().is_ok(), "status {status} should be valid");
        }
    }

    #[test]
    fn validate_rejects_updated_at_before_created_at() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: "open".to_string(),
            created_at: ts("2025-01-02T00:00:00Z"),
            updated_at: ts("2025-01-01T00:00:00Z"),
            ..Default::default()
        };

        let err = issue.validate().unwrap_err();
        assert!(
            err.to_string()
                .contains("updated_at cannot be earlier than created_at")
        );
    }

    #[test]
    fn validate_accepts_equal_created_and_updated_timestamps() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: "open".to_string(),
            created_at: ts("2025-01-01T00:00:00Z"),
            updated_at: ts("2025-01-01T00:00:00Z"),
            ..Default::default()
        };

        assert!(issue.validate().is_ok());
    }

    #[test]
    fn validate_rejects_closed_at_before_created_at() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: "closed".to_string(),
            created_at: ts("2025-01-02T00:00:00Z"),
            closed_at: ts("2025-01-01T00:00:00Z"),
            ..Default::default()
        };

        let err = issue.validate().unwrap_err();
        assert!(
            err.to_string()
                .contains("closed_at cannot be earlier than created_at")
        );
    }

    #[test]
    fn validate_accepts_equal_created_and_closed_timestamps() {
        let issue = Issue {
            id: "X-1".to_string(),
            title: "Test".to_string(),
            issue_type: "task".to_string(),
            status: "closed".to_string(),
            created_at: ts("2025-01-01T00:00:00Z"),
            closed_at: ts("2025-01-01T00:00:00Z"),
            ..Default::default()
        };

        assert!(issue.validate().is_ok());
    }

    // -- Sprint tests --

    #[test]
    fn sprint_is_active_at_within_range() {
        let sprint = Sprint {
            id: "s1".to_string(),
            name: "Sprint 1".to_string(),
            start_date: Some("2026-01-01T00:00:00Z".parse().unwrap()),
            end_date: Some("2026-01-14T00:00:00Z".parse().unwrap()),
            bead_ids: Vec::new(),
        };
        let mid: DateTime<Utc> = "2026-01-07T12:00:00Z".parse().unwrap();
        assert!(sprint.is_active_at(mid));
    }

    #[test]
    fn sprint_not_active_outside_range() {
        let sprint = Sprint {
            id: "s1".to_string(),
            name: "Sprint 1".to_string(),
            start_date: Some("2026-01-01T00:00:00Z".parse().unwrap()),
            end_date: Some("2026-01-14T00:00:00Z".parse().unwrap()),
            bead_ids: Vec::new(),
        };
        let before: DateTime<Utc> = "2025-12-31T00:00:00Z".parse().unwrap();
        let after: DateTime<Utc> = "2026-01-15T00:00:00Z".parse().unwrap();
        assert!(!sprint.is_active_at(before));
        assert!(!sprint.is_active_at(after));
    }

    #[test]
    fn sprint_not_active_without_dates() {
        let sprint = Sprint {
            start_date: None,
            end_date: None,
            ..Default::default()
        };
        let now: DateTime<Utc> = "2026-01-07T00:00:00Z".parse().unwrap();
        assert!(!sprint.is_active_at(now));
    }

    #[test]
    fn sprint_active_at_boundary() {
        let sprint = Sprint {
            start_date: Some("2026-01-01T00:00:00Z".parse().unwrap()),
            end_date: Some("2026-01-14T00:00:00Z".parse().unwrap()),
            ..Default::default()
        };
        let at_start: DateTime<Utc> = "2026-01-01T00:00:00Z".parse().unwrap();
        let at_end: DateTime<Utc> = "2026-01-14T00:00:00Z".parse().unwrap();
        assert!(sprint.is_active_at(at_start), "active at start boundary");
        assert!(sprint.is_active_at(at_end), "active at end boundary");
    }

    // -- Serde round-trip --

    #[test]
    fn issue_deserializes_with_defaults() {
        let json = r#"{"id":"X-1","title":"Test"}"#;
        let issue: Issue = serde_json::from_str(json).unwrap();
        assert_eq!(issue.id, "X-1");
        assert_eq!(issue.priority, 3); // default
        assert_eq!(issue.status, "");
        assert!(issue.labels.is_empty());
        assert!(issue.dependencies.is_empty());
    }

    #[test]
    fn dependency_deserializes_type_field() {
        let json = r#"{"issue_id":"A","depends_on_id":"B","type":"blocks"}"#;
        let dep: Dependency = serde_json::from_str(json).unwrap();
        assert_eq!(dep.dep_type, "blocks");
        assert!(dep.is_blocking());
    }
}