agpm-cli 0.4.9

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
use predicates::prelude::*;
use tokio::fs;

use crate::common::{DirAssert, FileAssert, ManifestBuilder, TestProject};
use crate::fixtures::ManifestFixture;

/// Test installing from a manifest when no lockfile exists
#[tokio::test]
async fn test_install_creates_lockfile() {
    let project = TestProject::new().await.unwrap();

    // Create mock source repositories - can reuse standard repo with different dependency names
    let (_official_repo, official_url) = project.create_standard_v1_repo("official").await.unwrap();
    let community_repo = project.create_source_repo("community").await.unwrap();
    // Create a different agent to avoid path conflicts
    community_repo
        .add_resource("agents", "helper", "# Helper Agent\n\nA helper agent")
        .await
        .unwrap();
    community_repo.commit_all("Add helper").unwrap();
    community_repo.tag_version("v1.0.0").unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).unwrap();

    // Create manifest using ManifestBuilder
    let manifest = ManifestBuilder::new()
        .add_sources(&[("official", &official_url), ("community", &community_url)])
        .add_standard_agent("my-agent", "official", "agents/test-agent.md")
        .add_standard_agent("helper", "community", "agents/helper.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install command
    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    output.assert_success();
    assert!(
        output.stdout.contains("Installing")
            || output.stdout.contains("Cloning")
            || output.stdout.contains("Installed"),
        "Expected install progress message, got: {}",
        output.stdout
    );

    // Verify lockfile was created
    let lockfile_path = project.project_path().join("agpm.lock");
    FileAssert::exists(&lockfile_path).await;

    // Verify lockfile content structure
    let lockfile_content = fs::read_to_string(&lockfile_path).await.unwrap();
    assert!(lockfile_content.contains("version = 1"));
    assert!(lockfile_content.contains("[[sources]]"));
    assert!(lockfile_content.contains("[[agents]]"));
    assert!(lockfile_content.contains("my-agent"));
    assert!(lockfile_content.contains("helper"));

    // Verify agents were installed
    let agents_dir = project.project_path().join(".claude/agents");
    assert!(agents_dir.join("test-agent.md").exists());
    assert!(agents_dir.join("helper.md").exists());
}

/// Test installing when lockfile already exists
#[tokio::test]
async fn test_install_with_existing_lockfile() {
    let project = TestProject::new().await.unwrap();

    // Create mock source repositories
    let official_repo = project.create_source_repo("official").await.unwrap();
    official_repo.add_resource("agents", "my-agent", "# My Agent\n\nA test agent").await.unwrap();
    official_repo.commit_all("Add my agent").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();
    let official_url = official_repo.bare_file_url(project.sources_path()).unwrap();
    let official_sha = official_repo.git.get_commit_hash().unwrap();

    let community_repo = project.create_source_repo("community").await.unwrap();
    community_repo
        .add_resource("agents", "helper", "# Helper Agent\n\nA helper agent")
        .await
        .unwrap();
    community_repo.commit_all("Add helper agent").unwrap();
    community_repo.tag_version("v1.0.0").unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).unwrap();
    let community_sha = community_repo.git.get_commit_hash().unwrap();

    // Create manifest using ManifestBuilder
    let manifest = ManifestBuilder::new()
        .add_sources(&[("official", &official_url), ("community", &community_url)])
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .add_standard_agent("helper", "community", "agents/helper.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Create a matching lockfile with the actual commit SHAs
    let lockfile_content = format!(
        r#"# Auto-generated lockfile - DO NOT EDIT
version = 1

[[sources]]
name = "official"
url = "{}"
commit = "{}"
fetched_at = "2024-01-01T00:00:00Z"

[[sources]]
name = "community"
url = "{}"
commit = "{}"
fetched_at = "2024-01-01T00:00:00Z"

[[agents]]
name = "my-agent"
source = "official"
url = "{}"
path = "agents/my-agent.md"
version = "v1.0.0"
resolved_commit = "{}"
checksum = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
installed_at = ".claude/agents/my-agent.md"
artifact_type = "claude-code"

[[agents]]
name = "helper"
source = "community"
url = "{}"
path = "agents/helper.md"
version = "v1.0.0"
resolved_commit = "{}"
checksum = "sha256:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"
installed_at = ".claude/agents/helper.md"
artifact_type = "claude-code"
"#,
        official_url,
        official_sha,
        community_url,
        community_sha,
        official_url,
        official_sha, // my-agent url and commit
        community_url,
        community_sha // helper url and commit
    );

    fs::write(project.project_path().join("agpm.lock"), lockfile_content).await.unwrap();

    // Run install command (with --frozen to use existing lockfile without updating)
    let output = project.run_agpm(&["install", "--frozen"]).unwrap();
    output.assert_success();
    assert!(
        output.stdout.contains("Installing")
            || output.stdout.contains("Cloning")
            || output.stdout.contains("Installed"),
        "Expected install progress message, got: {}",
        output.stdout
    );

    // Verify agents directory was created and populated (prefix stripped)
    let agents_dir = project.project_path().join(".claude").join("agents");
    DirAssert::exists(&agents_dir).await;
    DirAssert::contains_file(&agents_dir, "my-agent.md").await;
    DirAssert::contains_file(&agents_dir, "helper.md").await;
}

