pointbreak 0.5.0

Durable terminal code review for changes humans and coding agents collaborate on together
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
use std::process::Command;

use serde_json::Value;

#[allow(dead_code)]
#[path = "support/git_repo.rs"]
mod git_repo;

use git_repo::GitRepo;

#[test]
fn review_capture_creates_revision_from_subdir() {
    let repo = modified_repo();
    let subdir = repo.path().join("src");

    let output = shore(["capture", "--repo", subdir.to_str().unwrap()]);

    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json = parse_json(&output.stdout);
    assert_eq!(json["schema"], "shore.review-capture");
    assert_eq!(json["version"], 1);
    assert!(
        json["revision"]["id"]
            .as_str()
            .unwrap()
            .starts_with("rev:sha256:")
    );
    assert!(
        json["revision"]["revisionId"]
            .as_str()
            .unwrap()
            .starts_with("rev:")
    );
    assert!(
        json["revision"]["objectId"]
            .as_str()
            .unwrap()
            .starts_with("obj:")
    );
    assert_eq!(json["revision"]["base"]["kind"], "git_commit");
    assert_eq!(json["revision"]["target"]["kind"], "git_working_tree");
    assert!(
        json["revision"]["objectArtifactContentHash"]
            .as_str()
            .unwrap()
            .starts_with("sha256:")
    );
    assert!(json.get("statePath").is_none());
    assert!(json.get("snapshotArtifactPath").is_none());
    assert_eq!(json["eventsCreatedByType"]["work_object_proposed"], 1);
}

#[test]
fn review_capture_is_idempotent_for_unchanged_diff() {
    let repo = modified_repo();

    let first = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    let second = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);

    assert_eq!(first["revision"]["id"], second["revision"]["id"]);
    assert_eq!(second["eventsCreated"], 0);
    assert!(second["eventsExisting"].as_u64().unwrap() >= 1);
}

#[test]
fn review_capture_changes_when_untracked_content_changes() {
    let repo = modified_repo();

    let first = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    repo.write("untracked.txt", "new review content\n");
    let second = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);

    assert_ne!(first["revision"]["id"], second["revision"]["id"]);
}

#[test]
fn review_capture_writes_through_to_the_shared_store_without_a_link_step() {
    let repo = GitRepo::new();
    repo.write("src/lib.rs", "pub fn value() -> u32 { 1 }\n");
    repo.commit_all("base");

    repo.write("src/lib.rs", "pub fn value() -> u32 { 2 }\n");
    let capture = shore(["capture", "--repo", repo.path().to_str().unwrap()]);
    assert!(
        capture.status.success(),
        "capture stderr:\n{}",
        String::from_utf8_lossy(&capture.stderr)
    );
    let capture_stdout = String::from_utf8(capture.stdout).unwrap();
    let _capture_json = parse_json(capture_stdout.as_bytes());

    // No storage paths leak into the capture JSON.
    assert!(!capture_stdout.contains(".git"));
    assert!(!capture_stdout.contains(".shore/data"));

    // The capture landed in the shared common-dir store with no link step.
    assert!(
        common_dir_store(&repo).join("events").is_dir(),
        "the capture lands in the shared common-dir store"
    );

    // `store status` sees the captured event in place — eventCount reflects the
    // write-through capture, with the single-store view (no clone/family refs).
    let status = shore(["store", "status", "--repo", repo.path().to_str().unwrap()]);
    assert!(
        status.status.success(),
        "status stderr:\n{}",
        String::from_utf8_lossy(&status.stderr)
    );
    let status_json = parse_json(&status.stdout);
    assert_eq!(status_json["mode"], "local");
    assert_eq!(status_json["inventory"]["eventCount"], 2);
    assert!(status_json.get("cloneRef").is_none() || status_json["cloneRef"].is_null());
}

