agpm-cli 0.4.8

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
//! Integration tests for .gitignore management functionality
//!
//! These tests verify that AGPM correctly manages .gitignore files
//! based on the target.gitignore configuration setting.

use anyhow::Result;
use std::path::Path;
use tokio::fs;

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

/// Helper to create a test manifest with gitignore configuration
async fn create_test_manifest(gitignore: bool, _source_dir: &Path) -> String {
    // Use relative paths from project directory to sources directory
    // This avoids deep nesting from absolute temp paths
    ManifestBuilder::new()
        .with_target_config(|t| {
            t.agents(".claude/agents")
                .snippets(".agpm/snippets")
                .commands(".claude/commands")
                .gitignore(gitignore)
        })
        .add_agent("test-agent", |d| d.path("../sources/source/agents/test.md").flatten(false))
        .add_snippet("test-snippet", |d| {
            d.path("../sources/source/snippets/test.md").flatten(false)
        })
        .add_command("test-command", |d| {
            d.path("../sources/source/commands/test.md").flatten(false)
        })
        .build()
}

/// Helper to create a test manifest without explicit gitignore setting
async fn create_test_manifest_default(_source_dir: &Path) -> String {
    // Use relative paths from project directory to sources directory
    ManifestBuilder::new()
        .with_target_config(|t| {
            t.agents(".claude/agents").snippets(".agpm/snippets").commands(".claude/commands")
        })
        .add_agent("test-agent", |d| d.path("../sources/source/agents/test.md").flatten(false))
        .build()
}

/// Create test source files that can be installed
async fn create_test_source_files(project: &TestProject) -> Result<()> {
    let source_dir = project.sources_path().join("source");

    // Create the directories
    fs::create_dir_all(source_dir.join("agents")).await?;
    fs::create_dir_all(source_dir.join("snippets")).await?;
    fs::create_dir_all(source_dir.join("commands")).await?;

    // Create source files
    fs::write(source_dir.join("agents/test.md"), "# Test Agent\n").await?;
    fs::write(source_dir.join("snippets/test.md"), "# Test Snippet\n").await?;
    fs::write(source_dir.join("commands/test.md"), "# Test Command\n").await?;

    Ok(())
}

#[tokio::test]
async fn test_gitignore_enabled_by_default() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create manifest without explicit gitignore setting (should default to true)
    project.write_manifest(&create_test_manifest_default(&source_dir).await).await.unwrap();

    // Run install command (let it generate the lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that .gitignore was created
    let gitignore_path = project.project_path().join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created by default");

    // Check that it has the expected structure
    let content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(content.contains("AGPM managed entries"));
    assert!(content.contains("# End of AGPM managed entries"));
}

#[tokio::test]
async fn test_gitignore_explicitly_enabled() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create manifest with gitignore = true
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install command (let it generate the lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that .gitignore was created
    let gitignore_path = project.project_path().join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created");

    // Verify content structure
    let content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(content.contains("AGPM managed entries"));
    assert!(content.contains("AGPM managed entries - do not edit below this line"));
    assert!(content.contains("# End of AGPM managed entries"));
}

// Test removed: gitignore is now always enabled (no longer configurable via manifest.target.gitignore)

#[tokio::test]
async fn test_gitignore_preserves_user_entries() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create .claude directory
    fs::create_dir_all(project.project_path().join(".claude")).await.unwrap();

    // Create existing gitignore with user entries
    let gitignore_path = project.project_path().join(".gitignore");
    let user_content = r#"# User's custom comment
user-file.txt
temp/

# AGPM managed entries - do not edit below this line
.claude/agents/old-agent.md
# End of AGPM managed entries
"#;
    fs::write(&gitignore_path, user_content).await.unwrap();

    // Create manifest with gitignore enabled
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install command (let it generate the lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that user entries are preserved
    let updated_content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(updated_content.contains("# User's custom comment"));
    assert!(updated_content.contains("user-file.txt"));
    assert!(updated_content.contains("temp/"));

    // Check that AGPM section exists (entries will be based on what was actually installed)
    assert!(updated_content.contains("AGPM managed entries"));
    assert!(updated_content.contains("# End of AGPM managed entries"));
    assert!(updated_content.contains(".agpm/snippets/sources/source/snippets/test.md"));
}

#[tokio::test]
async fn test_gitignore_preserves_content_after_agpm_section() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create .claude directory
    fs::create_dir_all(project.project_path().join(".claude")).await.unwrap();

    // Create existing gitignore with content after AGPM section
    let gitignore_path = project.project_path().join(".gitignore");
    let user_content = r#"# Project gitignore
