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
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

//! Tests for git operations using system git
//!
//! These tests verify that system git operations produce expected results
//! and that content hash is preserved across git clone roundtrips.

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

/// Test environment for git operations
struct GitTestEnv {
    _temp: TempDir,
    dir: PathBuf,
}

impl GitTestEnv {
    fn new() -> Self {
        let temp = TempDir::new().expect("Failed to create temp dir");
        let dir = temp.path().to_path_buf();
        Self { _temp: temp, dir }
    }

    /// Run git command and return output
    fn git(&self, args: &[&str]) -> std::process::Output {
        Command::new("git")
            .args(args)
            .current_dir(&self.dir)
            .env("GIT_AUTHOR_NAME", "Test")
            .env("GIT_AUTHOR_EMAIL", "test@test.com")
            .env("GIT_COMMITTER_NAME", "Test")
            .env("GIT_COMMITTER_EMAIL", "test@test.com")
            .output()
            .expect("Failed to run git")
    }

    /// Create test files in the directory
    fn create_test_files(&self) {
        fs::write(self.dir.join("README.md"), "# Test Project\n").unwrap();
        fs::write(
            self.dir.join("main.rs"),
            "fn main() { println!(\"hello\"); }\n",
        )
        .unwrap();

        let src_dir = self.dir.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(
            src_dir.join("lib.rs"),
            "pub fn add(a: i32, b: i32) -> i32 { a + b }\n",
        )
        .unwrap();
        fs::write(src_dir.join("util.rs"), "pub fn helper() {}\n").unwrap();
    }
}

#[test]
fn test_git_init_creates_valid_repo() {
    let env = GitTestEnv::new();
    env.create_test_files();

    // Use command-line git to initialize
    let output = env.git(&["init", "-b", "main"]);
    assert!(output.status.success(), "git init failed");

    // Check .git directory exists
    assert!(env.dir.join(".git").exists(), ".git directory should exist");
    assert!(
        env.dir.join(".git/objects").exists(),
        ".git/objects should exist"
    );
    assert!(env.dir.join(".git/refs").exists(), ".git/refs should exist");
}

#[test]
fn test_git_add_and_commit() {
    let env = GitTestEnv::new();
    env.create_test_files();

    // Initialize and commit with git
    env.git(&["init", "-b", "main"]);
    env.git(&["add", "."]);
    let output = env.git(&["commit", "-m", "Initial commit"]);
    assert!(output.status.success(), "git commit failed");

    // Verify commit exists
    let log_output = env.git(&["log", "--oneline"]);
    assert!(log_output.status.success());
    let log = String::from_utf8_lossy(&log_output.stdout);
    assert!(
        log.contains("Initial commit"),
        "commit message should be in log"
    );

    // Verify tree
    let tree_output = env.git(&["ls-tree", "-r", "HEAD"]);
    assert!(tree_output.status.success());
    let tree = String::from_utf8_lossy(&tree_output.stdout);
    assert!(tree.contains("README.md"), "README.md should be in tree");
    assert!(tree.contains("main.rs"), "main.rs should be in tree");
    assert!(tree.contains("src/lib.rs"), "src/lib.rs should be in tree");
    assert!(
        tree.contains("src/util.rs"),
        "src/util.rs should be in tree"
    );
}

#[test]
fn test_git_tree_structure() {
    let env = GitTestEnv::new();
    env.create_test_files();

    // Initialize and commit
    env.git(&["init", "-b", "main"]);
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "Test commit"]);

    // Get tree hash
    let tree_output = env.git(&["rev-parse", "HEAD^{tree}"]);
    assert!(tree_output.status.success());
    let tree_hash = String::from_utf8_lossy(&tree_output.stdout)
        .trim()
        .to_string();

    // Verify tree hash is valid (40 hex chars for SHA-1)
    assert_eq!(tree_hash.len(), 40, "tree hash should be 40 hex chars");
    assert!(
        tree_hash.chars().all(|c| c.is_ascii_hexdigit()),
        "tree hash should be hex"
    );

    // Get tree contents
    let ls_tree = env.git(&["ls-tree", &tree_hash]);
    assert!(ls_tree.status.success());
    let tree_content = String::from_utf8_lossy(&ls_tree.stdout);

    // Verify entries
    assert!(
        tree_content.contains("100644 blob"),
        "should have blob entries"
    );
    assert!(
        tree_content.contains("040000 tree"),
        "should have tree entry for src/"
    );
}

