claude_assets 1.5.1

CLI for installing Claude Code artifacts (rules, skills, commands) via symlinks
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
//! CLI integration tests for `claude_assets` (`cla` binary).
// allow: test doc comments reference many function names; backtick-wrapping all is noisy
#![ allow( clippy::doc_markdown ) ]
//!
//! ## Test Coverage
//!
//! | TC | Scenario | Expected |
//! |----|----------|----------|
//! | cli01 | `cla .kinds` with PRO_CLAUDE set | Exit 0; ≥6 lines showing kinds |
//! | cli02 | `cla .list` with PRO_CLAUDE unset | Exit 2; error mentions PRO_CLAUDE |
//! | cli03 | `cla .list kind::rule` with source dir empty | Exit 0; "No artifacts" message |
//! | cli04 | `cla .install kind::rule name::rust` | Exit 0; creates symlink |
//! | cli05 | `cla .install kind::rule name::nonexistent` | Exit 2; "not found" error |
//! | cli06 | `cla .install kind::rule name::rust` second time | Exit 0; "Reinstalled" message |
//! | cli07 | `cla .uninstall kind::rule name::rust` installed | Exit 0; "Uninstalled" message |
//! | cli08 | `cla .uninstall kind::rule name::rust` not installed | Exit 0; "Not installed" message |
//! | cli09 | `cla .list kind::rule` with 2 source, 1 installed | Exit 0; ● and ○ markers |
//! | cli10 | `cla .install` without kind:: | Exit 1; error mentions kind:: |
//! | cli11 | `cla .install kind::invalid name::x` | Exit 1; "unknown kind" error |
//! | cli12 | `cla .list installed::true` | Exit 1; error says expected 0 or 1 |
//! | cli13 | `cla list` (no dot) | Exit 1; error says must start with '.' |

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

// ── helpers ───────────────────────────────────────────────────────────────────

fn cla() -> Command
{
  Command::cargo_bin( "cla" ).unwrap()
}

/// Write a dummy `.md` source file for `kind`/`name` in `src_dir/<kind>s/`.
fn write_source_file( src_dir : &std::path::Path, kind : &str, name : &str )
{
  let dir = src_dir.join( format!( "{kind}s" ) );
  fs::create_dir_all( &dir ).unwrap();
  fs::write( dir.join( format!( "{name}.md" ) ), b"# test" ).unwrap();
}

// ── cli01 ─────────────────────────────────────────────────────────────────────

/// cli01: `.kinds` with PRO_CLAUDE set exits 0 and shows all 6 kinds.
///
/// Root Cause: kinds_routine must succeed even when .claude/ is absent.
/// Why Not Caught: no test existed.
/// Fix Applied: kinds_routine reads only env var, no filesystem access required.
/// Prevention: always run .kinds as a smoke test after any routine change.
/// Pitfall: if PRO_CLAUDE is set to a nonexistent path, .kinds still works (display only).
#[ test ]
fn cli01_kinds_exits_0_with_six_kinds()
{
  let dir = TempDir::new().unwrap();
  let out = cla()
    .args( [ ".kinds" ] )
    .env( "PRO_CLAUDE", dir.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  let lines : Vec< _ > = stdout.lines().filter( |l| !l.trim().is_empty() ).collect();
  assert!( lines.len() >= 6, "must show at least 6 kinds, got {}: {stdout}", lines.len() );
  assert!( stdout.contains( "rule" ),    "must mention rule, got: {stdout}" );
  assert!( stdout.contains( "command" ), "must mention command, got: {stdout}" );
}

// ── cli02 ─────────────────────────────────────────────────────────────────────

/// cli02: `.list` without PRO_CLAUDE exits 2 with actionable error.
///
/// Root Cause: AssetPaths::from_env() must return a typed error, not panic.
/// Why Not Caught: no test existed.
/// Fix Applied: from_env() returns AssetPathsError::EnvVarNotSet; mapped to InternalError (exit 2).
/// Prevention: always test with both PRO_CLAUDE and PRO unset.
/// Pitfall: PRO may be set in CI; clear both vars explicitly.
#[ test ]
fn cli02_list_without_env_exits_2()
{
  let out = cla()
    .args( [ ".list" ] )
    .env_remove( "PRO_CLAUDE" )
    .env_remove( "PRO" )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 2 ), "exit must be 2, got: {:?}", out.status );
  let stderr = String::from_utf8_lossy( &out.stderr );
  assert!(
    stderr.contains( "PRO_CLAUDE" ),
    "error must mention PRO_CLAUDE, got: {stderr}",
  );
}

// ── cli03 ─────────────────────────────────────────────────────────────────────