#[test]
fn capture_preserves_inline_rows_for_normal_added_file() {
    let repo = bounded_added_file_repo();
    let _capture = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);

    let snapshots_dir = common_dir_store(&repo).join("artifacts/objects");
    let artifact_path = std::fs::read_dir(&snapshots_dir)
        .expect("snapshots dir exists")
        .filter_map(|entry| entry.ok())
        .find(|entry| entry.path().extension().and_then(|s| s.to_str()) == Some("json"))
        .map(|entry| entry.path())
        .expect("at least one object artifact");
    let artifact: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&artifact_path).expect("read artifact"))
            .expect("artifact JSON parses");

    let files = artifact["snapshot"]["files"]
        .as_array()
        .expect("files array");
    let added = files
        .iter()
        .find(|f| f["new_path"].as_str() == Some("notes/added.txt"))
        .expect("captured added file present");
    let hunks = added["hunks"].as_array().expect("hunks array");

    // V1: every captured row stays inline in the artifact JSON; no elision.
    assert_eq!(hunks.len(), 1);
    let rows = hunks[0]["rows"].as_array().expect("rows array");
    assert_eq!(rows.len(), 50);
    let metadata = added["metadata_rows"]
        .as_array()
        .expect("metadata_rows array");
    assert!(metadata.is_empty());
}

#[test]
fn capture_with_base_captures_committed_range_on_clean_worktree() {
    let repo = committed_repo();
    let head_oid = rev(&repo, "HEAD");

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--base",
        "HEAD~1",
    ]);

    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json = parse_json(stdout.as_bytes());
    assert_eq!(json["revision"]["base"]["kind"], "git_commit");
    assert_eq!(json["revision"]["target"]["kind"], "git_commit");
    assert_eq!(json["revision"]["target"]["commitOid"], head_oid);
    assert!(
        !stdout.contains("worktreeRoot"),
        "range capture document must not carry a worktree path: {stdout}"
    );
}

#[test]
fn capture_with_base_and_target_pins_both_endpoints() {
    let repo = committed_repo();
    repo.write("src/lib.rs", "pub fn value() -> u32 { 3 }\n");
    repo.commit_all("third");
    let first_oid = rev(&repo, "HEAD~2");
    let second_oid = rev(&repo, "HEAD~1");
    let head_oid = rev(&repo, "HEAD");

    let json = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--base",
            &first_oid,
            "--target",
            "HEAD~1",
        ])
        .stdout,
    );

    assert_eq!(json["revision"]["base"]["commitOid"], first_oid);
    assert_eq!(json["revision"]["target"]["commitOid"], second_oid);
    assert_ne!(
        json["revision"]["target"]["commitOid"], head_oid,
        "target must not default to HEAD when --target is given"
    );
}

#[test]
fn capture_with_dirty_worktree_and_base_ignores_worktree_state() {
    let repo = committed_repo();
    repo.write("src/lib.rs", "pub fn value() -> u32 { 999 }\n");
    repo.write("untracked.txt", "untracked\n");

    let capture = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--base",
            "HEAD~1",
        ])
        .stdout,
    );
    let snapshot_id =
        pointbreak::model::ObjectId::new(capture["revision"]["objectId"].as_str().unwrap());

    let artifact = pointbreak::session::read_object_artifact(repo.path(), &snapshot_id)
        .expect("object artifact for the range capture");
    let paths: Vec<&str> = artifact
        .snapshot
        .files
        .iter()
        .filter_map(|file| file.new_path.as_deref())
        .collect();
    assert_eq!(paths, vec!["src/lib.rs"]);
    assert!(!paths.contains(&"untracked.txt"));
}

#[test]
fn capture_target_without_base_is_rejected() {
    let repo = committed_repo();

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--target",
        "HEAD",
    ]);

    assert!(!output.status.success());
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("--target requires --base"),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn capture_with_unresolvable_base_fails_honestly() {
    let repo = committed_repo();

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--base",
        "no-such-rev",
    ]);

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("no-such-rev"), "stderr:\n{stderr}");
    assert!(stderr.contains("commit"), "stderr:\n{stderr}");
}

#[test]
fn capture_with_non_commit_rev_fails_honestly() {
    let repo = committed_repo();

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--base",
        "HEAD:src/lib.rs",
    ]);

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("HEAD:src/lib.rs"), "stderr:\n{stderr}");
}

#[test]
fn capture_without_base_keeps_worktree_behavior() {
    let repo = modified_repo();
    shore(["capture", "--repo", repo.path().to_str().unwrap()]);

    let show =
        parse_json(&shore(["revision", "show", "--repo", repo.path().to_str().unwrap()]).stdout);

    assert_eq!(show["revision"]["source"]["kind"], "git_worktree");
    assert_eq!(show["revision"]["target"]["kind"], "git_working_tree");
}

