worktrunk 0.35.2

A CLI for Git worktree management, designed for parallel AI agent workflows
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
//! Snapshot tests for `wt list statusline` command.
//!
//! Tests the statusline output for shell prompts and Claude Code integration.

use crate::common::{TestRepo, repo, wt_command};
use insta::assert_snapshot;
use rstest::rstest;
use serde_json::Value;
use std::io::Write;
use std::process::Stdio;

/// Run statusline command with optional JSON piped to stdin
fn run_statusline_from_dir(
    repo: &TestRepo,
    args: &[&str],
    stdin_json: Option<&str>,
    cwd: &std::path::Path,
) -> String {
    let mut cmd = wt_command();
    cmd.current_dir(cwd);
    cmd.args(["list", "statusline"]);
    cmd.args(args);

    // Apply repo's git environment
    repo.configure_wt_cmd(&mut cmd);

    if stdin_json.is_some() {
        cmd.stdin(Stdio::piped());
    }
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());

    let mut child = cmd.spawn().expect("failed to spawn command");

    if let Some(json) = stdin_json {
        // Take ownership of stdin so we can drop it after writing
        let mut stdin = child.stdin.take().expect("failed to get stdin");
        stdin
            .write_all(json.as_bytes())
            .expect("failed to write stdin");
        // Explicitly close stdin by dropping it - this signals EOF to the child process.
        // On Windows, not closing stdin can cause the child to hang waiting for more input.
        drop(stdin);
    }

    let output = child.wait_with_output().expect("failed to wait for output");

    // Statusline outputs to stdout in interactive mode
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Return whichever has content (stdout for interactive)
    if !stdout.is_empty() {
        stdout.to_string()
    } else {
        stderr.to_string()
    }
}

fn run_statusline(repo: &TestRepo, args: &[&str], stdin_json: Option<&str>) -> String {
    run_statusline_from_dir(repo, args, stdin_json, repo.root_path())
}

// --- Test Setup Helpers ---

fn add_uncommitted_changes(repo: &TestRepo) {
    // Create uncommitted changes
    std::fs::write(repo.root_path().join("modified.txt"), "modified content").unwrap();
}

fn add_commits_ahead(repo: &mut TestRepo) {
    // Create feature branch with commits ahead
    let feature_path = repo.add_worktree("feature");

    // Add commits in the feature worktree
    std::fs::write(feature_path.join("feature.txt"), "feature content").unwrap();
    repo.git_command()
        .args(["add", "."])
        .current_dir(&feature_path)
        .run()
        .unwrap();
    repo.git_command()
        .args(["commit", "-m", "Feature commit 1"])
        .current_dir(&feature_path)
        .run()
        .unwrap();

    std::fs::write(feature_path.join("feature2.txt"), "more content").unwrap();
    repo.git_command()
        .args(["add", "."])
        .current_dir(&feature_path)
        .run()
        .unwrap();
    repo.git_command()
        .args(["commit", "-m", "Feature commit 2"])
        .current_dir(&feature_path)
        .run()
        .unwrap();
}

// --- Basic Tests ---

#[rstest]
fn test_statusline_basic(repo: TestRepo) {
    let output = run_statusline(&repo, &[], None);
    assert_snapshot!(output, @" main  ^|");
}

#[rstest]
fn test_statusline_with_changes(repo: TestRepo) {
    add_uncommitted_changes(&repo);
    let output = run_statusline(&repo, &[], None);
    assert_snapshot!(output, @" main  ?^|");
}

#[rstest]
fn test_statusline_commits_ahead(mut repo: TestRepo) {
    add_commits_ahead(&mut repo);
    // Run from the feature worktree to see commits ahead
    let feature_path = repo.worktree_path("feature");
    let output = run_statusline_from_dir(&repo, &[], None, feature_path);
    assert_snapshot!(output, @" feature  ↑  ↑2  ^+2");
}

// --- Claude Code Mode Tests ---