/// cli03: `.list kind::rule` with empty source dir exits 0 and says "No artifacts".
///
/// Root Cause: graceful degradation — missing source dir must not be an error.
/// Why Not Caught: no test existed.
/// Fix Applied: list_available() returns empty vec when source dir absent.
/// Prevention: always test with a fresh, empty PRO_CLAUDE dir.
/// Pitfall: if .list returns exit 1 here, `cla .list` fails for new repos.
#[ test ]
fn cli03_list_empty_source_exits_0()
{
  let dir = TempDir::new().unwrap();
  let out = cla()
    .args( [ ".list", "kind::rule" ] )
    .env( "PRO_CLAUDE", dir.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!(
    stdout.contains( "No artifacts" ),
    "must say 'No artifacts', got: {stdout}",
  );
}

// ── cli04 ─────────────────────────────────────────────────────────────────────

/// cli04: `.install kind::rule name::rust` creates a symlink; exit 0.
///
/// Root Cause: install must use symlink(), not copy().
/// Why Not Caught: no test existed.
/// Fix Applied: install() calls create_symlink() — dispatches to the correct platform API.
/// Prevention: verify symlink with read_link() after install.
/// Pitfall: stat shows same content for copy and symlink; read_link() distinguishes.
#[ test ]
fn cli04_install_creates_symlink()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();
  write_source_file( src.path(), "rule", "rust" );

  let out = cla()
    .args( [ ".install", "kind::rule", "name::rust" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status.code() );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!( stdout.contains( "Installed" ) || stdout.contains( "install" ), "must confirm install, got: {stdout}" );

  let link = tgt.path().join( ".claude" ).join( "rules" ).join( "rust.md" );
  assert!( fs::read_link( &link ).is_ok(), "target must be a symlink, got: {link:?}" );
}

// ── cli05 ─────────────────────────────────────────────────────────────────────

/// cli05: `.install kind::rule name::nonexistent` exits 2 with "not found" error.
///
/// Root Cause: install must fail clearly when source artifact is absent.
/// Why Not Caught: no test existed.
/// Fix Applied: install() returns AssetError::SourceNotFound for absent source.
/// Prevention: always test with a name that doesn't exist in the source.
/// Pitfall: if exit code is 1 (not 2), ast scripts may misinterpret as usage error.
#[ test ]
fn cli05_install_nonexistent_exits_2()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ ".install", "kind::rule", "name::nonexistent" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 2 ), "exit must be 2, got: {:?}", out.status );
  let stderr = String::from_utf8_lossy( &out.stderr );
  assert!(
    stderr.to_lowercase().contains( "not found" ) || stderr.contains( "nonexistent" ),
    "error must mention 'not found' or the name, got: {stderr}",
  );
}

// ── cli06 ─────────────────────────────────────────────────────────────────────