#[test]
fn test_git_working_dir_clean_detection() {
    let env = GitTestEnv::new();
    env.create_test_files();

    // Initialize and commit
    env.git(&["init", "-b", "main"]);
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "Initial"]);

    // Check clean status
    let status = env.git(&["status", "--porcelain"]);
    assert!(status.status.success());
    let status_out = String::from_utf8_lossy(&status.stdout);
    assert!(status_out.is_empty(), "working dir should be clean");

    // Modify a file
    fs::write(env.dir.join("README.md"), "# Modified\n").unwrap();

    // Check dirty status
    let status = env.git(&["status", "--porcelain"]);
    assert!(status.status.success());
    let status_out = String::from_utf8_lossy(&status.stdout);
    assert!(
        status_out.contains("README.md"),
        "should show modified file"
    );
}

#[test]
fn test_git_commit_with_parent() {
    let env = GitTestEnv::new();

    // Create initial file and commit
    fs::write(env.dir.join("file1.txt"), "content1").unwrap();
    env.git(&["init", "-b", "main"]);
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "First commit"]);

    // Get first commit hash
    let first_hash = env.git(&["rev-parse", "HEAD"]);
    let first_hash = String::from_utf8_lossy(&first_hash.stdout)
        .trim()
        .to_string();

    // Create second file and commit
    fs::write(env.dir.join("file2.txt"), "content2").unwrap();
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "Second commit"]);

    // Get second commit hash
    let second_hash = env.git(&["rev-parse", "HEAD"]);
    let second_hash = String::from_utf8_lossy(&second_hash.stdout)
        .trim()
        .to_string();

    // Verify parent relationship
    let parent_hash = env.git(&["rev-parse", "HEAD^"]);
    let parent_hash = String::from_utf8_lossy(&parent_hash.stdout)
        .trim()
        .to_string();

    assert_eq!(
        first_hash, parent_hash,
        "first commit should be parent of second"
    );
    assert_ne!(first_hash, second_hash, "commits should be different");
}

#[test]
fn test_git_root_commit_detection() {
    let env = GitTestEnv::new();

    // Create two commits
    fs::write(env.dir.join("file1.txt"), "content1").unwrap();
    env.git(&["init", "-b", "main"]);
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "Root commit"]);

    let root_hash = env.git(&["rev-parse", "HEAD"]);
    let root_hash = String::from_utf8_lossy(&root_hash.stdout)
        .trim()
        .to_string();

    fs::write(env.dir.join("file2.txt"), "content2").unwrap();
    env.git(&["add", "."]);
    env.git(&["commit", "-m", "Second commit"]);

    // Find root commit by walking back
    let log_output = env.git(&["rev-list", "--max-parents=0", "HEAD"]);
    let found_root = String::from_utf8_lossy(&log_output.stdout)
        .trim()
        .to_string();

    assert_eq!(root_hash, found_root, "should find the root commit");
}