/// Create snapshot settings that normalize path output for statusline tests.
///
/// The statusline output varies by platform:
/// - Linux: Raw path is filtered by auto-bound settings to `_REPO_`
/// - macOS: Fish-style abbreviation (e.g., `/p/v/f/.../repo`) bypasses auto-bound filters
///
/// This function normalizes both cases to a consistent `[PATH]` placeholder.
fn claude_code_snapshot_settings() -> insta::Settings {
    let mut settings = insta::Settings::clone_current();
    // Normalize _REPO_ (from auto-bound filters on Linux) to [PATH]
    settings.add_filter(r"_REPO_", "[PATH]");
    // Normalize fish-abbreviated paths (on macOS) to [PATH]
    settings.add_filter(r"/[a-zA-Z0-9/._-]+/repo", "[PATH]");
    // Strip leading ANSI reset code if present (output starts with [0m)
    settings.add_filter(r"^\x1b\[0m ", "");
    settings
}

/// Escape a path for use in JSON strings.
/// On Windows, backslashes must be escaped as double backslashes.
fn escape_path_for_json(path: &std::path::Path) -> String {
    path.display().to_string().replace('\\', "\\\\")
}

#[rstest]
fn test_statusline_claude_code_full_context(repo: TestRepo) {
    add_uncommitted_changes(&repo);

    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "hook_event_name": "Status",
            "session_id": "test-session",
            "model": {{
                "id": "claude-opus-4-1",
                "display_name": "Opus"
            }},
            "workspace": {{
                "current_dir": "{escaped_path}",
                "project_dir": "{escaped_path}"
            }},
            "version": "1.0.80"
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ?^|  | Opus");
    });
}

#[rstest]
fn test_statusline_claude_code_minimal(repo: TestRepo) {
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(r#"{{"workspace": {{"current_dir": "{escaped_path}"}}}}"#,);

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ^|");
    });
}

#[rstest]
fn test_statusline_claude_code_with_model(repo: TestRepo) {
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Haiku"}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ^|  | Haiku");
    });
}

// --- Context Gauge Tests ---

#[rstest]
fn test_statusline_claude_code_with_context_gauge(repo: TestRepo) {
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Opus"}},
            "context_window": {{"used_percentage": 42}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ^|  | Opus  🌕 42%");
    });
}

#[rstest]
fn test_statusline_claude_code_context_gauge_low(repo: TestRepo) {
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Opus"}},
            "context_window": {{"used_percentage": 5}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ^|  | Opus  🌕 5%");
    });
}

#[rstest]
fn test_statusline_claude_code_context_gauge_high(repo: TestRepo) {
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Opus"}},
            "context_window": {{"used_percentage": 98}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        assert_snapshot!(output, @"[PATH]  main  ^|  | Opus  🌑 98%");
    });
}

#[rstest]
fn test_statusline_claude_code_missing_context_window(repo: TestRepo) {
    // When context_window is missing, no gauge should be displayed
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Opus"}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=claude-code"], Some(&json));
    claude_code_snapshot_settings().bind(|| {
        // No gauge symbol (○◔◑◕●) should appear
        assert!(
            !output.contains('')
                && !output.contains('')
                && !output.contains('')
                && !output.contains('')
                && !output.contains(''),
            "No gauge should appear when context_window is missing: {output}"
        );
        assert_snapshot!(output, @"[PATH]  main  ^|  | Opus");
    });
}

// --- Directive Mode Tests ---
// Note: With the new WORKTRUNK_DIRECTIVE_FILE architecture, data output (like statusline)
// still goes to stdout. The directive file is only used for shell directives like
// `cd '/path'`. So this test is no longer needed - statusline behavior is the same
// regardless of whether WORKTRUNK_DIRECTIVE_FILE is set.

// --- Branch Display Tests ---

