beads_rust 0.2.20

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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
mod common;
use common::cli::{BrWorkspace, extract_json_payload, parse_list_issues, run_br};
use serde_json::Value;
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.
    //
    // beads_rust-44rc rewrite (2026-05-09): originally pinned the
    // pretty-printed JSON format `"title": "First Issue"` (with a space
    // after `:`). After commit `f26bf73f fix(output): fail on stdout
    // serialization errors` and the streaming-perf migration in
    // `src/output/context.rs::json` (`serde_json::to_writer`, compact
    // format), the JSON has no whitespace between key and value. Switched
    // to semantic JSON parse + invariant checks so the test is robust to
    // format changes.
    let output = run_br(&workspace, ["list", "--json"], "list_json");
    assert!(output.status.success());

    let payload: Value = serde_json::from_str(output.stdout.trim())
        .expect("br list --json output must be valid JSON");
    let issues = payload
        .get("issues")
        .and_then(Value::as_array)
        .expect("expected `issues` array in br list --json output");

    let first = issues
        .iter()
        .find(|issue| issue.get("title").and_then(Value::as_str) == Some("First Issue"))
        .expect("expected an issue with title \"First Issue\" in br list --json output");

    let labels: Vec<&str> = first
        .get("labels")
        .and_then(Value::as_array)
        .map(|arr| arr.iter().filter_map(Value::as_str).collect())
        .unwrap_or_default();

    assert!(
        labels.contains(&"bug"),
        "expected `bug` label on First Issue; got labels={labels:?}"
    );
    assert!(
        labels.contains(&"frontend"),
        "expected `frontend` label on First Issue; got labels={labels:?}"
    );
}

#[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\""));
}

/// beads_rust#304: `### Agent Context` maps to the issue's `agent_context`
/// field and surfaces in `--json` output when set; issues without the
/// section omit the field.
#[test]
fn test_markdown_import_agent_context_json() {
    let workspace = BrWorkspace::new();

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

    let md_path = workspace.root.join("issues.md");
    let content = r#"## With Context
### Type
epic
### Agent Context
{"skills": ["porting-to-rust"], "constraints": ["no rusqlite; fsqlite only"]}

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

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_agent_ctx_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 array = json.as_array().expect("json array");
    assert_eq!(array.len(), 2);

    let with_ctx = array
        .iter()
        .find(|issue| issue.get("title").and_then(Value::as_str) == Some("With Context"))
        .expect("expected an issue titled \"With Context\"");
    let agent_context = with_ctx
        .get("agent_context")
        .and_then(Value::as_str)
        .expect("agent_context must be present in JSON when set");
    assert!(
        agent_context.contains("porting-to-rust"),
        "agent_context content not preserved: {agent_context}"
    );
    assert!(
        agent_context.contains("no rusqlite; fsqlite only"),
        "agent_context content not preserved: {agent_context}"
    );

    let without_ctx = array
        .iter()
        .find(|issue| issue.get("title").and_then(Value::as_str) == Some("Without Context"))
        .expect("expected an issue titled \"Without Context\"");
    assert!(
        without_ctx.get("agent_context").is_none(),
        "agent_context must be omitted from JSON when unset, got: {without_ctx}"
    );

    // Confirm persistence: `br show --json` echoes the stored agent_context.
    let with_id = with_ctx
        .get("id")
        .and_then(Value::as_str)
        .expect("created issue must have an id");
    let show = run_br(&workspace, ["show", with_id, "--json"], "show_agent_ctx");
    assert!(show.status.success(), "show failed: {}", show.stderr);
    assert!(
        show.stdout.contains("porting-to-rust"),
        "stored agent_context should round-trip through show --json: {}",
        show.stdout
    );
}

#[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")
    );
}

