heddle-cli 0.10.3

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
use super::*;

#[test]
fn native_capture_excludes_git_admin_storage_created_after_init() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    std::fs::write(temp.path().join("source.txt"), "one\n").unwrap();
    heddle(&["capture", "-m", "one"], Some(temp.path())).unwrap();

    init_git_repo(temp.path());
    std::fs::write(temp.path().join("source.txt"), "two\n").unwrap();
    heddle(&["capture", "-m", "two"], Some(temp.path())).unwrap();

    let patch = heddle(&["diff", "HEAD~1", "HEAD", "--patch"], Some(temp.path())).unwrap();
    assert!(patch.contains("source.txt"));
    assert!(
        !patch.contains(".git/"),
        "Git admin files leaked into Native history: {patch}"
    );

    let status = heddle(&["status", "--output", "json"], Some(temp.path())).unwrap();
    let status: Value = serde_json::from_str(&status).unwrap();
    assert_eq!(status["changed_path_count"], 0, "{status}");
}

#[test]
fn status_text_counts_dirty_worktree_paths() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    std::fs::write(temp.path().join("dirty.txt"), "pending").unwrap();

    let text = heddle(&["--output", "text", "status"], Some(temp.path())).unwrap();
    assert!(
        text.contains("Changed paths: 1"),
        "dirty worktree should not render as zero changed paths: {text}"
    );
    assert!(
        text.contains("Changes not yet saved") && text.contains("dirty.txt"),
        "status should still list the dirty file: {text}"
    );
    assert!(
        text.contains("Verdict: work in progress")
            && text.contains("Lifecycle: active")
            && text.contains("Work in progress")
            && !text.contains("Verdict: blocked")
            && !text.contains("Lifecycle: blocked")
            && !text.contains("Blocked by"),
        "ordinary dirty work should read like work in progress, not failure: {text}"
    );
    assert!(
        !text.contains("Tracked changes: 0"),
        "old contradictory label should not appear: {text}"
    );
}

#[test]
fn merged_thread_list_reads_integrated_not_actionable() {
    let temp = TempDir::new().unwrap();
    init_git_repo(temp.path());
    std::fs::write(temp.path().join("base.txt"), "base").unwrap();
    git_commit_all(temp.path(), "seed");
    heddle(&["adopt"], Some(temp.path())).unwrap();

    let started: Value = serde_json::from_str(
        &heddle(
            &[
                "--output",
                "json",
                "start",
                "feature/polish",
                "--workspace",
                "auto",
            ],
            Some(temp.path()),
        )
        .unwrap(),
    )
    .unwrap();
    let thread_path = std::path::PathBuf::from(started["execution_path"].as_str().unwrap());
    std::fs::write(thread_path.join("polish.txt"), "premium").unwrap();

    let landed: Value = serde_json::from_str(
        &heddle(
            &["--output", "json", "land", "--thread", "feature/polish"],
            Some(temp.path()),
        )
        .unwrap(),
    )
    .unwrap();
    assert_eq!(landed["status"], "landed");

    heddle(&["thread", "refresh", "feature/polish"], Some(temp.path())).unwrap();
    let listed: Value = serde_json::from_str(
        &heddle(&["thread", "list", "--output", "json"], Some(temp.path())).unwrap(),
    )
    .unwrap();
    let thread = listed["threads"]
        .as_array()
        .unwrap()
        .iter()
        .find(|thread| thread["name"] == "feature/polish")
        .expect("merged thread should be listed");
    assert_eq!(thread["thread_state"], "merged");
    assert_eq!(thread["coordination_status"], "clean");

    let text = heddle(&["--output", "text", "thread", "list"], Some(temp.path())).unwrap();
    let row = text
        .lines()
        .find(|line| line.contains("feature/polish"))
        .unwrap_or("");
    assert!(
        row.contains("clean") && !row.contains("ahead") && !row.contains("stale"),
        "merged thread row should not look actionable: {text}"
    );
    assert!(
        text.contains("lifecycle: merged"),
        "merged state should be visible: {text}"
    );
}

