Skip to main content

coman/cli/
test_ops.rs

1//! Test runner for API collections
2//!
3//! This module provides the test command functionality for the CLI.
4
5use colored::Colorize;
6
7use crate::cli::{commands::Commands, manager::ManagerCommands, request::RequestCommands};
8use crate::core::collection_manager::CollectionManager;
9
10impl Commands {
11    pub async fn run_tests(&self, collection_name: &str) -> Result<(), Box<dyn std::error::Error>> {
12        let manager = CollectionManager::default();
13        let collection = manager
14            .get_collection(collection_name)
15            .await
16            .unwrap()
17            .ok_or_else(|| format!("Collection '{}' not found", collection_name))?;
18
19        if let Some(requests) = &collection.requests {
20            for request in requests {
21                let command = ManagerCommands::get_endpoint_command(collection_name, &request.name)
22                    .await
23                    .ok_or_else(|| {
24                        format!(
25                            "Endpoint '{}' not found in collection '{}'",
26                            request.name, collection_name
27                        )
28                    })?;
29
30                let stdin_input = Vec::new();
31                // Run the request
32                match command.execute_request(false, stdin_input, false).await {
33                    Ok((response, elapsed)) => {
34                        // Print the test result in the same format as print_request_method
35                        println!(
36                            "[{}] {} - {} ({} ms)\n",
37                            command.to_string().bold().bright_yellow(),
38                            response.url.bold().bright_white(),
39                            RequestCommands::colorize_status(response.status),
40                            elapsed
41                        );
42                    }
43                    Err(e) => {
44                        // Print error message and continue
45                        println!("Failed: {}", e);
46                    }
47                }
48            }
49        } else {
50            println!("No requests found in collection '{}'", collection_name);
51        }
52
53        println!("All tests completed for collection '{}'", collection_name);
54        Ok(())
55    }
56}