beads_rust 0.3.2

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
//! E2E tests for the `comments` command.
//!
//! Tests cover:
//! - Adding comments to issues
//! - Listing comments on issues
//! - JSON output validation
//! - Error cases (non-existent issues, empty comments)
//! - Edge cases (special characters, long comments, closed issues)

mod common;

use common::cli::{BrWorkspace, extract_json_payload, run_br};
use serde_json::Value;

fn parse_created_id(stdout: &str) -> String {
    let line = stdout.lines().next().unwrap_or("");
    // Handle both formats: "Created bd-xxx: title" and "✓ Created bd-xxx: title"
    let normalized = line.strip_prefix("✓ ").unwrap_or(line);
    let id_part = normalized
        .strip_prefix("Created ")
        .and_then(|rest| rest.split(':').next())
        .unwrap_or("");
    id_part.trim().to_string()
}

/// Test 1: Add single comment, verify in list
#[test]
fn e2e_comments_add_single_and_list() {
    let _log = common::test_log("e2e_comments_add_single_and_list");
    let workspace = BrWorkspace::new();

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

    // Create an issue
    let create = run_br(&workspace, ["create", "Test issue for comments"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);
    assert!(!id.is_empty(), "missing created id");

    // Add a comment
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "This is my first comment"],
        "add_comment",
    );
    assert!(add.status.success(), "add comment failed: {}", add.stderr);

    // List comments
    let list = run_br(&workspace, ["comments", "list", &id], "list_comments");
    assert!(
        list.status.success(),
        "list comments failed: {}",
        list.stderr
    );
    assert!(
        list.stdout.contains("This is my first comment"),
        "comment not found in list output"
    );
}

/// Test 2: Add multiple comments, verify order (newest last)
#[test]
fn e2e_comments_add_multiple_verify_order() {
    let _log = common::test_log("e2e_comments_add_multiple_verify_order");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Multiple comments test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add three comments
    let add1 = run_br(
        &workspace,
        ["comments", "add", &id, "First comment"],
        "add_comment1",
    );
    assert!(
        add1.status.success(),
        "add comment 1 failed: {}",
        add1.stderr
    );

    let add2 = run_br(
        &workspace,
        ["comments", "add", &id, "Second comment"],
        "add_comment2",
    );
    assert!(
        add2.status.success(),
        "add comment 2 failed: {}",
        add2.stderr
    );

    let add3 = run_br(
        &workspace,
        ["comments", "add", &id, "Third comment"],
        "add_comment3",
    );
    assert!(
        add3.status.success(),
        "add comment 3 failed: {}",
        add3.stderr
    );

    // List comments in JSON format to verify order
    let list = run_br(&workspace, ["comments", "list", &id, "--json"], "list_json");
    assert!(list.status.success(), "list json failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse comments json");

    assert_eq!(comments.len(), 3, "should have 3 comments");

    // Verify comments are in order (first, second, third)
    let texts: Vec<&str> = comments.iter().filter_map(|c| c["text"].as_str()).collect();
    assert_eq!(texts[0], "First comment");
    assert_eq!(texts[1], "Second comment");
    assert_eq!(texts[2], "Third comment");
}

/// Test 3: List comments with --json, validate structure
#[test]
fn e2e_comments_list_json_structure() {
    let _log = common::test_log("e2e_comments_list_json_structure");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "JSON structure test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add a comment with explicit author
    let add = run_br(
        &workspace,
        [
            "comments",
            "add",
            &id,
            "--author",
            "test-user",
            "JSON structure comment",
        ],
        "add_comment",
    );
    assert!(add.status.success(), "add comment failed: {}", add.stderr);

    // List in JSON format
    let list = run_br(&workspace, ["comments", "list", &id, "--json"], "list_json");
    assert!(list.status.success(), "list json failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse comments json");

    assert_eq!(comments.len(), 1, "should have 1 comment");
    let comment = &comments[0];

    // Validate structure
    assert!(
        comment["id"].is_number() || comment["id"].is_string(),
        "comment should have id"
    );
    assert_eq!(comment["text"], "JSON structure comment");
    assert_eq!(comment["author"], "test-user"); // invariant: hardcoded actor name, not an issue ID
    assert!(
        comment["created_at"].is_string(),
        "comment should have created_at"
    );
}

