beads_rust 0.1.42

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
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
mod common;
use common::cli::{BrWorkspace, extract_json_payload, parse_list_issues, run_br};
use std::fs;

#[test]
fn test_markdown_import() {
    let workspace = BrWorkspace::new();

    // Initialize
    let output = run_br(&workspace, ["init"], "init");
    assert!(output.status.success(), "init failed");

    // Create markdown file
    let md_path = workspace.root.join("issues.md");
    // We use content_safe below. The logic validation of dependencies is commented out
    // because we can't easily refer to new issue IDs in markdown import without placeholders.

    let content_safe = r"## First Issue
### Priority
1
### Labels
bug, frontend

## Second Issue
Implicit description here.

### Type
feature
";

    fs::write(&md_path, content_safe).expect("write md");

    // Run create --file
    let output = run_br(&workspace, ["create", "--file", "issues.md"], "create_md");
    println!("stdout:\n{}", output.stdout);
    println!("stderr:\n{}", output.stderr);
    assert!(output.status.success(), "create --file failed");

    assert!(output.stdout.contains("✓ Created 2 issues from issues.md:"));
    assert!(
        output
            .stdout
            .lines()
            .any(|line| line.starts_with("  ") && line.contains(": First Issue")),
        "expected indented created-issue line in stdout: {}",
        output.stdout
    );

    // Verify list
    let output = run_br(&workspace, ["list"], "list");
    assert!(output.status.success());
    assert!(output.stdout.contains("First Issue"));
    assert!(output.stdout.contains("Second Issue"));
    assert!(output.stdout.contains("P1]")); // Priority 1 (format: [● P1])

    // Verify labels on First Issue using JSON output
    let output = run_br(&workspace, ["list", "--json"], "list_json");
    assert!(output.status.success());

    assert!(output.stdout.contains(r#""title": "First Issue"#));
    assert!(output.stdout.contains(r#""labels": ["#));
    assert!(output.stdout.contains(r#""bug"#));
    assert!(output.stdout.contains(r#""frontend"#));
}

#[test]
fn test_markdown_import_json_output() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_json");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## One
### Type
task

## Two
### Type
bug
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_json",
    );
    assert!(output.status.success(), "create --file --json failed");

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let array = json.as_array().expect("json array");
    assert_eq!(array.len(), 2);
    assert!(payload.contains("\"One\""));
    assert!(payload.contains("\"Two\""));
}

#[test]
fn test_markdown_import_updates_last_touched_context() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_last_touched_import");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## First imported
### Type
task

## Second imported
### Type
bug
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md"],
        "create_import_last_touched",
    );
    assert!(
        output.status.success(),
        "create --file failed: {}",
        output.stderr
    );

    let update = run_br(
        &workspace,
        ["update", "--status", "in_progress"],
        "update_after_import_last_touched",
    );
    assert!(update.status.success(), "update failed: {}", update.stderr);

    let list = run_br(
        &workspace,
        ["list", "--json"],
        "list_after_import_last_touched",
    );
    assert!(list.status.success(), "list failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    // Handle both bare array and paginated {"issues": [...]} formats
    let issues = if let Some(arr) = json.as_array() {
        arr.clone()
    } else {
        json["issues"]
            .as_array()
            .expect("json issues array")
            .clone()
    };
    let second = issues
        .iter()
        .find(|issue| issue["title"] == "Second imported")
        .expect("second imported issue");
    assert_eq!(second["status"], "in_progress");
}

#[test]
fn test_markdown_import_implicit_description_keeps_first_non_empty_line_only() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_implicit_description");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Implicit Description Issue
First line becomes description
This line should be ignored

### Type
task
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_implicit_description_json",
    );
    assert!(output.status.success(), "create --file --json failed");

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 1);
    assert_eq!(
        issues[0]["description"].as_str(),
        Some("First line becomes description")
    );
}

#[test]
fn test_markdown_import_rejects_dry_run() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_dry_run");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## DryRun Issue
### Type
task
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--dry-run"],
        "create_dry_run",
    );
    assert!(!output.status.success(), "dry-run should fail with --file");
    assert!(
        output
            .stderr
            .contains("--dry-run is not supported with --file")
    );
}

#[test]
fn test_markdown_import_rejects_title_argument() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_title_arg");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Bulk Issue
### Type
task
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "SingleTitle", "--file", "issues.md"],
        "create_title_arg",
    );
    assert!(
        !output.status.success(),
        "title argument should fail with --file"
    );
    assert!(
        output
            .stderr
            .contains("cannot be combined with title arguments")
    );
}