/// Test install command without agpm.toml
#[tokio::test]
async fn test_install_without_manifest() {
    let project = TestProject::new().await.unwrap();

    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("No agpm.toml found"),
        "Expected manifest not found error, got: {}",
        output.stderr
    );
}

/// Test install with invalid manifest syntax
#[tokio::test]
async fn test_install_invalid_manifest_syntax() {
    let project = TestProject::new().await.unwrap();
    let manifest_content = ManifestFixture::invalid_syntax().content;
    project.write_manifest(&manifest_content).await.unwrap();

    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("Invalid manifest file syntax"),
        "Expected syntax error, got: {}",
        output.stderr
    );
}

/// Test install with missing required fields in manifest
#[tokio::test]
async fn test_install_missing_manifest_fields() {
    let project = TestProject::new().await.unwrap();
    let manifest_content = ManifestFixture::missing_fields().content;
    project.write_manifest(&manifest_content).await.unwrap();

    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("Missing required field"),
        "Expected missing field error, got: {}",
        output.stderr
    );
}

/// Test install with --parallel flag
#[tokio::test]
async fn test_install_parallel_flag() {
    let project = TestProject::new().await.unwrap();

    // Create source repository
    let official_repo = project.create_source_repo("official").await.unwrap();
    official_repo.add_resource("agents", "my-agent", "# My Agent\n\nA test agent").await.unwrap();
    official_repo.commit_all("Add agent").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();
    let official_url = official_repo.bare_file_url(project.sources_path()).unwrap();

    let community_repo = project.create_source_repo("community").await.unwrap();
    community_repo
        .add_resource("agents", "helper", "# Helper Agent\n\nA helper agent")
        .await
        .unwrap();
    community_repo.commit_all("Add helper agent").unwrap();
    community_repo.tag_version("v1.0.0").unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).unwrap();

    // Create manifest using ManifestBuilder
    let manifest = ManifestBuilder::new()
        .add_sources(&[("official", &official_url), ("community", &community_url)])
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .add_standard_agent("helper", "community", "agents/helper.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install command
    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    output.assert_success();
    assert!(
        output.stdout.contains("Installing")
            || output.stdout.contains("Cloning")
            || output.stdout.contains("Installed"),
        "Expected install progress message, got: {}",
        output.stdout
    );

    // Verify that files were installed
    let agents_dir = project.project_path().join(".claude").join("agents");
    assert!(agents_dir.join("my-agent.md").exists());
    assert!(agents_dir.join("helper.md").exists());
}

