cosh-tools 0.1.4

Tools crate for cosh - coding agent for the terminal
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
use super::super::types::{FsMetadata, FsWrite, TargetFile};
use super::super::write::write;
use std::path::{Path, PathBuf};

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

/// Unique scratch root: each test gets its own directory, so tests never
/// depend on (or touch) a developer's real project tree.
struct TempRoot(PathBuf);

impl TempRoot {
    fn new(label: &str) -> Self {
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let id = COUNTER.fetch_add(1, Ordering::Relaxed);
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time is after UNIX_EPOCH")
            .as_nanos();
        let dir = std::env::temp_dir().join(format!("cosh_fs_write_{label}_{id}_{nanos}"));
        std::fs::create_dir_all(&dir).expect("create temp root");
        Self(dir)
    }

    fn path(&self) -> &Path {
        &self.0
    }

    /// Path of a fixture file inside the root.
    fn file(&self, name: &str) -> String {
        self.0.join(name).to_string_lossy().into_owned()
    }
}

impl Drop for TempRoot {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn meta_root(root: &Path) -> FsMetadata {
    FsMetadata {
        root: root.to_path_buf(),
        allowlist: None,
        blocklist: None,
    }
}

#[tokio::test]
async fn write_creates_file_and_returns_hash_header() {
    let root = TempRoot::new("create");
    let path = root.file("ftest.txt");
    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "hello world".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0].header.contains(&path),
        "header must carry the written path: {}",
        results[0].header
    );
    assert!(results[0].warnings.is_none());
    assert_eq!(std::fs::read_to_string(&path).unwrap(), "hello world");
}

#[tokio::test]
async fn write_creates_multiple_files_in_single_call() {
    let root = TempRoot::new("multi");
    let path_a = root.file("ftest.txt");
    let path_b = root.file("ftst2.txt");
    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![
                TargetFile {
                    path: path_a.clone(),
                    text: "hello world".to_string(),
                    file_hash: None,
                },
                TargetFile {
                    path: path_b.clone(),
                    text: "test".to_string(),
                    file_hash: None,
                },
            ],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 2);
    assert!(results[0].header.contains(&path_a));
    assert!(results[0].warnings.is_none());
    assert!(results[1].header.contains(&path_b));
    assert!(results[1].warnings.is_none());
    assert_eq!(std::fs::read_to_string(&path_a).unwrap(), "hello world");
    assert_eq!(std::fs::read_to_string(&path_b).unwrap(), "test");
}

#[tokio::test]
async fn write_denied_when_path_is_in_blocklist() {
    let root = TempRoot::new("blocked");
    let path = root.file("ftest.txt");
    let metadata = FsMetadata {
        root: root.path().to_path_buf(),
        allowlist: None,
        blocklist: Some(vec![PathBuf::from(&path)]),
    };

    let result = write(
        metadata,
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "should not be written".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("write permission denied")
    );
    assert!(
        !Path::new(&path).exists(),
        "blocked file must not be written"
    );
}

#[tokio::test]
async fn write_reports_empty_text_inline_and_skips_file() {
    let root = TempRoot::new("empty");
    let path = root.file("cosh_test_empty.txt");

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("text is empty")
    );
    assert!(!Path::new(&path).exists());
}