///
/// Git updates worktree metadata (`branch` field in `git worktree list`) when
/// you checkout a different branch. This test verifies that statusline correctly
/// shows the new branch name after such a checkout.
#[rstest]
fn test_statusline_reflects_checked_out_branch(mut repo: TestRepo) {
    // Create a feature worktree
    let feature_path = repo.add_worktree("feature");

    // Verify statusline shows "feature" initially
    let output = run_statusline_from_dir(&repo, &[], None, &feature_path);
    assert!(
        output.contains("feature"),
        "statusline should show 'feature' for feature worktree, got: {output}"
    );

    // Create and checkout a different branch "other" in the feature worktree
    repo.git_command().args(["branch", "other"]).run().unwrap();
    let checkout_output = repo
        .git_command()
        .args(["checkout", "other"])
        .current_dir(&feature_path)
        .run()
        .unwrap();
    assert!(
        checkout_output.status.success(),
        "checkout should succeed: {}",
        String::from_utf8_lossy(&checkout_output.stderr)
    );

    // Verify statusline now shows "other"
    let output = run_statusline_from_dir(&repo, &[], None, &feature_path);
    assert!(
        output.contains("other"),
        "statusline should show 'other' after checkout, got: {output}"
    );
    assert!(
        !output.contains("feature"),
        "statusline should not show 'feature' after checkout, got: {output}"
    );
}

#[rstest]
fn test_statusline_detached_head(mut repo: TestRepo) {
    // Create a feature worktree
    let feature_path = repo.add_worktree("feature");

    // Detach HEAD
    repo.git_command()
        .args(["checkout", "--detach"])
        .current_dir(&feature_path)
        .run()
        .unwrap();

    // Verify statusline shows HEAD (not "feature")
    let output = run_statusline_from_dir(&repo, &[], None, &feature_path);
    // In detached state, we show "HEAD" as the branch name
    assert!(
        output.contains("HEAD") || !output.contains("feature"),
        "statusline should not show 'feature' in detached HEAD, got: {output}"
    );
}

// --- URL Display Tests ---

#[rstest]
fn test_statusline_with_url(repo: TestRepo) {
    // Configure URL template with simple branch variable (no hash_port for deterministic output)
    repo.write_project_config(
        r#"[list]
url = "http://{{ branch }}.localhost:3000"
"#,
    );

    let output = run_statusline(&repo, &[], None);
    // Shows `?` because writing project config creates uncommitted file
    assert_snapshot!(output, @" main  ?^|  http://main.localhost:3000");
}

#[rstest]
fn test_statusline_url_in_feature_worktree(mut repo: TestRepo) {
    // Configure URL template with simple branch variable
    repo.write_project_config(
        r#"[list]
url = "http://{{ branch }}.localhost:3000"
"#,
    );

    // Commit the project config so it's visible in worktrees
    repo.run_git(&["add", ".config/wt.toml"]);
    repo.run_git(&["commit", "-m", "Add project config"]);

    // Create feature worktree
    let feature_path = repo.add_worktree("feature");

    // Run statusline from feature worktree
    let output = run_statusline_from_dir(&repo, &[], None, &feature_path);
    assert_snapshot!(output, @" feature  _  http://feature.localhost:3000");
}

// --- JSON Format Tests ---

#[rstest]
fn test_statusline_json_basic(repo: TestRepo) {
    let output = run_statusline(&repo, &["--format=json"], None);
    let parsed: Value = serde_json::from_str(&output).expect("should be valid JSON");

    // Should be an array with one item
    let items = parsed.as_array().expect("should be an array");
    assert_eq!(
        items.len(),
        1,
        "should have exactly one item (current worktree)"
    );

    let item = &items[0];

    // Check essential fields
    assert_eq!(item["branch"], "main");
    assert_eq!(item["kind"], "worktree");
    assert!(item["is_current"].as_bool().unwrap());
    assert!(item["is_main"].as_bool().unwrap());

    // commit object should exist with sha
    assert!(item["commit"]["sha"].is_string());
    assert!(item["commit"]["short_sha"].is_string());
}

#[rstest]
fn test_statusline_json_with_changes(repo: TestRepo) {
    // Create uncommitted changes
    std::fs::write(repo.root_path().join("modified.txt"), "modified content").unwrap();

    let output = run_statusline(&repo, &["--format=json"], None);
    let parsed: Value = serde_json::from_str(&output).expect("should be valid JSON");

    let item = &parsed[0];
    assert_eq!(item["branch"], "main");

    // Should have working_tree status
    let working_tree = &item["working_tree"];
    assert!(
        working_tree["untracked"].as_bool().unwrap(),
        "should show untracked file"
    );
}