#[test]
fn capture_accepts_supersedes_and_records_no_lineage_attach() {
    let repo = modified_repo();

    // The first revision (no predecessors).
    let first = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    let first_id = first["revision"]["id"].as_str().unwrap().to_owned();
    // A capture never attaches lineage now.
    assert!(first.get("lineageAttach").is_none());

    // A changed file yields a distinct revision that supersedes the first.
    repo.write("src/lib.rs", "pub fn value() -> u32 { 3 }\n");
    let second = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--supersedes",
            &first_id,
        ])
        .stdout,
    );
    let second_id = second["revision"]["id"].as_str().unwrap().to_owned();
    assert_ne!(first_id, second_id);
    assert!(second.get("lineageAttach").is_none());

    // The supersession is readable: --revision on the superseded id resolves to
    // the single current head (the second revision).
    let resolved = parse_json(
        &shore([
            "revision",
            "show",
            &first_id,
            "--repo",
            repo.path().to_str().unwrap(),
        ])
        .stdout,
    );
    assert_eq!(resolved["revision"]["id"], second_id.as_str());
}

#[test]
fn rebased_recapture_with_stable_object_id_writes_distinct_artifact() {
    let repo = GitRepo::new();
    let base = (1..=12)
        .map(|line| format!("preamble {line}\n"))
        .chain(["pub fn value() -> u32 { 1 }\n".to_owned()])
        .chain((1..=6).map(|line| format!("trailer {line}\n")))
        .collect::<String>();
    repo.write("src/lib.rs", &base);
    repo.commit_all("base");

    let reviewed = base.replace("pub fn value() -> u32 { 1 }", "pub fn value() -> u32 { 2 }");
    repo.write("src/lib.rs", reviewed);
    let first = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    let first_id = first["revision"]["id"].as_str().unwrap().to_owned();
    let first_object = first["revision"]["objectId"].as_str().unwrap().to_owned();
    let first_artifact_hash = first["revision"]["objectArtifactContentHash"]
        .as_str()
        .unwrap()
        .to_owned();

    repo.git(["checkout", "--", "src/lib.rs"]);
    let rebased_base = format!("inserted upstream line\n{base}");
    repo.write("src/lib.rs", &rebased_base);
    repo.commit_all("upstream insert");
    let rebased_reviewed =
        rebased_base.replace("pub fn value() -> u32 { 1 }", "pub fn value() -> u32 { 2 }");
    repo.write("src/lib.rs", rebased_reviewed);

    let recapture = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--supersedes",
        &first_id,
    ]);

    assert!(
        recapture.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&recapture.stderr)
    );
    let second = parse_json(&recapture.stdout);
    assert_eq!(second["revision"]["objectId"], first_object);
    assert_ne!(second["revision"]["id"], first["revision"]["id"]);
    assert_ne!(
        second["revision"]["objectArtifactContentHash"]
            .as_str()
            .unwrap(),
        first_artifact_hash
    );
}

#[test]
fn capture_with_base_twice_reports_existing_event() {
    let repo = committed_repo();

    let first = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--base",
            "HEAD~1",
        ])
        .stdout,
    );
    let second = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--base",
            "HEAD~1",
        ])
        .stdout,
    );

    assert_eq!(first["revision"]["id"], second["revision"]["id"]);
    assert_eq!(second["eventsCreated"], 0);
    // The capture event plus the auto-recorded HEAD-tipping ref association.
    assert_eq!(second["eventsExisting"], 2);
}

fn committed_repo() -> GitRepo {
    let repo = GitRepo::new();
    repo.write("src/lib.rs", "pub fn value() -> u32 { 1 }\n");
    repo.commit_all("base");
    repo.write("src/lib.rs", "pub fn value() -> u32 { 2 }\n");
    repo.commit_all("change");
    repo
}

fn rev(repo: &GitRepo, rev: &str) -> String {
    repo.git(["rev-parse", rev]).stdout.trim().to_owned()
}

