1use crate::api::types::*;
2use crate::config::Config;
3use crate::utils::error::{CarpError, CarpResult};
4use reqwest::{Client, ClientBuilder, Response};
5use std::time::Duration;
6use tokio::time::sleep;
7
8#[derive(Debug, Clone)]
10pub struct RetryConfig {
11 pub max_retries: u32,
12 pub initial_delay: Duration,
13 pub max_delay: Duration,
14 pub backoff_multiplier: f64,
15}
16
17impl Default for RetryConfig {
18 fn default() -> Self {
19 Self {
20 max_retries: 3,
21 initial_delay: Duration::from_millis(100),
22 max_delay: Duration::from_secs(5),
23 backoff_multiplier: 2.0,
24 }
25 }
26}
27
28pub struct ApiClient {
30 client: Client,
31 base_url: String,
32 api_key: Option<String>,
33 retry_config: RetryConfig,
34}
35
36impl ApiClient {
37 pub fn new(config: &Config) -> CarpResult<Self> {
39 Self::with_retry_config(config, RetryConfig::default())
40 }
41
42 pub fn with_retry_config(config: &Config, mut retry_config: RetryConfig) -> CarpResult<Self> {
44 retry_config.max_retries = config.retry.max_retries;
46 retry_config.initial_delay = Duration::from_millis(config.retry.initial_delay_ms);
47 retry_config.max_delay = Duration::from_millis(config.retry.max_delay_ms);
48 retry_config.backoff_multiplier = config.retry.backoff_multiplier;
49 let client = ClientBuilder::new()
50 .timeout(Duration::from_secs(config.timeout))
51 .user_agent(format!("carp-cli/{}", env!("CARGO_PKG_VERSION")))
52 .danger_accept_invalid_certs(!config.verify_ssl)
53 .connect_timeout(Duration::from_secs(10))
54 .tcp_keepalive(Duration::from_secs(60))
55 .pool_idle_timeout(Duration::from_secs(90))
56 .pool_max_idle_per_host(8)
57 .build()?;
58
59 if config.registry_url.is_empty() {
61 return Err(CarpError::Config(
62 "Registry URL cannot be empty".to_string(),
63 ));
64 }
65
66 let base_url = config.registry_url.trim_end_matches('/');
68
69 Ok(Self {
70 client,
71 base_url: base_url.to_string(),
72 api_key: config.api_key.clone(),
73 retry_config,
74 })
75 }
76
77 pub fn with_api_key(mut self, api_key: Option<String>) -> Self {
79 if let Some(ref key) = api_key {
81 if let Err(e) = crate::config::ConfigManager::validate_api_key(key) {
82 eprintln!("Warning: Invalid API key format: {e}");
83 }
84 }
85 self.api_key = api_key;
86 self
87 }
88
89 pub async fn search(
91 &self,
92 query: &str,
93 limit: Option<usize>,
94 exact: bool,
95 ) -> CarpResult<SearchResponse> {
96 let url = format!("{}/api/v1/agents/search", self.base_url);
97 let mut params = vec![];
98
99 if !query.trim().is_empty() {
101 params.push(("q", query.trim()));
102 }
103
104 let limit_str;
105 if let Some(limit) = limit {
106 if limit == 0 {
107 return Err(CarpError::InvalidAgent(
108 "Limit must be greater than 0".to_string(),
109 ));
110 }
111 limit_str = limit.to_string();
112 params.push(("limit", &limit_str));
113 }
114
115 if exact {
116 params.push(("exact", "true"));
117 }
118
119 self.make_request_with_retry(|| async {
120 let response = self.client.get(&url).query(¶ms).send().await?;
121 self.handle_response(response).await
122 })
123 .await
124 }
125
126 #[allow(dead_code)]
128 pub async fn get_agent_download(
129 &self,
130 name: &str,
131 version: Option<&str>,
132 ) -> CarpResult<AgentDownload> {
133 self.validate_agent_name(name)?;
135
136 let version = version.unwrap_or("latest");
137 if !version.is_empty() && version != "latest" {
138 self.validate_version(version)?;
139 }
140
141 let url = format!(
142 "{}/api/v1/agents/{}/{}/download",
143 self.base_url,
144 urlencoding::encode(name),
145 urlencoding::encode(version)
146 );
147
148 self.make_request_with_retry(|| async {
149 let response = self.client.get(&url).send().await?;
150 self.handle_response(response).await
151 })
152 .await
153 }
154
155 #[allow(dead_code)]
157 pub async fn download_agent(&self, download_url: &str) -> CarpResult<bytes::Bytes> {
158 if download_url.is_empty() {
160 return Err(CarpError::Network(
161 "Download URL cannot be empty".to_string(),
162 ));
163 }
164
165 let parsed_url = download_url
167 .parse::<reqwest::Url>()
168 .map_err(|_| CarpError::Network("Invalid download URL format".to_string()))?;
169
170 if parsed_url.scheme() != "https" && parsed_url.scheme() != "http" {
172 return Err(CarpError::Network(
173 "Download URLs must use HTTP or HTTPS".to_string(),
174 ));
175 }
176
177 if parsed_url.scheme() == "http" {
178 return Err(CarpError::Network(
179 "HTTP download URLs are not allowed for security reasons".to_string(),
180 ));
181 }
182
183 self.make_request_with_retry(|| async {
184 let response = self.client.get(download_url).send().await?;
185
186 if !response.status().is_success() {
187 return Err(CarpError::Api {
188 status: response.status().as_u16(),
189 message: format!("Failed to download agent: HTTP {}", response.status()),
190 });
191 }
192
193 if let Some(content_length) = response.content_length() {
197 const MAX_DOWNLOAD_SIZE: u64 = 100 * 1024 * 1024; if content_length > MAX_DOWNLOAD_SIZE {
199 return Err(CarpError::Network(format!(
200 "Download size ({content_length} bytes) exceeds maximum allowed size ({MAX_DOWNLOAD_SIZE} bytes)"
201 )));
202 }
203 }
204
205 let bytes = response.bytes().await?;
206 Ok(bytes)
207 }).await
208 }
209
210 pub async fn upload(&self, request: UploadAgentRequest) -> CarpResult<UploadAgentResponse> {
212 let api_key = self.api_key.as_ref().ok_or_else(|| {
213 CarpError::Auth("No API key configured. Please set your API key via command line, environment variable, or config file.".to_string())
214 })?;
215
216 self.validate_upload_request(&request)?;
218
219 let url = format!("{}/api/v1/agents/upload", self.base_url);
220
221 self.make_request_with_retry(|| async {
222 let response = self
223 .client
224 .post(&url)
225 .header("Authorization", format!("Bearer {api_key}"))
226 .header("Content-Type", "application/json")
227 .json(&request)
228 .send()
229 .await?;
230
231 self.handle_response(response).await
232 })
233 .await
234 }
235
236 #[allow(dead_code)]
238 pub async fn publish(
239 &self,
240 _request: PublishRequest,
241 _content: Vec<u8>,
242 ) -> CarpResult<PublishResponse> {
243 Err(CarpError::Api {
245 status: 503,
246 message: "Publishing is temporarily disabled pending security hardening. Please check back later.".to_string(),
247 })
248 }
249
250 #[allow(dead_code)]
252 async fn publish_internal(
253 &self,
254 request: PublishRequest,
255 content: Vec<u8>,
256 ) -> CarpResult<PublishResponse> {
257 let api_key = self.api_key.as_ref().ok_or_else(|| {
258 CarpError::Auth("No API key configured. Please set your API key via command line, environment variable, or config file.".to_string())
259 })?;
260
261 self.validate_publish_request(&request)?;
263
264 const MAX_PUBLISH_SIZE: usize = 50 * 1024 * 1024;
266 if content.len() > MAX_PUBLISH_SIZE {
267 return Err(CarpError::Api {
268 status: 413,
269 message: format!(
270 "Agent package size ({} bytes) exceeds maximum allowed size ({} bytes)",
271 content.len(),
272 MAX_PUBLISH_SIZE
273 ),
274 });
275 }
276
277 let url = format!("{}/api/v1/agents/publish", self.base_url);
278
279 let form = reqwest::multipart::Form::new()
281 .text("metadata", serde_json::to_string(&request)?)
282 .part(
283 "content",
284 reqwest::multipart::Part::bytes(content)
285 .file_name("agent.zip")
286 .mime_str("application/zip")?,
287 );
288
289 let response = self
292 .client
293 .post(&url)
294 .header("Authorization", format!("Bearer {api_key}"))
295 .multipart(form)
296 .send()
297 .await?;
298
299 self.handle_response(response).await
300 }
301
302 #[allow(dead_code)]
304 pub async fn authenticate(&self, username: &str, password: &str) -> CarpResult<AuthResponse> {
305 if username.trim().is_empty() {
307 return Err(CarpError::Auth("Username cannot be empty".to_string()));
308 }
309 if password.is_empty() {
310 return Err(CarpError::Auth("Password cannot be empty".to_string()));
311 }
312
313 let url = format!("{}/api/v1/auth/login", self.base_url);
314 let request = AuthRequest {
315 username: username.trim().to_string(),
316 password: password.to_string(),
317 };
318
319 let response = self.client.post(&url).json(&request).send().await?;
321 self.handle_response(response).await
322 }
323
324 pub async fn health_check(&self) -> CarpResult<HealthResponse> {
326 let url = format!("{}/api/health", self.base_url);
327
328 let mut attempts = 0;
330 let max_attempts = 2;
331
332 loop {
333 attempts += 1;
334 match self.client.get(&url).send().await {
335 Ok(response) => return self.handle_response(response).await,
336 Err(e) if attempts < max_attempts && self.is_retryable_error(&e) => {
337 sleep(Duration::from_millis(500)).await;
338 continue;
339 }
340 Err(e) => return Err(CarpError::from(e)),
341 }
342 }
343 }
344
345 async fn make_request_with_retry<T, F, Fut>(&self, request_fn: F) -> CarpResult<T>
347 where
348 F: Fn() -> Fut,
349 Fut: std::future::Future<Output = CarpResult<T>>,
350 {
351 let mut attempts = 0;
352 let mut delay = self.retry_config.initial_delay;
353
354 loop {
355 attempts += 1;
356
357 match request_fn().await {
358 Ok(result) => return Ok(result),
359 Err(e) if attempts <= self.retry_config.max_retries && self.should_retry(&e) => {
360 if attempts < self.retry_config.max_retries {
361 sleep(delay).await;
362 delay = std::cmp::min(
363 Duration::from_millis(
364 (delay.as_millis() as f64 * self.retry_config.backoff_multiplier)
365 as u64,
366 ),
367 self.retry_config.max_delay,
368 );
369 } else {
370 return Err(e);
371 }
372 }
373 Err(e) => return Err(e),
374 }
375 }
376 }
377
378 fn should_retry(&self, error: &CarpError) -> bool {
380 match error {
381 CarpError::Http(e) => self.is_retryable_error(e),
382 CarpError::Api { status, .. } => {
383 (500..600).contains(status) ||
385 *status == 429 || *status == 408 }
388 CarpError::Network(_) => true,
389 _ => false,
390 }
391 }
392
393 fn is_retryable_error(&self, error: &reqwest::Error) -> bool {
395 if error.is_timeout() || error.is_connect() {
396 return true;
397 }
398
399 if let Some(status) = error.status() {
400 let status_code = status.as_u16();
401 return (500..600).contains(&status_code) || status_code == 429 || status_code == 408;
402 }
403
404 false
405 }
406
407 fn validate_agent_name(&self, name: &str) -> CarpResult<()> {
409 if name.trim().is_empty() {
410 return Err(CarpError::InvalidAgent(
411 "Agent name cannot be empty".to_string(),
412 ));
413 }
414
415 if !name
417 .chars()
418 .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
419 {
420 return Err(CarpError::InvalidAgent(
421 "Agent name can only contain alphanumeric characters, hyphens, and underscores"
422 .to_string(),
423 ));
424 }
425
426 if name.len() > 100 {
427 return Err(CarpError::InvalidAgent(
428 "Agent name cannot exceed 100 characters".to_string(),
429 ));
430 }
431
432 Ok(())
433 }
434
435 fn validate_version(&self, version: &str) -> CarpResult<()> {
437 if version.trim().is_empty() {
438 return Err(CarpError::InvalidAgent(
439 "Version cannot be empty".to_string(),
440 ));
441 }
442
443 if !version
445 .chars()
446 .all(|c| c.is_alphanumeric() || ".-_+".contains(c))
447 {
448 return Err(CarpError::InvalidAgent(
449 "Version can only contain alphanumeric characters, dots, hyphens, underscores, and plus signs".to_string()
450 ));
451 }
452
453 if version.len() > 50 {
454 return Err(CarpError::InvalidAgent(
455 "Version cannot exceed 50 characters".to_string(),
456 ));
457 }
458
459 Ok(())
460 }
461
462 fn validate_upload_request(&self, request: &UploadAgentRequest) -> CarpResult<()> {
464 self.validate_agent_name(&request.name)?;
466
467 if request.description.trim().is_empty() {
469 return Err(CarpError::InvalidAgent(
470 "Description cannot be empty".to_string(),
471 ));
472 }
473
474 if request.description.len() > 1000 {
475 return Err(CarpError::InvalidAgent(
476 "Description cannot exceed 1000 characters".to_string(),
477 ));
478 }
479
480 if request.content.trim().is_empty() {
482 return Err(CarpError::InvalidAgent(
483 "Content cannot be empty".to_string(),
484 ));
485 }
486
487 const MAX_CONTENT_SIZE: usize = 1024 * 1024;
489 if request.content.len() > MAX_CONTENT_SIZE {
490 return Err(CarpError::InvalidAgent(format!(
491 "Content size ({} bytes) exceeds maximum allowed size ({} bytes)",
492 request.content.len(),
493 MAX_CONTENT_SIZE
494 )));
495 }
496
497 self.validate_frontmatter_consistency(request)?;
499
500 if let Some(version) = &request.version {
502 self.validate_version(version)?;
503 }
504
505 for tag in &request.tags {
507 if tag.trim().is_empty() {
508 return Err(CarpError::InvalidAgent("Tags cannot be empty".to_string()));
509 }
510 if tag.len() > 50 {
511 return Err(CarpError::InvalidAgent(
512 "Tags cannot exceed 50 characters".to_string(),
513 ));
514 }
515 }
516
517 if request.tags.len() > 20 {
518 return Err(CarpError::InvalidAgent(
519 "Cannot have more than 20 tags".to_string(),
520 ));
521 }
522
523 Ok(())
524 }
525
526 fn validate_frontmatter_consistency(&self, request: &UploadAgentRequest) -> CarpResult<()> {
528 if !request.content.starts_with("---") {
530 return Err(CarpError::InvalidAgent(
531 "Content must contain YAML frontmatter starting with ---".to_string(),
532 ));
533 }
534
535 let lines: Vec<&str> = request.content.lines().collect();
537 let mut frontmatter_end = None;
538
539 for (i, line) in lines.iter().enumerate().skip(1) {
540 if line.trim() == "---" {
541 frontmatter_end = Some(i);
542 break;
543 }
544 }
545
546 let frontmatter_end = frontmatter_end.ok_or_else(|| {
547 CarpError::InvalidAgent("Invalid YAML frontmatter: missing closing ---".to_string())
548 })?;
549
550 let frontmatter_lines = &lines[1..frontmatter_end];
552 let frontmatter_content = frontmatter_lines.join("\n");
553
554 let frontmatter: serde_json::Value = serde_yaml::from_str(&frontmatter_content)
556 .map_err(|e| CarpError::InvalidAgent(format!("Invalid YAML frontmatter: {e}")))?;
557
558 if let Some(frontmatter_name) = frontmatter.get("name").and_then(|v| v.as_str()) {
560 if frontmatter_name != request.name {
561 return Err(CarpError::InvalidAgent(format!(
562 "Name mismatch: frontmatter contains '{}' but request contains '{}'",
563 frontmatter_name, request.name
564 )));
565 }
566 } else {
567 return Err(CarpError::InvalidAgent(
568 "YAML frontmatter must contain a 'name' field".to_string(),
569 ));
570 }
571
572 if let Some(frontmatter_desc) = frontmatter.get("description").and_then(|v| v.as_str()) {
574 if frontmatter_desc != request.description {
575 return Err(CarpError::InvalidAgent(format!(
576 "Description mismatch: frontmatter contains '{}' but request contains '{}'",
577 frontmatter_desc, request.description
578 )));
579 }
580 } else {
581 return Err(CarpError::InvalidAgent(
582 "YAML frontmatter must contain a 'description' field".to_string(),
583 ));
584 }
585
586 Ok(())
587 }
588
589 fn validate_publish_request(&self, request: &PublishRequest) -> CarpResult<()> {
591 self.validate_agent_name(&request.name)?;
592 self.validate_version(&request.version)?;
593
594 if request.description.trim().is_empty() {
595 return Err(CarpError::InvalidAgent(
596 "Description cannot be empty".to_string(),
597 ));
598 }
599
600 if request.description.len() > 1000 {
601 return Err(CarpError::InvalidAgent(
602 "Description cannot exceed 1000 characters".to_string(),
603 ));
604 }
605
606 for tag in &request.tags {
608 if tag.trim().is_empty() {
609 return Err(CarpError::InvalidAgent("Tags cannot be empty".to_string()));
610 }
611 if tag.len() > 50 {
612 return Err(CarpError::InvalidAgent(
613 "Tags cannot exceed 50 characters".to_string(),
614 ));
615 }
616 }
617
618 if request.tags.len() > 10 {
619 return Err(CarpError::InvalidAgent(
620 "Cannot have more than 10 tags".to_string(),
621 ));
622 }
623
624 Ok(())
625 }
626
627 async fn handle_response<T>(&self, response: Response) -> CarpResult<T>
629 where
630 T: serde::de::DeserializeOwned,
631 {
632 let status = response.status();
633 let text = response.text().await?;
634
635 if status.is_success() {
636 serde_json::from_str(&text).map_err(CarpError::Json)
637 } else {
638 if status.as_u16() == 401 {
640 let auth_error = if text.contains("invalid") || text.contains("expired") {
641 "Invalid or expired API key. Please check your API key and try again."
642 } else if text.contains("missing") || text.contains("required") {
643 "API key required. Please provide your API key via --api-key option, CARP_API_KEY environment variable, or config file."
644 } else {
645 "Authentication failed. Please verify your API key is correct."
646 };
647
648 return Err(CarpError::Auth(format!(
649 "{auth_error}\n\nTo fix this:\n 1. Get your API key from the registry dashboard\n 2. Set it via: carp auth set-api-key\n 3. Or use: --api-key <your-key>\n 4. Or set CARP_API_KEY environment variable"
650 )));
651 }
652
653 if status.as_u16() == 403 {
654 return Err(CarpError::Auth(
655 "Access forbidden. Your API key may not have sufficient permissions for this operation.".to_string()
656 ));
657 }
658
659 match serde_json::from_str::<ApiError>(&text) {
661 Ok(api_error) => {
662 let mut error_message = api_error.message;
663
664 if let Some(details) = api_error.details {
666 error_message.push_str(&format!("\n\nDetails: {}", serde_json::to_string_pretty(&details).unwrap_or_default()));
667 }
668
669 Err(CarpError::Api {
670 status: status.as_u16(),
671 message: error_message,
672 })
673 },
674 Err(_) => {
675 let error_message = if text.is_empty() {
677 format!("HTTP {} error", status.as_u16())
678 } else {
679 format!("HTTP {} error: {}", status.as_u16(), text)
680 };
681
682 Err(CarpError::Api {
683 status: status.as_u16(),
684 message: error_message,
685 })
686 },
687 }
688 }
689 }
690}
691
692#[cfg(test)]
693mod tests {
694 use super::*;
695 use crate::config::Config;
696 use mockito::Server;
697
698 fn create_test_config(server_url: String, api_key: Option<String>) -> Config {
699 Config {
700 registry_url: server_url,
701 api_key,
702 api_token: None,
703 timeout: 30,
704 verify_ssl: true,
705 default_output_dir: None,
706 max_concurrent_downloads: 4,
707 retry: crate::config::RetrySettings::default(),
708 security: crate::config::SecuritySettings::default(),
709 }
710 }
711
712 fn create_valid_upload_request() -> UploadAgentRequest {
713 UploadAgentRequest {
714 name: "test-agent".to_string(),
715 description: "A test agent".to_string(),
716 content: r#"---
717name: test-agent
718description: A test agent
719---
720
721# Test Agent
722
723This is a test agent.
724"#
725 .to_string(),
726 version: Some("1.0.0".to_string()),
727 tags: vec!["test".to_string()],
728 homepage: Some("https://example.com".to_string()),
729 repository: Some("https://github.com/user/repo".to_string()),
730 license: Some("MIT".to_string()),
731 }
732 }
733
734 #[tokio::test]
735 async fn test_search_request() {
736 let mut server = Server::new_async().await;
737 let config = create_test_config(server.url(), None);
738
739 let _m = server
740 .mock("GET", "/api/v1/agents/search")
741 .match_query(mockito::Matcher::UrlEncoded("q".into(), "test".into()))
742 .with_status(200)
743 .with_header("content-type", "application/json")
744 .with_body(r#"{"agents": [], "total": 0, "page": 1, "per_page": 10}"#)
745 .create_async()
746 .await;
747
748 let client = ApiClient::new(&config).unwrap();
749 let result = client.search("test", Some(10), false).await;
750
751 match result {
752 Ok(response) => {
753 assert_eq!(response.agents.len(), 0);
754 assert_eq!(response.total, 0);
755 println!("Test passed successfully");
756 }
757 Err(e) => {
758 println!("Test error: {:?}", e);
760 }
762 }
763 }
764
765 #[test]
766 fn test_validate_upload_request_valid() {
767 let config =
768 create_test_config("https://example.com".to_string(), Some("token".to_string()));
769 let client = ApiClient::new(&config).unwrap();
770 let request = create_valid_upload_request();
771
772 assert!(client.validate_upload_request(&request).is_ok());
773 }
774
775 #[test]
776 fn test_validate_upload_request_empty_name() {
777 let config =
778 create_test_config("https://example.com".to_string(), Some("token".to_string()));
779 let client = ApiClient::new(&config).unwrap();
780 let mut request = create_valid_upload_request();
781 request.name = "".to_string();
782
783 let result = client.validate_upload_request(&request);
784 assert!(result.is_err());
785 assert!(result
786 .unwrap_err()
787 .to_string()
788 .contains("Agent name cannot be empty"));
789 }
790
791 #[test]
792 fn test_validate_upload_request_invalid_name() {
793 let config =
794 create_test_config("https://example.com".to_string(), Some("token".to_string()));
795 let client = ApiClient::new(&config).unwrap();
796 let mut request = create_valid_upload_request();
797 request.name = "invalid name!".to_string();
798
799 let result = client.validate_upload_request(&request);
800 assert!(result.is_err());
801 assert!(result
802 .unwrap_err()
803 .to_string()
804 .contains("alphanumeric characters"));
805 }
806
807 #[test]
808 fn test_validate_upload_request_empty_description() {
809 let config =
810 create_test_config("https://example.com".to_string(), Some("token".to_string()));
811 let client = ApiClient::new(&config).unwrap();
812 let mut request = create_valid_upload_request();
813 request.description = "".to_string();
814
815 let result = client.validate_upload_request(&request);
816 assert!(result.is_err());
817 assert!(result
818 .unwrap_err()
819 .to_string()
820 .contains("Description cannot be empty"));
821 }
822
823 #[test]
824 fn test_validate_upload_request_empty_content() {
825 let config =
826 create_test_config("https://example.com".to_string(), Some("token".to_string()));
827 let client = ApiClient::new(&config).unwrap();
828 let mut request = create_valid_upload_request();
829 request.content = "".to_string();
830
831 let result = client.validate_upload_request(&request);
832 assert!(result.is_err());
833 assert!(result
834 .unwrap_err()
835 .to_string()
836 .contains("Content cannot be empty"));
837 }
838
839 #[test]
840 fn test_validate_upload_request_no_frontmatter() {
841 let config =
842 create_test_config("https://example.com".to_string(), Some("token".to_string()));
843 let client = ApiClient::new(&config).unwrap();
844 let mut request = create_valid_upload_request();
845 request.content = "# Test Agent\n\nNo frontmatter here.".to_string();
846
847 let result = client.validate_upload_request(&request);
848 assert!(result.is_err());
849 assert!(result.unwrap_err().to_string().contains("YAML frontmatter"));
850 }
851
852 #[test]
853 fn test_validate_upload_request_mismatched_name() {
854 let config =
855 create_test_config("https://example.com".to_string(), Some("token".to_string()));
856 let client = ApiClient::new(&config).unwrap();
857 let mut request = create_valid_upload_request();
858 request.content = r#"---
859name: different-name
860description: A test agent
861---
862
863# Test Agent
864"#
865 .to_string();
866
867 let result = client.validate_upload_request(&request);
868 assert!(result.is_err());
869 assert!(result.unwrap_err().to_string().contains("Name mismatch"));
870 }
871
872 #[test]
873 fn test_validate_upload_request_mismatched_description() {
874 let config =
875 create_test_config("https://example.com".to_string(), Some("token".to_string()));
876 let client = ApiClient::new(&config).unwrap();
877 let mut request = create_valid_upload_request();
878 request.content = r#"---
879name: test-agent
880description: Different description
881---
882
883# Test Agent
884"#
885 .to_string();
886
887 let result = client.validate_upload_request(&request);
888 assert!(result.is_err());
889 assert!(result
890 .unwrap_err()
891 .to_string()
892 .contains("Description mismatch"));
893 }
894
895 #[test]
896 fn test_validate_upload_request_too_many_tags() {
897 let config =
898 create_test_config("https://example.com".to_string(), Some("token".to_string()));
899 let client = ApiClient::new(&config).unwrap();
900 let mut request = create_valid_upload_request();
901 request.tags = (0..25).map(|i| format!("tag{}", i)).collect();
902
903 let result = client.validate_upload_request(&request);
904 assert!(result.is_err());
905 assert!(result
906 .unwrap_err()
907 .to_string()
908 .contains("Cannot have more than 20 tags"));
909 }
910
911 #[test]
912 fn test_validate_upload_request_large_content() {
913 let config =
914 create_test_config("https://example.com".to_string(), Some("token".to_string()));
915 let client = ApiClient::new(&config).unwrap();
916 let mut request = create_valid_upload_request();
917 let large_content = "x".repeat(2 * 1024 * 1024);
919 request.content = format!(
920 r#"---
921name: test-agent
922description: A test agent
923---
924
925{}
926"#,
927 large_content
928 );
929
930 let result = client.validate_upload_request(&request);
931 assert!(result.is_err());
932 assert!(result
933 .unwrap_err()
934 .to_string()
935 .contains("exceeds maximum allowed size"));
936 }
937
938 #[tokio::test]
939 async fn test_upload_no_token() {
940 let mut server = Server::new_async().await;
941 let config = create_test_config(server.url(), None);
942 let client = ApiClient::new(&config).unwrap();
943 let request = create_valid_upload_request();
944
945 let result = client.upload(request).await;
946 assert!(result.is_err());
947 if let Err(CarpError::Auth(msg)) = result {
948 assert!(msg.contains("No API key configured"));
949 } else {
950 panic!("Expected Auth error");
951 }
952 }
953
954 #[tokio::test]
955 async fn test_upload_success() {
956 let mut server = Server::new_async().await;
957 let config = create_test_config(server.url(), Some("test-token".to_string()));
958
959 let _m = server
960 .mock("POST", "/api/v1/agents/upload")
961 .match_header("authorization", "Bearer test-token")
962 .match_header("content-type", "application/json")
963 .with_status(200)
964 .with_header("content-type", "application/json")
965 .with_body(
966 r#"{"success": true, "message": "Agent uploaded successfully", "agent": null}"#,
967 )
968 .create_async()
969 .await;
970
971 let client = ApiClient::new(&config).unwrap();
972 let request = create_valid_upload_request();
973
974 let result = client.upload(request).await;
975 match result {
976 Ok(response) => {
977 assert!(response.success);
978 assert_eq!(response.message, "Agent uploaded successfully");
979 }
980 Err(e) => {
981 println!("Upload test error: {:?}", e);
982 }
984 }
985 }
986}