/// Test install with local dependencies
#[tokio::test]
async fn test_install_local_dependencies() {
    let project = TestProject::new().await.unwrap();

    // Create local files referenced in manifest
    // The manifest expects ../local-agents/helper.md relative to project
    let parent_dir = project.project_path().parent().unwrap();
    let local_agents_dir = parent_dir.join("local-agents");
    fs::create_dir_all(&local_agents_dir).await.unwrap();
    fs::write(local_agents_dir.join("helper.md"), "# Local Agent Helper\n\nThis is a local agent.")
        .await
        .unwrap();

    // Create local snippet in project directory
    project
        .create_local_resource(
            "snippets/local-utils.md",
            "# Local Utils\n\nThis is a local snippet.",
        )
        .await
        .unwrap();

    // Add official source for the remote dependency using new helper
    let (_official_repo, official_url) = project.create_standard_v1_repo("official").await.unwrap();

    // Create manifest with ManifestBuilder showing local + remote dependencies
    let manifest = ManifestBuilder::new()
        .add_source("official", &official_url)
        .add_standard_agent("my-agent", "official", "agents/test-agent.md")
        .add_agent("local-agent", |d| d.path("../local-agents/helper.md").flatten(false))
        .add_local_snippet("local-utils", "./snippets/local-utils.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install command
    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    output.assert_success();
    assert!(
        output.stdout.contains("Installing")
            || output.stdout.contains("Cloning")
            || output.stdout.contains("Installed"),
        "Expected install progress message, got: {}",
        output.stdout
    );

    // Verify lockfile was created and contains all dependencies
    let lockfile_content =
        fs::read_to_string(project.project_path().join("agpm.lock")).await.unwrap();
    assert!(lockfile_content.contains("my-agent")); // remote dependency
    assert!(lockfile_content.contains("local-agent")); // local dependency
    assert!(lockfile_content.contains("local-utils")); // local dependency

    // Verify all dependencies were installed
    let agents_dir = project.project_path().join(".claude").join("agents");
    assert!(agents_dir.join("test-agent.md").exists()); // From create_standard_v1_repo

    // Local files preserve their relative path structure (after stripping ../)
    // ../local-agents/helper.md becomes local-agents/helper.md, installed to .claude/agents/local-agents/helper.md
    let local_helper_path = project.project_path().join(".claude/agents/local-agents/helper.md");
    assert!(local_helper_path.exists(), "Local helper should be at {:?}", local_helper_path);

    // Snippets now default to .agpm/snippets (agpm artifact type)
    // ./snippets/local-utils.md has ./ stripped, then snippets/ stripped → local-utils.md
    let local_utils_path = project.project_path().join(".agpm/snippets/local-utils.md");
    assert!(local_utils_path.exists(), "Local utils should be at {:?}", local_utils_path);
}

/// Test install with verbose output
#[tokio::test]
async fn test_install_verbose() {
    let project = TestProject::new().await.unwrap();

    // Create mock source with required file
    let official_repo = project.create_source_repo("official").await.unwrap();
    official_repo.add_resource("agents", "my-agent", "# My Agent\n\nA test agent").await.unwrap();
    official_repo.commit_all("Add my agent").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();
    let official_url = official_repo.bare_file_url(project.sources_path()).unwrap();

    // Create manifest using ManifestBuilder
    let manifest = ManifestBuilder::new()
        .add_source("official", &official_url)
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install command with verbose flag
    let output = project.run_agpm(&["install", "--no-cache", "--verbose"]).unwrap();
    output.assert_success();
    assert!(
        output.stdout.contains("Installing")
            || output.stdout.contains("Cloning")
            || output.stdout.contains("Installed"),
        "Expected install progress message, got: {}",
        output.stdout
    );
}

/// Test install with quiet output
#[tokio::test]
async fn test_install_quiet() {
    let project = TestProject::new().await.unwrap();

    // Create mock source repository
    let official_repo = project.create_source_repo("official").await.unwrap();
    official_repo.add_resource("agents", "my-agent", "# My Agent\n\nA test agent").await.unwrap();
    official_repo.commit_all("Add my agent").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();
    let official_url = official_repo.bare_file_url(project.sources_path()).unwrap();

    // Create manifest using ManifestBuilder
    let manifest = ManifestBuilder::new()
        .add_source("official", &official_url)
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install command with quiet flag
    let output = project.run_agpm(&["install", "--no-cache", "--quiet"]).unwrap();
    output.assert_success();
}

/// Test install with network simulation failure
#[tokio::test]
async fn test_install_network_failure() {
    let project = TestProject::new().await.unwrap();

    // Create a manifest with non-existent local sources to simulate failure
    let manifest_content = r#"
[sources]
official = "file:///non/existent/path/to/repo"

[agents]
my-agent = { source = "official", path = "agents/my-agent.md", version = "v1.0.0" }
"#;
    project.write_manifest(manifest_content).await.unwrap();

    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("Failed to clone")
            || output.stderr.contains("does not exist")
            || output.stderr.contains("Local repository path does not exist"),
        "Expected clone failure error, got: {}",
        output.stderr
    );
}

/// Test install help command
#[tokio::test]
async fn test_install_help() {
    let mut cmd = assert_cmd::Command::cargo_bin("agpm").unwrap();
    cmd.arg("install")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Install Claude Code resources from manifest"))
        .stdout(predicate::str::contains("--max-parallel"))
        .stdout(predicate::str::contains("--no-lock"))
        .stdout(predicate::str::contains("--frozen"));
}

/// Test install with corrupted lockfile
#[tokio::test]
async fn test_install_corrupted_lockfile() {
    let project = TestProject::new().await.unwrap();
    let manifest_content = ManifestFixture::basic().content;
    project.write_manifest(&manifest_content).await.unwrap();

    // Create corrupted lockfile
    fs::write(project.project_path().join("agpm.lock"), "corrupted content").await.unwrap();

    let output = project.run_agpm(&["install", "--no-cache"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("Invalid or corrupted lockfile detected")
            || output.stderr.contains("Invalid lockfile syntax")
            || output.stderr.contains("Failed to parse lockfile"),
        "Expected lockfile error, got: {}",
        output.stderr
    );
}