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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use agpm_cli::utils::normalize_path_for_storage;
use anyhow::Result;
use tokio::fs;

use crate::common::{ManifestBuilder, TestProject, TestSourceRepo};
use crate::test_config;

/// Helper to initialize a git repository with tags, branches, and commits
async fn setup_git_repo_with_versions(repo: &TestSourceRepo) -> Result<String> {
    let repo_path = &repo.path;
    let git = &repo.git;

    // Create directory structure
    fs::create_dir_all(repo_path.join("agents")).await?;
    fs::create_dir_all(repo_path.join("snippets")).await?;

    // Create v1.0.0 content
    fs::write(repo_path.join("agents/example.md"), "# Example Agent v1.0.0\nInitial version")
        .await?;
    fs::write(repo_path.join("snippets/utils.md"), "# Utils Snippet v1.0.0\nInitial version")
        .await?;

    // Commit and tag v1.0.0
    git.add_all()?;
    git.commit("Initial commit v1.0.0")?;
    git.tag("v1.0.0")?;

    // Get commit hash for v1.0.0
    let v1_commit = git.get_commit_hash()?;

    // Create v1.1.0 content
    fs::write(repo_path.join("agents/example.md"), "# Example Agent v1.1.0\nMinor update").await?;
    git.add_all()?;
    git.commit("Version 1.1.0")?;
    git.tag("v1.1.0")?;

    // Create v1.2.0 content
    fs::write(repo_path.join("agents/example.md"), "# Example Agent v1.2.0\nAnother minor update")
        .await?;
    git.add_all()?;
    git.commit("Version 1.2.0")?;
    git.tag("v1.2.0")?;

    // Create v2.0.0 content
    fs::write(repo_path.join("agents/example.md"), "# Example Agent v2.0.0\nMajor version").await?;
    git.add_all()?;
    git.commit("Version 2.0.0 - Breaking changes")?;
    git.tag("v2.0.0")?;

    // Ensure we're on 'main' branch (git's default branch name varies)
    git.ensure_branch("main")?;

    git.create_branch("develop")?;

    fs::write(
        repo_path.join("agents/example.md"),
        "# Example Agent - Development\nUnstable development version",
    )
    .await?;
    fs::write(
        repo_path.join("agents/experimental.md"),
        "# Experimental Agent\nOnly in develop branch",
    )
    .await?;

    git.add_all()?;
    git.commit("Development changes")?;

    git.create_branch("feature/new-agent")?;

    fs::write(repo_path.join("agents/feature.md"), "# Feature Agent\nNew feature in progress")
        .await?;

    git.add_all()?;
    git.commit("Add feature agent")?;

    // Ensure we're on 'main' branch (git's default branch name varies)
    git.ensure_branch("main")?;

    Ok(v1_commit)
}

#[tokio::test]
async fn test_install_with_exact_version_tag() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    // Setup git repository with versions
    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with exact version
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| d.source("versioned").path("agents/example.md").version("v1.0.0"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Run install
    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Check installed file contains v1.0.0 content
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.0.0"));
    assert!(!installed.contains("v1.1.0"));
    assert!(!installed.contains("v2.0.0"));
}

#[tokio::test]
async fn test_install_with_caret_version_range() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with caret range (^1.0.0 should match 1.2.0 but not 2.0.0)
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| d.source("versioned").path("agents/example.md").version("^1.0.0"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get v1.2.0 (highest compatible version)
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.2.0"));
    assert!(!installed.contains("v2.0.0"));
}

#[tokio::test]
async fn test_install_with_tilde_version_range() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with tilde range (~1.1.0 should match 1.1.x but not 1.2.0)
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| d.source("versioned").path("agents/example.md").version("~1.1.0"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get v1.1.0 (only patch updates allowed)
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.1.0"));
    assert!(!installed.contains("v1.2.0"));
}