/// cli06: installing twice is idempotent — second call exits 0 with "Reinstalled".
///
/// Root Cause: repeated installs must succeed, not fail on existing symlink.
/// Why Not Caught: no test existed.
/// Fix Applied: install() removes and recreates the symlink on second call.
/// Prevention: always run install twice and assert both succeed.
/// Pitfall: if second call errors, automation scripts that `cla .install` unconditionally will break.
#[ test ]
fn cli06_install_idempotent_reinstalls()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();
  write_source_file( src.path(), "rule", "go" );

  // First install.
  cla()
    .args( [ ".install", "kind::rule", "name::go" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .assert()
    .success();

  // Second install — must be idempotent.
  let out = cla()
    .args( [ ".install", "kind::rule", "name::go" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "second install must exit 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!(
    stdout.contains( "Reinstalled" ) || stdout.contains( "install" ),
    "must confirm reinstall, got: {stdout}",
  );
}

// ── cli07 ─────────────────────────────────────────────────────────────────────

/// cli07: `.uninstall kind::rule name::rust` on installed artifact exits 0 with "Uninstalled".
///
/// Root Cause: uninstall must remove symlink and confirm removal.
/// Why Not Caught: no test existed.
/// Fix Applied: uninstall() removes symlink and returns Uninstalled action.
/// Prevention: verify symlink is absent after uninstall.
/// Pitfall: if uninstall exits 2, it may be confused with a data-unavailable error.
#[ test ]
fn cli07_uninstall_installed_artifact()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();
  write_source_file( src.path(), "rule", "rust" );

  // Install first.
  cla()
    .args( [ ".install", "kind::rule", "name::rust" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .assert()
    .success();

  let link = tgt.path().join( ".claude" ).join( "rules" ).join( "rust.md" );
  assert!( fs::symlink_metadata( &link ).is_ok(), "symlink must exist before uninstall" );

  // Uninstall.
  let out = cla()
    .args( [ ".uninstall", "kind::rule", "name::rust" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!(
    stdout.contains( "Uninstalled" ) || stdout.contains( "uninstall" ),
    "must confirm uninstall, got: {stdout}",
  );
  assert!( !link.exists(), "symlink must be absent after uninstall" );
}

// ── cli08 ─────────────────────────────────────────────────────────────────────

/// cli08: `.uninstall kind::rule name::rust` when not installed exits 0 (not error).
///
/// Root Cause: uninstall of absent artifact must be idempotent (not error).
/// Why Not Caught: no test existed.
/// Fix Applied: uninstall() returns NotInstalled action for absent path.
/// Prevention: always test uninstall on a never-installed name.
/// Pitfall: if uninstall exits 2 here, `make clean` style scripts break.
#[ test ]
fn cli08_uninstall_not_installed_exits_0()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ ".uninstall", "kind::rule", "name::ghost" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!(
    stdout.contains( "Not installed" ) || stdout.contains( "not installed" ),
    "must say 'Not installed', got: {stdout}",
  );
}

// ── cli09 ─────────────────────────────────────────────────────────────────────

/// cli09: `.list kind::rule` shows ● for installed and ○ for available.
///
/// Root Cause: list_all() must merge available and installed with correct markers.
/// Why Not Caught: no test existed.
/// Fix Applied: list_routine() prints "●" for Installed and "○" for Available.
/// Prevention: install one of two rules, then assert both markers appear.
/// Pitfall: if list only shows installed, available artifacts are invisible to the user.
#[ test ]
fn cli09_list_shows_installed_and_available_markers()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();
  write_source_file( src.path(), "rule", "rust" );
  write_source_file( src.path(), "rule", "python" );

  // Install only rust.
  cla()
    .args( [ ".install", "kind::rule", "name::rust" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .assert()
    .success();

  let out = cla()
    .args( [ ".list", "kind::rule" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert!( out.status.success(), "exit must be 0, got: {:?}", out.status );
  let stdout = String::from_utf8_lossy( &out.stdout );
  assert!( stdout.contains( '' ), "must show ● for installed, got: {stdout}" );
  assert!( stdout.contains( '' ), "must show ○ for available, got: {stdout}" );
}

// ── cli10 ─────────────────────────────────────────────────────────────────────

/// cli10: `.install` without kind:: exits 1 (argument missing = usage error).
///
/// Root Cause: kind:: is required; missing it is a usage error (exit 1, not 2).
/// Why Not Caught: no test existed.
/// Fix Applied: require_str() returns ArgumentMissing (exit 1) for empty kind.
/// Prevention: always test required params with absent values.
/// Pitfall: exit 2 for missing args would be confused with runtime errors.
#[ test ]
fn cli10_install_without_kind_exits_1()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ ".install", "name::rust" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 1 ), "exit must be 1, got: {:?}", out.status );
}

// ── cli11 ─────────────────────────────────────────────────────────────────────

/// cli11: `.install kind::invalid name::x` exits 1 for unknown kind.
///
/// Root Cause: invalid kind string is a usage error (exit 1).
/// Why Not Caught: no test existed.
/// Fix Applied: parse_kind() returns ArgumentTypeMismatch (exit 1) for unknown string.
/// Prevention: always test with a kind string not in the supported set.
/// Pitfall: exit 2 for unknown kinds conflates input errors with runtime errors.
#[ test ]
fn cli11_install_invalid_kind_exits_1()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ ".install", "kind::invalid", "name::x" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 1 ), "exit must be 1, got: {:?}", out.status );
  let stderr = String::from_utf8_lossy( &out.stderr );
  assert!(
    stderr.contains( "unknown kind" ) || stderr.contains( "invalid" ),
    "must mention unknown kind, got: {stderr}",
  );
}

// ── cli12 ─────────────────────────────────────────────────────────────────────

/// cli12: `.list installed::true` exits 1 — boolean expects `0` or `1`, not `"true"`.
///
/// Root Cause: unilang bool params accept only 0/1 integer tokens, not string "true"/"false".
/// Why Not Caught: no test for boolean argument validation existed.
/// Fix Applied: adapter normalise_bool_value() rejects any value other than "0" or "1".
/// Prevention: test every boolean param with a plausible-but-wrong string value.
/// Pitfall: users familiar with other CLIs expect "true" to work — error must be clear.
#[ test ]
fn cli12_installed_true_string_exits_1()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ ".list", "installed::true" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 1 ), "exit must be 1, got: {:?}", out.status );
  let stderr = String::from_utf8_lossy( &out.stderr );
  assert!(
    stderr.contains( "expected 0 or 1" ),
    "error must say 'expected 0 or 1', got: {stderr}",
  );
}

// ── cli13 ─────────────────────────────────────────────────────────────────────

/// cli13: `list` (no dot prefix) exits 1 — commands must start with `.`.
///
/// Root Cause: adapter enforces dot-prefix as a namespace invariant for commands.
/// Why Not Caught: no test for bare (undotted) command names existed.
/// Fix Applied: argv_to_unilang_tokens() rejects first arg without `.` prefix.
/// Prevention: test the bare name of every registered command.
/// Pitfall: error should hint at the correct form (`.list`) for discoverability.
#[ test ]
fn cli13_bare_command_without_dot_exits_1()
{
  let src = TempDir::new().unwrap();
  let tgt = TempDir::new().unwrap();

  let out = cla()
    .args( [ "list" ] )
    .env( "PRO_CLAUDE", src.path() )
    .current_dir( tgt.path() )
    .output()
    .unwrap();

  assert_eq!( out.status.code(), Some( 1 ), "exit must be 1, got: {:?}", out.status );
  let stderr = String::from_utf8_lossy( &out.stderr );
  assert!(
    stderr.contains( "start with '.'" ) || stderr.contains( "list" ),
    "error must mention dot prefix, got: {stderr}",
  );
}