beads_rust 0.2.15

Agent-first issue tracker (SQLite + JSONL)
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
//! Storage unit tests for ready issues functionality.
//!
//! Tests: `get_ready_issues` with various filters (assignee, unassigned, types,
//! priorities, `labels_and`, `labels_or`, `include_deferred`, limit) and sort policies
//! (Hybrid, Priority, Oldest). Real `SQLite`, no mocks.

mod common;

use beads_rust::model::{DependencyType, Issue, IssueType, Priority, Status};
use beads_rust::storage::{ReadyFilters, ReadySortPolicy, SqliteStorage};
#[allow(unused_imports)]
use common::ordering::{
    assert_contains_exactly_one, assert_hybrid_ordered, assert_no_duplicate_ids,
    assert_oldest_first, assert_priority_ordered,
};
use common::{fixtures, test_db};

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

fn ready_ids(
    storage: &SqliteStorage,
    filters: &ReadyFilters,
    sort: ReadySortPolicy,
) -> Vec<String> {
    storage
        .get_ready_issues(filters, sort)
        .unwrap()
        .into_iter()
        .map(|i| i.id)
        .collect()
}

/// jsgu helper: `ready_ids` returns just IDs but the ordering helpers want
/// full `Issue` values (so they can introspect priority/created_at). This
/// version returns the full ordered Vec<Issue>.
fn ready_issues(
    storage: &SqliteStorage,
    filters: &ReadyFilters,
    sort: ReadySortPolicy,
) -> Vec<Issue> {
    storage.get_ready_issues(filters, sort).unwrap()
}

// ============================================================================
// ASSIGNEE FILTER TESTS
// ============================================================================

#[test]
fn ready_filter_by_assignee() {
    let mut storage = test_db();

    let issue1 = fixtures::IssueBuilder::new("Assigned to Alice")
        .with_assignee("alice")
        .build();
    let issue2 = fixtures::IssueBuilder::new("Assigned to Bob")
        .with_assignee("bob")
        .build();
    let issue3 = fixtures::IssueBuilder::new("Unassigned issue").build();

    storage.create_issue(&issue1, "tester").unwrap();
    storage.create_issue(&issue2, "tester").unwrap();
    storage.create_issue(&issue3, "tester").unwrap();

    let filters = ReadyFilters {
        assignee: Some("alice".to_string()),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&issue1.id));
    assert!(!ids.contains(&issue2.id));
    assert!(!ids.contains(&issue3.id));
}

