agpm-cli 0.4.3

AGent Package Manager - A Git-based package manager for Claude 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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! 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 assert_cmd::Command;
use std::path::Path;
use tempfile::TempDir;
use tokio::fs;

mod common;
use common::TestProject;

/// Helper to create a test manifest with gitignore configuration
async fn create_test_manifest(gitignore: bool, source_dir: &Path) -> String {
    // Convert path to string with forward slashes for TOML compatibility
    let source_path = source_dir.display().to_string().replace('\\', "/");
    format!(
        r#"
[sources]

[target]
agents = ".claude/agents"
snippets = ".agpm/snippets"
commands = ".claude/commands"
gitignore = {}

[agents.test-agent]
path = "{}/agents/test.md"

[snippets.test-snippet]
path = "{}/snippets/test.md"

[commands.test-command]
path = "{}/commands/test.md"
"#,
        gitignore, source_path, source_path, source_path
    )
}

/// Helper to create a test manifest without explicit gitignore setting
async fn create_test_manifest_default(source_dir: &Path) -> String {
    // Convert path to string with forward slashes for TOML compatibility
    let source_path = source_dir.display().to_string().replace('\\', "/");
    format!(
        r#"
[sources]

[target]
agents = ".claude/agents"
snippets = ".agpm/snippets"
commands = ".claude/commands"

[agents.test-agent]
path = "{}/agents/test.md"
"#,
        source_path
    )
}

/// Helper to create a test lockfile with installed resources
async fn create_test_lockfile() -> String {
    r#"
version = 1

[[agents]]
name = "test-agent"
path = "source/agents/test.md"
checksum = ""
installed_at = ".claude/agents/test-agent.md"

[[snippets]]
name = "test-snippet"
path = "source/snippets/test.md"
checksum = ""
installed_at = ".agpm/snippets/test-snippet.md"
artifact_type = "agpm"

[[commands]]
name = "test-command"
path = "source/commands/test.md"
checksum = ""
installed_at = ".claude/commands/test-command.md"
"#
    .to_string()
}

