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
//! Utilities for working with manifest files
//!
//! This module provides common functionality for loading and validating
//! manifest files across different commands.

use crate::core::error::ErrorContext;
use crate::manifest::Manifest;
use anyhow::{Context, Result, anyhow};
use std::path::Path;

/// Load a project manifest from the standard location
///
/// This function looks for a `agpm.toml` file in the given project directory
/// and returns a parsed Manifest. It provides consistent error messages
/// across all commands.
///
/// # Arguments
///
/// * `project_dir` - The project directory to search for agpm.toml
///
/// # Returns
///
/// * `Ok(Manifest)` - The parsed manifest
/// * `Err` - If the manifest doesn't exist or can't be parsed
///
/// # Example
///
/// ```no_run
/// # use anyhow::Result;
/// # fn example() -> Result<()> {
/// use std::path::Path;
/// use agpm_cli::utils::manifest_utils::load_project_manifest;
///
/// let manifest = load_project_manifest(Path::new("."))?;
/// # Ok(())
/// # }
/// ```
pub fn load_project_manifest(project_dir: &Path) -> Result<Manifest> {
    let manifest_path = project_dir.join("agpm.toml");

    if !manifest_path.exists() {
        return Err(anyhow!("No agpm.toml found in {}", project_dir.display()).context(
            ErrorContext {
                error: crate::core::AgpmError::ManifestNotFound,
                suggestion: Some("Run 'agpm init' to create a new project".to_string()),
                details: Some(format!("Expected manifest at: {}", manifest_path.display())),
            },
        ));
    }

    Manifest::load(&manifest_path).with_context(|| ErrorContext {
        error: crate::core::AgpmError::ManifestParseError {
            file: manifest_path.display().to_string(),
            reason: "Failed to parse manifest".to_string(),
        },
        suggestion: Some("Check that agpm.toml is valid TOML syntax".to_string()),
        details: Some(format!("Manifest path: {}", manifest_path.display())),
    })
}

/// Load a manifest from a specific path with validation
///
/// This function loads a manifest from any path and optionally validates
/// that it contains required sections.
///
/// # Arguments
///
/// * `manifest_path` - Path to the manifest file
/// * `require_sources` - Whether to require at least one source
/// * `require_dependencies` - Whether to require at least one dependency
///
/// # Returns
///
/// * `Ok(Manifest)` - The parsed and validated manifest
/// * `Err` - If the manifest can't be loaded or validation fails
pub fn load_and_validate_manifest(
    manifest_path: &Path,
    require_sources: bool,
    require_dependencies: bool,
) -> Result<Manifest> {
    if !manifest_path.exists() {
        return Err(anyhow!("Manifest file not found: {}", manifest_path.display()));
    }

    let manifest = Manifest::load(manifest_path)?;

    if require_sources && manifest.sources.is_empty() {
        return Err(anyhow!("No sources defined in manifest").context(ErrorContext {
            error: crate::core::AgpmError::ManifestValidationError {
                reason: "No sources defined in manifest".to_string(),
            },
            suggestion: Some("Add at least one source using 'agpm add source'".to_string()),
            details: None,
        }));
    }

    if require_dependencies
        && (manifest.agents.is_empty()
            && manifest.snippets.is_empty()
            && manifest.commands.is_empty()
            && manifest.mcp_servers.is_empty())
    {
        return Err(anyhow!("No dependencies defined in manifest").context(ErrorContext {
            error: crate::core::AgpmError::ManifestValidationError {
                reason: "No dependencies defined in manifest".to_string(),
            },
            suggestion: Some("Add dependencies using 'agpm add dep'".to_string()),
            details: None,
        }));
    }

    Ok(manifest)
}

/// Check if a manifest exists in the project directory
///
/// # Arguments
///
/// * `project_dir` - The project directory to check
///
/// # Returns
///
/// * `true` if agpm.toml exists, `false` otherwise
pub fn manifest_exists(project_dir: &Path) -> bool {
    project_dir.join("agpm.toml").exists()
}

/// Get the path to the manifest file
///
/// # Arguments
///
/// * `project_dir` - The project directory
///
/// # Returns
///
/// The path to agpm.toml in the project directory
pub fn manifest_path(project_dir: &Path) -> std::path::PathBuf {
    project_dir.join("agpm.toml")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_load_project_manifest_missing() {
        let temp_dir = tempdir().unwrap();
        let result = load_project_manifest(temp_dir.path());
        assert!(result.is_err());
        // The error will contain both the initial message and the context
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(err_str.contains("agpm.toml") || err_str.contains("Manifest"));
    }

    #[test]
    fn test_load_project_manifest_invalid() {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");
        fs::write(&manifest_path, "invalid toml {").unwrap();

        let result = load_project_manifest(temp_dir.path());
        assert!(result.is_err());
        // Just verify it's an error - the specific message format may vary
    }

    #[test]
    fn test_manifest_exists() {
        let temp_dir = tempdir().unwrap();
        assert!(!manifest_exists(temp_dir.path()));

        let manifest_path = temp_dir.path().join("agpm.toml");
        fs::write(&manifest_path, "[sources]").unwrap();
        assert!(manifest_exists(temp_dir.path()));
    }

    #[test]
    fn test_manifest_path() {
        let temp_dir = tempdir().unwrap();
        let path = manifest_path(temp_dir.path());
        assert_eq!(path, temp_dir.path().join("agpm.toml"));
    }

    #[test]
    fn test_load_and_validate_manifest_success() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create valid manifest with sources and dependencies
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"

[agents]
test-agent = { source = "test", path = "agent.md", version = "v1.0.0" }
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should succeed with both validations
        let manifest = load_and_validate_manifest(&manifest_path, true, true)?;
        assert!(!manifest.sources.is_empty());
        assert!(!manifest.agents.is_empty());
        Ok(())
    }

    #[test]
    fn test_load_and_validate_manifest_no_sources() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest without sources
        let content = r#"
