agpm-cli 0.4.14

AGent Package Manager - A Git-based package manager for coding agents
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
//! Skill lifecycle tests.
//!
//! Tests for transitive dependencies, validation, listing, removal,
//! complete removal/reinstallation, and private patches.

use crate::common::{ManifestBuilder, TestProject};
use anyhow::Result;
use std::fs;

#[tokio::test]
async fn test_skill_with_transitive_dependencies() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create dependency resources
    source
        .add_resource(
            "agents",
            "base-agent",
            r#"---
name: Base Agent
description: A base agent for testing
---
# Base Agent
"#,
        )
        .await?;

    source
        .create_file(
            "snippets/utils.md",
            r#"---
name: Utility Snippets
description: Useful utility snippets
---
# Utility Snippets
"#,
        )
        .await?;

    // Create skill that depends on both
    source
        .create_skill(
            "complex-skill",
            r#"---
name: Complex Skill
description: A skill with dependencies
dependencies:
  agents:
    - path: agents/base-agent.md
  snippets:
    - path: snippets/utils.md
---
# Complex Skill

This skill depends on other resources.
"#,
        )
        .await?;

    source.commit_all("Add skill with dependencies")?;

    // Install the skill
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("complex-skill", |d| {
            d.source("test").path("skills/complex-skill").version("HEAD")
        })
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    project.run_agpm(&["install"])?;

    // Verify skill and its dependencies were installed
    assert!(project.project_path().join(".claude/skills/agpm/complex-skill").exists());
    assert!(project.project_path().join(".claude/agents/agpm/base-agent.md").exists());
    assert!(project.project_path().join(".claude/snippets/agpm/utils.md").exists());
    Ok(())
}

#[tokio::test]
async fn test_skill_validation() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create a valid skill
    source
        .create_skill(
            "valid-skill",
            r#"---
name: Valid Skill
description: A properly formatted skill
---
# Valid Skill
"#,
        )
        .await?;

    source.commit_all("Add valid skill")?;

    // Create manifest
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("valid-skill", |d| d.source("test").path("skills/valid-skill").version("HEAD"))
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    // Install the skill
    project.run_agpm(&["install"])?;

    // Run validation to verify the installation
    let result = project.run_agpm(&["validate", "--paths"])?;
    assert!(result.success);

    // Verify skill was installed correctly
    assert!(project.project_path().join(".claude/skills/agpm/valid-skill").exists());
    Ok(())
}

#[tokio::test]
async fn test_skill_list_command() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create skills
    source
        .create_skill(
            "skill-a",
            r#"---
name: Skill A
description: First skill for listing
---
# Skill A
"#,
        )
        .await?;

    source
        .create_skill(
            "skill-b",
            r#"---
name: Skill B
description: Second skill for listing
---
# Skill B
"#,
        )
        .await?;

    source.commit_all("Add skills for listing")?;

    // Create manifest
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("skill-a", |d| d.source("test").path("skills/skill-a").version("HEAD"))
        .add_skill("skill-b", |d| d.source("test").path("skills/skill-b").version("HEAD"))
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    // Install skills
    project.run_agpm(&["install"])?;

    // List skills (use --type skill since there's no --skills flag)
    let result = project.run_agpm(&["list", "--type", "skill"])?;
    assert!(
        result.success,
        "list command failed: stdout={}, stderr={}",
        result.stdout, result.stderr
    );
    assert!(result.stdout.contains("skill-a"), "skill-a not found in stdout: {}", result.stdout);
    assert!(result.stdout.contains("skill-b"), "skill-b not found in stdout: {}", result.stdout);
    Ok(())
}

#[tokio::test]
async fn test_remove_skill() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create a skill
    source
        .create_skill(
            "removable-skill",
            r#"---
name: Removable Skill
description: A skill that can be removed
---
# Removable Skill
"#,
        )
        .await?;

    source.commit_all("Add removable skill")?;

    // Create manifest and install
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("removable-skill", |d| {
            d.source("test").path("skills/removable-skill").version("HEAD")
        })
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    project.run_agpm(&["install"])?;

    // Verify skill is installed
    assert!(project.project_path().join(".claude/skills/agpm/removable-skill").exists());

    // Remove skill from manifest
    project.run_agpm(&["remove", "dep", "skill", "removable-skill"])?;

    // Verify skill was removed from manifest
    // Test assertion: agpm.toml must exist after successful agpm command execution
    let manifest_content = fs::read_to_string(project.project_path().join("agpm.toml")).unwrap();
    assert!(!manifest_content.contains("removable-skill"));
    Ok(())
}

