cmn-hypha 0.3.0

CMN CLI tool — spawn, grow, release, taste, bond, and absorb spores on the Code Mycelial Network
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
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod common;
use common::*;
use std::fs;

// ═══════════════════════════════════════════
// Version output
// ═══════════════════════════════════════════

#[test]
fn test_version_json_output() {
    let env = TestEnv::new();
    let output = env.hypha(&["--version"]);
    let text = combined_text(&output);
    let json = parse_json_last_line(&text);
    assert_eq!(json["code"], "ok", "version should output ok: {}", text);
    assert!(
        json["result"]["version"].is_string(),
        "version should include version string: {}",
        text
    );
}

#[test]
fn test_root_help_is_top_level_only() {
    let env = TestEnv::new();
    let output = env.hypha(&["--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success(), "help should succeed: {}", stdout);
    assert!(
        stdout.contains("Commands:"),
        "root help should list first-level commands: {}",
        stdout
    );
    assert!(
        stdout.contains("  skill"),
        "root help should include the skill command: {}",
        stdout
    );
    assert!(
        !stdout.contains(""),
        "root help should not recursively expand all commands: {}",
        stdout
    );
    assert!(
        !stdout.contains("URI types:"),
        "root help should not include subcommand detail sections: {}",
        stdout
    );
}

#[test]
fn test_recursive_help_is_recursive() {
    let env = TestEnv::new();
    let output = env.hypha(&["--help", "--recursive"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "recursive help should succeed: {}",
        stdout
    );
    assert!(
        stdout.contains(""),
        "recursive help should retain recursive command sections: {}",
        stdout
    );
    assert!(
        stdout.contains("URI types:"),
        "recursive help should include subcommand detail sections: {}",
        stdout
    );
}

#[test]
fn test_nested_help_is_single_layer() {
    let env = TestEnv::new();
    let output = env.hypha(&["hatch", "--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "hatch help should succeed: {}",
        stdout
    );
    assert!(
        stdout.contains("Commands:"),
        "hatch help should list direct child commands: {}",
        stdout
    );
    assert!(
        !stdout.contains("") && !stdout.contains("Usage: set"),
        "hatch help should not recursively expand grandchildren: {}",
        stdout
    );
}

#[test]
fn test_help_subcommand_exits_success() {
    let env = TestEnv::new();
    let output = env.hypha(&["sense", "--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "scoped help should succeed: {}",
        combined_text(&output)
    );
    assert!(
        stdout.contains("Usage:"),
        "scoped help should print normal help: {}",
        stdout
    );
}

// ═══════════════════════════════════════════
// Grow / Absorb / Bond / Lineage error paths
// ═══════════════════════════════════════════

#[test]
fn test_grow_not_spawned_dir() {
    let env = TestEnv::new();
    let dir = tempfile::tempdir().unwrap();
    let spore_dir = dir.path().to_path_buf();
    // Create a bare spore.core.json but no .cmn/spawned-from/
    fs::write(
        spore_dir.join("spore.core.json"),
        r#"{"$schema":"https://cmn.dev/schemas/v1/spore-core.json","name":"test","synopsis":"t","intent":["t"],"license":"MIT","tree":{"algorithm":"blob_tree_blake3_nfc","exclude_names":[],"follow_rules":[]}}"#,
    )
    .unwrap();
    let output = env.hypha_in_dir(&["grow"], &spore_dir);
    let text = combined_text(&output);
    assert!(!output.status.success(), "grow should fail: {}", text);
    let json = parse_json_last_line(&text);
    assert_eq!(json["code"], "grow_error", "should be grow_error: {}", text);
}

#[test]
fn test_absorb_no_uri() {
    let env = TestEnv::new();
    let output = env.hypha(&["absorb"]);
    let text = combined_text(&output);
    assert!(
        !output.status.success(),
        "absorb with no args should fail: {}",
        text
    );
}

#[test]
fn test_bond_empty_dir() {
    let env = TestEnv::new();
    let dir = tempfile::tempdir().unwrap();
    let output = env.hypha_in_dir(&["bond"], &dir.path().to_path_buf());
    let text = combined_text(&output);
    assert!(!output.status.success(), "bond should fail: {}", text);
}

#[test]
fn test_lineage_no_synapse() {
    let env = TestEnv::new();
    let output = env.hypha(&["lineage", "cmn://example.com/b3.test"]);
    let text = combined_text(&output);
    assert!(
        !output.status.success(),
        "lineage without synapse should fail: {}",
        text
    );
}
#[test]
fn test_error_returns_json() {
    let env = TestEnv::new();

    // Try to get status of non-existent site
    let output = env.hypha(&["mycelium", "status", "nonexistent.local"]);

    // Should fail
    assert!(!output.status.success());

    // Should output valid JSON error (errors go to stderr)
    let stderr = combined_text(&output);
    assert!(
        stderr.contains("\"error\":"),
        "error should be JSON: {}",
        stderr
    );
    assert!(
        stderr.contains("\"code\":"),
        "error should have code field: {}",
        stderr
    );
    assert!(
        stderr.contains("\"error\""),
        "error should have error message: {}",
        stderr
    );
    let json = parse_json_last_line(&stderr);
    assert!(
        json["hint"].as_str().is_some_and(|h| !h.is_empty()),
        "error should include an actionable hint: {}",
        stderr
    );
}

#[test]
fn test_cli_parse_error_has_afdata_hint() {
    let env = TestEnv::new();
    let output = env.hypha(&["--output", "xml", "sense", "cmn://cmn.dev"]);
    let text = combined_text(&output);
    assert!(
        !output.status.success(),
        "parse error should fail: {}",
        text
    );
    let json = parse_json_last_line(&text);
    assert_eq!(json["code"], "error", "should be an error: {}", text);
    assert_eq!(
        json["error_code"], "invalid_request",
        "should use standard CLI error shape: {}",
        text
    );
    assert_eq!(
        json["retryable"], false,
        "should not be retryable: {}",
        text
    );
    assert!(
        json["trace"]["duration_ms"].is_number(),
        "should include trace duration: {}",
        text
    );
    assert!(
        json["hint"].as_str().is_some_and(|h| !h.is_empty()),
        "should include hint: {}",
        text
    );
}

#[test]
fn test_skill_install_status_uninstall_custom_dir() {
    let env = TestEnv::new();
    let dir = tempfile::tempdir().unwrap();
    let skills_dir = dir.path().to_string_lossy().to_string();

    let install = env.hypha(&[
        "skill",
        "install",
        "--agent",
        "codex",
        "--skills-dir",
        &skills_dir,
    ]);
    let text = combined_text(&install);
    assert!(install.status.success(), "skill install failed: {}", text);
    let json = parse_json_last_line(&text);
    assert_eq!(
        json["code"], "skill_install",
        "bad install output: {}",
        text
    );
    assert_eq!(
        json["installed"], true,
        "install should report success: {}",
        text
    );
    assert!(
        json["hint"].as_str().is_some_and(|h| !h.is_empty()),
        "install should include operator hint: {}",
        text
    );
    assert!(dir.path().join("hypha").join("SKILL.md").is_file());

    let status = env.hypha(&[
        "skill",
        "status",
        "--agent",
        "codex",
        "--skills-dir",
        &skills_dir,
    ]);
    let text = combined_text(&status);
    assert!(status.status.success(), "skill status failed: {}", text);
    let json = parse_json_last_line(&text);
    assert_eq!(json["code"], "skill_status", "bad status output: {}", text);
    assert_eq!(
        json["installed_all"], true,
        "skill should be installed: {}",
        text
    );
    assert_eq!(
        json["current_all"], true,
        "skill should be current: {}",
        text
    );

    let uninstall = env.hypha(&[
        "skill",
        "uninstall",
        "--agent",
        "codex",
        "--skills-dir",
        &skills_dir,
    ]);
    let text = combined_text(&uninstall);
    assert!(
        uninstall.status.success(),
        "skill uninstall failed: {}",
        text
    );
    let json = parse_json_last_line(&text);
    assert_eq!(
        json["code"], "skill_uninstall",
        "bad uninstall output: {}",
        text
    );
    assert_eq!(
        json["removed_any"], true,
        "skill should be removed: {}",
        text
    );
    assert!(!dir.path().join("hypha").join("SKILL.md").exists());
}

#[test]
fn test_exit_codes() {
    let env = TestEnv::new();

    // Successful operation
    let output = env.hypha(&["mycelium", "root", "test.local"]);
    assert_eq!(
        output.status.code(),
        Some(0),
        "success should return exit code 0"
    );

    // Second root also succeeds (updates the site)
    let output = env.hypha(&["mycelium", "root", "test.local"]);
    assert_eq!(
        output.status.code(),
        Some(0),
        "update should also return exit code 0"
    );

    // Failed operation (invalid command)
    let output = env.hypha(&["mycelium", "invalid-command"]);
    assert_ne!(
        output.status.code(),
        Some(0),
        "invalid command should return non-zero exit code"
    );
}

#[test]
fn test_sense_invalid_uri() {
    let env = TestEnv::new();

    // sense with invalid URI should fail
    let output = env.hypha(&["sense", "invalid-uri"]);

    assert!(
        !output.status.success(),
        "sense should fail with invalid URI"
    );

    let stderr = combined_text(&output);
    assert!(
        stderr.contains("\"error\":"),
        "should return error: {}",
        stderr
    );
}

#[test]
fn test_sense_without_network() {
    let env = TestEnv::new();

    // Set up a site with a released spore
    env.hypha(&[
        "mycelium",
        "root",
        "test.local",
        "--endpoints-base",
        "https://test.local",
    ]);

    let spore_dir = env.dir.join("spore");
    fs::create_dir_all(&spore_dir).unwrap();
    fs::write(spore_dir.join("index.js"), "console.log('test');").unwrap();

    env.hypha_in_dir(
        &[
            "hatch",
            "--domain",
            "test.local",
            "--id",
            "test-spore",
            "--name",
            "test",
            "--intent",
            "Test release",
        ],
        &spore_dir,
    );

    // Release the spore
    let output = env.hypha_in_dir(&["release", "--domain", "test.local"], &spore_dir);
    assert!(output.status.success());

    // Find the spore hash from generated files
    let spore_dir_path = env.site_dir("test.local").join("public/cmn/spore");
    let spore_files: Vec<_> = fs::read_dir(&spore_dir_path)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.path()
                .file_name()
                .is_some_and(|n| n.to_string_lossy().starts_with("b3."))
        })
        .collect();

    if spore_files.is_empty() {
        return; // Skip if no spore files found
    }

    // Extract hash from filename (b3.xxxx.json -> b3.xxxx)
    let filename = spore_files[0]
        .path()
        .file_stem()
        .unwrap()
        .to_string_lossy()
        .to_string();

    // Create CMN URI
    let uri = format!("cmn://test.local/{}", filename);

    // Sense will fail because we can't fetch cmn.json from network in tests
    let output = env.hypha(&["sense", &uri]);

    let text = combined_text(&output);

    // Should fail with a cmn.json fetch error (network unreachable), not a parse error
    assert!(
        text.contains("cmn_failed") || text.contains("Failed to fetch"),
        "should fail at cmn.json fetch stage, got: {}",
        text
    );
}

#[test]
fn test_taste_invalid_uri() {
    let env = TestEnv::new();

    // taste with invalid URI should fail
    let output = env.hypha(&["taste", "invalid-uri"]);

    assert!(
        !output.status.success(),
        "taste should fail with invalid URI"
    );

    let stderr = combined_text(&output);
    assert!(
        stderr.contains("\"error\":"),
        "should return error: {}",
        stderr
    );
}

#[test]
fn test_taste_record_not_cached() {
    let env = TestEnv::new();

    // Try to record a taste verdict for a spore that isn't cached
    let output = env.hypha(&[
        "taste",
        "cmn://example.com/b3.111111111111111111111111111111111111111111",
        "--verdict",
        "safe",
    ]);

    assert!(
        !output.status.success(),
        "taste record should fail for non-cached spore"
    );

    let stderr = combined_text(&output);
    assert!(
        stderr.contains("NOT_CACHED"),
        "should return NOT_CACHED: {}",
        stderr
    );
}

#[test]
fn test_taste_invalid_verdict() {
    let env = TestEnv::new();

    // Try to record an invalid taste verdict
    let output = env.hypha(&[
        "taste",
        "cmn://example.com/b3.111111111111111111111111111111111111111111",
        "--verdict",
        "yummy",
    ]);

    assert!(
        !output.status.success(),
        "taste should fail with invalid verdict"
    );

    let stderr = combined_text(&output);
    assert!(
        stderr.contains("invalid value 'yummy' for '--verdict <VERDICT>'"),
        "should explain invalid verdict value: {}",
        stderr
    );
    assert!(
        stderr.contains("sweet, fresh, safe, rotten, toxic"),
        "should list accepted verdicts: {}",
        stderr
    );
}