stax 0.96.7

Fast stacked Git branches and PRs
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
use crate::common;

use common::{OutputAssertions, TestRepo};
use serde_json::Value;
use std::io::Write;
use std::process::{Command, Stdio};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Merge a branch into the current branch using git (fast-forward).
fn merge_branch(repo: &TestRepo, branch: &str) {
    let out = repo.git(&[
        "merge",
        "--no-ff",
        branch,
        "-m",
        &format!("Merge {}", branch),
    ]);
    assert!(
        out.status.success(),
        "Failed to merge branch {}: {}",
        branch,
        TestRepo::stderr(&out)
    );
}

fn write_branch_pr_metadata(repo: &TestRepo, branch: &str, parent_branch: &str, pr_number: u64) {
    write_branch_pr_metadata_with_state(repo, branch, parent_branch, pr_number, "OPEN");
}

fn write_branch_pr_metadata_with_state(
    repo: &TestRepo,
    branch: &str,
    parent_branch: &str,
    pr_number: u64,
    state: &str,
) {
    let metadata = serde_json::json!({
        "parentBranchName": parent_branch,
        "parentBranchRevision": repo.get_commit_sha(parent_branch),
        "prInfo": {
            "number": pr_number,
            "state": state
        }
    });

    let mut child = Command::new("git")
        .args(["hash-object", "-w", "--stdin"])
        .current_dir(repo.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("Failed to hash metadata blob");
    child
        .stdin
        .as_mut()
        .expect("metadata hash stdin")
        .write_all(metadata.to_string().as_bytes())
        .expect("Failed to write metadata JSON");
    let output = child.wait_with_output().expect("Failed to hash metadata");
    assert!(
        output.status.success(),
        "git hash-object failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let blob_hash = String::from_utf8(output.stdout)
        .expect("metadata hash UTF-8")
        .trim()
        .to_string();

    let update_ref = repo.git(&[
        "update-ref",
        &format!("refs/branch-metadata/{}", branch),
        &blob_hash,
    ]);
    assert!(
        update_ref.status.success(),
        "git update-ref failed: {}",
        TestRepo::stderr(&update_ref)
    );
}

// ---------------------------------------------------------------------------
// Phase 1 — read-only listing
// ---------------------------------------------------------------------------

#[test]
fn sweep_exits_zero_on_empty_branch_set() {
    // Just trunk + current (same branch) — nothing to classify.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
}

#[test]
fn sweep_classifies_merged_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Create a branch, add a commit, merge it into main
    repo.run_stax(&["bc", "feature-a"]).assert_success();
    repo.create_file("a.txt", "hello");
    repo.commit("add a");

    // Go back to main and merge
    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "feature-a");

    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    assert!(
        stdout.contains("merged"),
        "Expected 'merged' bucket in output:\n{}",
        stdout
    );
    assert!(
        stdout.contains("feature-a"),
        "Expected 'feature-a' in merged output:\n{}",
        stdout
    );
}

#[test]
fn sweep_does_not_list_trunk_or_current() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Create and immediately go back — current = main
    repo.run_stax(&["bc", "side"]).assert_success();
    repo.run_stax(&["t"]).assert_success();

    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    // main (trunk) should never appear as a candidate
    let lines_with_main: Vec<&str> = stdout
        .lines()
        .filter(|l| l.contains("main") && (l.contains("✓") || l.contains("⚑") || l.contains("○")))
        .collect();
    assert!(
        lines_with_main.is_empty(),
        "trunk 'main' should not appear as a deletable branch:\n{}",
        stdout
    );
}

#[test]
fn sweep_does_not_list_current_merged_branch_as_deletable() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "current-merged"]).assert_success();
    repo.create_file("current.txt", "current");
    repo.commit("current branch commit");

    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "current-merged");
    repo.git(&["checkout", "current-merged"]);

    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);

    let candidate_lines: Vec<&str> = stdout
        .lines()
        .filter(|l| {
            l.contains("current-merged") && (l.contains("✓") || l.contains("⚑") || l.contains("○"))
        })
        .collect();
    assert!(
        candidate_lines.is_empty(),
        "current branch must not appear as a sweep candidate:\n{}",
        stdout
    );
    assert!(
        !stdout.contains("delete 1 merged/gone branch"),
        "summary tip must not count the current branch as deletable:\n{}",
        stdout
    );
}

#[test]
fn sweep_json_does_not_list_current_merged_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "current-json-merged"])
        .assert_success();
    repo.create_file("current-json.txt", "current");
    repo.commit("current json branch commit");

    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "current-json-merged");
    repo.git(&["checkout", "current-json-merged"]);

    let out = repo.run_stax(&["sweep", "--json"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    let parsed: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("sweep --json should output valid JSON: {}\n{}", e, stdout));
    let branches = parsed["branches"]
        .as_array()
        .expect("JSON should have branches array");

    assert!(
        branches
            .iter()
            .all(|b| b["name"].as_str() != Some("current-json-merged")),
        "current branch must not appear in sweep JSON:\n{}",
        stdout
    );
}

