worktrunk 0.43.0

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
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
//! Tests for `wt list` command with user config

use crate::common::{TestRepo, repo, setup_snapshot_settings, wt_command};
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
use std::fs;

#[rstest]
fn test_list_config_full_enabled(repo: TestRepo) {
    fs::write(
        repo.test_config_path(),
        r#"[list]
full = true
"#,
    )
    .unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_config_branches_enabled(repo: TestRepo) {
    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    fs::write(
        repo.test_config_path(),
        r#"[list]
branches = true
"#,
    )
    .unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_config_cli_override(repo: TestRepo) {
    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    fs::write(
        repo.test_config_path(),
        r#"[list]
branches = false
"#,
    )
    .unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // CLI flag --branches should override config
        cmd.arg("list")
            .arg("--branches")
            .current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_config_full_and_branches(repo: TestRepo) {
    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    fs::write(
        repo.test_config_path(),
        r#"[list]
full = true
branches = true
"#,
    )
    .unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_no_config(repo: TestRepo) {
    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    // No user config — verify defaults are used (branches not shown).

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_project_url_column(repo: TestRepo) {
    // Create project config with URL template
    repo.write_project_config(
        r#"[list]
url = "http://localhost:{{ branch | hash_port }}"
"#,
    );

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

#[rstest]
fn test_list_json_url_fields(repo: TestRepo) {
    // Create project config with URL template
    repo.write_project_config(
        r#"[list]
url = "http://localhost:{{ branch | hash_port }}"
"#,
    );

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.args(["list", "--format=json"])
        .current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse JSON and verify URL fields
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let items = json.as_array().unwrap();
    assert!(!items.is_empty());

    let first = &items[0];
    // URL should be present with hash_port result (port in 10000-19999 range)
    let url = first["url"].as_str().unwrap();
    assert!(url.starts_with("http://localhost:"));
    let port: u16 = url.split(':').next_back().unwrap().parse().unwrap();
    assert!((10000..=19999).contains(&port));

    // url_active is present but we can't test its value - depends on whether
    // something happens to be listening on the hashed port
    assert!(first["url_active"].is_boolean());
}

#[rstest]
fn test_list_json_no_url_without_template(repo: TestRepo) {
    // No project config means no URL template configured.

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.args(["list", "--format=json"])
        .current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse JSON and verify URL fields are null
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let items = json.as_array().unwrap();
    assert!(!items.is_empty());

    let first = &items[0];
    // URL should be null when no template configured
    assert!(first["url"].is_null());
    assert!(first["url_active"].is_null());
}

///
/// Only worktrees should have URLs - branches without worktrees can't have running dev servers.
#[rstest]
fn test_list_url_with_branches_flag(repo: TestRepo) {
    // Remove fixture worktrees and their branches to isolate test (keep only main worktree)
    for branch in &["feature-a", "feature-b", "feature-c"] {
        let worktree_path = repo
            .root_path()
            .parent()
            .unwrap()
            .join(format!("repo.{}", branch));
        if worktree_path.exists() {
            let _ = repo
                .git_command()
                .args([
                    "worktree",
                    "remove",
                    "--force",
                    worktree_path.to_str().unwrap(),
                ])
                .run();
        }
        // Delete the branch after removing the worktree
        let _ = repo.git_command().args(["branch", "-D", branch]).run();
    }

    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    // Create project config with URL template
    repo.write_project_config(
        r#"[list]
url = "http://localhost:{{ branch | hash_port }}"
"#,
    );

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.args(["list", "--branches", "--format=json"])
        .current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse JSON
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let items = json.as_array().unwrap();
    assert_eq!(items.len(), 2); // main worktree + feature branch

    // Worktree should have URL, branch should not (no dev server running for branches)
    let worktree = items.iter().find(|i| i["kind"] == "worktree").unwrap();
    let branch = items.iter().find(|i| i["kind"] == "branch").unwrap();

    assert!(
        worktree["url"]
            .as_str()
            .unwrap()
            .starts_with("http://localhost:"),
        "Worktree should have URL"
    );
    assert!(
        branch["url"].is_null(),
        "Branch without worktree should not have URL"
    );
    assert!(
        branch["url_active"].is_null(),
        "Branch without worktree should not have url_active"
    );
}

#[rstest]
fn test_list_url_with_branch_variable(repo: TestRepo) {
    // Create project config with {{ branch }} in URL
    repo.write_project_config(
        r#"[list]
url = "http://localhost:8080/{{ branch }}"
"#,
    );

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.args(["list", "--format=json"])
        .current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Parse JSON and verify URL contains branch name
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let items = json.as_array().unwrap();
    let first = &items[0];

    let url = first["url"].as_str().unwrap();
    assert_eq!(url, "http://localhost:8080/main");
}

/// Test that task-timeout-ms config option is parsed correctly.
/// We use a very short timeout (1ms) to trigger timeouts.
#[rstest]
fn test_list_config_timeout_triggers_timeouts(repo: TestRepo) {
    fs::write(
        repo.test_config_path(),
        r#"[list]
task-timeout-ms = 1
"#,
    )
    .unwrap();

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.arg("list").current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // With a 1ms timeout, some tasks should time out
    // The footer should show the timeout count
    assert!(
        stderr.contains("timed out") || output.status.success(),
        "Expected either timeout message in footer or success (if git was fast enough)"
    );
}

/// Test that task-timeout-ms = 0 explicitly disables timeout.
#[rstest]
fn test_list_config_timeout_zero_means_no_timeout(repo: TestRepo) {
    fs::write(
        repo.test_config_path(),
        r#"[list]
task-timeout-ms = 0
"#,
    )
    .unwrap();

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.arg("list").current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // With task-timeout-ms = 0, there should be no timeout
    assert!(
        !stderr.contains("timed out"),
        "Expected no timeout message with task-timeout-ms = 0, but got: {}",
        stderr
    );
}

/// Regression: setting a typed env-var override (e.g. `WORKTRUNK__LIST__TIMEOUT_MS`)
/// must not wipe unrelated fields in the same section.
///
/// Previously, the `config` crate's Environment source emitted values as strings,
/// so `timeout-ms: Option<u64>` failed to deserialize and the whole `UserConfig`
/// silently fell back to defaults — dropping `list.branches = true` and hiding
/// the `feature` branch from `wt list` output.
///
/// The snapshot captures both stdout (feature branch present with the
/// "1 branches" summary line) and the empty stderr (no silent fallback
/// warning) — if the fix regresses, the diff shows the missing branch.
#[rstest]
fn test_list_config_env_override_preserves_file_fields(repo: TestRepo) {
    // Create a branch without a worktree
    repo.run_git(&["branch", "feature"]);

    // Write to the test config path (the one `configure_wt_cmd` points
    // WORKTRUNK_CONFIG_PATH at); an XDG config under a temp HOME would be
    // ignored because WORKTRUNK_CONFIG_PATH takes precedence.
    fs::write(
        repo.test_config_path(),
        r#"[list]
branches = true
"#,
    )
    .unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // Typed env-var override that must coerce a string → u64. The bug was
        // at deserialize time, so any value reproduces it; 0 (disabled) is
        // chosen so the timeout doesn't affect output.
        cmd.env("WORKTRUNK__LIST__TIMEOUT_MS", "0");
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// When `UserConfig::load()` fails (e.g. user config has a wrong field type),
/// `Repository::user_config()` falls back to defaults but must surface the
/// error on stderr — a silent `log::warn!` would hide it from anyone not
/// running with `RUST_LOG=warn`.
///
/// The snapshot pins both the warning prefix (`▲`) and the exact wording so
/// an accidental downgrade back to `log::warn!` or a rewording is caught.
#[rstest]
fn test_list_config_malformed_config_warns_on_stderr(repo: TestRepo) {
    // `list.branches` is typed `Option<bool>`; a string here fails serde
    // deserialization and triggers the fallback path.
    fs::write(
        repo.test_config_path(),
        r#"[list]
branches = "not-a-bool"
"#,
    )
    .unwrap();

    let mut settings = setup_snapshot_settings(&repo);
    // `format_path_for_display` produces different strings depending on
    // whether HOME contains the tempdir — macOS tempdir lives under
    // /var/folders (absolute), Linux CI tempdir lives under HOME (tilde).
    // The default `~/…` → `_PARENT_/…` filter only fires in the tilde case,
    // so normalize the Linux form back to `[TEST_CONFIG]` for a stable
    // snapshot across platforms.
    settings.add_filter(r"_PARENT_/[^\s,]*test-config\.toml", "[TEST_CONFIG]");
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// When a WORKTRUNK_* env var override fails (e.g. a string value for a typed
/// field), the warning must blame env vars — not the config file — and list
/// the override vars currently set.
#[rstest]
fn test_list_config_env_override_bad_value_warns_on_stderr(repo: TestRepo) {
    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // `list.branches` is Option<bool>; "not-a-bool" can't coerce.
        cmd.env("WORKTRUNK__LIST__BRANCHES", "not-a-bool");
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// Numeric-looking env var values for String fields must not break config
/// loading. WORKTRUNK_WORKTREE_PATH=42 should be treated as the string "42",
/// not the integer 42 (which would fail to deserialize into Option<String>).
#[rstest]
fn test_list_config_env_override_numeric_string_field(repo: TestRepo) {
    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // worktree-path is Option<String>; "42" must round-trip as a string
        cmd.env("WORKTRUNK_WORKTREE_PATH", "42");
        cmd.arg("list").current_dir(repo.root_path());

        let output = cmd.output().unwrap();
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            !stderr.contains("Failed"),
            "numeric string should not fail: {stderr}"
        );
        assert!(output.status.success());
    });
}

/// Mixed typed+string env vars: one var needs typed (e.g., timeout-ms is u64,
/// "100" → Integer) and another needs string (e.g., worktree-path is String,
/// "42" → String). Both must resolve correctly without dropping the config.
#[rstest]
fn test_list_config_env_override_mixed_typed_and_string(repo: TestRepo) {
    // Write a config file so we can verify it's preserved
    fs::write(repo.test_config_path(), "[list]\nbranches = true\n").unwrap();
    repo.run_git(&["branch", "feature"]);

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // timeout-ms needs Integer(100) for u64 field
        cmd.env("WORKTRUNK__LIST__TIMEOUT_MS", "100");
        // worktree-path needs String("42") for Option<String> field
        cmd.env("WORKTRUNK_WORKTREE_PATH", "42");
        cmd.arg("list").current_dir(repo.root_path());

        let output = cmd.output().unwrap();
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            !stderr.contains("Failed"),
            "mixed typed+string env vars should not fail: {stderr}"
        );
        assert!(output.status.success(), "exit code should be 0: {stderr}");
        // Verify file config is preserved (branches = true shows the branch)
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(
            stdout.contains("feature"),
            "file config (branches=true) should be preserved: {stdout}"
        );
    });
}

/// Bad env var with valid file config: the file config must be preserved.
/// Before the load_with_warnings refactor, any env var failure would drop
/// the entire config (including file-based settings) to defaults.
#[rstest]
fn test_list_config_env_override_bad_value_preserves_file_config(repo: TestRepo) {
    // File config enables branch listing
    fs::write(repo.test_config_path(), "[list]\nbranches = true\n").unwrap();
    repo.run_git(&["branch", "feature"]);

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // Bad env var: "not-a-bool" for a bool field
        cmd.env("WORKTRUNK__LIST__BRANCHES", "not-a-bool");
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// Env var that deserializes successfully but fails validation (empty
/// worktree-path). Exercises the validation-after-env-overlay path.
#[rstest]
fn test_list_config_env_override_validation_failure(repo: TestRepo) {
    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        // Empty worktree-path deserializes as Some("") but fails validation
        cmd.env("WORKTRUNK_WORKTREE_PATH", "");
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// Bad values in non-section fields (projects, skip-*-prompt) must still be
/// attributed to the file, not to env vars.
#[rstest]
fn test_list_config_malformed_non_section_field_warns_on_stderr(repo: TestRepo) {
    fs::write(
        repo.test_config_path(),
        "skip-shell-integration-prompt = \"not-a-bool\"\n",
    )
    .unwrap();

    let mut settings = setup_snapshot_settings(&repo);
    settings.add_filter(r"_PARENT_/[^\s,]*test-config\.toml", "[TEST_CONFIG]");
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// Validation errors (e.g. empty worktree-path) are neither file parse
/// errors nor env-var errors — they fire after successful deserialization.
#[rstest]
fn test_list_config_validation_error_warns_on_stderr(repo: TestRepo) {
    fs::write(repo.test_config_path(), "worktree-path = \"\"\n").unwrap();

    let settings = setup_snapshot_settings(&repo);
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// System config with a section-field type error must be attributed to the file.
#[rstest]
fn test_list_config_malformed_system_config_warns_on_stderr(repo: TestRepo) {
    let system_config = repo.root_path().join("system-config.toml");
    fs::write(&system_config, "[list]\nbranches = \"not-a-bool\"\n").unwrap();

    let mut settings = setup_snapshot_settings(&repo);
    settings.add_filter(r"_REPO_/system-config\.toml", "[TEST_SYSTEM_CONFIG_FILE]");
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.env("WORKTRUNK_SYSTEM_CONFIG_PATH", &system_config);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// System config with a non-section field type error must be attributed to the file.
#[rstest]
fn test_list_config_malformed_system_config_non_section_field(repo: TestRepo) {
    let system_config = repo.root_path().join("system-config.toml");
    fs::write(
        &system_config,
        "skip-shell-integration-prompt = \"not-a-bool\"\n",
    )
    .unwrap();

    let mut settings = setup_snapshot_settings(&repo);
    settings.add_filter(r"_REPO_/system-config\.toml", "[TEST_SYSTEM_CONFIG_FILE]");
    settings.bind(|| {
        let mut cmd = wt_command();
        repo.configure_wt_cmd(&mut cmd);
        cmd.env("WORKTRUNK_SYSTEM_CONFIG_PATH", &system_config);
        cmd.arg("list").current_dir(repo.root_path());

        assert_cmd_snapshot!(cmd);
    });
}

/// Test that --full disables the task timeout.
#[rstest]
fn test_list_config_timeout_disabled_with_full(repo: TestRepo) {
    fs::write(
        repo.test_config_path(),
        r#"[list]
task-timeout-ms = 1
"#,
    )
    .unwrap();

    let mut cmd = wt_command();
    repo.configure_wt_cmd(&mut cmd);
    cmd.args(["list", "--full"]).current_dir(repo.root_path());

    let output = cmd.output().unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);

    // With --full, the timeout is disabled so we shouldn't see timeout messages
    // (though tasks may still fail for other reasons)
    assert!(
        !stderr.contains("timed out"),
        "Expected no timeout message with --full flag, but got: {}",
        stderr
    );
}