skill-cli 0.3.0

Command-line interface for the Skill runtime - install, run, and manage AI agent skills
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Error Handling and Edge Case Tests
//!
//! This module contains comprehensive error handling tests for the Claude Bridge
//! skill generation, covering:
//!
//! - Missing or invalid manifest files
//! - Filesystem permission errors
//! - Concurrent generation scenarios
//! - Partial failure recovery
//! - Invalid input sanitization
//! - Error message quality validation
//!
//! # Running Tests
//!
//! ```bash
//! # Run all error tests
//! cargo test --test error_tests -- --ignored
//!
//! # Run specific error category
//! cargo test test_error_missing_manifest -- --ignored
//! ```

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::Path;
use tempfile::TempDir;

// ============================================================================
// Manifest Error Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary
fn test_error_missing_manifest() {
    let temp = TempDir::new().unwrap();

    // Try to generate without manifest file
    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .assert()
        .failure()
        .stderr(predicate::str::contains("manifest").or(predicate::str::contains("not found")));
}

#[test]
#[ignore] // Requires skill binary
fn test_error_invalid_toml_manifest() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Create manifest with invalid TOML syntax
    fs::write(&manifest_path, "invalid { toml [ syntax").unwrap();

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("TOML")
                .or(predicate::str::contains("parse"))
                .or(predicate::str::contains("invalid")),
        );
}

#[test]
#[ignore] // Requires skill binary
fn test_error_missing_required_manifest_fields() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Create manifest missing required fields
    fs::write(
        &manifest_path,
        r#"
[skills.incomplete]
# Missing description and other required fields
source = "./incomplete"
"#,
    )
    .unwrap();

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("required")
                .or(predicate::str::contains("missing"))
                .or(predicate::str::contains("description")),
        );
}

#[test]
#[ignore] // Requires skill binary
fn test_error_invalid_skill_name() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.valid-skill]
source = "./valid"
runtime = "wasm"
description = "A valid skill"
"#,
    )
    .unwrap();

    // Try to generate non-existent skill
    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--skill")
        .arg("nonexistent-skill")
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("not found")
                .and(predicate::str::contains("nonexistent-skill")),
        );
}

#[test]
#[ignore] // Requires skill binary
fn test_error_no_skills_in_manifest() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Create empty manifest
    fs::write(&manifest_path, "# Empty manifest\n").unwrap();

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("No skills")
                .or(predicate::str::contains("empty"))
                .or(predicate::str::contains("found")),
        );
}

// ============================================================================
// Filesystem Permission Error Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary and Unix permissions
#[cfg(unix)]
fn test_error_output_dir_not_writable() {
    use std::os::unix::fs::PermissionsExt;

    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Create valid manifest
    fs::write(
        &manifest_path,
        r#"
[skills.test-skill]
source = "./test"
runtime = "wasm"
description = "Test skill"
"#,
    )
    .unwrap();

    let output_dir = temp.path().join("readonly");
    fs::create_dir(&output_dir).unwrap();

    // Make directory read-only
    let mut perms = fs::metadata(&output_dir).unwrap().permissions();
    perms.set_mode(0o444); // r--r--r--
    fs::set_permissions(&output_dir, perms).unwrap();

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .assert()
        .failure()
        .stderr(
            predicate::str::contains("permission")
                .or(predicate::str::contains("Permission"))
                .or(predicate::str::contains("denied")),
        );

    // Clean up: restore permissions so temp dir can be deleted
    let mut perms = fs::metadata(&output_dir).unwrap().permissions();
    perms.set_mode(0o755);
    fs::set_permissions(&output_dir, perms).unwrap();
}

#[test]
#[ignore] // Requires skill binary
fn test_error_script_generation_failure() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.test-skill]
source = "./test"
runtime = "wasm"
description = "Test skill"
"#,
    )
    .unwrap();

    let output_dir = temp.path().join("skills");
    fs::create_dir_all(&output_dir).unwrap();

    let skill_dir = output_dir.join("test-skill");
    fs::create_dir(&skill_dir).unwrap();

    // Create a file where scripts/ directory should be
    let scripts_path = skill_dir.join("scripts");
    fs::write(&scripts_path, "I am a file, not a directory").unwrap();

    // This should fail because scripts exists as a file
    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .arg("--force")
        .assert()
        .failure();
}

// ============================================================================
// Concurrent Generation Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary, slow test
fn test_concurrent_generation_safety() {
    use std::sync::Arc;
    use std::thread;

    let temp = Arc::new(TempDir::new().unwrap());
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.test-skill]
source = "./test"
runtime = "wasm"
description = "Test skill for concurrency"
"#,
    )
    .unwrap();

    let output_dir = temp.path().join("skills");
    fs::create_dir(&output_dir).unwrap();

    let mut handles = vec![];

    // Spawn 3 concurrent generation processes
    for i in 0..3 {
        let temp_path = temp.path().to_path_buf();
        let output = output_dir.clone();

        let handle = thread::spawn(move || {
            println!("Thread {} starting generation", i);
            Command::cargo_bin("skill")
                .unwrap()
                .current_dir(&temp_path)
                .arg("claude")
                .arg("generate")
                .arg("--output")
                .arg(&output)
                .arg("--force")
                .output()
                .expect("Failed to execute command");
        });

        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    // Verify generated files are not corrupted
    let skill_md = output_dir.join("test-skill").join("SKILL.md");
    assert!(
        skill_md.exists(),
        "SKILL.md should exist after concurrent generation"
    );

    let content = fs::read_to_string(skill_md).unwrap();
    assert!(
        content.contains("---"),
        "SKILL.md should have valid YAML frontmatter"
    );
    assert!(
        content.contains("name:"),
        "SKILL.md should have name field"
    );
}

