stax 0.50.2

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
//! Additional integration tests for code coverage
//! Targeting edge cases and less-tested code paths

mod common;

use common::{OutputAssertions, TestRepo};
use serde_json::Value;

// =============================================================================
// Downstack Command Tests
// =============================================================================

#[test]
fn test_downstack_get_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["downstack", "get"]);
    output.assert_success();
}

#[test]
fn test_downstack_get_with_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b", "c"]);

    let output = repo.run_stax(&["downstack", "get"]);
    output.assert_success();
}

// =============================================================================
// Upstack Command Tests
// =============================================================================

#[test]
fn test_upstack_restack_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["upstack", "restack"]);
    // Should handle gracefully on trunk
    let _ = output;
}

#[test]
fn test_upstack_restack_with_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);
    repo.run_stax(&["checkout", "a"]);

    let output = repo.run_stax(&["upstack", "restack"]);
    output.assert_success();
}

// =============================================================================
// Branch Create with Parent Tests
// =============================================================================

#[test]
fn test_branch_create_from_different_parent() {
    let repo = TestRepo::new();
    repo.create_stack(&["base"]);
    repo.run_stax(&["trunk"]);

    // Create another branch from trunk
    let output = repo.run_stax(&["bc", "sibling"]);
    output.assert_success();
}

#[test]
fn test_branch_create_deep_nesting() {
    let repo = TestRepo::new();

    for i in 0..5 {
        let name = format!("level-{}", i);
        let output = repo.run_stax(&["bc", &name]);
        output.assert_success();
    }

    // Verify stack depth
    let output = repo.run_stax(&["status"]);
    output.assert_success();
}

// =============================================================================
// Log Command Variations
// =============================================================================

#[test]
fn test_log_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["log"]);
    output.assert_success();
}

#[test]
fn test_log_alias_ll() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature"]);

    let output = repo.run_stax(&["ll"]);
    output.assert_success();
}

#[test]
fn test_log_with_empty_stack() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["log"]);
    output.assert_success();
}

// =============================================================================
// Trunk Command Tests
// =============================================================================

#[test]
fn test_trunk_alias_t() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature"]);

    let output = repo.run_stax(&["t"]);
    output.assert_success();

    // Verify we're on trunk
    let current = repo.current_branch();
    assert!(current == "main" || current == "master");
}

#[test]
fn test_trunk_when_on_trunk() {
    let repo = TestRepo::new();

    // Already on trunk
    let output = repo.run_stax(&["trunk"]);
    output.assert_success();
}

// =============================================================================
// Modify Command Tests
// =============================================================================

#[test]
fn test_modify_with_staged_changes() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature"]);

    // Create a change and stage it
    repo.create_file("new_file.txt", "content");
    repo.git(&["add", "new_file.txt"]);

    let output = repo.run_stax(&["modify"]);
    // Should amend the commit
    output.assert_success();
}

#[test]
fn test_modify_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["modify"]);
    // Should fail or warn on trunk
    let _ = output;
}

// =============================================================================
// Doctor Command Tests
// =============================================================================

#[test]
fn test_doctor_with_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b", "c"]);

    let output = repo.run_stax(&["doctor"]);
    output.assert_success();
}

#[test]
fn test_doctor_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["doctor"]);
    output.assert_success();
}

// =============================================================================
// Restack Command Tests
// =============================================================================

#[test]
fn test_restack_clean_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);

    let output = repo.run_stax(&["restack"]);
    output.assert_success();
}

#[test]
fn test_restack_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["restack"]);
    // Should handle trunk case
    let _ = output;
}

#[test]
fn test_restack_with_parent_changes() {
    let repo = TestRepo::new();
    repo.create_stack(&["parent", "child"]);

    // Go to parent and add a commit
    repo.run_stax(&["checkout", "parent"]);
    repo.create_file("parent_file.txt", "content");
    repo.commit("Parent change");

    // Go to child and restack
    repo.run_stax(&["checkout", "child"]);
    let output = repo.run_stax(&["restack"]);
    output.assert_success();
}