temp/

# AGPM managed entries - do not edit below this line
.claude/agents/old-agent.md
# End of AGPM managed entries

# Additional entries after AGPM section
local-config.json
debug/
# End comment
"#;
    fs::write(&gitignore_path, user_content).await.unwrap();

    // Create manifest with gitignore enabled
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install command (let it generate the lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that all sections are preserved
    let updated_content = fs::read_to_string(&gitignore_path).await.unwrap();

    // Check content before AGPM section
    assert!(updated_content.contains("# Project gitignore"));
    assert!(updated_content.contains("temp/"));

    // Check AGPM section is updated
    assert!(updated_content.contains("AGPM managed entries"));
    assert!(updated_content.contains("# End of AGPM managed entries"));
    assert!(updated_content.contains(".agpm/snippets/sources/source/snippets/test.md"));

    // Check content after AGPM section is preserved
    assert!(updated_content.contains("# Additional entries after AGPM section"));
    assert!(updated_content.contains("local-config.json"));
    assert!(updated_content.contains("debug/"));
    assert!(updated_content.contains("# End comment"));

    // Verify old AGPM entry is removed
    assert!(!updated_content.contains(".claude/agents/old-agent.md"));
}

#[tokio::test]
async fn test_gitignore_update_command() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create manifest
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install first to create initial lockfile
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Run update command (which should also update gitignore)
    project.run_agpm(&["update", "--quiet"]).unwrap().assert_success();

    // Check that .gitignore exists after update
    let gitignore_path = project.project_path().join(".gitignore");
    if gitignore_path.exists() {
        let content = fs::read_to_string(&gitignore_path).await.unwrap();
        assert!(content.contains("AGPM managed entries"));
    }
}

#[tokio::test]
async fn test_gitignore_handles_external_paths() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();

    // Create a test repository with both agent and script
    let repo = project.create_source_repo("test-source").await.unwrap();

    // Create agent
    repo.add_resource("agents", "test-agent", "# Test Agent\n").await.unwrap();

    // Create script
    fs::create_dir_all(repo.path.join("scripts")).await.unwrap();
    fs::write(repo.path.join("scripts/test.sh"), "#!/bin/bash\necho 'test'\n").await.unwrap();

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

    let url = repo.bare_file_url(project.sources_path()).unwrap();

    // Create manifest with script and agent
    let manifest_content = ManifestBuilder::new()
        .add_source("test-source", &url)
        .with_gitignore(true)
        .add_script("external-script", |d| {
            d.source("test-source").path("scripts/test.sh").version("v1.0.0")
        })
        .add_agent("internal-agent", |d| {
            d.source("test-source").path("agents/test-agent.md").version("v1.0.0")
        })
        .build();
    project.write_manifest(&manifest_content).await.unwrap();

    // Run install command
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check gitignore content
    let gitignore_path = project.project_path().join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created");

    let content = fs::read_to_string(&gitignore_path).await.unwrap();

    // Both resources should be listed in gitignore
    assert!(content.contains("AGPM managed entries"), "Should have AGPM section");
    assert!(content.contains("# End of AGPM managed entries"), "Should have end marker");

    // Scripts default to .claude/scripts/ directory
    // Paths are preserved as-is from dependency specification
    assert!(
        content.contains(".claude/scripts/test.sh")
            || content.contains(".claude/scripts/external-script.sh"),
        "Script path should be in gitignore. Content:\n{}",
        content
    );

    // Agents go to .claude/agents/
    // Paths are preserved as-is from dependency specification
    assert!(
        content.contains(".claude/agents/test-agent.md")
            || content.contains(".claude/agents/internal-agent.md"),
        "Agent path should be in gitignore. Content:\n{}",
        content
    );
}

#[tokio::test]
async fn test_gitignore_empty_lockfile() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();

    // Create manifest with no dependencies
    let manifest_content = ManifestBuilder::new()
        .with_target_config(|t| {
            t.agents(".claude/agents")
                .snippets(".agpm/snippets")
                .commands(".claude/commands")
                .gitignore(true)
        })
        .build();
    project.write_manifest(&manifest_content).await.unwrap();

    // Run install command (will generate empty lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that .gitignore is created even with no resources
    let gitignore_path = project.project_path().join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created even with empty lockfile");

    let content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(content.contains("AGPM managed entries"));
    assert!(content.contains("# End of AGPM managed entries"));
}

