agpm-cli 0.4.10

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
//! Tests for basic template rendering functionality.
//!
//! Covers:
//! - Simple variable substitution
//! - Template syntax validation
//! - Basic resource information injection
//! - Literal block handling

use anyhow::Result;
use tokio::fs;

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

/// Test basic template variable substitution in markdown files.
#[tokio::test]
async fn test_basic_template_substitution() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create an agent with template variables
    test_repo
        .add_resource(
            "agents",
            "test-agent",
            r#"---
title: Test Agent
agpm:
  templating: true
---
# {{ agpm.resource.name }}

This agent is installed at: `{{ agpm.resource.install_path }}`
Version: {{ agpm.resource.version }}
"#,
        )
        .await?;

    test_repo.commit_all("Add test agent")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    // Create manifest
    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_agent("test-agent", |d| {
            d.source("test-repo").path("agents/test-agent.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

    // Install - templating enabled via frontmatter
    let output = project.run_agpm(&["install"])?;
    assert!(output.success);

    // Read the installed file and verify template variables were replaced
    let installed_path = project.project_path().join(".claude/agents/test-agent.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify variables were substituted - name includes resource type directory
    assert!(
        content.contains("# agents/test-agent"),
        "Resource name should be substituted with canonical format"
    );

    // Check for platform-native path separators
    #[cfg(windows)]
    let expected_path = "installed at: `.claude\\agents\\test-agent.md`";
    #[cfg(not(windows))]
    let expected_path = "installed at: `.claude/agents/test-agent.md`";

    assert!(
        content.contains(expected_path),
        "Install path should be substituted with platform-native separators. Content:\n{}",
        content
    );
    assert!(content.contains("Version: v1.0.0"), "Version should be substituted");

    // Verify original template syntax is gone
    assert!(!content.contains("{{ agpm"), "Template syntax should be replaced");

    Ok(())
}

/// Test that files without templating enabled can contain template-like syntax.
///
/// This is critical for snippets containing JSDoc or other documentation that uses
/// curly braces (e.g., `@param {{id: number, name: string}} user`).
#[tokio::test]
async fn test_non_templated_files_with_curly_braces() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create a snippet with JSDoc-style syntax but NO templating enabled
    // This should install successfully without attempting to parse the {{ }} as templates
    test_repo
        .add_resource(
            "snippets",
            "javascript-snippet",
            r#"---
title: JavaScript Snippet
---
// JavaScript code with arrow functions
const calculateSum = (a, b) => {
    return a + b;
};

// Template literal syntax in JavaScript
const message = `Hello, ${name}!`;

// Object destructuring
const { firstName, lastName } = person;

// Array destructuring with rest
const [first, ...rest] = items;

console.log(calculateSum(5, 3));
console.log(message);
console.log(firstName, lastName);
console.log(first, rest);
"#,
        )
        .await?;

    test_repo.commit_all("Add JavaScript snippet")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    // Create manifest
    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_snippet("javascript-snippet", |d| {
            d.source("test-repo")
                .path("snippets/javascript-snippet.md")
                .version("v1.0.0")
                .tool("agpm")
        })
        .build();

    project.write_manifest(&manifest).await?;

    // Install the snippet
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed");

    // Read the installed file and verify it wasn't processed as a template
    let installed_path = project.project_path().join(".agpm/snippets/javascript-snippet.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify JavaScript syntax is preserved exactly
    assert!(content.contains("const calculateSum = (a, b) => {"));
    assert!(content.contains("const message = `Hello, ${name}!`;"));
    assert!(content.contains("const { firstName, lastName } = person;"));
    assert!(content.contains("const [first, ...rest] = items;"));
    assert!(content.contains("console.log(calculateSum(5, 3));"));

    Ok(())
}

/// Test that resources can reference each other via templates.
#[tokio::test]
async fn test_dependency_references() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create a helper snippet first
    test_repo
        .add_resource(
            "snippets",
            "helper",
            r#"---
title: Helper Functions
agpm:
  templating: true
---
# Helper Functions

This file contains helper functions.

## Function List
- sum
- multiply
- divide
"#,
        )
        .await?;

    // Create an agent that references the snippet via content filter
    test_repo
        .add_resource(
            "agents",
            "main-agent",
            r#"---
title: Main Agent
dependencies:
  snippets:
    - path: snippets/helper.md
      tool: agpm
      name: helper
agpm:
  templating: true
---
# Main Agent

This agent uses helper functions from snippets.

{{ agpm.deps.snippets.helper.content }}

## Usage

See helper functions above.
"#,
        )
        .await?;

    test_repo.commit_all("Add agent and snippet")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    // Create manifest with both dependencies
    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_snippet("helper", |d| {
            d.source("test-repo").path("snippets/helper.md").version("v1.0.0").tool("agpm")
        })
        .add_agent("main-agent", |d| {
            d.source("test-repo").path("agents/main-agent.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

    // Install both resources
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed. stderr: {}", output.stderr);

    // Read the installed agent file
    let agent_path = project.project_path().join(".claude/agents/main-agent.md");
    let content = fs::read_to_string(&agent_path).await?;

    // Verify snippet content was embedded
    assert!(content.contains("# Helper Functions"));
    assert!(content.contains("## Function List"));
    assert!(content.contains("- sum"));
    assert!(content.contains("- multiply"));
    assert!(content.contains("- divide"));

    Ok(())
}

/// Test that templating can be disabled via frontmatter.
#[tokio::test]
async fn test_opt_out_via_frontmatter() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create an agent with templating disabled in frontmatter
    test_repo
        .add_resource(
            "agents",
            "no-template",
            r#"---
title: No Template Agent
agpm:
  templating: false
---
# Agent with Literal Syntax

This file contains literal template syntax: {{ agpm.resource.name }}

The syntax should not be processed.
"#,
        )
        .await?;

    test_repo.commit_all("Add agent")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_agent("no-template", |d| {
            d.source("test-repo").path("agents/no-template.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

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

    // Read the installed file
    let installed_path = project.project_path().join(".claude/agents/no-template.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify template syntax was NOT processed
    assert!(
        content.contains("{{ agpm.resource.name }}"),
        "Template syntax should remain literal when templating is disabled"
    );

    Ok(())
}

/// Test that templating is disabled by default (template syntax preserved).
#[tokio::test]
async fn test_templating_disabled_by_default() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create an agent with template variables
    test_repo
        .add_resource(
            "agents",
            "test-agent",
            r#"---
title: Test Agent
---
# {{ agpm.resource.name }}

Install path: {{ agpm.resource.install_path }}
"#,
        )
        .await?;

    test_repo.commit_all("Add agent")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_agent("test-agent", |d| {
            d.source("test-repo").path("agents/test-agent.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

    // Install without --templating flag (templating disabled by default)
    let output = project.run_agpm(&["install"])?;
    assert!(output.success);

    // Read the installed file
    let installed_path = project.project_path().join(".claude/agents/test-agent.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify template syntax was NOT processed (default behavior)
    assert!(
        content.contains("# {{ agpm.resource.name }}"),
        "Template syntax should remain literal by default"
    );
    assert!(
        content.contains("{{ agpm.resource.install_path }}"),
        "All template syntax should be preserved by default"
    );

    Ok(())
}

/// Test that files without template syntax are unchanged.
#[tokio::test]
async fn test_no_template_syntax() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create a file without any template syntax but with templating enabled
    test_repo
        .add_resource(
            "agents",
            "plain-agent",
            r#"---
title: Plain Agent
agpm:
  templating: true
---
# Plain Agent

This agent has no template syntax.

## Features

- Feature 1
- Feature 2
- Feature 3

## Usage

Just use it normally.
"#,
        )
        .await?;

    test_repo.commit_all("Add plain agent")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_agent("plain-agent", |d| {
            d.source("test-repo").path("agents/plain-agent.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed");

    // Read the installed file
    let installed_path = project.project_path().join(".claude/agents/plain-agent.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify content is unchanged
    assert!(content.contains("# Plain Agent"));
    assert!(content.contains("This agent has no template syntax."));
    assert!(content.contains("- Feature 1"));
    assert!(content.contains("Just use it normally."));

    Ok(())
}

/// Test conditional rendering with {% if %} blocks.
#[tokio::test]
async fn test_conditional_rendering() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    test_repo
        .add_resource(
            "agents",
            "conditional",
            r#"---
title: Conditional Agent
agpm:
  templating: true
---
# Conditional Content

{% if agpm.resource.source %}
This resource is from source: {{ agpm.resource.source }}
{% else %}
This is a local resource.
{% endif %}

{% if agpm.resource.version %}
Version: {{ agpm.resource.version }}
{% endif %}
"#,
        )
        .await?;

    test_repo.commit_all("Add agent")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_agent("conditional", |d| {
            d.source("test-repo").path("agents/conditional.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

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

    let installed_path = project.project_path().join(".claude/agents/conditional.md");
    let content = fs::read_to_string(&installed_path).await?;

    // Verify conditional blocks were processed
    assert!(
        content.contains("This resource is from source: test-repo"),
        "Conditional block should render when condition is true"
    );
    assert!(!content.contains("This is a local resource"), "Alternative block should not render");
    assert!(
        content.contains("Version: v1.0.0"),
        "Optional block should render when variable exists"
    );

    Ok(())
}

/// Test loop over dependencies with {% for %} blocks.
#[tokio::test]
async fn test_loop_over_dependencies() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;
    let test_repo = project.create_source_repo("test-repo").await?;

    // Create multiple snippets
    test_repo
        .add_resource(
            "snippets",
            "helper1",
            r#"---
title: Helper 1
agpm:
  templating: true
---
# Helper 1

This is helper 1.
"#,
        )
        .await?;

    test_repo
        .add_resource(
            "snippets",
            "helper2",
            r#"---
title: Helper 2
agpm:
  templating: true
---
# Helper 2

This is helper 2.
"#,
        )
        .await?;

    test_repo
        .add_resource(
            "snippets",
            "helper3",
            r#"---
title: Helper 3
agpm:
  templating: true
---
# Helper 3

This is helper 3.
"#,
        )
        .await?;

    // Create an agent that loops over snippets
    test_repo
        .add_resource(
            "agents",
            "looping-agent",
            r#"---
title: Looping Agent
dependencies:
  snippets:
    - path: snippets/helper1.md
      tool: agpm
      name: helper1
    - path: snippets/helper2.md
      tool: agpm
      name: helper2
    - path: snippets/helper3.md
      tool: agpm
      name: helper3
agpm:
  templating: true
---
# Looping Agent

## Available Helpers

{% for name, snippet in agpm.deps.snippets %}
### {{ name }}
{{ snippet.content }}
{% endfor %}

## Count

There are {{ agpm.deps.snippets | length }} helpers available.
"#,
        )
        .await?;

    test_repo.commit_all("Add resources")?;
    test_repo.tag_version("v1.0.0")?;

    let repo_url = test_repo.bare_file_url(project.sources_path())?;

    let manifest = ManifestBuilder::new()
        .add_source("test-repo", &repo_url)
        .add_snippet("helper1", |d| {
            d.source("test-repo").path("snippets/helper1.md").version("v1.0.0").tool("agpm")
        })
        .add_snippet("helper2", |d| {
            d.source("test-repo").path("snippets/helper2.md").version("v1.0.0").tool("agpm")
        })
        .add_snippet("helper3", |d| {
            d.source("test-repo").path("snippets/helper3.md").version("v1.0.0").tool("agpm")
        })
        .add_agent("looping-agent", |d| {
            d.source("test-repo").path("agents/looping-agent.md").version("v1.0.0")
        })
        .build();

    project.write_manifest(&manifest).await?;

    // Install all resources
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed");

    // Read the installed agent file
    let agent_path = project.project_path().join(".claude/agents/looping-agent.md");
    let content = fs::read_to_string(&agent_path).await?;

    assert!(content.contains("### helper1"));
    assert!(content.contains("# Helper 1"));
    assert!(content.contains("### helper2"));
    assert!(content.contains("# Helper 2"));
    assert!(content.contains("### helper3"));
    assert!(content.contains("# Helper 3"));
    assert!(content.contains("There are 3 helpers available."));

    Ok(())
}