#[rstest]
fn test_statusline_json_feature_branch(mut repo: TestRepo) {
    // Create feature worktree with commits
    let feature_path = repo.add_worktree("feature");

    std::fs::write(feature_path.join("feature.txt"), "content").unwrap();
    repo.git_command()
        .args(["add", "."])
        .current_dir(&feature_path)
        .run()
        .unwrap();
    repo.git_command()
        .args(["commit", "-m", "Feature commit"])
        .current_dir(&feature_path)
        .run()
        .unwrap();

    let output = run_statusline_from_dir(&repo, &["--format=json"], None, &feature_path);
    let parsed: Value = serde_json::from_str(&output).expect("should be valid JSON");

    let item = &parsed[0];
    assert_eq!(item["branch"], "feature");
    assert!(item["is_current"].as_bool().unwrap());
    assert!(!item["is_main"].as_bool().unwrap());

    // Should have ahead/behind counts (commits ahead of main)
    assert!(
        item["main"]["ahead"].as_u64().unwrap() >= 1,
        "should be ahead of main"
    );
}

#[rstest]
fn test_statusline_json_ignores_claude_code(repo: TestRepo) {
    // When --format=json is used, --claude-code should be ignored
    let escaped_path = escape_path_for_json(repo.root_path());
    let json = format!(
        r#"{{
            "workspace": {{"current_dir": "{escaped_path}"}},
            "model": {{"display_name": "Opus"}}
        }}"#,
    );

    let output = run_statusline(&repo, &["--format=json", "--claude-code"], Some(&json));
    let parsed: Value = serde_json::from_str(&output).expect("should be valid JSON");

    // Should still produce JSON output (not statusline format)
    assert!(parsed.is_array(), "should produce JSON array output");
    let item = &parsed[0];
    assert_eq!(item["branch"], "main");
}

/// Tests that statusline correctly identifies nested worktrees.
///
/// When worktrees are placed inside other worktrees (e.g., `.worktrees/` layout),
/// the detection must use git rev-parse --show-toplevel rather than prefix matching,
/// which would incorrectly match the parent worktree.
///
/// Regression test for: prefix matching with starts_with would incorrectly identify
/// the main worktree when running from a nested worktree.
#[rstest]
fn test_statusline_nested_worktree(mut repo: TestRepo) {
    // Create a worktree nested inside the main repo (like .worktrees/ layout)
    let nested_path = repo.root_path().join(".worktrees").join("feature");
    let nested_worktree = repo.add_worktree_at_path("feature", &nested_path);

    // Run statusline from inside the nested worktree - should show "feature", not "main"
    let output = repo
        .wt_command()
        .current_dir(&nested_worktree)
        .args(["list", "statusline"])
        .output()
        .expect("statusline should succeed");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("feature"),
        "Nested worktree should show 'feature' branch, got: {stdout}"
    );
    assert!(
        !stdout.contains("main"),
        "Nested worktree should NOT show 'main' branch, got: {stdout}"
    );
}

/// Tests that JSON output correctly identifies nested worktrees.
#[rstest]
fn test_statusline_json_nested_worktree(mut repo: TestRepo) {
    // Create a worktree nested inside the main repo
    let nested_path = repo.root_path().join(".worktrees").join("feature");
    let nested_worktree = repo.add_worktree_at_path("feature", &nested_path);

    // Run statusline --format=json from inside the nested worktree
    let output = repo
        .wt_command()
        .current_dir(&nested_worktree)
        .args(["list", "statusline", "--format=json"])
        .output()
        .expect("statusline should succeed");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Value = serde_json::from_str(&stdout).expect("should be valid JSON");

    assert!(parsed.is_array(), "should produce JSON array");
    let items = parsed.as_array().unwrap();
    assert_eq!(items.len(), 1, "should have exactly one item");
    assert_eq!(
        items[0]["branch"], "feature",
        "Nested worktree should report 'feature' branch, not parent"
    );
}