[agents]
test-agent = { path = "../local/agent.md" }
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should fail when requiring sources
        let result = load_and_validate_manifest(&manifest_path, true, false);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No sources"));

        // Should succeed when not requiring sources
        load_and_validate_manifest(&manifest_path, false, false)?;
        Ok(())
    }

    #[test]
    fn test_load_and_validate_manifest_no_dependencies() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest with only sources
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should fail when requiring dependencies
        let result = load_and_validate_manifest(&manifest_path, false, true);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No dependencies"));

        // Should succeed when not requiring dependencies
        load_and_validate_manifest(&manifest_path, false, false)?;
        Ok(())
    }

    #[test]
    fn test_load_and_validate_manifest_nonexistent() {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("nonexistent.toml");

        let result = load_and_validate_manifest(&manifest_path, false, false);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn test_load_and_validate_manifest_with_snippets() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest with snippets dependency
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"

[snippets]
test-snippet = { source = "test", path = "snippet.md", version = "v1.0.0" }
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should succeed when requiring dependencies (has snippets)
        load_and_validate_manifest(&manifest_path, true, true)?;
        Ok(())
    }

    #[test]
    fn test_load_and_validate_manifest_with_commands() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest with commands dependency
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"

[commands]
test-command = { source = "test", path = "command.md", version = "v1.0.0" }
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should succeed when requiring dependencies (has commands)
        load_and_validate_manifest(&manifest_path, true, true)?;
        Ok(())
    }

    #[test]
    fn test_load_and_validate_manifest_with_mcp_servers() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest with MCP servers dependency
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"

[mcp-servers]
test-server = "../local/mcp-servers/test-server.json"
"#;
        fs::write(&manifest_path, content).unwrap();

        // Should succeed when requiring dependencies (has MCP servers)
        load_and_validate_manifest(&manifest_path, true, true)?;
        Ok(())
    }

    #[test]
    fn test_load_project_manifest_valid() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create a valid manifest
        let content = r#"
[sources]
test = "https://github.com/test/repo.git"

[agents]
test-agent = { source = "test", path = "agent.md", version = "v1.0.0" }
"#;
        fs::write(&manifest_path, content).unwrap();

        let manifest = load_project_manifest(temp_dir.path())?;
        assert_eq!(manifest.sources.len(), 1);
        assert_eq!(manifest.agents.len(), 1);
        Ok(())
    }

    #[test]
    fn test_load_and_validate_empty_manifest() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create an empty but valid manifest
        let content = "";
        fs::write(&manifest_path, content).unwrap();

        // Should succeed when not requiring anything
        load_and_validate_manifest(&manifest_path, false, false)?;

        // Should fail when requiring sources
        let result = load_and_validate_manifest(&manifest_path, true, false);
        assert!(result.is_err());

        // Should fail when requiring dependencies
        let result = load_and_validate_manifest(&manifest_path, false, true);
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn test_manifest_validation_mixed_dependencies() -> Result<()> {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest with multiple types of dependencies
        let content = r#"
[sources]
source1 = "https://github.com/test/repo1.git"
source2 = "https://github.com/test/repo2.git"

[agents]
agent1 = { source = "source1", path = "agent1.md", version = "v1.0.0" }

[snippets]
snippet1 = { source = "source2", path = "snippet1.md", version = "v2.0.0" }

[commands]
cmd1 = { source = "source1", path = "cmd1.md", version = "v1.0.0" }
"#;
        fs::write(&manifest_path, content).unwrap();

        let manifest = load_and_validate_manifest(&manifest_path, true, true)?;
        assert_eq!(manifest.sources.len(), 2);
        assert_eq!(manifest.agents.len(), 1);
        assert_eq!(manifest.snippets.len(), 1);
        assert_eq!(manifest.commands.len(), 1);
        Ok(())
    }

    #[test]
    fn test_error_context_in_load_project_manifest() {
        let temp_dir = tempdir().unwrap();

        // Test missing manifest error
        let result = load_project_manifest(temp_dir.path());
        assert!(result.is_err());

        let err_chain = result.unwrap_err();
        let err_str = format!("{:?}", err_chain);

        // Should contain error context with suggestion
        assert!(err_str.contains("agpm.toml") || err_str.contains("init"));
    }

    #[test]
    fn test_error_context_in_validation() {
        let temp_dir = tempdir().unwrap();
        let manifest_path = temp_dir.path().join("agpm.toml");

        // Create manifest without sources
        fs::write(&manifest_path, "").unwrap();

        // Test no sources error
        let result = load_and_validate_manifest(&manifest_path, true, false);
        assert!(result.is_err());

        let err_chain = result.unwrap_err();
        let err_str = format!("{:?}", err_chain);
        assert!(err_str.contains("source") || err_str.contains("No sources"));

        // Test no dependencies error
        let result = load_and_validate_manifest(&manifest_path, false, true);
        assert!(result.is_err());

        let err_chain = result.unwrap_err();
        let err_str = format!("{:?}", err_chain);
        assert!(err_str.contains("dependencies") || err_str.contains("No dependencies"));
    }
}