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
41        .registry
42        .add_marketplace(test_marketplace)
43        .await?;
44    tracing::info!("test marketplace added");
45
46    // Test creating and installing a sample plugin
47    let sample_plugin = PluginManifest {
48        id: "test-plugin".to_string(),
49        name: "Test Plugin".to_string(),
50        version: "1.0.0".to_string(),
51        description: "A test plugin for marketplace system".to_string(),
52        entrypoint: PathBuf::from("bin/test-plugin"),
53        capabilities: vec!["test".to_string()],
54        source: "https://github.com/test/test-plugin".to_string(),
55        trust_level: Some(crate::config::PluginTrustLevel::Sandbox),
56        dependencies: vec![],
57        author: "Test Author".to_string(),
58        license: "MIT".to_string(),
59        homepage: "https://github.com/test/test-plugin".to_string(),
60        repository: "https://github.com/test/test-plugin".to_string(),
61    };
62
63    // Install the plugin
64    marketplace_system
65        .installer
66        .install_plugin(&sample_plugin)
67        .await?;
68    tracing::info!("sample plugin installed");
69
70    // Verify the plugin is installed
71    let is_installed = marketplace_system
72        .installer
73        .is_installed("test-plugin")
74        .await;
75    assert!(is_installed, "Plugin should be installed");
76    tracing::info!("plugin installation verified");
77
78    // Test uninstalling the plugin
79    marketplace_system
80        .installer
81        .uninstall_plugin("test-plugin")
82        .await?;
83    tracing::info!("sample plugin uninstalled");
84
85    // Verify the plugin is uninstalled
86    let is_installed_after = marketplace_system
87        .installer
88        .is_installed("test-plugin")
89        .await;
90    assert!(!is_installed_after, "Plugin should be uninstalled");
91    tracing::info!("plugin uninstallation verified");
92
93    tracing::info!("all marketplace system tests passed");
94    Ok(())
95}
96
97/// Test marketplace configuration system
98pub async fn test_marketplace_config() -> Result<()> {
99    tracing::info!("testing marketplace configuration system");
100
101    // Create a temporary directory for testing
102    let temp_dir = tempfile::tempdir()?;
103    let config_path = temp_dir.path().join("marketplace-config-test.toml");
104
105    // Create initial settings
106    let mut settings = MarketplaceSettings::default();
107    settings.auto_update.marketplaces = true;
108    settings.security.default_trust_level = crate::config::PluginTrustLevel::Trusted;
109
110    // Add a test marketplace
111    let marketplace = MarketplaceSource::GitHub {
112        id: "test-gh".to_string(),
113        owner: "test".to_string(),
114        repo: "test-repo".to_string(),
115        refspec: Some("main".to_string()),
116    };
117    settings.add_marketplace(marketplace);
118
119    // Add a test plugin
120    let plugin = InstalledPlugin {
121        id: "config-test-plugin".to_string(),
122        name: "Config Test Plugin".to_string(),
123        version: "1.0.0".to_string(),
124        source: "test-marketplace".to_string(),
125        install_path: PathBuf::from("/tmp/test"),
126        enabled: true,
127        trust_level: crate::config::PluginTrustLevel::Sandbox,
128        installed_at: "2023-01-01".to_string(),
129    };
130    settings.add_installed_plugin(plugin);
131
132    // Save settings to file
133    settings.save_to_file(&config_path).await?;
134    tracing::info!("marketplace settings saved to file");
135
136    // Load settings from file
137    let loaded_settings = MarketplaceSettings::load_from_file(&config_path).await?;
138    tracing::info!("marketplace settings loaded from file");
139
140    // Verify loaded settings
141    assert_eq!(
142        settings.auto_update.marketplaces,
143        loaded_settings.auto_update.marketplaces
144    );
145    assert_eq!(
146        settings.security.default_trust_level,
147        loaded_settings.security.default_trust_level
148    );
149    assert_eq!(
150        settings.marketplaces.len(),
151        loaded_settings.marketplaces.len()
152    );
153    assert_eq!(
154        settings.installed_plugins.len(),
155        loaded_settings.installed_plugins.len()
156    );
157
158    tracing::info!("configuration system tests passed");
159    Ok(())
160}
161
162/// Test plugin validation functionality
163pub fn test_plugin_validation() -> Result<()> {
164    tracing::info!("testing plugin validation");
165
166    // Create a valid plugin manifest
167    let valid_plugin = PluginManifest {
168        id: "valid-plugin".to_string(),
169        name: "Valid Plugin".to_string(),
170        version: "1.0.0".to_string(),
171        description: "A valid test plugin".to_string(),
172        entrypoint: PathBuf::from("bin/valid-plugin"),
173        capabilities: vec!["test".to_string()],
174        source: "https://github.com/test/valid-plugin".to_string(),
175        trust_level: Some(crate::config::PluginTrustLevel::Sandbox),
176        dependencies: vec![],
177        author: "Test Author".to_string(),
178        license: "MIT".to_string(),
179        homepage: "https://github.com/test/valid-plugin".to_string(),
180        repository: "https://github.com/test/valid-plugin".to_string(),
181    };
182
183    // Create an invalid plugin manifest (missing required fields)
184    let invalid_plugin = PluginManifest {
185        id: "".to_string(),   // Invalid: empty ID
186        name: "".to_string(), // Invalid: empty name
187        version: "1.0.0".to_string(),
188        description: "An invalid test plugin".to_string(),
189        entrypoint: PathBuf::from(""),
190        capabilities: vec![],
191        source: "".to_string(), // Invalid: empty source
192        trust_level: None,
193        dependencies: vec![],
194        author: "Test Author".to_string(),
195        license: "MIT".to_string(),
196        homepage: "https://github.com/test/invalid-plugin".to_string(),
197        repository: "https://github.com/test/invalid-plugin".to_string(),
198    };
199
200    // Create a plugin installer to test validation
201    let temp_dir = tempfile::tempdir()?;
202    let installer = super::installer::PluginInstaller::new(temp_dir.path().to_path_buf(), None);
203
204    // Test validation of valid plugin (should pass)
205    let valid_result = installer.validate_manifest(&valid_plugin);
206    if valid_result.is_err() {
207        anyhow::bail!("Valid plugin should pass validation");
208    }
209    tracing::info!("valid plugin validation passed");
210
211    // Test validation of invalid plugin (should fail)
212    let invalid_result = installer.validate_manifest(&invalid_plugin);
213    if invalid_result.is_ok() {
214        anyhow::bail!("Invalid plugin should fail validation");
215    }
216    tracing::info!("invalid plugin validation failed as expected");
217
218    tracing::info!("plugin validation tests passed");
219    Ok(())
220}
221
222/// Run all marketplace tests
223pub async fn run_all_tests() -> Result<()> {
224    tracing::info!("running marketplace system tests");
225
226    test_plugin_validation()?;
227
228    test_marketplace_config().await?;
229
230    test_marketplace_system().await?;
231
232    tracing::info!("all tests completed successfully");
233    Ok(())
234}
235
236#[cfg(test)]
237mod tests {
238    use super::*;
239
240    #[tokio::test]
241    async fn test_full_marketplace_workflow() {
242        // This test runs the full marketplace workflow
243        let result = run_all_tests().await;
244        assert!(result.is_ok(), "Marketplace tests should pass");
245    }
246}