/// The shared common-dir store a clone resolves by default
/// (`<git-common-dir>/shore`). Every non-ephemeral worktree reads and writes
/// here, so post-capture store assertions look here instead of the raw
/// worktree-local `.shore/data`.
fn common_dir_store(repo: &GitRepo) -> std::path::PathBuf {
    let common_dir = repo
        .git(["rev-parse", "--path-format=absolute", "--git-common-dir"])
        .stdout
        .trim()
        .to_owned();
    std::path::Path::new(&common_dir).join("shore")
}

#[test]
fn text_capture_ack_shows_short_revision_and_diffstat() {
    let repo = modified_repo();
    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--format",
        "text",
    ]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("captured"), "stdout:\n{stdout}");
    // short_ref form of the revision id, e.g. rev:1ace028b.
    assert!(stdout.contains("rev:"), "stdout:\n{stdout}");
    // modified_repo changes exactly one file.
    assert!(stdout.contains("1 file"), "stdout:\n{stdout}");
    // Bespoke rendering, not the JSON fallback.
    assert!(!stdout.contains("\"schema\""), "stdout:\n{stdout}");
    assert!(stdout.lines().count() <= 8, "stdout:\n{stdout}");
}

fn bounded_added_file_repo() -> GitRepo {
    let repo = GitRepo::new();
    repo.write("README.md", "base\n");
    repo.commit_all("base");
    let body = (1..=50).map(|n| format!("line {n}\n")).collect::<String>();
    repo.write("notes/added.txt", body);
    repo
}

fn shore<I, S>(args: I) -> std::process::Output
where
    I: IntoIterator<Item = S>,
    S: AsRef<std::ffi::OsStr>,
{
    Command::new(env!("CARGO_BIN_EXE_shore"))
        .args(args)
        .env_remove("SHORE_LOG")
        .env_remove("RUST_LOG")
        .output()
        .expect("run shore binary")
}

fn parse_json(stdout: &[u8]) -> Value {
    serde_json::from_slice(stdout).expect("stdout is valid JSON")
}

fn modified_repo() -> GitRepo {
    let repo = GitRepo::new();
    repo.write("src/lib.rs", "pub fn value() -> u32 { 1 }\n");
    repo.commit_all("base");
    repo.write("src/lib.rs", "pub fn value() -> u32 { 2 }\n");
    repo
}

fn two_dir_repo() -> GitRepo {
    let repo = GitRepo::new();
    repo.write("a/one.txt", "one\n");
    repo.write("b/two.txt", "two\n");
    repo.commit_all("base");
    repo.write("a/one.txt", "one changed\n");
    repo.write("b/two.txt", "two changed\n");
    repo
}

#[test]
fn capture_is_available_at_the_top_level() {
    let repo = modified_repo();

    let output = shore(["capture", "--repo", repo.path().to_str().unwrap()]);

    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let document = parse_json(&output.stdout);
    assert_eq!(document["schema"], "shore.review-capture"); // INV-1: schema tag is frozen
}

#[test]
fn abbreviated_supersedes_resolves_to_the_full_id_before_it_is_stored() {
    let repo = modified_repo();
    let first = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    let first_id = first["revision"]["id"].as_str().unwrap().to_owned();
    // first_id = "rev:sha256:<64hex>" (capture.rs's revision ids always take this shape).
    let fragment = &first_id["rev:sha256:".len()..][..8];

    repo.write("src/lib.rs", "pub fn value() -> u32 { 3 }\n");
    let second = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--supersedes",
            fragment,
        ])
        .stdout,
    );
    let second_id = second["revision"]["id"].as_str().unwrap().to_owned();

    let resolved = parse_json(
        &shore([
            "revision",
            "show",
            &first_id,
            "--repo",
            repo.path().to_str().unwrap(),
        ])
        .stdout,
    );
    assert_eq!(
        resolved["revision"]["id"], second_id,
        "if the raw fragment had been stored instead of the resolved full id, \
         `--revision {first_id}` would not resolve to the second revision"
    );
}

#[test]
fn review_capture_with_path_scopes_the_captured_revision() {
    let repo = two_dir_repo();

    let scoped = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--path",
        "a",
    ]);
    assert!(
        scoped.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&scoped.stderr)
    );
    let scoped = parse_json(&scoped.stdout);

    let unscoped = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);

    // A scoped capture is a distinct revision from the whole-repo capture of the
    // same worktree (their content and provenance both differ here).
    assert_ne!(scoped["revision"]["id"], unscoped["revision"]["id"]);
}