/// Test 4: Add comment to issue with existing comments
#[test]
fn e2e_comments_add_to_existing() {
    let _log = common::test_log("e2e_comments_add_to_existing");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Existing comments test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add first comment
    let add1 = run_br(
        &workspace,
        ["comments", "add", &id, "Existing comment"],
        "add_comment1",
    );
    assert!(
        add1.status.success(),
        "add comment 1 failed: {}",
        add1.stderr
    );

    // Verify one comment
    let list1 = run_br(&workspace, ["comments", "list", &id, "--json"], "list1");
    assert!(list1.status.success(), "list1 failed: {}", list1.stderr);
    let payload1 = extract_json_payload(&list1.stdout);
    let comments1: Vec<Value> = serde_json::from_str(&payload1).expect("parse json");
    assert_eq!(comments1.len(), 1, "should have 1 comment");

    // Add another comment
    let add2 = run_br(
        &workspace,
        ["comments", "add", &id, "New comment added"],
        "add_comment2",
    );
    assert!(
        add2.status.success(),
        "add comment 2 failed: {}",
        add2.stderr
    );

    // Verify two comments
    let list2 = run_br(&workspace, ["comments", "list", &id, "--json"], "list2");
    assert!(list2.status.success(), "list2 failed: {}", list2.stderr);
    let payload2 = extract_json_payload(&list2.stdout);
    let comments2: Vec<Value> = serde_json::from_str(&payload2).expect("parse json");
    assert_eq!(comments2.len(), 2, "should have 2 comments");
}

/// Test 5: Add comment to non-existent issue → error
#[test]
fn e2e_comments_add_nonexistent_issue() {
    let _log = common::test_log("e2e_comments_add_nonexistent_issue");
    let workspace = BrWorkspace::new();

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

    // Try to add comment to non-existent issue
    let add = run_br(
        &workspace,
        ["comments", "add", "bd-nonexistent", "This should fail"],
        "add_nonexistent",
    );
    assert!(
        !add.status.success(),
        "add comment to non-existent issue should fail"
    );
    assert!(
        add.stderr.contains("not found")
            || add.stderr.contains("Issue")
            || add.stderr.contains("error"),
        "error message should indicate issue not found: {}",
        add.stderr
    );
}

/// Test 6: Add empty comment → error or rejection
#[test]
fn e2e_comments_add_empty() {
    let _log = common::test_log("e2e_comments_add_empty");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Empty comment test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Try to add empty comment (no text arguments)
    let add = run_br(&workspace, ["comments", "add", &id], "add_empty");
    // This might either fail or succeed with empty - check behavior
    // Most implementations reject empty comments
    if add.status.success() {
        // If it succeeded, verify comment list
        let list = run_br(
            &workspace,
            ["comments", "list", &id, "--json"],
            "list_empty",
        );
        let payload = extract_json_payload(&list.stdout);
        let comments: Vec<Value> = serde_json::from_str(&payload).unwrap_or_default();
        // Either no comment was added, or an empty comment exists
        assert!(
            comments.is_empty()
                || comments
                    .iter()
                    .all(|c| c["text"].as_str().is_none_or(str::is_empty)),
            "empty comment handling"
        );
    } else {
        // Expected: error for empty comment
        assert!(
            add.stderr.contains("empty")
                || add.stderr.contains("required")
                || add.stderr.contains("text"),
            "error message should indicate empty comment rejected: {}",
            add.stderr
        );
    }
}

/// Test 7: List comments on issue with no comments → empty list
#[test]
fn e2e_comments_list_empty() {
    let _log = common::test_log("e2e_comments_list_empty");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "No comments issue"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // List comments on issue with no comments
    let list = run_br(
        &workspace,
        ["comments", "list", &id, "--json"],
        "list_empty",
    );
    assert!(
        list.status.success(),
        "list empty comments failed: {}",
        list.stderr
    );

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse json");
    assert!(comments.is_empty(), "should have 0 comments");
}

/// Test 8: Comment with special characters (quotes, newlines, unicode)
#[test]
fn e2e_comments_special_characters() {
    let _log = common::test_log("e2e_comments_special_characters");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Special chars test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add comment with special characters using --message flag for complex text
    let special_text = "Quote: \"hello\" and apostrophe's and emoji: 🚀";
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "--message", special_text],
        "add_special",
    );
    assert!(
        add.status.success(),
        "add special comment failed: {}",
        add.stderr
    );

    // Verify comment was stored correctly
    let list = run_br(
        &workspace,
        ["comments", "list", &id, "--json"],
        "list_special",
    );
    assert!(list.status.success(), "list failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse json");
    assert_eq!(comments.len(), 1, "should have 1 comment");

    let text = comments[0]["text"].as_str().expect("text field");
    assert!(text.contains("Quote:"), "should contain quote");
    assert!(text.contains("hello"), "should contain quoted text");
    assert!(
        text.contains("apostrophe") || text.contains('\''),
        "should contain apostrophe"
    );
}

/// Test 9: Very long comment (near limits)
#[test]
fn e2e_comments_long_text() {
    let _log = common::test_log("e2e_comments_long_text");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Long comment test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Create a long comment (10KB)
    let long_text = "x".repeat(10_000);
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "--message", &long_text],
        "add_long",
    );
    assert!(
        add.status.success(),
        "add long comment failed: {}",
        add.stderr
    );

    // Verify comment was stored
    let list = run_br(&workspace, ["comments", "list", &id, "--json"], "list_long");
    assert!(list.status.success(), "list failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse json");
    assert_eq!(comments.len(), 1, "should have 1 comment");

    let text = comments[0]["text"].as_str().expect("text field");
    assert_eq!(text.len(), 10_000, "comment should be 10KB");
}

