fledge 0.16.0

Dev-lifecycle CLI — scaffolding, tasks, lanes, plugins, and more.
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
mod common;
use common::*;

use std::fs;
use std::process::Command;
use tempfile::TempDir;

// MARK: - spec commands
// Spec commands
// ──────────────────────────────────────────────────────────

#[test]
fn cli_spec_check_succeeds_in_project() {
    let output = run_fledge(&["spec", "check"]);
    // This runs against the fledge project itself which has specs
    assert!(output.status.success());
}

#[test]
fn cli_spec_init_in_new_dir() {
    let tmp = TempDir::new().unwrap();
    // init git repo first since spec needs project root
    Command::new("git")
        .args(["init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    let output = run_fledge_in(tmp.path(), &["spec", "init"]);
    assert!(output.status.success());
    assert!(tmp.path().join("specs").exists());
}

#[test]
fn cli_spec_new_creates_spec() {
    let tmp = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    // Init spec-sync first
    run_fledge_in(tmp.path(), &["spec", "init"]);

    let output = run_fledge_in(tmp.path(), &["spec", "new", "auth"]);
    assert!(output.status.success());
    assert!(tmp.path().join("specs/auth").exists());
}

#[test]
fn cli_spec_list_in_project() {
    let output = run_fledge(&["spec", "list"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("spec(s) found"),
        "expected summary line in output: {stdout}"
    );
}

#[test]
fn cli_spec_list_ls_alias() {
    let output = run_fledge(&["spec", "ls"]);
    assert!(output.status.success());
}

#[test]
fn cli_spec_list_json_valid() {
    let output = run_fledge(&["spec", "list", "--json"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    // Tier-D envelope: {schema_version: 1, action: "spec_list", specs: [...]}
    assert_eq!(parsed["schema_version"].as_u64(), Some(1));
    assert_eq!(parsed["action"].as_str(), Some("spec_list"));
    let specs = parsed["specs"].as_array().expect("specs array");
    assert!(!specs.is_empty(), "fledge project should have specs");
    let first = &specs[0];
    for field in [
        "name",
        "version",
        "status",
        "path",
        "files",
        "section_count",
        "required_sections",
        "companions",
        "missing_companions",
    ] {
        assert!(first.get(field).is_some(), "missing field: {field}");
    }
}

#[test]
fn cli_spec_list_json_empty_dir() {
    let tmp = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    run_fledge_in(tmp.path(), &["spec", "init"]);

    let output = run_fledge_in(tmp.path(), &["spec", "list", "--json"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap();
    assert_eq!(parsed["schema_version"].as_u64(), Some(1));
    assert!(parsed["specs"].as_array().unwrap().is_empty());
}

#[test]
fn cli_spec_show_existing_module() {
    let output = run_fledge(&["spec", "show", "spec"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("spec"));
    assert!(stdout.contains("sections"));
}

#[test]
fn cli_spec_show_json_valid() {
    let output = run_fledge(&["spec", "show", "spec", "--json"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    // Tier-D envelope: {schema_version: 1, action: "spec_show", spec: {...}}
    assert_eq!(parsed["schema_version"].as_u64(), Some(1));
    assert_eq!(parsed["action"].as_str(), Some("spec_show"));
    let spec = &parsed["spec"];
    assert!(spec.is_object());
    assert_eq!(spec["name"].as_str(), Some("spec"));
    assert!(spec["sections"].is_array());
    assert!(spec["companions"].is_array());
    assert!(spec["missing_companions"].is_array());
}

#[test]
fn cli_spec_show_missing_module_fails() {
    let output = run_fledge(&["spec", "show", "definitely-not-a-real-spec-xyz"]);
    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("No spec found") || stderr.contains("not"));
}

#[test]
fn cli_spec_check_json_valid() {
    let output = run_fledge(&["spec", "check", "--json"]);
    // May pass or fail on the repo's specs; either way stdout must be JSON
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    // Tier-D envelope
    assert_eq!(parsed["schema_version"].as_u64(), Some(1));
    assert_eq!(parsed["action"].as_str(), Some("spec_check"));
    assert!(parsed["specs"].is_array());
    assert!(parsed["totals"].is_object());
    assert!(parsed["totals"]["checked"].is_number());
    assert!(parsed["totals"]["errors"].is_number());
    assert!(parsed["totals"]["warnings"].is_number());
    assert!(parsed["strict"].is_boolean());
}

#[test]
fn cli_spec_check_json_spec_shape() {
    let output = run_fledge(&["spec", "check", "--json"]);
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let specs = parsed["specs"].as_array().unwrap();
    assert!(!specs.is_empty(), "fledge repo should have specs");
    let first = &specs[0];
    for field in [
        "name",
        "version",
        "status",
        "file_count",
        "section_count",
        "required_count",
        "errors",
        "warnings",
    ] {
        assert!(first.get(field).is_some(), "missing field: {field}");
    }
    assert!(first["errors"].is_array());
    assert!(first["warnings"].is_array());
}

#[test]
fn cli_work_start_help_shows_json_flag() {
    let output = run_fledge(&["work", "start", "--help"]);
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("--json"));
}

#[test]
fn cli_work_pr_help_shows_json_flag() {
    let output = run_fledge(&["work", "pr", "--help"]);
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("--json"));
}

#[test]
fn cli_work_status_help_shows_json_flag() {
    let output = run_fledge(&["work", "status", "--help"]);
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("--json"));
}

#[test]
fn cli_work_status_json_in_repo() {
    // Run inside a temp git repo with a real branch — avoids the detached-HEAD
    // situation that CI check-out sometimes produces.
    let tmp = TempDir::new().unwrap();
    Command::new("git")
        .args(["init", "-b", "main"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    Command::new("git")
        .args(["config", "user.name", "Test"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    Command::new("git")
        .args(["commit", "--allow-empty", "-m", "init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    Command::new("git")
        .args(["checkout", "-b", "feature"])
        .current_dir(tmp.path())
        .output()
        .unwrap();

    let output = run_fledge_in(tmp.path(), &["work", "status", "--json"]);
    assert!(
        output.status.success(),
        "work status --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(parsed.is_object());
    assert_eq!(parsed["branch"].as_str(), Some("feature"));
    assert_eq!(parsed["default"].as_str(), Some("main"));
    assert!(parsed["ahead"].is_number());
    // behind is either a number or null (base-not-fetched sentinel)
    assert!(parsed["behind"].is_number() || parsed["behind"].is_null());
    // pr is either null or an object — both are valid
    assert!(parsed.get("pr").is_some());
}

// ──────────────────────────────────────────────────────────

// MARK: - spec edge cases
// Spec edge cases
// ──────────────────────────────────────────────────────────

#[test]
fn cli_spec_check_in_empty_dir_fails() {
    let tmp = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    let output = run_fledge_in(tmp.path(), &["spec", "check"]);
    // No specs dir — should fail or warn
    let stderr = String::from_utf8(output.stderr).unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Either exits nonzero or prints a message about missing specs
    assert!(
        !output.status.success()
            || stdout.contains("No specs")
            || stderr.contains("No specs")
            || stdout.contains("specs"),
        "expected some feedback about missing specs, got stdout: {stdout}, stderr: {stderr}"
    );
}

#[test]
fn cli_spec_new_duplicate_name() {
    let tmp = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    run_fledge_in(tmp.path(), &["spec", "init"]);
    run_fledge_in(tmp.path(), &["spec", "new", "auth"]);

    // Second creation should fail or warn
    let output = run_fledge_in(tmp.path(), &["spec", "new", "auth"]);
    assert!(
        !output.status.success() || {
            let stderr = String::from_utf8(output.stderr.clone()).unwrap();
            let stdout = String::from_utf8(output.stdout.clone()).unwrap();
            stderr.contains("exists") || stdout.contains("exists")
        },
        "expected duplicate spec warning"
    );
}

// ──────────────────────────────────────────────────────────
// Changelog edge cases
// ──────────────────────────────────────────────────────────

#[test]
fn cli_changelog_nonexistent_tag_fails() {
    let output = run_fledge(&["changelog", "--tag", "v999.999.999"]);
    // Should fail or return empty — shouldn't panic
    if !output.status.success() {
        let stderr = String::from_utf8(output.stderr).unwrap();
        assert!(
            stderr.contains("not found") || stderr.contains("999"),
            "expected tag-not-found error, got: {stderr}"
        );
    }
}

#[test]
fn cli_changelog_zero_limit() {
    let output = run_fledge(&["changelog", "--limit", "0"]);
    // Should succeed with empty output or handle gracefully
    assert!(output.status.success());
}

#[test]
fn cli_changelog_in_non_git_dir() {
    let tmp = TempDir::new().unwrap();
    let output = run_fledge_in(tmp.path(), &["changelog"]);
    // Not a git repo — should fail gracefully
    assert!(
        !output.status.success() || {
            let stdout = String::from_utf8(output.stdout.clone()).unwrap();
            stdout.contains("No tags") || stdout.is_empty()
        }
    );
}

// ──────────────────────────────────────────────────────────
// Doctor edge cases
// ──────────────────────────────────────────────────────────

#[test]
fn cli_doctor_in_empty_dir() {
    let tmp = TempDir::new().unwrap();
    let output = run_fledge_in(tmp.path(), &["doctor"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("fledge") || stdout.contains("Git"));
}

#[test]
fn cli_doctor_json_in_empty_dir() {
    let tmp = TempDir::new().unwrap();
    let output = run_fledge_in(tmp.path(), &["doctor", "--json"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(parsed["sections"].is_array());
}

// ──────────────────────────────────────────────────────────
// Validate-template edge cases
// ──────────────────────────────────────────────────────────

#[test]
fn cli_validate_template_nonexistent_path() {
    let output = run_fledge(&["templates", "validate", "/tmp/no-such-path-ever-12345"]);
    assert!(!output.status.success());
}

#[test]
fn cli_validate_template_empty_template_toml() {
    let tmp = TempDir::new().unwrap();
    let tpl = tmp.path().join("empty-tpl");
    fs::create_dir_all(&tpl).unwrap();
    fs::write(tpl.join("template.toml"), "").unwrap();
    let output = run_fledge(&["templates", "validate", tpl.to_str().unwrap()]);
    assert!(!output.status.success());
}

#[test]
fn cli_validate_template_missing_name_field() {
    let tmp = TempDir::new().unwrap();
    let tpl = tmp.path().join("noname");
    fs::create_dir_all(&tpl).unwrap();
    fs::write(
        tpl.join("template.toml"),
        r#"[template]
description = "Missing name field"

[files]
ignore = ["template.toml"]
"#,
    )
    .unwrap();
    fs::write(tpl.join("file.txt"), "content").unwrap();
    let output = run_fledge(&["templates", "validate", tpl.to_str().unwrap()]);
    assert!(!output.status.success());
}

#[test]
fn cli_validate_template_missing_description() {
    let tmp = TempDir::new().unwrap();
    let tpl = tmp.path().join("nodesc");
    fs::create_dir_all(&tpl).unwrap();
    fs::write(
        tpl.join("template.toml"),
        r#"[template]
name = "nodesc"

[files]
ignore = ["template.toml"]
"#,
    )
    .unwrap();
    fs::write(tpl.join("file.txt"), "content").unwrap();
    let output = run_fledge(&["templates", "validate", tpl.to_str().unwrap()]);
    // Missing description might be a warning or error
    let _status = output.status;
}

// ──────────────────────────────────────────────────────────