Skip to main content

vtcode_core/marketplace/
testing.rs

1//! Testing module for VT Code marketplace system
2//!
3//! This module provides tests and examples for using the marketplace system with actual sources.
4
5use std::path::PathBuf;
6
7use anyhow::Result;
8
9use crate::marketplace::{
10    MarketplaceConfig, MarketplaceSource, MarketplaceSystem, PluginManifest,
11    config::{InstalledPlugin, MarketplaceSettings},
12};
13
14/// Test the marketplace system with sample configurations
15pub async fn test_marketplace_system() -> Result<()> {
16    tracing::info!("testing marketplace system");
17
18    // Create a temporary directory for testing
19    let temp_dir = tempfile::tempdir()?;
20    let base_dir = temp_dir.path().to_path_buf();
21
22    // Create a plugin runtime (using a dummy one for testing)
23    let plugin_runtime = None; // In real usage, this would be a proper PluginRuntime
24
25    // Create marketplace system
26    let config = MarketplaceConfig::default();
27    let marketplace_system = MarketplaceSystem::new(base_dir.clone(), config, plugin_runtime);
28
29    // Initialize the system
30    marketplace_system.initialize().await?;
31    tracing::info!("marketplace system initialized");
32
33    // Test adding a marketplace
34    let test_marketplace = MarketplaceSource::Git {
35        id: "test-marketplace".to_string(),
36        url: "https://github.com/test/test-marketplace".to_string(),
37        refspec: Some("main".to_string()),
38    };
39
40    marketplace_system.registry.add_marketplace(test_marketplace).await?;
41    tracing::info!("test marketplace added");
42
43    // Test creating and installing a sample plugin
44    let sample_plugin = PluginManifest {
45        id: "test-plugin".to_string(),
46        name: "Test Plugin".to_string(),
47        version: "1.0.0".to_string(),
48        description: "A test plugin for marketplace system".to_string(),
49        entrypoint: PathBuf::from("bin/test-plugin"),
50        capabilities: vec!["test".to_string()],
51        source: "https://github.com/test/test-plugin".to_string(),
52        trust_level: Some(crate::config::PluginTrustLevel::Sandbox),
53        dependencies: vec![],
54        author: "Test Author".to_string(),
55        license: "MIT".to_string(),
56        homepage: "https://github.com/test/test-plugin".to_string(),
57        repository: "https://github.com/test/test-plugin".to_string(),
58    };
59
60    // Install the plugin
61    marketplace_system.installer.install_plugin(&sample_plugin).await?;
62    tracing::info!("sample plugin installed");
63
64    // Verify the plugin is installed
65    let is_installed = marketplace_system.installer.is_installed("test-plugin").await;
66    assert!(is_installed, "Plugin should be installed");
67    tracing::info!("plugin installation verified");
68
69    // Test uninstalling the plugin
70    marketplace_system.installer.uninstall_plugin("test-plugin").await?;
71    tracing::info!("sample plugin uninstalled");
72
73    // Verify the plugin is uninstalled
74    let is_installed_after = marketplace_system.installer.is_installed("test-plugin").await;
75    assert!(!is_installed_after, "Plugin should be uninstalled");
76    tracing::info!("plugin uninstallation verified");
77
78    tracing::info!("all marketplace system tests passed");
79    Ok(())
80}
81
82/// Test marketplace configuration system
83pub async fn test_marketplace_config() -> Result<()> {
84    tracing::info!("testing marketplace configuration system");
85
86    // Create a temporary directory for testing
87    let temp_dir = tempfile::tempdir()?;
88    let config_path = temp_dir.path().join("marketplace-config-test.toml");
89
90    // Create initial settings
91    let mut settings = MarketplaceSettings::default();
92    settings.auto_update.marketplaces = true;
93    settings.security.default_trust_level = crate::config::PluginTrustLevel::Trusted;
94
95    // Add a test marketplace
96    let marketplace = MarketplaceSource::GitHub {
97        id: "test-gh".to_string(),
98        owner: "test".to_string(),
99        repo: "test-repo".to_string(),
100        refspec: Some("main".to_string()),
101    };
102    settings.add_marketplace(marketplace);
103
104    // Add a test plugin
105    let plugin = InstalledPlugin {
106        id: "config-test-plugin".to_string(),
107        name: "Config Test Plugin".to_string(),
108        version: "1.0.0".to_string(),
109        source: "test-marketplace".to_string(),
110        install_path: PathBuf::from("/tmp/test"),
111        enabled: true,
112        trust_level: crate::config::PluginTrustLevel::Sandbox,
113        installed_at: "2023-01-01".to_string(),
114    };
115    settings.add_installed_plugin(plugin);
116
117    // Save settings to file
118    settings.save_to_file(&config_path).await?;
119    tracing::info!("marketplace settings saved to file");
120
121    // Load settings from file
122    let loaded_settings = MarketplaceSettings::load_from_file(&config_path).await?;
123    tracing::info!("marketplace settings loaded from file");
124
125    // Verify loaded settings
126    assert_eq!(settings.auto_update.marketplaces, loaded_settings.auto_update.marketplaces);
127    assert_eq!(settings.security.default_trust_level, loaded_settings.security.default_trust_level);
128    assert_eq!(settings.marketplaces.len(), loaded_settings.marketplaces.len());
129    assert_eq!(settings.installed_plugins.len(), loaded_settings.installed_plugins.len());
130
131    tracing::info!("configuration system tests passed");
132    Ok(())
133}
134
135/// Test plugin validation functionality
136pub fn test_plugin_validation() -> Result<()> {
137    tracing::info!("testing plugin validation");
138
139    // Create a valid plugin manifest
140    let valid_plugin = PluginManifest {
141        id: "valid-plugin".to_string(),
142        name: "Valid Plugin".to_string(),
143        version: "1.0.0".to_string(),
144        description: "A valid test plugin".to_string(),
145        entrypoint: PathBuf::from("bin/valid-plugin"),
146        capabilities: vec!["test".to_string()],
147        source: "https://github.com/test/valid-plugin".to_string(),
148        trust_level: Some(crate::config::PluginTrustLevel::Sandbox),
149        dependencies: vec![],
150        author: "Test Author".to_string(),
151        license: "MIT".to_string(),
152        homepage: "https://github.com/test/valid-plugin".to_string(),
153        repository: "https://github.com/test/valid-plugin".to_string(),
154    };
155
156    // Create an invalid plugin manifest (missing required fields)
157    let invalid_plugin = PluginManifest {
158        id: "".to_string(),   // Invalid: empty ID
159        name: "".to_string(), // Invalid: empty name
160        version: "1.0.0".to_string(),
161        description: "An invalid test plugin".to_string(),
162        entrypoint: PathBuf::from(""),
163        capabilities: vec![],
164        source: "".to_string(), // Invalid: empty source
165        trust_level: None,
166        dependencies: vec![],
167        author: "Test Author".to_string(),
168        license: "MIT".to_string(),
169        homepage: "https://github.com/test/invalid-plugin".to_string(),
170        repository: "https://github.com/test/invalid-plugin".to_string(),
171    };
172
173    // Create a plugin installer to test validation
174    let temp_dir = tempfile::tempdir()?;
175    let installer = super::installer::PluginInstaller::new(temp_dir.path().to_path_buf(), None);
176
177    // Test validation of valid plugin (should pass)
178    let valid_result = installer.validate_manifest(&valid_plugin);
179    if valid_result.is_err() {
180        anyhow::bail!("Valid plugin should pass validation");
181    }
182    tracing::info!("valid plugin validation passed");
183
184    // Test validation of invalid plugin (should fail)
185    let invalid_result = installer.validate_manifest(&invalid_plugin);
186    if invalid_result.is_ok() {
187        anyhow::bail!("Invalid plugin should fail validation");
188    }
189    tracing::info!("invalid plugin validation failed as expected");
190
191    tracing::info!("plugin validation tests passed");
192    Ok(())
193}
194
195/// Run all marketplace tests
196pub async fn run_all_tests() -> Result<()> {
197    tracing::info!("running marketplace system tests");
198
199    test_plugin_validation()?;
200
201    test_marketplace_config().await?;
202
203    test_marketplace_system().await?;
204
205    tracing::info!("all tests completed successfully");
206    Ok(())
207}
208
209#[cfg(test)]
210mod tests {
211    use super::*;
212
213    #[tokio::test]
214    async fn test_full_marketplace_workflow() {
215        // This test runs the full marketplace workflow
216        let result = run_all_tests().await;
217        assert!(result.is_ok(), "Marketplace tests should pass");
218    }
219}