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
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
use agpm_cli::utils::normalize_path_for_storage;
use predicates::prelude::*;
use std::env;
use tokio::fs;

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

/// Test validating a valid manifest
#[tokio::test]
async fn test_validate_valid_manifest() {
    let project = TestProject::new().await.unwrap();

    // Create mock sources
    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.add_resource("snippets", "utils", "# Utils\n\nA test snippet").await.unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

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

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).await.unwrap();

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Valid"));
}

/// Test validating manifest without project
#[tokio::test]
async fn test_validate_no_manifest() {
    let project = TestProject::new().await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("No agpm.toml found"));
}

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

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Syntax error"));
    assert!(output.stdout.contains("TOML parsing failed"));
}

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

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Missing required field"));
    assert!(output.stdout.contains("path"));
}

/// Test validating with --sources flag to check source availability
#[tokio::test]
async fn test_validate_sources() {
    let project = TestProject::new().await.unwrap();

    // Add 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.add_resource("snippets", "utils", "# Utils\n\nA test snippet").await.unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

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

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).await.unwrap();

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--sources"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Sources accessible"));
}

/// Test validating sources that are not accessible
#[tokio::test]
async fn test_validate_inaccessible_sources() {
    let project = TestProject::new().await.unwrap();

    // Create manifest with file:// URLs pointing to non-existent sources
    let manifest = ManifestBuilder::new()
        .add_sources(&[
            ("official", "file:///non/existent/path"),
            ("community", "file:///another/non/existent/path"),
        ])
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .add_standard_agent("helper", "community", "agents/helper.md")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--sources"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Source not accessible"));
}

/// Test validating with --dependencies flag to check dependency resolution
#[tokio::test]
async fn test_validate_dependencies() {
    let project = TestProject::new().await.unwrap();

    // Add mock source repositories with the required files
    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.add_resource("snippets", "utils", "# Utils\n\nA test snippet").await.unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

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

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).await.unwrap();

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--resolve"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Dependencies resolvable"));
}

/// Test validating dependencies that don't exist in sources\
/// Note: Current implementation validates source accessibility but not individual file existence
#[tokio::test]
async fn test_validate_missing_dependencies() {
    let project = TestProject::new().await.unwrap();

    // Add mock source repositories but without the required files
    let official_repo = project.create_source_repo("official").await.unwrap();
    official_repo
        .add_resource("agents", "other-agent", "# Other Agent\n\nA different agent")
        .await
        .unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();

    let manifest = ManifestBuilder::new()
        .add_source("official", &official_url)
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--resolve"]).unwrap();
    assert!(output.success); // Current implementation validates source accessibility, not file existence
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Dependencies resolvable"));
}