#[tokio::test]
async fn test_skill_complete_removal_and_reinstallation() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create a skill with multiple files for comprehensive testing
    source
        .create_skill(
            "comprehensive-skill",
            r#"---
name: Comprehensive Test Skill
description: A skill with multiple files for testing complete removal
model: claude-3-opus
temperature: "0.7"
---
# Comprehensive Test Skill

This skill tests complete removal and reinstallation.
"#,
        )
        .await?;

    // Add additional files to the skill directory
    let skill_source_dir = source.path.join("skills").join("comprehensive-skill");
    fs::write(skill_source_dir.join("config.json"), r#"{"setting": "value"}"#)?;
    fs::write(skill_source_dir.join("script.sh"), "#!/bin/bash\necho 'Hello World'")?;

    // Create a subdirectory with nested content
    fs::create_dir_all(skill_source_dir.join("utils"))?;
    fs::write(skill_source_dir.join("utils/helper.txt"), "Helper content")?;

    source.commit_all("Add comprehensive skill with multiple files")?;

    // Install the skill
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("comprehensive-skill", |d| {
            d.source("test").path("skills/comprehensive-skill").version("HEAD")
        })
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    project.run_agpm(&["install"])?;

    // Verify skill was installed completely
    let skill_path = project.project_path().join(".claude/skills/agpm/comprehensive-skill");
    assert!(skill_path.exists(), "Skill directory should exist after installation");
    assert!(skill_path.is_dir(), "Skill should be a directory");
    assert!(skill_path.join("SKILL.md").exists(), "SKILL.md should exist");
    assert!(skill_path.join("config.json").exists(), "config.json should exist");
    assert!(skill_path.join("script.sh").exists(), "script.sh should exist");
    assert!(skill_path.join("utils/helper.txt").exists(), "Nested file should exist");

    // Verify skill appears in lockfile with checksum
    let lockfile_content = project.read_lockfile().await?;
    assert!(lockfile_content.contains("comprehensive-skill"), "Skill should be in lockfile");
    assert!(lockfile_content.contains("checksum = \"sha256:"), "Skill should have checksum");

    // Add an extra file directly to installed directory (should be removed during reinstallation)
    fs::write(skill_path.join("extra-file.txt"), "This should be removed")?;
    assert!(skill_path.join("extra-file.txt").exists(), "Extra file should exist initially");

    // Remove the skill from the manifest
    project.run_agpm(&["remove", "dep", "skill", "comprehensive-skill"])?;

    // Verify complete removal: directory should be gone
    assert!(!skill_path.exists(), "Skill directory should be completely removed after removal");
    assert!(
        !project.project_path().join(".claude/skills/agpm/comprehensive-skill").exists(),
        "Skill directory should not exist in any form"
    );

    // Verify skill was removed from manifest
    // Test assertion: agpm.toml must exist after successful agpm remove command
    let updated_manifest = fs::read_to_string(project.project_path().join("agpm.toml")).unwrap();
    assert!(
        !updated_manifest.contains("comprehensive-skill"),
        "Skill should be removed from manifest"
    );

    // Verify skill was removed from lockfile
    let updated_lockfile = project.read_lockfile().await?;
    assert!(
        !updated_lockfile.contains("comprehensive-skill"),
        "Skill should be removed from lockfile"
    );

    // Verify no artifacts remain - the entire skills directory structure for this skill should be gone
    let skills_dir = project.project_path().join(".claude/skills/agpm");
    if skills_dir.exists() {
        let entries: Vec<_> = fs::read_dir(skills_dir)?.collect::<Result<Vec<_>, _>>()?;
        assert!(
            !entries.iter().any(|entry| {
                entry.file_name().to_string_lossy().contains("comprehensive-skill")
            }),
            "No skill-related artifacts should remain"
        );
    }

    // Now re-add the skill to the manifest and reinstall
    let reinstallation_manifest = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("comprehensive-skill", |d| {
            d.source("test").path("skills/comprehensive-skill").version("HEAD")
        })
        .with_claude_code_tool()
        .build();
    project.write_manifest(&reinstallation_manifest).await?;

    project.run_agpm(&["install"])?;

    // Verify successful reinstallation
    assert!(skill_path.exists(), "Skill directory should exist after reinstallation");
    assert!(skill_path.is_dir(), "Skill should be a directory after reinstallation");
    assert!(skill_path.join("SKILL.md").exists(), "SKILL.md should exist after reinstallation");
    assert!(
        skill_path.join("config.json").exists(),
        "config.json should exist after reinstallation"
    );
    assert!(skill_path.join("script.sh").exists(), "script.sh should exist after reinstallation");
    assert!(
        skill_path.join("utils/helper.txt").exists(),
        "Nested file should exist after reinstallation"
    );

    // Verify extra file was removed during reinstallation (clean reinstall)
    assert!(
        !skill_path.join("extra-file.txt").exists(),
        "Extra file should be removed during clean reinstallation"
    );

    // Verify skill appears back in lockfile with new checksum
    let final_lockfile = project.read_lockfile().await?;
    assert!(final_lockfile.contains("comprehensive-skill"), "Skill should be back in lockfile");
    assert!(
        final_lockfile.contains("checksum = \"sha256:"),
        "Skill should have checksum after reinstallation"
    );

    // Verify content integrity after reinstallation
    // Test assertion: SKILL.md must exist after successful reinstallation (verified by asserts above)
    let skill_content = fs::read_to_string(skill_path.join("SKILL.md")).unwrap();
    assert!(skill_content.contains("Comprehensive Test Skill"), "Skill content should be correct");

    // Test assertion: config.json must exist after successful reinstallation (verified by asserts above)
    let config_content = fs::read_to_string(skill_path.join("config.json")).unwrap();
    assert!(config_content.contains("\"setting\""), "Config file should be correct");

    // Test assertion: utils/helper.txt must exist after successful reinstallation (verified by asserts above)
    let helper_content = fs::read_to_string(skill_path.join("utils/helper.txt")).unwrap();
    assert_eq!(helper_content, "Helper content", "Nested file content should be correct");

    Ok(())
}