#[test]
fn test_restack_auto_stash_pop_restores_dirty_current_worktree() {
    let repo = TestRepo::new();
    let branches = repo.create_stack(&["a", "b", "c"]);

    // Move trunk forward so branches need restack
    repo.run_stax(&["t"]);
    repo.create_file("main-update.txt", "main update");
    repo.commit("Main update");

    // Go to tip branch, make dirty changes
    repo.run_stax(&["checkout", &branches[2]]);
    repo.create_file("dirty.txt", "committed content\n");
    repo.commit("Add dirty file");
    repo.create_file("dirty.txt", "committed content\nlocal change\n");

    // Restack with auto-stash -- dirty changes should be restored after
    let output = repo.run_stax(&["restack", "--auto-stash-pop"]);
    output.assert_success();

    let status = repo.git(&["status", "--porcelain"]);
    assert!(status.status.success());
    let stdout = TestRepo::stdout(&status);
    assert!(
        stdout.contains("dirty.txt"),
        "Expected dirty current worktree changes to be restored, got:\n{}",
        stdout
    );
}

#[test]
fn test_restack_stop_here_excludes_descendants() {
    let repo = TestRepo::new();
    let branches = repo.create_stack(&["stop-a", "stop-b", "stop-c"]);

    repo.run_stax(&["t"]).assert_success();
    repo.create_file("root-change.txt", "updated trunk content");
    repo.commit("Trunk change");

    repo.run_stax(&["checkout", &branches[1]]).assert_success();

    let output = repo.run_stax(&["restack", "--stop-here"]);
    output.assert_success();

    let status = repo.get_status_json();
    assert_eq!(
        branch_needs_restack(&status, &branches[0]),
        Some(false),
        "expected ancestor '{}' to be restacked",
        branches[0]
    );
    assert_eq!(
        branch_needs_restack(&status, &branches[1]),
        Some(false),
        "expected current branch '{}' to be restacked",
        branches[1]
    );
    assert_eq!(
        branch_needs_restack(&status, &branches[2]),
        Some(true),
        "expected descendant '{}' to remain needing restack",
        branches[2]
    );
}

// =============================================================================
// Status Command Tests
// =============================================================================

#[test]
fn test_status_on_untracked_branch() {
    let repo = TestRepo::new();

    // Create a git branch without stax tracking
    repo.git(&["checkout", "-b", "untracked"]);

    let output = repo.run_stax(&["status"]);
    // Should handle untracked branch
    let _ = output;
}

#[test]
fn test_status_long() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);

    // ll is the long status alias
    let output = repo.run_stax(&["ll"]);
    output.assert_success();
}

// =============================================================================
// Branch Track/Untrack Tests
// =============================================================================

fn branch_needs_restack(status: &Value, branch: &str) -> Option<bool> {
    status["branches"].as_array().and_then(|branches| {
        branches
            .iter()
            .find(|b| b["name"].as_str() == Some(branch))
            .and_then(|b| b["needs_restack"].as_bool())
    })
}

#[test]
fn test_branch_untrack() {
    let repo = TestRepo::new();
    repo.create_stack(&["tracked"]);
    let branch = repo.current_branch();

    // Ensure metadata exists before untrack.
    let metadata_ref = format!("refs/branch-metadata/{}", branch);
    let before = repo.git(&["show", &metadata_ref]);
    assert!(
        before.status.success(),
        "Expected metadata ref to exist before untrack"
    );

    let output = repo.run_stax(&["branch", "untrack", &branch]);
    output.assert_success();

    // Git branch should remain.
    assert!(repo.list_branches().contains(&branch));

    // Metadata should be removed.
    let after = repo.git(&["show", &metadata_ref]);
    assert!(
        !after.status.success(),
        "Expected metadata ref to be removed after untrack"
    );
}

// =============================================================================
// Checkout Edge Cases
// =============================================================================

#[test]
fn test_checkout_nonexistent_branch() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["checkout", "nonexistent"]);
    output.assert_failure();
}

#[test]
fn test_checkout_with_stack() {
    let repo = TestRepo::new();
    let branches = repo.create_stack(&["a", "b", "c"]);

    // Checkout middle branch using the actual branch name (may include configured prefix)
    let output = repo.run_stax(&["checkout", &branches[1]]);
    output.assert_success();

    assert_eq!(repo.current_branch(), branches[1]);
}

// =============================================================================
// Diff Command Tests
// =============================================================================

#[test]
fn test_diff_with_changes() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature"]);

    // Add some changes
    repo.create_file("new_file.txt", "content");
    repo.commit("Added file");

    let output = repo.run_stax(&["diff"]);
    output.assert_success();
}

#[test]
fn test_diff_empty_branch() {
    let repo = TestRepo::new();
    repo.create_stack(&["empty"]);

    // No additional commits on this branch
    let output = repo.run_stax(&["diff"]);
    output.assert_success();
}

// =============================================================================
// Config Command Tests
// =============================================================================

#[test]
fn test_config_shows_values() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["config"]);
    output.assert_success();
}