#[test]
fn thread_create_reports_ref_only_not_isolated_checkout() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();

    let created: Value = serde_json::from_str(
        &heddle(
            &["--output", "json", "thread", "create", "feature/ref-only"],
            Some(temp.path()),
        )
        .unwrap(),
    )
    .unwrap();
    let thread = &created["thread"];
    assert_eq!(thread["is_isolated"], false, "{created}");
    assert!(thread["path"].is_null(), "{created}");
    assert!(thread["execution_path"].is_null(), "{created}");
    assert_eq!(thread["visibility"], "ref_only", "{created}");

    let show = heddle(
        &["--output", "text", "thread", "show", "feature/ref-only"],
        Some(temp.path()),
    )
    .unwrap();
    assert!(
        show.contains("Checkout: no dedicated checkout") && !show.contains("Path:"),
        "ref-only thread should not be described as an isolated materialized checkout: {show}"
    );
}

#[test]
fn status_does_not_advertise_ready_thread_for_another_target() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    std::fs::write(temp.path().join("base.txt"), "base").unwrap();
    heddle(&["capture", "-m", "base"], Some(temp.path())).unwrap();

    let started: Value = serde_json::from_str(
        &heddle(
            &[
                "--output",
                "json",
                "start",
                "feature/ready-main",
                "--workspace",
                "materialized",
            ],
            Some(temp.path()),
        )
        .unwrap(),
    )
    .unwrap();
    let thread_path = std::path::PathBuf::from(started["execution_path"].as_str().unwrap());
    std::fs::write(thread_path.join("feature.txt"), "ready").unwrap();
    heddle(
        &["--output", "json", "ready", "-m", "ready for main"],
        Some(&thread_path),
    )
    .unwrap();

    heddle(&["thread", "create", "support/other"], Some(temp.path())).unwrap();
    heddle(&["thread", "switch", "support/other"], Some(temp.path())).unwrap();
    let status: Value =
        serde_json::from_str(&heddle(&["--output", "json", "status"], Some(temp.path())).unwrap())
            .unwrap();

    assert_ne!(
        status["recommended_action"], "heddle land --thread feature/ready-main",
        "status on a non-target thread must not suggest merging target-main work into the active thread: {status}"
    );
    assert_eq!(
        status["verification"]["workflow_status"], "clean",
        "{status}"
    );
    assert!(
        status["verification"]["workflow_summary"]
            .as_str()
            .is_some_and(|summary| summary.contains("target another thread")),
        "workflow summary should explain the scoped ready thread: {status}"
    );
}

#[test]
fn human_thread_and_status_output_use_polished_labels() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    heddle(
        &["start", "feature/visible", "--workspace", "materialized"],
        Some(temp.path()),
    )
    .unwrap();

    let status = heddle(&["--output", "text", "status"], Some(temp.path())).unwrap();
    let show = heddle(
        &["--output", "text", "thread", "show", "feature/visible"],
        Some(temp.path()),
    )
    .unwrap();
    let list = heddle(&["--output", "text", "thread", "list"], Some(temp.path())).unwrap();
    let doctor = heddle(&["--output", "text", "doctor"], Some(temp.path())).unwrap();
    let captures = heddle(
        &["--output", "text", "thread", "captures", "feature/visible"],
        Some(temp.path()),
    )
    .unwrap();
    let combined = format!("{status}\n{show}\n{list}\n{doctor}\n{captures}");
    for leaked in [
        " materialized",
        "[materialized",
        "thread mode:",
        "Mode: materialized",
        "Last 5 captures",
        "Captures on",
        "No captures recorded",
        "thread_state",
        "Freshness: unknown",
        "freshness: unknown",
        "tip only",
        "history imported",
    ] {
        assert!(
            !combined.contains(leaked),
            "human output should not leak {leaked:?}: {combined}"
        );
    }

    let json: Value = serde_json::from_str(
        &heddle(
            &["--output", "json", "thread", "show", "feature/visible"],
            Some(temp.path()),
        )
        .unwrap(),
    )
    .unwrap();
    // `--workspace visible` (alias of heavy) without `--path` resolves
    // to ThreadMode::Materialized: a real on-disk checkout managed by
    // Heddle. Materialized is reserved for explicit `--path` callers.
    assert_eq!(json["thread_mode"], "materialized");
    assert_eq!(json["freshness"], "current");
}

#[test]
fn status_output_modes_are_explicit_under_non_tty_capture() {
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();

    // Default is text — no surprise JSON on pipes/subprocesses/`| less`.
    let default = heddle(&["status"], Some(temp.path())).unwrap();
    assert!(
        default.contains("Heddle status") && serde_json::from_str::<Value>(&default).is_err(),
        "default status should be text, not JSON: {default}"
    );

    let json = heddle(&["--output", "json", "status"], Some(temp.path())).unwrap();
    serde_json::from_str::<Value>(&json)
        .unwrap_or_else(|err| panic!("--output json status should be JSON: {err}: {json}"));

    let text = heddle(&["--output", "text", "status"], Some(temp.path())).unwrap();
    assert!(
        text.contains("Heddle status") && serde_json::from_str::<Value>(&text).is_err(),
        "--output text should match the default: {text}"
    );
}