#[test]
fn review_capture_path_is_repeatable() {
    let repo = two_dir_repo();

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--path",
        "a",
        "--path",
        "b",
    ]);
    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn review_capture_with_path_matching_nothing_fails_with_a_pathspec_error() {
    let repo = two_dir_repo();

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--path",
        "docs",
    ]);

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("matched no changed files") && stderr.contains("docs"),
        "stderr:\n{stderr}"
    );
}

#[test]
fn scoped_capture_surfaces_its_pathspecs_in_show_revisions_and_history() {
    let repo = two_dir_repo();
    let captured = parse_json(
        &shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--path",
            "a",
        ])
        .stdout,
    );
    let revision_id = captured["revision"]["id"].as_str().unwrap();

    let shown = parse_json(
        &shore([
            "revision",
            "show",
            revision_id,
            "--repo",
            repo.path().to_str().unwrap(),
        ])
        .stdout,
    );
    assert_eq!(shown["revision"]["source"]["pathspecs"][0], "a");

    let listed =
        parse_json(&shore(["revision", "list", "--repo", repo.path().to_str().unwrap()]).stdout);
    let entry = listed["entries"]
        .as_array()
        .unwrap()
        .iter()
        .find(|entry| entry["revisionId"] == revision_id)
        .expect("scoped revision listed");
    assert_eq!(entry["source"]["pathspecs"][0], "a");

    let history = parse_json(
        &shore([
            "history",
            "--repo",
            repo.path().to_str().unwrap(),
            "--revision",
            revision_id,
        ])
        .stdout,
    );
    let capture_entry = history["entries"]
        .as_array()
        .unwrap()
        .iter()
        .find(|entry| entry["summary"]["kind"] == "revision_captured")
        .expect("capture entry in history");
    assert_eq!(capture_entry["summary"]["source"]["pathspecs"][0], "a");
}

#[test]
fn differently_scoped_range_captures_stay_distinct_in_revisions() {
    // Two path-scoped captures of the same range are different review units;
    // the list surface must show both, each with its own source.pathspecs,
    // instead of collapsing them into one row on the shared target OID.
    let repo = two_dir_repo();
    repo.commit_all("change");

    for scope in ["a", "b"] {
        let output = shore([
            "capture",
            "--repo",
            repo.path().to_str().unwrap(),
            "--base",
            "HEAD~1",
            "--target",
            "HEAD",
            "--path",
            scope,
        ]);
        assert!(
            output.status.success(),
            "stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    let listed =
        parse_json(&shore(["revision", "list", "--repo", repo.path().to_str().unwrap()]).stdout);
    assert_eq!(listed["revisionCount"], 2);
    let mut scopes: Vec<String> = listed["entries"]
        .as_array()
        .unwrap()
        .iter()
        .map(|entry| {
            entry["source"]["pathspecs"][0]
                .as_str()
                .expect("each scoped entry surfaces its own pathspecs")
                .to_owned()
        })
        .collect();
    scopes.sort();
    assert_eq!(scopes, vec!["a".to_owned(), "b".to_owned()]);
}

#[test]
fn unscoped_capture_surfaces_no_pathspecs_key() {
    let repo = two_dir_repo();
    let captured = parse_json(&shore(["capture", "--repo", repo.path().to_str().unwrap()]).stdout);
    let revision_id = captured["revision"]["id"].as_str().unwrap();

    let shown = parse_json(
        &shore([
            "revision",
            "show",
            revision_id,
            "--repo",
            repo.path().to_str().unwrap(),
        ])
        .stdout,
    );
    assert!(shown["revision"]["source"].get("pathspecs").is_none());
}

#[test]
fn review_capture_path_composes_with_base_and_target() {
    let repo = two_dir_repo();
    repo.commit_all("change");

    let output = shore([
        "capture",
        "--repo",
        repo.path().to_str().unwrap(),
        "--base",
        "HEAD~1",
        "--target",
        "HEAD",
        "--path",
        "a",
    ]);
    assert!(
        output.status.success(),
        "stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json = parse_json(&output.stdout);
    assert_eq!(json["revision"]["base"]["kind"], "git_commit");
    assert_eq!(json["revision"]["target"]["kind"], "git_commit");
}