// =============================================================================
// Multiple Stacks Tests
// =============================================================================

#[test]
fn test_multiple_independent_stacks() {
    let repo = TestRepo::new();

    // Create first stack
    repo.run_stax(&["bc", "stack1-a"]);
    repo.create_file("s1.txt", "content");
    repo.commit("Stack 1 commit");

    // Return to trunk and create second stack
    repo.run_stax(&["trunk"]);
    repo.run_stax(&["bc", "stack2-a"]);
    repo.create_file("s2.txt", "content");
    repo.commit("Stack 2 commit");

    // Verify both stacks exist
    let output = repo.run_stax(&["status"]);
    output.assert_success();
}

// =============================================================================
// Navigation Edge Cases
// =============================================================================

#[test]
fn test_up_from_top() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);

    // Already at top
    let output = repo.run_stax(&["up"]);
    // Should handle gracefully
    output.assert_success();
}

#[test]
fn test_down_from_bottom() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);
    repo.run_stax(&["bottom"]);

    // Already at bottom (closest to trunk)
    let output = repo.run_stax(&["down"]);
    // Should handle gracefully
    let _ = output;
}

#[test]
fn test_up_with_count_exceeding_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);
    repo.run_stax(&["bottom"]);

    // Try to go up more than stack depth
    let output = repo.run_stax(&["up", "10"]);
    output.assert_success();
}

// =============================================================================
// Branch Squash Tests
// =============================================================================

#[test]
fn test_branch_squash_single_commit() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature"]);

    // Only one commit
    let output = repo.run_stax(&["branch", "squash"]);
    // Should succeed even with single commit
    let _ = output;
}

// =============================================================================
// Rename Command Tests
// =============================================================================

#[test]
fn test_rename_to_existing_name() {
    let repo = TestRepo::new();
    repo.create_stack(&["existing"]);
    repo.run_stax(&["trunk"]);
    repo.create_stack(&["to-rename"]);

    let output = repo.run_stax(&["rename", "existing"]);
    // Should fail - branch already exists
    output.assert_failure();
}

#[test]
fn test_rename_on_trunk() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["rename", "new-name"]);
    // Should fail - can't rename trunk
    output.assert_failure();
}

// =============================================================================
// Auth Command Tests
// =============================================================================

#[test]
fn test_auth_status() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["auth", "status"]);
    // Should show auth status
    let _ = output;
}

#[test]
fn test_auth_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["auth", "--help"]);
    output.assert_success();
}

// =============================================================================
// Continue Command Tests
// =============================================================================

#[test]
fn test_continue_no_rebase() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["continue"]);
    output.assert_success();
    output.assert_stdout_contains("No rebase");
}

// =============================================================================
// PR Command Tests
// =============================================================================

#[test]
fn test_pr_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["pr", "--help"]);
    output.assert_success();
}

// =============================================================================
// Submit Command Tests
// =============================================================================

#[test]
fn test_submit_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["submit", "--help"]);
    output.assert_success();
}

// =============================================================================
// Undo/Redo Command Tests
// =============================================================================

#[test]
fn test_undo_no_operations() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["undo"]);
    // Should fail gracefully - no operations to undo
    output.assert_failure();
}

#[test]
fn test_redo_no_operations() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["redo"]);
    // Should fail gracefully - no operations to redo
    output.assert_failure();
}

// =============================================================================
// Merge Command Tests
// =============================================================================

#[test]
fn test_merge_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["merge", "--help"]);
    output.assert_success();
}

// =============================================================================
// Range Diff Tests
// =============================================================================

#[test]
fn test_range_diff_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["range-diff", "--help"]);
    output.assert_success();
}

// =============================================================================
// Complex Stack Operations
// =============================================================================

#[test]
fn test_branch_fold_in_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b"]);

    // After create_stack we're on the leaf branch (b / cesar/b).
    // Fold it into its parent using --yes to skip the interactive confirmation
    // prompt (which requires a TTY that tests don't have).
    let output = repo.run_stax(&["branch", "fold", "--yes"]);
    output.assert_success();
}

#[test]
fn test_reparent_in_stack() {
    let repo = TestRepo::new();
    repo.create_stack(&["a", "b", "c"]);

    // Reparent c to have a as parent instead of b
    let output = repo.run_stax(&["branch", "reparent", "a"]);
    // Should work
    let _ = output;
}

// =============================================================================
// Sync Command Tests
// =============================================================================

#[test]
fn test_sync_help() {
    let repo = TestRepo::new();

    let output = repo.run_stax(&["sync", "--help"]);
    output.assert_success();
}