#[test]
fn test_markdown_import_parent_argument_sets_global_default() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_parent_arg");
    assert!(output.status.success(), "init failed");

    let parent = run_br(&workspace, ["create", "Parent issue"], "create_parent");
    assert!(
        parent.status.success(),
        "create parent failed: {}",
        parent.stderr
    );

    let parent_id = parent
        .stdout
        .lines()
        .next()
        .unwrap_or("")
        .strip_prefix("✓ Created ")
        .and_then(|rest| rest.split(':').next())
        .unwrap_or("")
        .trim()
        .to_string();
    assert!(!parent_id.is_empty(), "expected parent issue id");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Child from import
### Type
task
";
    fs::write(&md_path, content).expect("write md");

    // --parent with --file sets a global default parent for imported issues
    let output = run_br(
        &workspace,
        [
            "create",
            "--file",
            "issues.md",
            "--parent",
            &parent_id,
            "--json",
        ],
        "create_parent_arg",
    );
    assert!(
        output.status.success(),
        "--parent with --file should work: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 1);

    // Verify the imported issue has a parent-child dependency on the parent
    let deps = issues[0]["dependencies"]
        .as_array()
        .expect("dependencies array");
    assert!(
        deps.iter().any(|d| {
            d["depends_on_id"].as_str() == Some(parent_id.as_str())
                && d["type"].as_str() == Some("parent-child")
        }),
        "imported issue should have parent-child dep on {parent_id}, got: {deps:?}"
    );
}

#[test]
fn test_markdown_import_rejects_external_ref_argument() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_external_ref_arg");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Imported issue
### Type
task
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        [
            "create",
            "--file",
            "issues.md",
            "--external-ref",
            "JIRA-123",
        ],
        "create_external_ref_arg",
    );
    assert!(
        !output.status.success(),
        "--external-ref should fail with --file"
    );
    assert!(
        output
            .stderr
            .contains("--external-ref is not supported with --file")
    );
}

#[test]
fn test_markdown_import_rejects_non_empty_file_without_issue_headers() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_no_headers");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"### Description
This file has content but no issue headers.
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md"],
        "create_no_headers",
    );
    assert!(
        !output.status.success(),
        "non-empty file without issue headers should fail"
    );
    assert!(
        output
            .stderr
            .contains("no issues found; expected '## Title' headers")
    );
}

#[test]
fn test_markdown_import_dependency_bullets_do_not_create_marker_dependency() {
    let workspace = BrWorkspace::new();

    let init = run_br(&workspace, ["init"], "init_bullet_deps");
    assert!(init.status.success(), "init failed");

    let blocker = run_br(
        &workspace,
        ["create", "Blocker for markdown import", "--json"],
        "create_blocker_json",
    );
    assert!(
        blocker.status.success(),
        "create blocker failed: {}",
        blocker.stderr
    );
    let blocker_payload = extract_json_payload(&blocker.stdout);
    let blocker_json: serde_json::Value =
        serde_json::from_str(&blocker_payload).expect("blocker json");
    let blocker_id = blocker_json["id"].as_str().expect("blocker id").to_string();

    let md_path = workspace.root.join("issues.md");
    let content =
        format!("## Imported issue\n### Dependencies\n- {blocker_id}\n- [ ] external:github#123\n");
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_bullet_deps_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 1);

    let dependencies = issues[0]["dependencies"]
        .as_array()
        .expect("dependencies array");
    assert_eq!(dependencies.len(), 2);
    assert!(
        dependencies
            .iter()
            .any(|dep| dep["depends_on_id"].as_str() == Some(blocker_id.as_str()))
    );
    assert!(
        dependencies
            .iter()
            .any(|dep| dep["depends_on_id"].as_str() == Some("external:github#123"))
    );
    assert!(
        dependencies
            .iter()
            .all(|dep| dep["depends_on_id"].as_str() != Some("-"))
    );
}

#[test]
fn test_markdown_import_invalid_dependency_warns() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_invalid_dep");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Issue With Bad Dep
### Dependencies
invalid-type:bd-123
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md"],
        "create_bad_dep",
    );
    assert!(
        output.status.success(),
        "create should succeed with warnings"
    );
    assert!(
        output
            .stderr
            .contains("warning: skipping invalid dependency type"),
        "expected warning for invalid dependency type"
    );
}

#[test]
fn test_markdown_import_all_failed_returns_error() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_all_failed");
    assert!(output.status.success(), "init failed");

    let md_path = workspace.root.join("issues.md");
    let content = r"## Broken One
### Priority
999

## Broken Two
### Priority
999
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_all_failed",
    );
    assert!(
        !output.status.success(),
        "all-failed markdown import should return an error"
    );
    assert!(
        output.stderr.contains("failed to create any issues from"),
        "expected summary failure, got: {}",
        output.stderr
    );

    let list = run_br(
        &workspace,
        ["list", "--json"],
        "list_after_all_failed_import",
    );
    assert!(list.status.success(), "list failed: {}", list.stderr);
    let listed = parse_list_issues(&list.stdout);
    assert_eq!(listed.len(), 0);
}