#[tokio::test]
async fn test_gitignore_idempotent() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create manifest
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install command
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Get content after first run
    let gitignore_path = project.project_path().join(".gitignore");
    let first_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path).await.unwrap()
    } else {
        String::new()
    };

    // Run again
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Get content after second run
    let second_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path).await.unwrap()
    } else {
        String::new()
    };

    // Content should be the same (idempotent)
    assert_eq!(first_content, second_content, "Gitignore should be idempotent");
}

#[tokio::test]
async fn test_gitignore_switch_enabled_disabled() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Start with gitignore enabled
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install with gitignore enabled
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    let gitignore_path = project.project_path().join(".gitignore");
    assert!(gitignore_path.exists(), "Gitignore should be created");

    // Now disable gitignore
    project.write_manifest(&create_test_manifest(false, &source_dir).await).await.unwrap();

    // Run install again
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Gitignore should still exist (we don't delete it)
    assert!(gitignore_path.exists(), "Gitignore should still exist when disabled");

    // Re-enable gitignore
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Add a user entry to the existing gitignore
    let content = fs::read_to_string(&gitignore_path).await.unwrap();
    let modified_content =
        content.replace("# AGPM managed entries", "user-custom.txt\n\n# AGPM managed entries");
    fs::write(&gitignore_path, modified_content).await.unwrap();

    // Run install again
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that user entry is preserved
    let final_content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(
        final_content.contains("user-custom.txt"),
        "User entries should be preserved when re-enabling"
    );
}

#[tokio::test]
async fn test_gitignore_actually_ignored_by_git() {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await.unwrap();
    let project_dir = project.project_path().to_path_buf();
    let source_dir = project.sources_path().join("source");

    create_test_source_files(&project).await.unwrap();

    let git = project.init_git_repo().unwrap();

    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // After stripping parent directory components from paths like "../sources/source/agents/test.md"
    // we get "sources/source/agents/test.md" which installs to ".claude/agents/sources/source/agents/test.md"
    assert!(project_dir.join(".claude/agents/sources/source/agents/test.md").exists());
    assert!(project_dir.join(".agpm/snippets/sources/source/snippets/test.md").exists());
    assert!(project_dir.join(".claude/commands/sources/source/commands/test.md").exists());

    git.add_all().unwrap();
    let status = git.status_porcelain().unwrap();

    assert!(
        !status.contains("sources/source/agents/test.md"),
        "Agent file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("sources/source/snippets/test.md"),
        "Snippet file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("sources/source/commands/test.md"),
        "Command file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        status.contains(".gitignore"),
        "Gitignore file should be tracked by git\nGit status:\n{}",
        status
    );
    assert!(
        status.contains("agpm.toml"),
        "Manifest should be tracked by git\nGit status:\n{}",
        status
    );
    assert!(
        status.contains("agpm.lock"),
        "Lockfile should be tracked by git\nGit status:\n{}",
        status
    );

    assert!(
        git.check_ignore(".claude/agents/sources/source/agents/test.md").unwrap(),
        "Agent file should be ignored by git check-ignore"
    );
    assert!(
        git.check_ignore(".agpm/snippets/sources/source/snippets/test.md").unwrap(),
        "Snippet file should be ignored by git check-ignore"
    );
    assert!(
        git.check_ignore(".claude/commands/sources/source/commands/test.md").unwrap(),
        "Command file should be ignored by git check-ignore"
    );
}

// Test removed: gitignore is now always enabled (no longer configurable via manifest.target.gitignore)

#[tokio::test]
async fn test_gitignore_malformed_existing() {
    agpm_cli::test_utils::init_test_logging(None);
    let project = TestProject::new().await.unwrap();
    let source_dir = project.sources_path().join("source");

    // Create source files
    create_test_source_files(&project).await.unwrap();

    // Create .claude directory
    fs::create_dir_all(project.project_path().join(".claude")).await.unwrap();

    // Create malformed gitignore (missing end marker)
    let gitignore_path = project.project_path().join(".gitignore");
    let malformed_content = r#"# Some content
user-file.txt

# AGPM managed entries - do not edit below this line
/old/entry.md
# Missing end marker!
"#;
    fs::write(&gitignore_path, malformed_content).await.unwrap();

    // Create manifest and run install
    project.write_manifest(&create_test_manifest(true, &source_dir).await).await.unwrap();

    // Run install command (let it generate the lockfile)
    project.run_agpm(&["install", "--quiet"]).unwrap().assert_success();

    // Check that gitignore was properly recreated
    let updated_content = fs::read_to_string(&gitignore_path).await.unwrap();
    assert!(updated_content.contains("# End of AGPM managed entries"));
    assert!(updated_content.contains("user-file.txt"));
    assert!(updated_content.contains("AGPM managed entries"));
}