/// Round-trip test: hash → git commit → clone → hash must match
///
/// This tests the critical invariant: content hashed during `hypha release`
/// must produce the same hash after `git clone` + checkout.
#[test]
fn test_hash_roundtrip_git_clone() {
    let source = GitTestEnv::new();

    // Create source files
    fs::write(source.dir.join("README.md"), "# Test\n").unwrap();
    fs::write(source.dir.join("lib.rs"), "pub fn hello() {}\n").unwrap();
    let sub = source.dir.join("src");
    fs::create_dir_all(&sub).unwrap();
    fs::write(sub.join("main.rs"), "fn main() {}\n").unwrap();

    // Compute hash (same params as spore.core.json: exclude_names=[".git"], follow_rules=[".gitignore"])
    let hash_before = hypha::tree::compute_tree_hash(
        &source.dir,
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash before");

    // Git init + add + commit
    source.git(&["init", "-b", "main"]);
    source.git(&["add", "."]);
    source.git(&["commit", "-m", "initial"]);

    // Clone to a new directory
    let clone_dir = TempDir::new().expect("clone dir");
    let output = Command::new("git")
        .args([
            "clone",
            &source.dir.display().to_string(),
            &clone_dir.path().display().to_string(),
        ])
        .output()
        .expect("git clone");
    assert!(output.status.success(), "git clone failed");

    // Compute hash on cloned content (same params)
    let hash_after = hypha::tree::compute_tree_hash(
        clone_dir.path(),
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash after");

    assert_eq!(
        hash_before, hash_after,
        "hash must match after git clone roundtrip"
    );
}

/// Round-trip test with .gitignore: gitignored files must be handled consistently
///
/// Verifies that follow_rules filenames (e.g., ".gitignore") are used consistently
/// in both hash computation and file collection.
#[test]
fn test_hash_roundtrip_with_gitignore() {
    let source = GitTestEnv::new();

    // Create .gitignore
    fs::write(source.dir.join(".gitignore"), "*.log\nbuild/\n").unwrap();

    // Create normal files
    fs::write(source.dir.join("README.md"), "# Test\n").unwrap();
    fs::write(source.dir.join("lib.rs"), "pub fn hello() {}\n").unwrap();

    // Create gitignored files
    fs::write(source.dir.join("debug.log"), "some log data\n").unwrap();
    let build = source.dir.join("build");
    fs::create_dir_all(&build).unwrap();
    fs::write(build.join("output.bin"), "binary data\n").unwrap();

    // Compute hash with follow_rules=[".gitignore"] (as spore.core.json uses)
    let hash_before = hypha::tree::compute_tree_hash(
        &source.dir,
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash before");

    // Git init + add + commit (git add respects .gitignore)
    source.git(&["init", "-b", "main"]);
    source.git(&["add", "."]);
    source.git(&["commit", "-m", "initial"]);

    // Clone to a new directory
    let clone_dir = TempDir::new().expect("clone dir");
    let output = Command::new("git")
        .args([
            "clone",
            &source.dir.display().to_string(),
            &clone_dir.path().display().to_string(),
        ])
        .output()
        .expect("git clone");
    assert!(output.status.success(), "git clone failed");

    // Compute hash on cloned content (same params)
    let hash_after = hypha::tree::compute_tree_hash(
        clone_dir.path(),
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash after");

    assert_eq!(
        hash_before, hash_after,
        "hash must match after git clone roundtrip (with .gitignore)"
    );
}

/// Test that executable files preserve their mode through git roundtrip
#[cfg(unix)]
#[test]
fn test_hash_roundtrip_executable_files() {
    use std::os::unix::fs::PermissionsExt;

    let source = GitTestEnv::new();

    // Create a regular file and an executable file
    fs::write(source.dir.join("README.md"), "# Test\n").unwrap();
    fs::write(source.dir.join("run.sh"), "#!/bin/sh\necho hi\n").unwrap();
    fs::set_permissions(source.dir.join("run.sh"), fs::Permissions::from_mode(0o755)).unwrap();

    // Compute hash
    let hash_before = hypha::tree::compute_tree_hash(
        &source.dir,
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash before");

    // Git init + add + commit
    source.git(&["init", "-b", "main"]);
    source.git(&["add", "."]);
    source.git(&["commit", "-m", "initial"]);

    // Clone
    let clone_dir = TempDir::new().expect("clone dir");
    let output = Command::new("git")
        .args([
            "clone",
            &source.dir.display().to_string(),
            &clone_dir.path().display().to_string(),
        ])
        .output()
        .expect("git clone");
    assert!(output.status.success(), "git clone failed");

    // Verify executable bit preserved
    let cloned_perms = fs::metadata(clone_dir.path().join("run.sh"))
        .unwrap()
        .permissions();
    assert!(
        cloned_perms.mode() & 0o111 != 0,
        "executable bit should be preserved after clone"
    );

    // Compute hash
    let hash_after = hypha::tree::compute_tree_hash(
        clone_dir.path(),
        &substrate::SporeTree {
            algorithm: "blob_tree_blake3_nfc".to_string(),
            exclude_names: vec![".git".to_string()],
            follow_rules: vec![".gitignore".to_string()],
        },
    )
    .expect("hash after");

    assert_eq!(
        hash_before, hash_after,
        "hash must match after git clone roundtrip (executable files)"
    );
}