// ============================================================================
// Partial Failure Recovery Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary
fn test_partial_failure_continues_generation() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Create manifest with one valid and one potentially problematic skill
    fs::write(
        &manifest_path,
        r#"
[skills.valid-skill]
source = "./valid"
runtime = "wasm"
description = "A valid skill"

[skills.test-skill-2]
source = "./test2"
runtime = "native"
description = "Another valid skill"
"#,
    )
    .unwrap();

    let output_dir = temp.path().join("skills");

    let result = Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .output()
        .unwrap();

    // Check if at least some skills were generated
    if output_dir.exists() {
        let entries: Vec<_> = fs::read_dir(&output_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .collect();

        if !entries.is_empty() {
            println!(
                "Generated {} skill(s) even with potential issues",
                entries.len()
            );
        }
    }

    // Output should contain information about any failures
    let stderr = String::from_utf8_lossy(&result.stderr);
    println!("stderr: {}", stderr);
}

// ============================================================================
// Path Traversal and Security Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary
fn test_path_traversal_prevention() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.test]
source = "./test"
runtime = "wasm"
description = "Test"
"#,
    )
    .unwrap();

    // Try to use path traversal in output directory
    let traversal_path = temp.path().join("..").join("..").join("..").join("etc");

    let result = Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&traversal_path)
        .output()
        .unwrap();

    // Should either reject the path or sanitize it
    // At minimum, should not write outside temp directory
    assert!(
        !result.status.success() || !Path::new("/etc/skills").exists(),
        "Should not write to /etc via path traversal"
    );
}

#[test]
#[ignore] // Requires skill binary
fn test_special_characters_in_paths() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.test]
source = "./test"
runtime = "wasm"
description = "Test"
"#,
    )
    .unwrap();

    // Test with spaces in output directory name
    let output_dir = temp.path().join("skills with spaces");

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .assert();

    // Should handle spaces correctly
    if output_dir.exists() {
        println!("Successfully handled spaces in path");
    }
}

// ============================================================================
// Error Message Quality Tests
// ============================================================================

#[test]
#[ignore] // Requires skill binary
fn test_error_message_quality() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    fs::write(
        &manifest_path,
        r#"
[skills.existing]
source = "./existing"
runtime = "wasm"
description = "An existing skill"
"#,
    )
    .unwrap();

    let result = Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--skill")
        .arg("nonexistent")
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&result.stderr);

    // Error message quality checks:
    // 1. Should explain what went wrong
    assert!(
        stderr.contains("not found") || stderr.contains("does not exist"),
        "Error should explain skill was not found"
    );

    // 2. Should mention the skill name
    assert!(
        stderr.contains("nonexistent"),
        "Error should mention the requested skill name"
    );

    // 3. Should not contain raw stack traces (unless in debug mode)
    let has_stack_trace = stderr.contains("panicked at")
        || stderr.contains("stack backtrace:")
        || stderr.contains("thread 'main'");

    if has_stack_trace {
        println!("Warning: Error message contains stack trace");
        println!("stderr: {}", stderr);
    }

    // 4. Error message should be reasonably concise
    let line_count = stderr.lines().count();
    assert!(
        line_count < 50,
        "Error message should be concise (got {} lines)",
        line_count
    );
}

#[test]
#[ignore] // Requires skill binary
fn test_helpful_error_suggestions() {
    let temp = TempDir::new().unwrap();

    // No manifest at all
    let result = Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .output()
        .unwrap();

    let stderr = String::from_utf8_lossy(&result.stderr);

    // Should suggest how to fix the issue
    let has_helpful_info = stderr.contains("create")
        || stderr.contains("initialize")
        || stderr.contains(".skill-engine.toml")
        || stderr.contains("manifest");

    assert!(
        has_helpful_info,
        "Error should provide helpful suggestions: {}",
        stderr
    );
}

// ============================================================================
// Data Edge Cases
// ============================================================================

#[test]
#[ignore] // Requires skill binary
fn test_extreme_values() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Skill name exactly 64 characters (max length)
    let long_name = "a".repeat(64);

    // Description exactly 1024 characters
    let long_desc = "d".repeat(1024);

    fs::write(
        &manifest_path,
        format!(
            r#"
[skills.{}]
source = "./test"
runtime = "wasm"
description = "{}"
"#,
            long_name, long_desc
        ),
    )
    .unwrap();

    let output_dir = temp.path().join("skills");

    // Should handle maximum length values
    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .assert();
}

#[test]
#[ignore] // Requires skill binary
fn test_unicode_in_descriptions() {
    let temp = TempDir::new().unwrap();
    let manifest_path = temp.path().join(".skill-engine.toml");

    // Unicode characters in description
    fs::write(
        &manifest_path,
        r#"
[skills.unicode-test]
source = "./test"
runtime = "wasm"
description = "Kubernetes 集群管理 🚀 Deploy to cloud"
"#,
    )
    .unwrap();

    let output_dir = temp.path().join("skills");

    Command::cargo_bin("skill")
        .unwrap()
        .current_dir(temp.path())
        .arg("claude")
        .arg("generate")
        .arg("--output")
        .arg(&output_dir)
        .assert()
        .success();

    // Verify unicode is preserved
    let skill_md = output_dir.join("unicode-test").join("SKILL.md");
    if skill_md.exists() {
        let content = fs::read_to_string(skill_md).unwrap();
        assert!(
            content.contains("集群") && content.contains("🚀"),
            "Unicode characters should be preserved"
        );
    }
}