fn init_git_repo(path: &std::path::Path) {
    std::process::Command::new("git")
        .args(["init"])
        .current_dir(path)
        .output()
        .expect("git init should run");
    std::process::Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(path)
        .output()
        .expect("git config email should run");
    std::process::Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(path)
        .output()
        .expect("git config name should run");
}

#[test]
fn status_long_default_shows_combined_verdict_not_component_axes() {
    // (a) Both axes clean → default long mode shows ONE combined
    // verdict and hides the per-axis Health/Coordination lines.
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();

    let text = heddle(&["--output", "text", "status"], Some(temp.path())).unwrap();
    assert!(
        text.contains("Verdict: clean"),
        "default long mode should show a combined verdict: {text}"
    );
    assert!(
        !text.contains("Health:") && !text.contains("Coordination:"),
        "default long mode must hide the per-axis component lines: {text}"
    );
}

#[test]
fn status_long_default_verdict_signals_non_clean_when_health_dirty() {
    // (a) Non-clean axis → the combined verdict must still surface the
    // problem so a default-view reader learns the checkout isn't clean,
    // without the per-axis lines being printed.
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    std::fs::write(temp.path().join("dirty.txt"), "pending").unwrap();

    let text = heddle(&["--output", "text", "status"], Some(temp.path())).unwrap();
    assert!(
        text.contains("Verdict: work in progress"),
        "a dirty worktree must read as a non-clean combined verdict: {text}"
    );
    assert!(
        !text.contains("Verdict: clean"),
        "combined verdict must not claim clean when an axis is dirty: {text}"
    );
    assert!(
        !text.contains("Health:") && !text.contains("Coordination:"),
        "default long mode must hide the per-axis component lines: {text}"
    );
    // The dirty health blocker is encoded as `coordination_status =
    // Blocked`; the combined verdict reason must not double-count it as a
    // coordination failure (heddle#276 r2 / cid 3327903846).
    assert!(
        !text.contains("coordination") && !text.contains("both need attention"),
        "ordinary dirty WIP must not surface a coordination warning: {text}"
    );
    assert!(
        text.contains("checkout health needs attention"),
        "the reason for dirty WIP must be the health/WIP reason alone: {text}"
    );
}

#[test]
fn status_long_verbose_shows_combined_verdict_and_both_axes() {
    // (b) `-v` long mode → combined verdict PLUS both component lines.
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();
    std::fs::write(temp.path().join("dirty.txt"), "pending").unwrap();

    let text = heddle(&["--output", "text", "-v", "status"], Some(temp.path())).unwrap();
    assert!(
        text.contains("Verdict: work in progress")
            && text.contains("Health: work in progress")
            && text.contains("Coordination: work in progress"),
        "verbose long mode should show the verdict and both component axes: {text}"
    );
}

#[test]
fn status_json_always_emits_both_component_fields() {
    // (c) JSON always carries BOTH component fields, schema unchanged,
    // for clean and non-clean cases alike.
    let temp = TempDir::new().unwrap();
    heddle(&["init"], Some(temp.path())).unwrap();

    let clean = heddle(&["status", "--output", "json"], Some(temp.path())).unwrap();
    let clean: Value = serde_json::from_str(&clean).unwrap();
    assert_eq!(clean["thread_health"], "clean");
    assert_eq!(clean["coordination_status"], "clean");

    std::fs::write(temp.path().join("dirty.txt"), "pending").unwrap();
    let dirty = heddle(&["status", "--output", "json"], Some(temp.path())).unwrap();
    let dirty: Value = serde_json::from_str(&dirty).unwrap();
    assert!(
        dirty["thread_health"].is_string() && dirty["coordination_status"].is_string(),
        "non-clean JSON must still carry both component fields: {dirty}"
    );
    assert_ne!(
        dirty["thread_health"], "clean",
        "dirty worktree should report a non-clean health in JSON: {dirty}"
    );
}

fn git_commit_all(path: &std::path::Path, message: &str) {
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(path)
        .output()
        .expect("git add should run");
    let output = std::process::Command::new("git")
        .args(["commit", "-m", message])
        .current_dir(path)
        .output()
        .expect("git commit should run");
    assert!(
        output.status.success(),
        "git commit failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}