#[test]
fn sweep_classifies_squash_merged_branch() {
    // A branch whose commits were applied to trunk via `git merge --squash`
    // (and then committed) is integrated even though its tip is not an ancestor
    // of trunk. sweep must classify it as merged, not active.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "feature-squashed"]).assert_success();
    repo.create_file("squashed.txt", "squashed contents");
    repo.commit("feature work to be squashed");

    // Squash-merge the feature into main, leaving the original branch in place.
    repo.run_stax(&["t"]).assert_success();
    let squash = repo.git(&["merge", "--squash", "feature-squashed"]);
    assert!(
        squash.status.success(),
        "git merge --squash failed: {}",
        TestRepo::stderr(&squash)
    );
    let commit = repo.git(&["commit", "-m", "Squash merge feature-squashed"]);
    assert!(
        commit.status.success(),
        "commit of squashed changes failed: {}",
        TestRepo::stderr(&commit)
    );

    let out = repo.run_stax(&["sweep", "--json"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    let parsed: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("sweep --json should output valid JSON: {}\n{}", e, stdout));
    let branch = parsed["branches"]
        .as_array()
        .expect("JSON should have branches array")
        .iter()
        .find(|b| b["name"].as_str() == Some("feature-squashed"))
        .expect("feature-squashed should appear in sweep JSON");
    assert_eq!(
        branch["status"].as_str(),
        Some("merged"),
        "squash-merged branch should be classified as merged, not active:\n{}",
        stdout
    );
}

#[test]
fn sweep_classifies_untracked_merged_branch() {
    // A branch created with plain git (not stax track) and then merged should
    // still be classified as merged by sweep.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Create an untracked branch with raw git
    repo.git(&["checkout", "-b", "untracked-merged"]);
    repo.create_file("untracked.txt", "x");
    repo.commit("untracked commit");

    // Merge into main
    repo.git(&["checkout", "main"]);
    merge_branch(&repo, "untracked-merged");

    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    assert!(
        stdout.contains("untracked-merged"),
        "Expected untracked merged branch in sweep output:\n{}",
        stdout
    );
    assert!(
        stdout.contains("merged"),
        "Expected 'merged' bucket:\n{}",
        stdout
    );
}

#[test]
fn sweep_classifies_stale_branch_with_custom_threshold() {
    // We can't fake old commit timestamps easily, but we can verify that a
    // branch with a very recent commit does NOT appear as stale with a 1-day
    // threshold (since it was just created).
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "fresh-branch"]).assert_success();
    repo.create_file("fresh.txt", "new");
    repo.commit("fresh commit");
    repo.run_stax(&["t"]).assert_success();

    let out = repo.run_stax(&["sweep", "--stale-days", "1"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);

    // A branch committed to right now must appear in the "active" bucket,
    // not in any "stale  (" bucket. We check specifically for the stale bucket
    // header rather than the word "stale" which also appears in the header line.
    assert!(
        !stdout.contains("stale  ("),
        "A just-created branch should not be in the stale bucket:\n{}",
        stdout
    );
    assert!(
        stdout.contains("active"),
        "fresh-branch should be in the active bucket:\n{}",
        stdout
    );
}