#[tokio::test]
async fn write_allowed_outside_root_when_path_in_allowlist() {
    let root = TempRoot::new("allow");
    // Outside the root: a sibling scratch file, not under the temp root.
    let outside = std::env::temp_dir().join(format!(
        "cosh_fs_write_allow_out_{}.txt",
        std::process::id()
    ));
    let _ = std::fs::remove_file(&outside);
    let outside_path = outside.to_string_lossy().into_owned();
    let metadata = FsMetadata {
        root: root.path().to_path_buf(),
        allowlist: Some(vec![outside.clone()]),
        blocklist: None,
    };

    let result = write(
        metadata,
        FsWrite {
            targets: vec![TargetFile {
                path: outside_path.clone(),
                text: "outside root but explicitly allowed".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0].header.contains(outside_path.as_str()),
        "header must carry the allowed path: {}",
        results[0].header
    );
    let _ = std::fs::remove_file(&outside);
}

#[tokio::test]
async fn write_errors_on_inconsistent_blocklist_and_allowlist() {
    let root = TempRoot::new("mismatch");
    let path = root.file("ftest.txt");
    let metadata = FsMetadata {
        root: root.path().to_path_buf(),
        allowlist: Some(vec![PathBuf::from(&path)]),
        blocklist: Some(vec![PathBuf::from(&path)]),
    };

    let result = write(
        metadata,
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "should never be written".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("both blocklist and allowlist"));
}

#[tokio::test]
async fn write_strips_hashline_prefixes_and_reports_warning() {
    let root = TempRoot::new("striphl");
    let path = root.file("cosh_test_strip_hashline.txt");
    let content = "[main.rs#ABCD]\n42: fn main() {\n43:     println!(\"hello\");\n44: }";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("auto-stripped hashline")
    );

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(!written.contains("[main.rs#ABCD]"));
    assert!(!written.contains("42:"));
    assert!(written.contains("fn main()"));
    assert!(written.contains("println!"));
}

#[tokio::test]
async fn write_strips_hashline_prefixes_without_bracket_header() {
    let root = TempRoot::new("strippfx");
    let path = root.file("cosh_test_strip_line_prefixes.txt");
    let content = "42: fn main() {\n43:     println!(\"hello\");\n44: }";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("auto-stripped hashline")
    );

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(!written.contains("42:"));
    assert!(written.contains("fn main()"));
}

#[tokio::test]
async fn write_does_not_strip_normal_content() {
    let root = TempRoot::new("nostrip");
    let path = root.file("cosh_test_no_strip.txt");
    let content = "fn main() {\n    println!(\"hello\");\n}";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].warnings.is_none());

    let written = std::fs::read_to_string(&path).unwrap();
    assert_eq!(written, content);
}

#[tokio::test]
#[cfg(unix)]
async fn write_chmods_executable_for_shebang() {
    use std::os::unix::fs::PermissionsExt;

    let root = TempRoot::new("shebang");
    let path = root.file("cosh_test_shebang.sh");
    let content = "#!/usr/bin/env bash\necho hello";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("made executable")
    );

    let meta = std::fs::metadata(&path).unwrap();
    assert!(
        meta.permissions().mode() & 0o111 != 0,
        "file should have execute bits"
    );
}

#[tokio::test]
async fn write_refuses_to_overwrite_auto_generated_file() {
    let root = TempRoot::new("generated");
    let path = root.file("cosh_test_generated.txt");
    let original = "// Code generated by tool. DO NOT EDIT.\noriginal\n";
    std::fs::write(&path, original).unwrap();

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "replacement".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(
        results[0]
            .warnings
            .as_deref()
            .unwrap()
            .contains("auto-generated"),
        "expected an auto-generated refusal, got: {:?}",
        results[0].warnings
    );
    // The original content is left untouched.
    assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
}

#[tokio::test]
async fn write_allows_creating_file_with_generated_marker() {
    // Creating a brand-new file is always allowed — the guard protects
    // overwriting existing generated files, not generating new ones.
    let root = TempRoot::new("gennew");
    let path = root.file("cosh_test_create_generated.txt");
    let content = "// Code generated by tool. DO NOT EDIT.\nhello\n";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].warnings.is_none());
    assert_eq!(std::fs::read_to_string(&path).unwrap(), content);
}

#[tokio::test]
async fn write_normalizes_crlf_to_lf() {
    // Parity with read: CRLF content is canonicalized to LF so the returned
    // hash/header matches what a follow-up read would report.
    let root = TempRoot::new("crlf");
    let path = root.file("cosh_test_crlf.txt");

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: "line1\r\nline2\r\n".to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].warnings.is_none());
    assert_eq!(std::fs::read_to_string(&path).unwrap(), "line1\nline2\n");
}

#[tokio::test]
#[cfg(unix)]
async fn write_does_not_chmod_without_shebang() {
    use std::os::unix::fs::PermissionsExt;

    let root = TempRoot::new("noshebang");
    let path = root.file("cosh_test_no_shebang.txt");
    let content = "plain text file";

    let result = write(
        meta_root(root.path()),
        FsWrite {
            targets: vec![TargetFile {
                path: path.clone(),
                text: content.to_string(),
                file_hash: None,
            }],
        },
    )
    .await;
    assert!(result.is_ok());
    let results = result.unwrap();
    assert_eq!(results.len(), 1);
    assert!(results[0].warnings.is_none());

    let meta = std::fs::metadata(&path).unwrap();
    let mode = meta.permissions().mode() & 0o111;
    assert_eq!(mode, 0, "file should NOT have execute bits");
}