/// Test 10: Comment on closed issue (should work)
#[test]
fn e2e_comments_on_closed_issue() {
    let _log = common::test_log("e2e_comments_on_closed_issue");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Closed issue test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Close the issue
    let close = run_br(
        &workspace,
        ["close", &id, "--reason", "Testing closed comments"],
        "close_issue",
    );
    assert!(close.status.success(), "close failed: {}", close.stderr);

    // Add comment to closed issue
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "Comment on closed issue"],
        "add_closed",
    );
    assert!(
        add.status.success(),
        "add comment to closed issue failed: {}",
        add.stderr
    );

    // Verify comment was added
    let list = run_br(
        &workspace,
        ["comments", "list", &id, "--json"],
        "list_closed",
    );
    assert!(list.status.success(), "list failed: {}", list.stderr);

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse json");
    assert_eq!(comments.len(), 1, "should have 1 comment");
    assert_eq!(comments[0]["text"], "Comment on closed issue");
}

/// Test: Comments add with --json output
#[test]
fn e2e_comments_add_json_output() {
    let _log = common::test_log("e2e_comments_add_json_output");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "JSON add test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add comment with --json output
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "--json", "JSON output comment"],
        "add_json",
    );
    assert!(add.status.success(), "add json failed: {}", add.stderr);

    // Verify JSON output
    let payload = extract_json_payload(&add.stdout);
    let result: Value = serde_json::from_str(&payload).expect("parse add json");

    // The result should contain information about the added comment
    assert!(
        result.is_object() || result.is_array(),
        "add --json should return structured output"
    );
}

/// Test: Comments shorthand (br comments <id> = br comments list <id>)
#[test]
fn e2e_comments_shorthand() {
    let _log = common::test_log("e2e_comments_shorthand");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Shorthand test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add a comment
    let add = run_br(
        &workspace,
        ["comments", "add", &id, "Shorthand comment"],
        "add_comment",
    );
    assert!(add.status.success(), "add comment failed: {}", add.stderr);

    // Use shorthand to list comments
    let list = run_br(&workspace, ["comments", &id], "list_shorthand");
    assert!(
        list.status.success(),
        "list shorthand failed: {}",
        list.stderr
    );
    assert!(
        list.stdout.contains("Shorthand comment"),
        "shorthand should list comments"
    );
}

/// Test: Comments are preserved in JSONL sync
#[test]
fn e2e_comments_sync_roundtrip() {
    let _log = common::test_log("e2e_comments_sync_roundtrip");
    let workspace = BrWorkspace::new();

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

    let create = run_br(&workspace, ["create", "Sync roundtrip test"], "create");
    assert!(create.status.success(), "create failed: {}", create.stderr);
    let id = parse_created_id(&create.stdout);

    // Add comments
    let add1 = run_br(
        &workspace,
        ["comments", "add", &id, "First sync comment"],
        "add_comment1",
    );
    assert!(
        add1.status.success(),
        "add comment 1 failed: {}",
        add1.stderr
    );

    let add2 = run_br(
        &workspace,
        ["comments", "add", &id, "Second sync comment"],
        "add_comment2",
    );
    assert!(
        add2.status.success(),
        "add comment 2 failed: {}",
        add2.stderr
    );

    // Export to JSONL
    let flush = run_br(&workspace, ["sync", "--flush-only"], "sync_flush");
    assert!(
        flush.status.success(),
        "sync flush failed: {}",
        flush.stderr
    );

    // Create a new workspace and import
    let workspace2 = BrWorkspace::new();
    let init2 = run_br(&workspace2, ["init"], "init2");
    assert!(init2.status.success(), "init2 failed: {}", init2.stderr);

    // Copy JSONL to new workspace
    let jsonl_src = workspace.root.join(".beads").join("issues.jsonl");
    let jsonl_dst = workspace2.root.join(".beads").join("issues.jsonl");
    std::fs::copy(&jsonl_src, &jsonl_dst).expect("copy jsonl");

    // Import
    let import = run_br(
        &workspace2,
        ["sync", "--import-only", "--force"],
        "sync_import",
    );
    assert!(import.status.success(), "import failed: {}", import.stderr);

    // Verify comments were imported
    let list = run_br(
        &workspace2,
        ["comments", "list", &id, "--json"],
        "list_after_import",
    );
    assert!(
        list.status.success(),
        "list after import failed: {}",
        list.stderr
    );

    let payload = extract_json_payload(&list.stdout);
    let comments: Vec<Value> = serde_json::from_str(&payload).expect("parse json");
    assert_eq!(comments.len(), 2, "should have 2 comments after import");

    let texts: Vec<&str> = comments.iter().filter_map(|c| c["text"].as_str()).collect();
    assert!(texts.contains(&"First sync comment"));
    assert!(texts.contains(&"Second sync comment"));
}