#[test]
fn test_markdown_import_whitespace_separated_typed_dependencies() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_whitespace_typed_deps");
    assert!(output.status.success(), "init failed");

    let blocker = run_br(
        &workspace,
        ["create", "Whitespace dependency blocker", "--json"],
        "create_whitespace_dep_blocker_json",
    );
    assert!(
        blocker.status.success(),
        "create blocker failed: {}",
        blocker.stderr
    );
    let blocker_payload = extract_json_payload(&blocker.stdout);
    let blocker_json: serde_json::Value =
        serde_json::from_str(&blocker_payload).expect("blocker json");
    let blocker_id = blocker_json["id"].as_str().expect("blocker id").to_string();

    let md_path = workspace.root.join("issues.md");
    let content =
        format!("## Imported issue\n### Dependencies\nblocks: {blocker_id} external:github#123\n");
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_whitespace_typed_deps_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 1);

    let dependencies = issues[0]["dependencies"]
        .as_array()
        .expect("dependencies array");
    assert_eq!(dependencies.len(), 2);
    assert!(
        dependencies
            .iter()
            .any(|dep| dep["depends_on_id"].as_str() == Some(blocker_id.as_str()))
    );
    assert!(
        dependencies
            .iter()
            .any(|dep| dep["depends_on_id"].as_str() == Some("external:github#123"))
    );
}

#[test]
fn test_markdown_import_standin_id_dependency_resolution() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_standin");
    assert!(output.status.success(), "init failed");

    // Create a markdown file where issues reference each other via stand-in IDs
    let md_path = workspace.root.join("issues.md");
    let content = r"## Build Database Schema
### ID
db-1
### Type
task
### Priority
0

## Build API Endpoints
### Type
feature
### Dependencies
- db-1
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_standin_deps_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 2);

    // The second issue (API Endpoints) should depend on the first (Database Schema)
    let db_id = issues[0]["id"].as_str().expect("db issue id");
    let api_deps = issues[1]["dependencies"]
        .as_array()
        .expect("api dependencies array");
    assert_eq!(
        api_deps.len(),
        1,
        "expected 1 dependency, got {}: {api_deps:?}",
        api_deps.len()
    );
    assert_eq!(
        api_deps[0]["depends_on_id"].as_str(),
        Some(db_id),
        "dependency should resolve stand-in 'db-1' to the generated ID of Database Schema"
    );
}

#[test]
fn test_markdown_import_title_based_dependency_resolution() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_title_dep");
    assert!(output.status.success(), "init failed");

    // Create a markdown file where issues reference each other by title (bulleted)
    let md_path = workspace.root.join("issues.md");
    let content = r"## Build API Endpoints
### Type
feature
### Dependencies
- Build Database Schema

## Build Database Schema
### Type
task
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_title_deps_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 2);

    // The first issue (API Endpoints) should depend on the second (Database Schema)
    // This tests forward-reference resolution (dep target defined later in file)
    let db_id = issues[1]["id"].as_str().expect("db issue id");
    let api_deps = issues[0]["dependencies"]
        .as_array()
        .expect("api dependencies array");
    assert_eq!(
        api_deps.len(),
        1,
        "expected 1 dependency, got {}: {api_deps:?}",
        api_deps.len()
    );
    assert_eq!(
        api_deps[0]["depends_on_id"].as_str(),
        Some(db_id),
        "dependency should resolve title 'Build Database Schema' to the generated ID"
    );
}

#[test]
fn test_markdown_import_title_with_colon_dependency_resolution() {
    let workspace = BrWorkspace::new();

    let output = run_br(&workspace, ["init"], "init_colon_title");
    assert!(output.status.success(), "init failed");

    // Titles containing colons must not be misinterpreted as typed deps
    let md_path = workspace.root.join("issues.md");
    let content = r"## Step 1: Setup Database
### Type
task

## Step 2: Build API
### Type
feature
### Dependencies
- Step 1: Setup Database
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_colon_title_deps_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let json: serde_json::Value = serde_json::from_str(&payload).expect("json parse");
    let issues = json.as_array().expect("json array");
    assert_eq!(issues.len(), 2);

    let db_id = issues[0]["id"].as_str().expect("setup issue id");
    let api_deps = issues[1]["dependencies"]
        .as_array()
        .expect("api dependencies array");
    assert_eq!(
        api_deps.len(),
        1,
        "expected 1 dependency (colon in title should not break resolution), got {}: {api_deps:?}",
        api_deps.len()
    );
    assert_eq!(
        api_deps[0]["depends_on_id"].as_str(),
        Some(db_id),
        "dependency should resolve title with colon to the generated ID"
    );
}