#[test]
fn sweep_shows_active_unmerged_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "work-in-progress"]).assert_success();
    repo.create_file("wip.txt", "wip");
    repo.commit("wip");
    repo.run_stax(&["t"]).assert_success();

    let out = repo.run_stax(&["sweep"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    assert!(
        stdout.contains("active"),
        "Expected 'active' bucket for unmerged branch:\n{}",
        stdout
    );
    assert!(
        stdout.contains("work-in-progress"),
        "Expected work-in-progress in active bucket:\n{}",
        stdout
    );
}

#[test]
fn sweep_does_not_treat_upstream_gone_with_local_work_as_deletable() {
    let repo = TestRepo::new_with_remote();
    repo.run_stax(&["init"]).assert_success();

    assert!(
        repo.git(&["checkout", "-b", "gone-with-local-work"])
            .status
            .success()
    );
    repo.create_file("pushed.txt", "pushed");
    repo.commit("pushed work");
    assert!(
        repo.git(&["push", "-u", "origin", "gone-with-local-work"])
            .status
            .success()
    );

    repo.create_file("local-only.txt", "local only");
    repo.commit("local-only work");

    assert!(repo.git(&["checkout", "main"]).status.success());
    assert!(
        repo.git(&["push", "origin", "--delete", "gone-with-local-work"])
            .status
            .success()
    );
    assert!(repo.git(&["fetch", "--prune", "origin"]).status.success());

    let out = repo.run_stax(&["sweep", "--json"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    let parsed: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("sweep --json should output valid JSON: {}\n{}", e, stdout));
    let branch = parsed["branches"]
        .as_array()
        .expect("JSON should have branches array")
        .iter()
        .find(|b| b["name"].as_str() == Some("gone-with-local-work"))
        .expect("gone-with-local-work should appear in sweep JSON");
    assert!(
        branch["status"].as_str() == Some("active"),
        "branch with local-only commits should be classified as active:\n{}",
        stdout
    );

    let delete_out = repo.run_stax(&["sweep", "--delete", "--force"]);
    delete_out.assert_success();

    let branches = repo.list_branches();
    assert!(
        branches.contains(&"gone-with-local-work".to_string()),
        "gone-with-local-work should survive sweep --delete --force:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_removes_gone_branch_without_unique_work() {
    let repo = TestRepo::new_with_remote();
    repo.run_stax(&["init"]).assert_success();

    assert!(
        repo.git(&["checkout", "-b", "gone-without-local-work"])
            .status
            .success()
    );
    assert!(
        repo.git(&["push", "-u", "origin", "gone-without-local-work"])
            .status
            .success()
    );

    assert!(repo.git(&["checkout", "main"]).status.success());
    assert!(
        repo.git(&["push", "origin", "--delete", "gone-without-local-work"])
            .status
            .success()
    );
    assert!(repo.git(&["fetch", "--prune", "origin"]).status.success());

    let out = repo.run_stax(&["sweep", "--json"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);
    let parsed: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("sweep --json should output valid JSON: {}\n{}", e, stdout));
    let branch = parsed["branches"]
        .as_array()
        .expect("JSON should have branches array")
        .iter()
        .find(|b| b["name"].as_str() == Some("gone-without-local-work"))
        .expect("gone-without-local-work should appear in sweep JSON");
    assert_eq!(
        branch["status"].as_str(),
        Some("merged"),
        "branch without unique commits should remain a safe deletion candidate"
    );

    let delete_out = repo.run_stax(&["sweep", "--delete", "--force"]);
    delete_out.assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.contains(&"gone-without-local-work".to_string()),
        "gone-without-local-work should be deleted:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_removes_tracked_pr_branch_when_upstream_was_deleted() {
    let repo = TestRepo::new_with_remote();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "squash-merged-pr"]).assert_success();
    repo.create_file("squash-pr.txt", "merged via forge");
    repo.commit("squash merged pr work");
    repo.git(&["push", "-u", "origin", "squash-merged-pr"]);
    write_branch_pr_metadata(&repo, "squash-merged-pr", "main", 42);

    repo.run_stax(&["t"]).assert_success();
    let remote_path = repo.remote_path().expect("remote repo path");
    let delete_remote = Command::new("git")
        .args([
            "--git-dir",
            remote_path.to_str().expect("remote path UTF-8"),
            "update-ref",
            "-d",
            "refs/heads/squash-merged-pr",
        ])
        .output()
        .expect("failed to delete branch directly from bare remote");
    assert!(
        delete_remote.status.success(),
        "failed to delete remote branch: {}",
        String::from_utf8_lossy(&delete_remote.stderr)
    );
    let stale_remote_ref = repo.git(&[
        "show-ref",
        "--verify",
        "refs/remotes/origin/squash-merged-pr",
    ]);
    assert!(
        stale_remote_ref.status.success(),
        "test setup should leave stale local remote-tracking ref before sweep"
    );

    let out = repo.run_stax(&["sweep", "--delete", "--force"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.contains(&"squash-merged-pr".to_string()),
        "sweep --delete --force should delete a tracked PR branch whose upstream was deleted:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_removes_branch_with_merged_pr_metadata() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "metadata-merged-pr"])
        .assert_success();
    repo.create_file("metadata-pr.txt", "merged according to forge metadata");
    repo.commit("metadata merged pr work");
    write_branch_pr_metadata_with_state(&repo, "metadata-merged-pr", "main", 43, "MERGED");

    repo.run_stax(&["t"]).assert_success();
    let out = repo.run_stax(&["sweep", "--delete", "--force"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.contains(&"metadata-merged-pr".to_string()),
        "sweep --delete --force should delete a branch whose PR metadata is MERGED:\n{:?}",
        branches
    );
}

// ---------------------------------------------------------------------------
// Phase 2 — opt-in deletion
// ---------------------------------------------------------------------------

#[test]
fn sweep_delete_removes_merged_branches() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "done-branch"]).assert_success();
    repo.create_file("done.txt", "done");
    repo.commit("done");
    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "done-branch");

    let out = repo.run_stax(&["sweep", "--delete", "--force"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.contains(&"done-branch".to_string()),
        "done-branch should have been deleted:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_requires_force_or_prompt() {
    // Without --force, the command should succeed (prompt) or require user input.
    // In tests we can't interact, but at minimum the dry-run (no --delete) must
    // not delete anything.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "keep-me"]).assert_success();
    repo.create_file("k.txt", "k");
    repo.commit("keep");
    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "keep-me");

    // sweep without --delete must not delete
    let out = repo.run_stax(&["sweep"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        branches.contains(&"keep-me".to_string()),
        "keep-me should still exist after read-only sweep:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_never_deletes_current_or_trunk() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // current = main, also the trunk
    let out = repo.run_stax(&["sweep", "--delete", "--force"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        branches.contains(&"main".to_string()),
        "trunk 'main' must never be deleted:\n{:?}",
        branches
    );
}

#[test]
fn sweep_delete_does_not_delete_stale_without_include_stale() {
    // A branch that is only stale (not merged or gone) must survive --delete
    // unless --include-stale is also given.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Create an untracked non-merged branch
    repo.git(&["checkout", "-b", "old-wip"]);
    repo.create_file("old.txt", "old");
    repo.commit("old work");
    repo.git(&["checkout", "main"]);

    // With a 0-day stale threshold everything is "stale", but without
    // --include-stale, --delete should NOT touch it.
    let out = repo.run_stax(&["sweep", "--delete", "--force", "--stale-days", "0"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        branches.contains(&"old-wip".to_string()),
        "old-wip should survive --delete without --include-stale:\n{:?}",
        branches
    );
}

#[test]
fn sweep_include_stale_requires_delete_flag() {
    // clap should reject --include-stale without --delete
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    let out = repo.run_stax(&["sweep", "--include-stale"]);
    assert!(
        !out.status.success(),
        "sweep --include-stale without --delete should fail:\n{}",
        TestRepo::stderr(&out)
    );
}

// ---------------------------------------------------------------------------
// Phase 3 — JSON output
// ---------------------------------------------------------------------------

#[test]
fn sweep_json_outputs_valid_json() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    repo.run_stax(&["bc", "branch-a"]).assert_success();
    repo.create_file("a.txt", "a");
    repo.commit("commit a");
    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "branch-a");

    repo.run_stax(&["bc", "branch-b"]).assert_success();
    repo.create_file("b.txt", "b");
    repo.commit("commit b");
    repo.run_stax(&["t"]).assert_success();

    let out = repo.run_stax(&["sweep", "--json"]);
    out.assert_success();
    let stdout = TestRepo::stdout(&out);

    let parsed: Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("sweep --json should output valid JSON: {}\n{}", e, stdout));

    let branches = parsed["branches"]
        .as_array()
        .expect("JSON should have branches array");

    let branch_a = branches
        .iter()
        .find(|b| b["name"].as_str() == Some("branch-a"))
        .expect("branch-a should appear in JSON");
    assert_eq!(
        branch_a["status"].as_str(),
        Some("merged"),
        "branch-a should be merged"
    );

    let branch_b = branches
        .iter()
        .find(|b| b["name"].as_str() == Some("branch-b"))
        .expect("branch-b should appear in JSON");
    assert_eq!(
        branch_b["status"].as_str(),
        Some("active"),
        "branch-b should be active"
    );
}

#[test]
fn sweep_json_conflicts_with_delete() {
    // --json and --delete are mutually exclusive
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    let out = repo.run_stax(&["sweep", "--json", "--delete"]);
    assert!(
        !out.status.success(),
        "sweep --json --delete should fail (conflicting flags):\n{}",
        TestRepo::stderr(&out)
    );
}

#[test]
fn sweep_reparents_tracked_children_on_delete() {
    // When a tracked branch is deleted via sweep, its stax-tracked child
    // should be reparented to trunk so `stax status` doesn't error out.
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Create parent branch (tracked)
    repo.run_stax(&["bc", "parent-merged"]).assert_success();
    repo.create_file("parent.txt", "p");
    repo.commit("parent commit");

    // Create child branch stacked on top (tracked)
    repo.run_stax(&["bc", "child-active"]).assert_success();
    repo.create_file("child.txt", "c");
    repo.commit("child commit");

    // Go back to main and merge the parent
    repo.run_stax(&["t"]).assert_success();
    merge_branch(&repo, "parent-merged");

    // sweep --delete should delete parent-merged and reparent child-active to main
    let out = repo.run_stax(&["sweep", "--delete", "--force"]);
    out.assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.contains(&"parent-merged".to_string()),
        "parent-merged should be deleted"
    );
    assert!(
        branches.contains(&"child-active".to_string()),
        "child-active should still exist"
    );

    // stax status should succeed (no orphan metadata errors)
    let status_out = repo.run_stax(&["status"]);
    status_out.assert_success();
}