#[tokio::test]
async fn test_install_with_branch_reference() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with branch reference
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("dev-example", |d| {
            d.source("versioned").path("agents/example.md").branch("develop")
        })
        .add_agent("experimental", |d| {
            d.source("versioned").path("agents/experimental.md").branch("develop")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Check we got develop branch content
    // Files use basename from path, not dependency name
    let example_content =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(example_content.contains("Development"));
    assert!(example_content.contains("Unstable"));

    // Check experimental agent exists (only in develop branch)
    assert!(project.project_path().join(".claude/agents/agpm/experimental.md").exists());
}

#[tokio::test]
async fn test_install_with_feature_branch() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with feature branch
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("feature", |d| {
            d.source("versioned").path("agents/feature.md").branch("feature/new-agent")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Check feature agent was installed
    let feature_content =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/feature.md"))
            .await
            .unwrap();
    assert!(feature_content.contains("Feature Agent"));
    assert!(feature_content.contains("New feature in progress"));
}

#[tokio::test]
async fn test_install_with_commit_hash() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    // Setup git repository and get v1.0.0 commit hash
    let v1_commit = setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with exact commit hash (rev)
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("pinned", |d| d.source("versioned").path("agents/example.md").rev(&v1_commit))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get exact v1.0.0 content
    // Files use basename from path, not dependency name
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.0.0"));
    assert!(installed.contains("Initial version"));
}

#[tokio::test]
async fn test_install_with_wildcard_version() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with wildcard "*"
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("any", |d| d.source("versioned").path("agents/example.md").version("*"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get v2.0.0 (highest available)
    // Files use basename from path, not dependency name
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v2.0.0"));
}

#[tokio::test]
async fn test_install_with_mixed_versioning_methods() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    let v1_commit = setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with mixed versioning methods
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("stable", |d| d.source("versioned").path("agents/example.md").version("v1.1.0"))
        .add_agent("compatible", |d| {
            d.source("versioned").path("agents/example.md").version("^1.0.0")
        })
        .add_agent("develop", |d| d.source("versioned").path("agents/example.md").branch("develop"))
        .add_agent("pinned", |d| d.source("versioned").path("agents/example.md").rev(&v1_commit))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    // Should fail due to version conflict - same resource with different versions
    assert!(!output.success, "Expected install to fail due to version conflicts");

    // Verify error message mentions the conflict
    assert!(
        output.stderr.contains("Version conflicts")
            || output.stderr.contains("Unresolvable SHA conflicts"),
        "Expected version conflict, got: {}",
        output.stderr
    );
    assert!(output.stderr.contains("agents/example"));
}

#[tokio::test]
async fn test_version_constraint_with_greater_than() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Test >=1.1.0 constraint
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| {
            d.source("versioned").path("agents/example.md").version(">=1.1.0")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get v2.0.0 (highest that satisfies >=1.1.0)
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v2.0.0"));
}

#[tokio::test]
async fn test_version_constraint_with_range() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Test complex range: >=1.1.0, <2.0.0
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| {
            d.source("versioned").path("agents/example.md").version(">=1.1.0, <2.0.0")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Should get v1.2.0 (highest that satisfies the range)
    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.2.0"));
    assert!(!installed.contains("v2.0.0"));
}

#[tokio::test]
async fn test_update_branch_reference() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with branch reference
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("dev", |d| d.source("versioned").path("agents/example.md").branch("develop"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Initial install
    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    // Modify the develop branch
    source_repo.git.checkout("develop").unwrap();

    fs::write(
        source_repo.path.join("agents/example.md"),
        "# Example Agent - Updated Development\nNewer unstable version",
    )
    .await
    .unwrap();

    source_repo.git.add_all().unwrap();
    source_repo.git.commit("Update develop branch").unwrap();

    // Run update to get latest develop branch
    let output = project.run_agpm(&["update"]).unwrap();
    output.assert_success();

    // Check we got the updated content
    // Files use basename from path, not dependency name
    let file_path = project.project_path().join(".claude/agents/agpm/example.md");
    let updated = fs::read_to_string(&file_path).await.unwrap_or_else(|e| {
        panic!("Failed to read file {file_path:?}: {e}");
    });
    println!("File content after update: {updated:?}");
    assert!(updated.contains("Updated Development"), "File content: {updated}");
    assert!(updated.contains("Newer unstable"));
}

#[tokio::test]
async fn test_lockfile_records_correct_version_info() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    let feature_commit = {
        source_repo.git.checkout("feature/new-agent").unwrap();
        let commit = source_repo.git.get_commit_hash().unwrap();
        // Ensure we're on 'main' branch (git's default branch name varies)
        source_repo.git.ensure_branch("main").unwrap();
        commit
    };

    // Create manifest with different version types
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("tagged", |d| d.source("versioned").path("agents/example.md").version("v1.1.0"))
        .add_agent("branched", |d| {
            d.source("versioned").path("agents/experimental.md").branch("develop")
        })
        .add_agent("committed", |d| {
            d.source("versioned").path("agents/feature.md").rev(&feature_commit)
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    // Should succeed - all dependencies have different paths
    output.assert_success();

    // Verify lockfile was created with all entries
    let lockfile = project.read_lockfile().await.unwrap();
    assert!(lockfile.contains("tagged"));
    assert!(lockfile.contains("branched"));
    assert!(lockfile.contains("committed"));
}

#[tokio::test]
async fn test_error_on_invalid_version_constraint() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with unsatisfiable version
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| {
            d.source("versioned").path("agents/example.md").version("v99.0.0")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // This should fail
    let output = project.run_agpm(&["install"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
    assert!(
        output.stderr.contains("Git operation failed")
            || output.stderr.contains("No matching version found")
            || output.stderr.contains("Failed to resolve version"),
        "Expected error about version not found, got: {}",
        output.stderr
    );
}

#[tokio::test]
async fn test_error_on_nonexistent_branch() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with non-existent branch
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| {
            d.source("versioned").path("agents/example.md").branch("nonexistent")
        })
        .build();
    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["install"]).unwrap();
    assert!(!output.success, "Expected command to fail but it succeeded");
}

#[tokio::test]
async fn test_frozen_install_uses_lockfile_versions() {
    test_config::init_test_env();
    let project = TestProject::new().await.unwrap();
    let source_repo = project.create_source_repo("versioned").await.unwrap();

    setup_git_repo_with_versions(&source_repo).await.unwrap();

    // Create manifest with version range
    let manifest = ManifestBuilder::new()
        .add_source(
            "versioned",
            &format!("file://{}", normalize_path_for_storage(&source_repo.path)),
        )
        .add_agent("example", |d| d.source("versioned").path("agents/example.md").version("^1.0.0"))
        .build();
    project.write_manifest(&manifest).await.unwrap();

    // Initial install (should get v1.2.0)
    let output = project.run_agpm(&["install"]).unwrap();
    output.assert_success();

    let lockfile = fs::read_to_string(project.project_path().join("agpm.lock")).await.unwrap();
    assert!(lockfile.contains("version = \"v1.2.0\""));

    // Delete installed files
    fs::remove_dir_all(project.project_path().join(".claude")).await.unwrap();

    // Run frozen install - should use lockfile version (v1.2.0) not latest (v2.0.0)
    let output = project.run_agpm(&["install", "--frozen"]).unwrap();
    output.assert_success();

    let installed =
        fs::read_to_string(project.project_path().join(".claude/agents/agpm/example.md"))
            .await
            .unwrap();
    assert!(installed.contains("v1.2.0"));
    assert!(!installed.contains("v2.0.0"));
}

/// Test that path collision detection works correctly for different scenarios
#[tokio::test]
async fn test_path_collision_detection() -> Result<()> {
    let project = TestProject::new().await?;
    let source_repo = project.create_source_repo("versioned").await?;

    setup_git_repo_with_versions(&source_repo).await?;

    // Test 1: Two dependencies pointing to same path should fail
    let manifest = ManifestBuilder::new()
        .add_source("versioned", &source_repo.file_url())
        .add_agent("version-one", |d| {
            d.source("versioned").path("agents/example.md").version("v1.0.0")
        })
        .add_agent("version-two", |d| {
            d.source("versioned").path("agents/example.md").version("v2.0.0")
        })
        .build();
    project.write_manifest(&manifest).await?;

    let output = project.run_agpm(&["install"])?;
    assert!(!output.success, "Expected collision for same path");
    // Should detect version conflict (same resource, different versions)
    assert!(
        output.stderr.contains("Version conflicts")
            || output.stderr.contains("Unresolvable SHA conflicts"),
        "Expected version conflict error, got: {}",
        output.stderr
    );
    assert!(output.stderr.contains("v1.0.0"));
    assert!(output.stderr.contains("v2.0.0"));

    // Test 2: Custom targets should allow same source path
    // Clean up from previous test
    let claude_dir = project.project_path().join(".claude");
    if claude_dir.exists() {
        fs::remove_dir_all(&claude_dir).await?;
    }
    let lock_file = project.project_path().join("agpm.lock");
    if lock_file.exists() {
        fs::remove_file(&lock_file).await?;
    }

    let manifest = ManifestBuilder::new()
        .add_source("versioned", &source_repo.file_url())
        .add_agent("version-one", |d| {
            d.source("versioned").path("agents/example.md").version("v1.0.0").target("v1")
        })
        .add_snippet("version-two", |d| {
            d.source("versioned").path("snippets/utils.md").version("v1.0.0").target("v2")
        })
        .build();
    project.write_manifest(&manifest).await?;

    let output = project.run_agpm(&["install"])?;
    output.assert_success();

    // Verify both files are installed with custom targets
    // Custom target "v1" becomes ".claude/agents/v1/example.md"
    // Custom target "v2" becomes ".agpm/snippets/v2/utils.md" (snippets default to agpm artifact type)
    let v1_path = project.project_path().join(".claude/agents/agpm/v1/example.md");
    let v2_path = project.project_path().join(".agpm/snippets/v2/utils.md");

    let v1 = fs::read_to_string(&v1_path)
        .await
        .unwrap_or_else(|e| panic!("Failed to read {}: {}", v1_path.display(), e));
    let v2 = fs::read_to_string(&v2_path)
        .await
        .unwrap_or_else(|e| panic!("Failed to read {}: {}", v2_path.display(), e));
    assert!(v1.contains("v1.0.0"));
    assert!(v2.contains("v1.0.0"));

    // Test 3: Different resource types shouldn't conflict
    // Clean up from previous test
    let claude_dir = project.project_path().join(".claude");
    if claude_dir.exists() {
        fs::remove_dir_all(&claude_dir).await?;
    }
    let lock_file = project.project_path().join("agpm.lock");
    if lock_file.exists() {
        fs::remove_file(&lock_file).await?;
    }

    let manifest = ManifestBuilder::new()
        .add_source("versioned", &source_repo.file_url())
        .add_agent("agent-one", |d| {
            d.source("versioned").path("agents/example.md").version("v1.0.0")
        })
        .add_snippet("snippet-one", |d| {
            d.source("versioned").path("snippets/utils.md").version("v1.0.0")
        })
        .build();
    project.write_manifest(&manifest).await?;

    let output = project.run_agpm(&["install"])?;
    output.assert_success();

    Ok(())
}