/// Create test source files that can be installed
async fn create_test_source_files(source_dir: &Path) -> Result<()> {
    // 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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest without explicit gitignore setting (should default to true)
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest_default(&source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was created
    let gitignore_path = project_dir.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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest with gitignore = true
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was created
    let gitignore_path = project_dir.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"));
}

#[tokio::test]
async fn test_gitignore_disabled() {
    agpm_cli::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest with gitignore = false
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(false, &source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore was NOT created
    let gitignore_path = project_dir.join(".gitignore");
    assert!(!gitignore_path.exists(), "Gitignore should not be created when disabled");
}

#[tokio::test]
async fn test_gitignore_preserves_user_entries() {
    agpm_cli::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

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

    // Create existing gitignore with user entries
    let gitignore_path = project_dir.join(".gitignore");
    let user_content = r#"# User's custom comment
*.backup
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
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // 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("*.backup"));
    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/test-snippet.md"));
}

#[tokio::test]
async fn test_gitignore_preserves_content_after_agpm_section() {
    agpm_cli::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

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

    // Create existing gitignore with content after AGPM section
    let gitignore_path = project_dir.join(".gitignore");
    let user_content = r#"# Project gitignore
*.backup
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
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // 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("*.backup"));
    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/test-snippet.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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create initial lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run update command (which should also update gitignore)
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("update")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore exists after update
    let gitignore_path = project_dir.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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();

    // Create manifest
    let manifest_path = project_dir.join("agpm.toml");
    let manifest_content = r#"
[sources]
test-source = "https://github.com/test/repo.git"

[target]
gitignore = true

[scripts.external-script]
source = "test-source"
path = "scripts/test.sh"
version = "v1.0.0"
"#;
    fs::write(&manifest_path, manifest_content).await.unwrap();

    // Create lockfile with resource installed outside .claude
    let lockfile_content = r#"
version = 1

[[sources]]
name = "test-source"
url = "https://github.com/test/repo.git"
commit = "abc123"

[[scripts]]
name = "external-script"
source = "test-source"
url = "https://github.com/test/repo.git"
path = "scripts/test.sh"
version = "v1.0.0"
resolved_commit = "abc123"
checksum = "sha256:test"
installed_at = "scripts/external.sh"

[[agents]]
name = "internal-agent"
source = "test-source"
url = "https://github.com/test/repo.git"
path = "agents/test.md"
version = "v1.0.0"
resolved_commit = "abc123"
checksum = "sha256:test"
installed_at = ".claude/agents/internal.md"
"#;
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, lockfile_content).await.unwrap();

    // Create directories
    fs::create_dir_all(project_dir.join(".claude/agents")).await.unwrap();
    fs::create_dir_all(project_dir.join("scripts")).await.unwrap();

    // Create resource files
    fs::write(project_dir.join("scripts/external.sh"), "#!/bin/bash\n").await.unwrap();
    fs::write(project_dir.join(".claude/agents/internal.md"), "# Internal\n").await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check gitignore content
    let gitignore_path = project_dir.join(".gitignore");
    if gitignore_path.exists() {
        let content = fs::read_to_string(&gitignore_path).await.unwrap();
        // External path should use ../
        assert!(content.contains("../scripts/external.sh"), "External paths should use ../ prefix");
        // Internal path should use /
        assert!(
            content.contains(".claude/agents/internal.md"),
            "Internal paths should use / prefix"
        );
    }
}

#[tokio::test]
async fn test_gitignore_empty_lockfile() {
    agpm_cli::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create empty lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, "version = 1\n").await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // Check that .gitignore is created even with no resources
    let gitignore_path = project_dir.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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Create manifest
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    // Create lockfile
    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

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

    // Run again
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // 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 temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

    // Start with gitignore enabled
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install with gitignore enabled
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

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

    // Now disable gitignore
    fs::write(&manifest_path, create_test_manifest(false, &source_dir).await).await.unwrap();

    // Run install again
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

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

    // Re-enable gitignore
    fs::write(&manifest_path, 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
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // 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(&source_dir).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();

    assert!(project_dir.join(".claude/agents/test-agent.md").exists());
    assert!(project_dir.join(".agpm/snippets/test-snippet.md").exists());
    assert!(project_dir.join(".claude/commands/test-command.md").exists());

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

    assert!(
        !status.contains("agents/test-agent.md"),
        "Agent file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("snippets/test-snippet.md"),
        "Snippet file should be ignored by git\nGit status:\n{}",
        status
    );
    assert!(
        !status.contains("commands/test-command.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/test-agent.md").unwrap(),
        "Agent file should be ignored by git check-ignore"
    );
    assert!(
        git.check_ignore(".agpm/snippets/test-snippet.md").unwrap(),
        "Snippet file should be ignored by git check-ignore"
    );
    assert!(
        git.check_ignore(".claude/commands/test-command.md").unwrap(),
        "Command file should be ignored by git check-ignore"
    );
}

#[tokio::test]
async fn test_gitignore_disabled_files_not_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(&source_dir).await.unwrap();

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

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

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

    assert!(project_dir.join(".claude/agents/test-agent.md").exists());
    assert!(project_dir.join(".agpm/snippets/test-snippet.md").exists());
    assert!(project_dir.join(".claude/commands/test-command.md").exists());

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

    assert!(
        status.contains("agents/test-agent.md"),
        "Agent file should NOT be ignored when gitignore is disabled
Git status:
{}",
        status
    );
    assert!(
        status.contains("snippets/test-snippet.md"),
        "Snippet file should NOT be ignored when gitignore is disabled
Git status:
{}",
        status
    );
    assert!(
        status.contains("commands/test-command.md"),
        "Command file should NOT be ignored when gitignore is disabled
Git status:
{}",
        status
    );

    assert!(
        !project_dir.join(".gitignore").exists(),
        "Gitignore file should not exist when disabled"
    );
}

#[tokio::test]
async fn test_gitignore_malformed_existing() {
    agpm_cli::test_utils::init_test_logging(None);
    let temp = TempDir::new().unwrap();
    let project_dir = temp.path();
    let source_dir = temp.path().join("source");

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

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

    // Create malformed gitignore (missing end marker)
    let gitignore_path = project_dir.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 lockfile
    let manifest_path = project_dir.join("agpm.toml");
    fs::write(&manifest_path, create_test_manifest(true, &source_dir).await).await.unwrap();

    let lockfile_path = project_dir.join("agpm.lock");
    fs::write(&lockfile_path, create_test_lockfile().await).await.unwrap();

    // Run install command
    Command::cargo_bin("agpm")
        .unwrap()
        .arg("install")
        .arg("--quiet")
        .current_dir(project_dir)
        .assert();

    // 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"));
}