#[tokio::test]
async fn test_skill_with_private_patches() -> Result<()> {
    let project = TestProject::new().await?;
    let source = project.create_source_repo("test").await?;

    // Create a skill
    source
        .create_skill(
            "patchable-skill",
            r#"---
name: Patchable Skill
description: A skill for testing private patches
model: claude-3-opus
---
# Patchable Skill
"#,
        )
        .await?;

    source.commit_all("Add patchable skill")?;

    // Create manifest with project patches
    let source_url = source.bare_file_url(project.sources_path()).await?;
    let manifest_content = ManifestBuilder::new()
        .add_source("test", &source_url)
        .add_skill("patchable-skill", |d| {
            d.source("test").path("skills/patchable-skill").version("HEAD")
        })
        .with_claude_code_tool()
        .build();
    project.write_manifest(&manifest_content).await?;

    // Create project patches
    let project_patches = r#"
[patch.skills.patchable-skill]
model = "claude-3-sonnet"
temperature = "0.5"
"#;
    fs::write(
        project.project_path().join("agpm.toml"),
        format!("{}\n{}", manifest_content, project_patches),
    )?;

    // Create private patches file
    let private_patches = r#"
[patch.skills.patchable-skill]
temperature = "0.9"
max_tokens = 1000
"#;
    fs::write(project.project_path().join("agpm.private.toml"), private_patches)?;

    // Install with both project and private patches
    project.run_agpm(&["install"])?;

    // Verify patches were applied
    let skill_path = project.project_path().join(".claude/skills/agpm/patchable-skill");
    // Test assertion: SKILL.md must exist after successful installation (directory created by install)
    let content = fs::read_to_string(skill_path.join("SKILL.md")).unwrap();

    // Project patch should be overridden by private patch
    assert!(content.contains("model: claude-3-sonnet"));
    assert!(content.contains("temperature: '0.9'")); // Private wins
    assert!(content.contains("max_tokens: 1000")); // Private only
    Ok(())
}