/// Test validating with --paths flag to check local file references
#[tokio::test]
async fn test_validate_local_paths() {
    let project = TestProject::new().await.unwrap();
    let manifest = ManifestFixture::with_local();
    project.write_manifest(&manifest.content).await.unwrap();

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

    // ./snippets/local-utils.md (relative to project directory)
    let snippets_dir = project.project_path().join("snippets");
    fs::create_dir_all(&snippets_dir).await.unwrap();
    fs::write(
        snippets_dir.join("local-utils.md"),
        "# Local Utils Snippet\n\nThis is a test snippet.",
    )
    .await
    .unwrap();

    let output = project.run_agpm(&["validate", "--paths"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Local paths exist"));
}

/// Test validating local paths that don't exist
#[tokio::test]
async fn test_validate_missing_local_paths() {
    let project = TestProject::new().await.unwrap();
    let manifest = ManifestFixture::with_local();
    project.write_manifest(&manifest.content).await.unwrap();

    // Don't create the local files to test validation failure

    let output = project.run_agpm(&["validate", "--paths"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Local path not found"));
    assert!(output.stdout.contains("../local-agents/helper.md"));
    assert!(output.stdout.contains("./snippets/local-utils.md"));
}

/// Test validating with --lockfile flag to check lockfile consistency
#[tokio::test]
async fn test_validate_lockfile_consistent() {
    let project = TestProject::new().await.unwrap();

    // Create mock sources
    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.add_resource("snippets", "utils", "# Utils\n\nA test snippet").await.unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

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

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).await.unwrap();

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

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

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

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

[[agents]]
name = "my-agent"
source = "official"
path = "agents/my-agent.md"
version = "v1.0.0"
resolved_commit = "abc123456789abcdef123456789abcdef12345678"
checksum = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
installed_at = "agents/my-agent.md"

[[agents]]
name = "helper"
source = "community"
path = "agents/helper.md"
version = "v1.0.0"
resolved_commit = "def456789abcdef123456789abcdef123456789ab"
checksum = "sha256:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"
installed_at = "agents/helper.md"

[[snippets]]
name = "utils"
source = "official"
path = "snippets/utils.md"
version = "v1.0.0"
resolved_commit = "abc123456789abcdef123456789abcdef12345678"
checksum = "sha256:74e6f7298a9c2d168935f58c6b6c5b5ea4c3df6a0b6b8d2e7b2a2b8c3d4e5f6a"
installed_at = "snippets/utils.md"
"#
    );

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

    let output = project.run_agpm(&["validate", "--check-lock"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Lockfile consistent"));
}

/// Test validating inconsistent lockfile
#[tokio::test]
async fn test_validate_lockfile_inconsistent() {
    let project = TestProject::new().await.unwrap();

    // Create manifest
    let manifest = ManifestBuilder::new()
        .add_source("official", "file:///fake/url")
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    // Create lockfile that doesn't match manifest
    let inconsistent_lockfile = r#"
# Auto-generated lockfile - DO NOT EDIT
version = 1

[[sources]]
name = "different"
url = "https://github.com/different/repo.git"
commit = "abc123456789abcdef123456789abcdef12345678"
fetched_at = "2024-01-01T00:00:00Z"

[[agents]]
name = "different-agent"
source = "different"
path = "agents/different.md"
version = "v1.0.0"
resolved_commit = "abc123456789abcdef123456789abcdef12345678"
checksum = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
installed_at = "agents/different.md"
"#;
    fs::write(project.project_path().join("agpm.lock"), inconsistent_lockfile).await.unwrap();

    let output = project.run_agpm(&["validate", "--check-lock"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Lockfile inconsistent"));
}

/// Test validating corrupted lockfile
#[tokio::test]
async fn test_validate_corrupted_lockfile() {
    let project = TestProject::new().await.unwrap();

    // Create a basic manifest
    let manifest = ManifestBuilder::new()
        .add_source("official", "file:///fake/url")
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

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

    let output = project.run_agpm(&["validate", "--check-lock"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Failed to parse lockfile"));
    assert!(output.stdout.contains("corrupted") || output.stdout.contains("Invalid"));
}

/// Test validating with --resolve and --check-lock flags (comprehensive validation)
#[tokio::test]
async fn test_validate_all() {
    let project = TestProject::new().await.unwrap();

    // Add 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.add_resource("snippets", "utils", "# Utils\n\nA test snippet").await.unwrap();
    official_repo.commit_all("Initial commit").unwrap();
    official_repo.tag_version("v1.0.0").unwrap();

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

    // Create manifest with file:// URLs
    let official_url = official_repo.bare_file_url(project.sources_path()).await.unwrap();
    let community_url = community_repo.bare_file_url(project.sources_path()).await.unwrap();

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

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

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

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

[[agents]]
name = "my-agent"
source = "official"
path = "agents/my-agent.md"
version = "v1.0.0"
resolved_commit = "abc123456789abcdef123456789abcdef12345678"
checksum = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
installed_at = "agents/my-agent.md"

[[agents]]
name = "helper"
source = "community"
path = "agents/helper.md"
version = "v1.0.0"
resolved_commit = "def456789abcdef123456789abcdef123456789ab"
checksum = "sha256:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"
installed_at = "agents/helper.md"

[[snippets]]
name = "utils"
source = "official"
path = "snippets/utils.md"
version = "v1.0.0"
resolved_commit = "abc123456789abcdef123456789abcdef12345678"
checksum = "sha256:74e6f7298a9c2d168935f58c6b6c5b5ea4c3df6a0b6b8d2e7b2a2b8c3d4e5f6a"
installed_at = "snippets/utils.md"
"#
    );

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

    let output = project.run_agpm(&["validate", "--resolve", "--check-lock"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
}

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

    let manifest = ManifestBuilder::new()
        .add_source("official", "file:///fake/url")
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--verbose"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains("Validating"));
    assert!(output.stdout.contains(""));
}

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

    let manifest = ManifestBuilder::new()
        .add_source("official", "file:///fake/url")
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--quiet"]).unwrap();
    assert!(output.success);

    // Should have minimal output in quiet mode
}

/// Test validating with JSON output format
#[tokio::test]
async fn test_validate_json_output() {
    let project = TestProject::new().await.unwrap();

    let manifest = ManifestBuilder::new()
        .add_source("official", "file:///fake/url")
        .add_standard_agent("my-agent", "official", "agents/my-agent.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--format", "json"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains("{"));
    assert!(output.stdout.contains("\"valid\""));
    assert!(output.stdout.contains("\"errors\""));
    assert!(output.stdout.contains("\"warnings\""));
}

/// Test validating specific file path
#[tokio::test]
async fn test_validate_specific_file() {
    let project = TestProject::new().await.unwrap();

    // Create a manifest that uses file:// URLs
    let sources_path_str = normalize_path_for_storage(project.sources_path());
    let official_url = format!("file://{}/official", sources_path_str);
    let community_url = format!("file://{}/community", sources_path_str);

    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")
        .add_standard_snippet("utils", "official", "snippets/utils.md")
        .build();

    let manifest_path = project.project_path().join("agpm.toml");
    fs::write(&manifest_path, manifest.trim()).await.unwrap();

    let output = project.run_agpm(&["validate", manifest_path.to_str().unwrap()]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Valid"));
}

/// Test validating with warnings (non-critical issues)
#[tokio::test]
async fn test_validate_with_warnings() {
    let project = TestProject::new().await.unwrap();

    // Create manifest with no dependencies (triggers "no dependencies" warning)
    let manifest = ManifestBuilder::new()
        .add_source("official", "https://github.com/example-org/agpm-official.git")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Valid"));
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Warning"));
}

/// Test validating with --strict flag (treat warnings as errors)
#[tokio::test]
async fn test_validate_strict_mode() {
    let project = TestProject::new().await.unwrap();

    // Create manifest with no dependencies (triggers "no dependencies" warning)
    let manifest = ManifestBuilder::new()
        .add_source("official", "https://github.com/example-org/agpm-official.git")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--strict"]).unwrap();
    assert!(!output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Strict mode"));
    assert!(output.stdout.contains("Warnings treated as errors"));
}

/// Test validate help command
#[tokio::test]
async fn test_validate_help() {
    let mut cmd = assert_cmd::Command::new(env!("CARGO_BIN_EXE_agpm"));
    cmd.arg("validate")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("--sources"))
        .stdout(predicate::str::contains("--resolve"));
}

/// Test validating empty manifest
#[tokio::test]
async fn test_validate_empty_manifest() {
    let project = TestProject::new().await.unwrap();

    // Create minimal/empty manifest
    let empty_manifest = r"
# Empty manifest
";
    project.write_manifest(empty_manifest).await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(output.success);
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("Valid"));
    assert!(output.stdout.contains(""));
    assert!(output.stdout.contains("No dependencies defined"));
}

/// Test validating with circular dependencies (if supported)
#[tokio::test]
async fn test_validate_circular_dependencies() {
    let project = TestProject::new().await.unwrap();

    // Create manifest that could lead to circular dependencies
    let manifest = ManifestBuilder::new()
        .add_sources(&[
            ("source1", "https://github.com/test/repo1.git"),
            ("source2", "https://github.com/test/repo2.git"),
        ])
        .add_standard_agent("agent-a", "source1", "agents/a.md")
        .add_standard_agent("agent-b", "source2", "agents/b.md")
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate", "--dependencies"]).unwrap();
    assert!(output.success); // Should handle gracefully
    assert!(output.stdout.contains(""));
}

/// Test validating manifest with unsupported resource type for tool
#[tokio::test]
async fn test_validate_unsupported_resource_type() {
    let project = TestProject::new().await.unwrap();

    // Create manifest with scripts using opencode tool (not supported)
    let manifest = ManifestBuilder::new()
        .add_source("community", "https://github.com/test/repo.git")
        .add_script("deploy", |d| {
            d.source("community").path("scripts/deploy.sh").version("v1.0.0").tool("opencode")
        })
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);

    // Check for enhanced error message components
    assert!(output.stdout.contains("Resource type 'scripts' is not supported by tool 'opencode'"));
    assert!(output.stdout.contains("Tool 'opencode' properly supports:"));
    assert!(output.stdout.contains("💡 Suggestions:"));
    assert!(output.stdout.contains("This resource type is supported by tools: 'claude-code'"));
    assert!(output.stdout.contains("You can fix this by:"));
    assert!(output.stdout.contains("Changing the 'tool' field to a supported tool"));
}

/// Test validating manifest with unsupported resource type shows alternative tools
#[tokio::test]
async fn test_validate_unsupported_shows_alternatives() {
    let project = TestProject::new().await.unwrap();

    // Create manifest with agents using agpm artifact type (not supported)
    let manifest = ManifestBuilder::new()
        .add_source("community", "https://github.com/test/repo.git")
        .add_agent("helper", |d| {
            d.source("community").path("agents/helper.md").version("v1.0.0").tool("agpm")
        })
        .build();

    project.write_manifest(&manifest).await.unwrap();

    let output = project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);

    // Check for enhanced error message showing alternatives
    assert!(output.stdout.contains("Resource type 'agents' is not supported by tool 'agpm'"));
    assert!(output.stdout.contains("This resource type is supported by tools:"));
    assert!(output.stdout.contains("'claude-code'"));
    assert!(output.stdout.contains("'opencode'"));
}

/// Test validating manifest with malformed merge target configuration
#[tokio::test]
async fn test_validate_malformed_merge_target_configuration() {
    let test_project = TestProject::new().await.unwrap();

    // Create manifest with malformed hooks configuration (path instead of merge_target)
    let manifest = ManifestBuilder::new()
        .add_source("test", "https://github.com/example/test.git")
        .with_malformed_hooks_tool() // This creates hooks with path instead of merge_target
        .add_hook("test-hook", |d| d.source("test").path("hooks/test.json").version("v1.0.0"))
        .build();

    test_project.write_manifest(&manifest).await.unwrap();

    let output = test_project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);

    let stdout = &output.stdout;
    assert!(stdout.contains("improperly configured"));
    assert!(stdout.contains("merge_target"));
    assert!(stdout.contains("claude-code"));
}

/// Test validating manifest with missing hooks configuration (now auto-merged with defaults)
#[tokio::test]
async fn test_validate_missing_merge_target_configuration() {
    let test_project = TestProject::new().await.unwrap();

    // Create manifest with missing hooks configuration
    let manifest = ManifestBuilder::new()
        .add_source("test", "https://github.com/example/test.git")
        .with_missing_hooks_tool() // This creates tool config without hooks
        .add_hook("test-hook", |d| d.source("test").path("hooks/test.json").version("v1.0.0"))
        .build();

    test_project.write_manifest(&manifest).await.unwrap();

    let output = test_project.run_agpm(&["validate"]).unwrap();
    assert!(
        output.success,
        "Validation should succeed because hooks are auto-merged from defaults"
    );

    let stdout = &output.stdout;
    assert!(stdout.contains("valid") || stdout.contains("") || !stdout.contains("not supported"));
    assert!(!stdout.contains("not supported"));
    assert!(!stdout.contains("improperly configured"));
}

/// Test validating manifest with properly configured merge targets
#[tokio::test]
async fn test_validate_properly_configured_merge_targets() {
    let test_project = TestProject::new().await.unwrap();

    // Create manifest with proper tool configuration
    let manifest = ManifestBuilder::new()
        .add_source("test", "https://github.com/example/test.git")
        .with_claude_code_tool() // Proper configuration with merge targets
        .add_hook("test-hook", |d| d.source("test").path("hooks/test.json").version("v1.0.0"))
        .add_mcp_server("test-server", |d| d.source("test").path("mcp/test.json").version("v1.0.0"))
        .build();

    test_project.write_manifest(&manifest).await.unwrap();

    let output = test_project.run_agpm(&["validate"]).unwrap();
    assert!(output.success);

    let stdout = &output.stdout;
    assert!(stdout.contains("✓ Valid agpm.toml"));
}

/// Test validating manifest with malformed MCP servers configuration
#[tokio::test]
async fn test_validate_malformed_mcp_servers_configuration() {
    let test_project = TestProject::new().await.unwrap();

    // Create manifest with malformed MCP servers configuration
    let manifest = ManifestBuilder::new()
        .add_source("test", "https://github.com/example/test.git")
        .with_tools_config(|t| {
            t.tool("claude-code", |c| {
                c.path(".claude")
                    .agents(crate::common::ResourceConfigBuilder::default().path("agents"))
                    .mcp_servers(crate::common::ResourceConfigBuilder::default()) // Empty - no path or merge_target
            })
        })
        .add_mcp_server("test-server", |d| d.source("test").path("mcp/test.json").version("v1.0.0"))
        .build();

    test_project.write_manifest(&manifest).await.unwrap();

    let output = test_project.run_agpm(&["validate"]).unwrap();
    assert!(!output.success);

    let stdout = &output.stdout;
    assert!(stdout.contains("improperly configured"));
    assert!(stdout.contains("mcp-servers"));
    assert!(stdout.contains("merge_target"));
}