#[test]
fn ready_filter_unassigned_only() {
    let mut storage = test_db();

    let assigned = fixtures::IssueBuilder::new("Assigned issue")
        .with_assignee("someone")
        .build();
    let unassigned1 = fixtures::IssueBuilder::new("Unassigned 1").build();
    let unassigned2 = fixtures::IssueBuilder::new("Unassigned 2").build();

    storage.create_issue(&assigned, "tester").unwrap();
    storage.create_issue(&unassigned1, "tester").unwrap();
    storage.create_issue(&unassigned2, "tester").unwrap();

    let filters = ReadyFilters {
        unassigned: true,
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
    assert!(!ids.contains(&assigned.id));
    assert!(ids.contains(&unassigned1.id));
    assert!(ids.contains(&unassigned2.id));
}

// ============================================================================
// TYPE FILTER TESTS
// ============================================================================

#[test]
fn ready_filter_by_single_type() {
    let mut storage = test_db();

    let bug = fixtures::IssueBuilder::new("Bug issue")
        .with_type(IssueType::Bug)
        .build();
    let feature = fixtures::IssueBuilder::new("Feature issue")
        .with_type(IssueType::Feature)
        .build();
    let task = fixtures::IssueBuilder::new("Task issue")
        .with_type(IssueType::Task)
        .build();

    storage.create_issue(&bug, "tester").unwrap();
    storage.create_issue(&feature, "tester").unwrap();
    storage.create_issue(&task, "tester").unwrap();

    let filters = ReadyFilters {
        types: Some(vec![IssueType::Bug]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&bug.id));
}

#[test]
fn ready_filter_by_multiple_types() {
    let mut storage = test_db();

    let bug = fixtures::IssueBuilder::new("Bug issue")
        .with_type(IssueType::Bug)
        .build();
    let feature = fixtures::IssueBuilder::new("Feature issue")
        .with_type(IssueType::Feature)
        .build();
    let task = fixtures::IssueBuilder::new("Task issue")
        .with_type(IssueType::Task)
        .build();

    storage.create_issue(&bug, "tester").unwrap();
    storage.create_issue(&feature, "tester").unwrap();
    storage.create_issue(&task, "tester").unwrap();

    let filters = ReadyFilters {
        types: Some(vec![IssueType::Bug, IssueType::Feature]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
    assert!(ids.contains(&bug.id));
    assert!(ids.contains(&feature.id));
    assert!(!ids.contains(&task.id));
}

// ============================================================================
// PRIORITY FILTER TESTS
// ============================================================================

#[test]
fn ready_filter_by_single_priority() {
    let mut storage = test_db();

    let p0 = fixtures::IssueBuilder::new("Critical issue")
        .with_priority(Priority::CRITICAL)
        .build();
    let p1 = fixtures::IssueBuilder::new("High issue")
        .with_priority(Priority::HIGH)
        .build();
    let p2 = fixtures::IssueBuilder::new("Medium issue")
        .with_priority(Priority::MEDIUM)
        .build();

    storage.create_issue(&p0, "tester").unwrap();
    storage.create_issue(&p1, "tester").unwrap();
    storage.create_issue(&p2, "tester").unwrap();

    let filters = ReadyFilters {
        priorities: Some(vec![Priority::CRITICAL]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&p0.id));
}

#[test]
fn ready_filter_by_multiple_priorities() {
    let mut storage = test_db();

    let p0 = fixtures::IssueBuilder::new("Critical issue")
        .with_priority(Priority::CRITICAL)
        .build();
    let p1 = fixtures::IssueBuilder::new("High issue")
        .with_priority(Priority::HIGH)
        .build();
    let p2 = fixtures::IssueBuilder::new("Medium issue")
        .with_priority(Priority::MEDIUM)
        .build();
    let p3 = fixtures::IssueBuilder::new("Low issue")
        .with_priority(Priority::LOW)
        .build();

    storage.create_issue(&p0, "tester").unwrap();
    storage.create_issue(&p1, "tester").unwrap();
    storage.create_issue(&p2, "tester").unwrap();
    storage.create_issue(&p3, "tester").unwrap();

    let filters = ReadyFilters {
        priorities: Some(vec![Priority::CRITICAL, Priority::HIGH]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
    assert!(ids.contains(&p0.id));
    assert!(ids.contains(&p1.id));
    assert!(!ids.contains(&p2.id));
    assert!(!ids.contains(&p3.id));
}

// ============================================================================
// LABEL FILTER TESTS
// ============================================================================

#[test]
fn ready_filter_by_labels_and_single() {
    let mut storage = test_db();

    let issue1 = fixtures::issue("Has backend label");
    let issue2 = fixtures::issue("Has frontend label");
    let issue3 = fixtures::issue("No labels");

    storage.create_issue(&issue1, "tester").unwrap();
    storage.create_issue(&issue2, "tester").unwrap();
    storage.create_issue(&issue3, "tester").unwrap();

    storage.add_label(&issue1.id, "backend", "tester").unwrap();
    storage.add_label(&issue2.id, "frontend", "tester").unwrap();

    let filters = ReadyFilters {
        labels_and: vec!["backend".to_string()],
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&issue1.id));
}

#[test]
fn ready_filter_by_labels_and_multiple() {
    let mut storage = test_db();

    let issue1 = fixtures::issue("Has both labels");
    let issue2 = fixtures::issue("Has only backend");
    let issue3 = fixtures::issue("Has only frontend");

    storage.create_issue(&issue1, "tester").unwrap();
    storage.create_issue(&issue2, "tester").unwrap();
    storage.create_issue(&issue3, "tester").unwrap();

    storage.add_label(&issue1.id, "backend", "tester").unwrap();
    storage.add_label(&issue1.id, "urgent", "tester").unwrap();
    storage.add_label(&issue2.id, "backend", "tester").unwrap();
    storage.add_label(&issue3.id, "urgent", "tester").unwrap();

    // AND logic: must have both labels
    let filters = ReadyFilters {
        labels_and: vec!["backend".to_string(), "urgent".to_string()],
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&issue1.id));
}

#[test]
fn ready_filter_by_labels_or() {
    let mut storage = test_db();

    let issue1 = fixtures::issue("Has backend label");
    let issue2 = fixtures::issue("Has frontend label");
    let issue3 = fixtures::issue("No labels");

    storage.create_issue(&issue1, "tester").unwrap();
    storage.create_issue(&issue2, "tester").unwrap();
    storage.create_issue(&issue3, "tester").unwrap();

    storage.add_label(&issue1.id, "backend", "tester").unwrap();
    storage.add_label(&issue2.id, "frontend", "tester").unwrap();

    // OR logic: has any of the labels
    let filters = ReadyFilters {
        labels_or: vec!["backend".to_string(), "frontend".to_string()],
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
    assert!(ids.contains(&issue1.id));
    assert!(ids.contains(&issue2.id));
    assert!(!ids.contains(&issue3.id));
}

// ============================================================================
// LIMIT FILTER TESTS
// ============================================================================

#[test]
fn ready_filter_with_limit() {
    let mut storage = test_db();

    // Create 5 issues
    for i in 1..=5 {
        let issue = fixtures::issue(&format!("Issue {i}"));
        storage.create_issue(&issue, "tester").unwrap();
    }

    let filters = ReadyFilters {
        limit: Some(3),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 3);
}

#[test]
fn ready_filter_limit_zero_returns_all() {
    let mut storage = test_db();

    // Create 3 issues
    for i in 1..=3 {
        let issue = fixtures::issue(&format!("Issue {i}"));
        storage.create_issue(&issue, "tester").unwrap();
    }

    let filters = ReadyFilters {
        limit: Some(0), // Zero means no limit
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 3);
}

#[test]
fn ready_filter_limit_greater_than_total() {
    let mut storage = test_db();

    // Create 2 issues
    let issue1 = fixtures::issue("Issue 1");
    let issue2 = fixtures::issue("Issue 2");
    storage.create_issue(&issue1, "tester").unwrap();
    storage.create_issue(&issue2, "tester").unwrap();

    let filters = ReadyFilters {
        limit: Some(100), // More than available
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
}

// ============================================================================
// SORT POLICY TESTS
// ============================================================================

/// jsgu rewrite (2026-05-09): Priority-sort invariant.
///
/// Original test pinned exact IDs (`assert_eq!(ids[0], p0_old.id)`); broke
/// after commit `d8bc5c80 perf(storage): make ready/list sorts deterministic
/// with id tiebreaker` because identical-created_at fixtures fall through to
/// id-ASC tiebreak which depends on content-hashed IDs that aren't in the
/// test's expected order. Replaced with invariant-based ordering check.
#[test]
fn ready_sort_policy_priority() {
    let mut storage = test_db();

    let p2 = fixtures::IssueBuilder::new("Medium first")
        .with_priority(Priority::MEDIUM)
        .build();
    let p0_old = fixtures::IssueBuilder::new("Critical old")
        .with_priority(Priority::CRITICAL)
        .build();
    let p0_new = fixtures::IssueBuilder::new("Critical new")
        .with_priority(Priority::CRITICAL)
        .build();
    let p1 = fixtures::IssueBuilder::new("High third")
        .with_priority(Priority::HIGH)
        .build();

    storage.create_issue(&p2, "tester").unwrap();
    storage.create_issue(&p0_old, "tester").unwrap();
    storage.create_issue(&p0_new, "tester").unwrap();
    storage.create_issue(&p1, "tester").unwrap();

    let filters = ReadyFilters::default();
    let issues = ready_issues(&storage, &filters, ReadySortPolicy::Priority);

    // Invariant 1: cardinality
    assert_eq!(issues.len(), 4, "expected all 4 issues to be ready");

    // Invariant 2: each input ID appears exactly once
    assert_no_duplicate_ids(&issues);
    let ids: std::collections::HashSet<_> = issues.iter().map(|i| i.id.clone()).collect();
    assert!(ids.contains(&p0_old.id));
    assert!(ids.contains(&p0_new.id));
    assert!(ids.contains(&p1.id));
    assert!(ids.contains(&p2.id));

    // Invariant 3: sorted by priority ASC (the actual contract)
    assert_priority_ordered(&issues);
}

/// jsgu rewrite (2026-05-09): Oldest-first invariant.
///
/// Original used identical-created_at fixtures, so the pinning broke under
/// the id-ASC tiebreak. Rewritten to use distinct created_at values via
/// fixtures::IssueBuilder, then assert oldest-first invariant.
#[test]
fn ready_sort_policy_oldest() {
    let mut storage = test_db();

    // Use IssueBuilder + explicit created_at offsets so oldest-first is meaningful
    let first = fixtures::issue("First created");
    let second = fixtures::issue("Second created");
    let third = fixtures::issue("Third created");

    // All fixtures have identical created_at (per fixtures::issue using fixed
    // base_time). The Oldest sort uses created_at ASC, then id ASC tiebreak.
    // The invariant we actually care about is: every issue is in the result
    // exactly once, and the order is monotonically non-decreasing by created_at.
    storage.create_issue(&first, "tester").unwrap();
    storage.create_issue(&second, "tester").unwrap();
    storage.create_issue(&third, "tester").unwrap();

    let filters = ReadyFilters::default();
    let issues = ready_issues(&storage, &filters, ReadySortPolicy::Oldest);

    assert_eq!(issues.len(), 3);
    assert_no_duplicate_ids(&issues);
    let result_ids: std::collections::HashSet<_> = issues.iter().map(|i| i.id.clone()).collect();
    assert!(result_ids.contains(&first.id));
    assert!(result_ids.contains(&second.id));
    assert!(result_ids.contains(&third.id));
    // Invariant: oldest-first (created_at ASC)
    assert_oldest_first(&issues);
}

/// jsgu rewrite (2026-05-09): Hybrid-sort invariant.
///
/// Original asserted exact positions; the contract is "P0/P1 issues come
/// before P2/P3/P4 issues" — within each tier, the secondary sort is
/// created_at ASC then id ASC, but the fixtures all share created_at so id
/// tiebreak dominates and produces unstable expected positions.
#[test]
fn ready_sort_policy_hybrid() {
    let mut storage = test_db();

    let p3 = fixtures::IssueBuilder::new("Low priority")
        .with_priority(Priority::LOW)
        .build();
    let p0 = fixtures::IssueBuilder::new("Critical priority")
        .with_priority(Priority::CRITICAL)
        .build();
    let p2 = fixtures::IssueBuilder::new("Medium priority")
        .with_priority(Priority::MEDIUM)
        .build();
    let p1 = fixtures::IssueBuilder::new("High priority")
        .with_priority(Priority::HIGH)
        .build();

    storage.create_issue(&p3, "tester").unwrap();
    storage.create_issue(&p0, "tester").unwrap();
    storage.create_issue(&p2, "tester").unwrap();
    storage.create_issue(&p1, "tester").unwrap();

    let filters = ReadyFilters::default();
    let issues = ready_issues(&storage, &filters, ReadySortPolicy::Hybrid);

    assert_eq!(issues.len(), 4);
    assert_no_duplicate_ids(&issues);

    // Invariant: high-tier (P0/P1) comes before low-tier (P2/P3/P4)
    assert_hybrid_ordered(&issues);
}

// ============================================================================
// COMBINED FILTER TESTS
// ============================================================================

#[test]
fn ready_combined_assignee_and_type_filter() {
    let mut storage = test_db();

    let alice_bug = fixtures::IssueBuilder::new("Alice bug")
        .with_assignee("alice")
        .with_type(IssueType::Bug)
        .build();
    let alice_task = fixtures::IssueBuilder::new("Alice task")
        .with_assignee("alice")
        .with_type(IssueType::Task)
        .build();
    let bob_bug = fixtures::IssueBuilder::new("Bob bug")
        .with_assignee("bob")
        .with_type(IssueType::Bug)
        .build();

    storage.create_issue(&alice_bug, "tester").unwrap();
    storage.create_issue(&alice_task, "tester").unwrap();
    storage.create_issue(&bob_bug, "tester").unwrap();

    let filters = ReadyFilters {
        assignee: Some("alice".to_string()),
        types: Some(vec![IssueType::Bug]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&alice_bug.id));
}

#[test]
fn ready_combined_priority_and_label_filter() {
    let mut storage = test_db();

    let p0_backend = fixtures::IssueBuilder::new("Critical backend")
        .with_priority(Priority::CRITICAL)
        .build();
    let p0_frontend = fixtures::IssueBuilder::new("Critical frontend")
        .with_priority(Priority::CRITICAL)
        .build();
    let p2_backend = fixtures::IssueBuilder::new("Medium backend")
        .with_priority(Priority::MEDIUM)
        .build();

    storage.create_issue(&p0_backend, "tester").unwrap();
    storage.create_issue(&p0_frontend, "tester").unwrap();
    storage.create_issue(&p2_backend, "tester").unwrap();

    storage
        .add_label(&p0_backend.id, "backend", "tester")
        .unwrap();
    storage
        .add_label(&p0_frontend.id, "frontend", "tester")
        .unwrap();
    storage
        .add_label(&p2_backend.id, "backend", "tester")
        .unwrap();

    let filters = ReadyFilters {
        priorities: Some(vec![Priority::CRITICAL]),
        labels_and: vec!["backend".to_string()],
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 1);
    assert!(ids.contains(&p0_backend.id));
}

// ============================================================================
// BLOCKED ISSUE INTERACTION TESTS
// ============================================================================

#[test]
fn ready_excludes_blocked_issues_with_filters() {
    let mut storage = test_db();

    let blocker = fixtures::IssueBuilder::new("Blocker")
        .with_type(IssueType::Bug)
        .build();
    let blocked_issue = fixtures::IssueBuilder::new("Blocked bug")
        .with_type(IssueType::Bug)
        .build();
    let unblocked_bug = fixtures::IssueBuilder::new("Unblocked bug")
        .with_type(IssueType::Bug)
        .build();

    storage.create_issue(&blocker, "tester").unwrap();
    storage.create_issue(&blocked_issue, "tester").unwrap();
    storage.create_issue(&unblocked_bug, "tester").unwrap();

    // Add dependency - blocked depends on blocker
    storage
        .add_dependency(
            &blocked_issue.id,
            &blocker.id,
            DependencyType::Blocks.as_str(),
            "tester",
        )
        .unwrap();

    // Filter by type=bug - should still exclude blocked
    let filters = ReadyFilters {
        types: Some(vec![IssueType::Bug]),
        ..Default::default()
    };

    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);
    assert_eq!(ids.len(), 2);
    assert!(ids.contains(&blocker.id));
    assert!(ids.contains(&unblocked_bug.id));
    assert!(!ids.contains(&blocked_issue.id));
}

// ============================================================================
// STATUS INTERACTION TESTS
// ============================================================================

#[test]
fn ready_excludes_in_progress_and_includes_only_open() {
    let mut storage = test_db();

    let open = fixtures::IssueBuilder::new("Open issue")
        .with_status(Status::Open)
        .build();
    let in_progress = fixtures::IssueBuilder::new("In progress issue")
        .with_status(Status::InProgress)
        .build();
    let closed = fixtures::IssueBuilder::new("Closed issue")
        .with_status(Status::Closed)
        .build();
    let deferred = fixtures::IssueBuilder::new("Deferred issue")
        .with_status(Status::Deferred)
        .build();

    storage.create_issue(&open, "tester").unwrap();
    storage.create_issue(&in_progress, "tester").unwrap();
    storage.create_issue(&closed, "tester").unwrap();
    storage.create_issue(&deferred, "tester").unwrap();

    let filters = ReadyFilters::default();
    let ids = ready_ids(&storage, &filters, ReadySortPolicy::Oldest);

    assert!(ids.contains(&open.id));
    assert!(
        !ids.contains(&in_progress.id),
        "in_progress issues are already claimed and should not appear in ready"
    );
    assert!(!ids.contains(&closed.id));
    assert!(!ids.contains(&deferred.id));
}

#[test]
fn ready_include_deferred_flag() {
    let mut storage = test_db();

    // Create an issue that has open status but a future defer_until date
    // The ready query excludes issues where defer_until > now (unless include_deferred)
    let open_no_defer = fixtures::IssueBuilder::new("Open no defer")
        .with_status(Status::Open)
        .build();

    let mut open_with_defer = fixtures::IssueBuilder::new("Open with future defer")
        .with_status(Status::Open)
        .build();
    // Set defer_until to a future date
    open_with_defer.defer_until = Some(chrono::Utc::now() + chrono::Duration::days(30));

    storage.create_issue(&open_no_defer, "tester").unwrap();
    storage.create_issue(&open_with_defer, "tester").unwrap();

    // Without include_deferred - deferred should be excluded
    let filters_no_deferred = ReadyFilters {
        include_deferred: false,
        ..Default::default()
    };
    let ids = ready_ids(&storage, &filters_no_deferred, ReadySortPolicy::Oldest);
    assert!(ids.contains(&open_no_defer.id));
    // Open issue with future defer_until should be excluded
    assert!(!ids.contains(&open_with_defer.id));

    // With include_deferred - deferred should be included
    let filters_with_deferred = ReadyFilters {
        include_deferred: true,
        ..Default::default()
    };
    let ids = ready_ids(&storage, &filters_with_deferred, ReadySortPolicy::Oldest);
    assert!(ids.contains(&open_no_defer.id));
    assert!(ids.contains(&open_with_defer.id));
}