// `br create --dry-run --file <md>` validates the bulk import file and reports
// what would be created without persisting to storage or JSONL. (#300)
#[test]
fn test_markdown_import_dry_run_validates_without_persisting() {
    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

## Second DryRun Issue
### Type
bug
";
    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 with --file should succeed; stderr={}",
        output.stderr
    );
    assert!(
        output.stdout.contains("Dry run: would create 2 issues"),
        "expected dry-run summary in stdout, got: {}",
        output.stdout
    );
    assert!(output.stdout.contains("DryRun Issue"));
    assert!(output.stdout.contains("Second DryRun Issue"));

    // Storage side-effects: nothing should be persisted.
    let list = run_br(&workspace, ["list"], "list_after_dry_run");
    assert!(list.status.success());
    assert!(
        !list.stdout.contains("DryRun Issue"),
        "dry-run must not write issues to storage; list output: {}",
        list.stdout
    );
}

// Dry-run JSON output must still emit the would-create payload so callers can
// pipe it into validation tools — even though nothing is persisted. (#300)
#[test]
fn test_markdown_import_dry_run_emits_json() {
    let workspace = BrWorkspace::new();

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

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

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--dry-run", "--json"],
        "create_dry_run_json",
    );
    assert!(
        output.status.success(),
        "dry-run --json with --file should succeed; stderr={}",
        output.stderr
    );
    let payload = extract_json_payload(&output.stdout);
    let parsed: Value =
        serde_json::from_str(&payload).expect("dry-run --json must emit valid JSON");
    let issues = parsed
        .as_array()
        .or_else(|| parsed.get("issues").and_then(Value::as_array))
        .expect("expected an array of would-create issues");
    assert_eq!(issues.len(), 1, "expected one would-create issue");
    assert_eq!(
        issues[0].get("title").and_then(Value::as_str),
        Some("JsonDryRun Issue")
    );
}

#[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_unresolved_item_parent_skips_only_that_issue() {
    let workspace = BrWorkspace::new();

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

    let md_path = workspace.root.join("issues.md");
    let content = r"## Child with missing parent
### Parent
does-not-exist

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

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_unresolved_item_parent",
    );
    assert!(
        output.status.success(),
        "one bad item parent should not abort the import: {}",
        output.stderr
    );
    assert!(
        output
            .stderr
            .contains("Failed to resolve parent for Child with missing parent"),
        "stderr should explain skipped parent resolution: {}",
        output.stderr
    );

    let payload = extract_json_payload(&output.stdout);
    let issues: Vec<Value> = serde_json::from_str(&payload).expect("json parse");
    assert_eq!(issues.len(), 1);
    assert_eq!(issues[0]["title"].as_str(), Some("Independent import"));
}

#[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("Issue not found: invalid-type:bd-123"),
        "expected warning for missing issue id"
    );
}

#[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"
    );
}

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

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

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

## Dependent
### Type
feature
### Dependencies
- Shared Target

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

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_duplicate_title_dep_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );
    assert!(
        output
            .stderr
            .contains("ambiguous dependency 'Shared Target'"),
        "expected ambiguous dependency warning, got: {}",
        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(), 3);

    let dependent = issues
        .iter()
        .find(|issue| issue["title"].as_str() == Some("Dependent"))
        .expect("dependent issue");
    let dep_count = dependent
        .get("dependencies")
        .and_then(serde_json::Value::as_array)
        .map_or(0, Vec::len);
    assert_eq!(
        dep_count, 0,
        "ambiguous title dependency should be skipped, got: {dependent:?}"
    );
}

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

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

    let md_path = workspace.root.join("issues.md");
    let content = r"## First Target
### ID
target

## Second Target
### ID
target

## Dependent
### Dependencies
- target
";
    fs::write(&md_path, content).expect("write md");

    let output = run_br(
        &workspace,
        ["create", "--file", "issues.md", "--json"],
        "create_duplicate_standin_dep_json",
    );
    assert!(
        output.status.success(),
        "create --file --json failed: {}",
        output.stderr
    );
    assert!(
        output.stderr.contains("ambiguous dependency 'target'"),
        "expected ambiguous dependency warning, got: {}",
        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(), 3);

    let dependent = issues
        .iter()
        .find(|issue| issue["title"].as_str() == Some("Dependent"))
        .expect("dependent issue");
    let dep_count = dependent
        .get("dependencies")
        .and_then(serde_json::Value::as_array)
        .map_or(0, Vec::len);
    assert_eq!(
        dep_count, 0,
        "ambiguous stand-in dependency should be skipped, got: {dependent:?}"
    );
}