use serde_json::json;
use std::process::{Child, Command};
use std::time::Duration;
#[tokio::test]
async fn test_query_rs_e2e_integration() {
println!("๐งช QueryRs End-to-End Integration Test");
println!("=====================================");
let server_process = start_test_server().await;
tokio::time::sleep(Duration::from_secs(5)).await;
match test_search_endpoint().await {
Ok(response) => {
println!("โ
Search endpoint test successful");
validate_search_response(&response).await;
test_document_persistence().await;
test_summarization_readiness().await;
println!("\n๐ END-TO-END INTEGRATION TEST PASSED!");
println!(
"๐ Complete flow working: Server -> Search -> Persistence -> Summarization Ready"
);
}
Err(e) => {
println!("โ ๏ธ Search endpoint test failed: {}", e);
println!("๐ This may be due to network issues or server startup time");
}
}
if let Some(mut process) = server_process {
let _ = process.kill();
let _ = process.wait();
println!("๐งน Test server stopped");
}
}
async fn start_test_server() -> Option<Child> {
println!("๐ Starting test server...");
let build_result = Command::new("cargo")
.args(["build", "--release", "--bin", "terraphim_server"])
.current_dir(".")
.output();
match build_result {
Ok(output) => {
if !output.status.success() {
println!(
"โ Failed to build server: {}",
String::from_utf8_lossy(&output.stderr)
);
return None;
}
println!("โ
Server built successfully");
}
Err(e) => {
println!("โ Failed to build server: {}", e);
return None;
}
}
let server_result = Command::new("./target/release/terraphim_server")
.args([
"--config",
"terraphim_server/default/combined_roles_config.json",
])
.current_dir(".")
.spawn();
match server_result {
Ok(process) => {
println!("โ
Test server started (PID: {})", process.id());
Some(process)
}
Err(e) => {
println!("โ Failed to start server: {}", e);
None
}
}
}
async fn test_search_endpoint() -> Result<serde_json::Value, Box<dyn std::error::Error>> {
println!("๐ Testing search endpoint...");
let client = reqwest::Client::new();
let search_payload = json!({
"search_term": "tokio",
"role": "Rust Engineer"
});
let response = client
.post("http://localhost:8000/documents/search")
.header("Content-Type", "application/json")
.json(&search_payload)
.send()
.await?;
if !response.status().is_success() {
return Err(format!("Search request failed with status: {}", response.status()).into());
}
let response_json: serde_json::Value = response.json().await?;
println!("โ
Search request successful");
Ok(response_json)
}
async fn validate_search_response(response: &serde_json::Value) {
println!("๐ Validating search response structure...");
assert!(
response.get("total").is_some(),
"Response should have 'total' field"
);
assert!(
response.get("results").is_some(),
"Response should have 'results' field"
);
let total = response.get("total").unwrap().as_u64().unwrap_or(0);
let empty_vec = vec![];
let results = response
.get("results")
.unwrap()
.as_array()
.unwrap_or(&empty_vec);
println!(" ๐ Total results: {}", total);
println!(" ๐ Results array length: {}", results.len());
assert!(total > 0, "Should have some search results");
assert!(!results.is_empty(), "Results array should not be empty");
if let Some(first_result) = results.first() {
assert!(
first_result.get("id").is_some(),
"Result should have 'id' field"
);
assert!(
first_result.get("title").is_some(),
"Result should have 'title' field"
);
assert!(
first_result.get("url").is_some(),
"Result should have 'url' field"
);
let result_id = first_result.get("id").unwrap().as_str().unwrap();
let result_title = first_result.get("title").unwrap().as_str().unwrap();
println!(" ๐ Sample result:");
println!(" ID: {}", result_id);
println!(" Title: {}", result_title);
}
println!("โ
Search response structure validated");
}
async fn test_document_persistence() {
println!("๐พ Testing document persistence...");
let client = reqwest::Client::new();
let search_payload = json!({
"search_term": "async",
"role": "Rust Engineer"
});
match client
.post("http://localhost:8000/documents/search")
.header("Content-Type", "application/json")
.json(&search_payload)
.send()
.await
{
Ok(response) => {
if response.status().is_success() {
let response_json: serde_json::Value = response.json().await.unwrap();
let results = response_json.get("results").unwrap().as_array().unwrap();
println!(" ๐ Found {} results for persistence test", results.len());
let mut valid_results = 0;
for result in results.iter().take(3) {
if result.get("id").is_some()
&& result.get("title").is_some()
&& result.get("body").is_some()
{
valid_results += 1;
}
}
assert!(
valid_results > 0,
"Should have valid results with proper structure"
);
println!(
" โ
{} results have valid structure for persistence",
valid_results
);
}
}
Err(e) => {
println!(" โ ๏ธ Persistence test failed: {}", e);
}
}
println!("โ
Document persistence test completed");
}
async fn test_summarization_readiness() {
println!("๐ค Testing summarization readiness...");
let client = reqwest::Client::new();
let search_payload = json!({
"search_term": "rust-performance",
"role": "Rust Engineer"
});
match client
.post("http://localhost:8000/documents/search")
.header("Content-Type", "application/json")
.json(&search_payload)
.send()
.await
{
Ok(response) => {
if response.status().is_success() {
let response_json: serde_json::Value = response.json().await.unwrap();
let summarization_tasks = response_json.get("summarization_tasks");
match summarization_tasks {
Some(tasks) => {
if tasks.is_array() {
let task_count = tasks.as_array().unwrap().len();
println!(" ๐ Summarization tasks: {}", task_count);
if task_count > 0 {
println!(" โ
Summarization tasks created successfully");
} else {
println!(" โ ๏ธ No summarization tasks created (may be expected)");
}
} else if tasks.is_null() {
println!(" โ ๏ธ Summarization tasks field is null");
} else {
println!(" โ ๏ธ Unexpected summarization_tasks format");
}
}
None => {
println!(" โ ๏ธ No summarization_tasks field in response");
}
}
let total = response_json.get("total").unwrap().as_u64().unwrap_or(0);
let results = response_json.get("results").unwrap().as_array().unwrap();
println!(" ๐ Total: {}, Results: {}", total, results.len());
if !results.is_empty() {
println!(" โ
Results available for summarization");
}
}
}
Err(e) => {
println!(" โ ๏ธ Summarization readiness test failed: {}", e);
}
}
println!("โ
Summarization readiness test completed");
}
#[tokio::test]
async fn test_server_configuration() {
println!("๐งช Server Configuration Test");
println!("============================");
let config_path = "terraphim_server/default/combined_roles_config.json";
match std::fs::read_to_string(config_path) {
Ok(config_content) => {
println!("โ
Configuration file found: {}", config_path);
match serde_json::from_str::<serde_json::Value>(&config_content) {
Ok(config) => {
println!("โ
Configuration file is valid JSON");
if let Some(roles) = config.get("roles") {
if let Some(rust_engineer) = roles.get("Rust Engineer") {
println!("โ
Rust Engineer role found in configuration");
if let Some(haystacks) = rust_engineer.get("haystacks") {
if let Some(haystack_array) = haystacks.as_array() {
let mut found_queryrs = false;
for haystack in haystack_array {
if let Some(service) = haystack.get("service") {
if service == "QueryRs" {
found_queryrs = true;
println!("โ
QueryRs haystack found");
if let Some(extra_params) =
haystack.get("extra_parameters")
{
if let Some(disable_enhancement) = extra_params
.get("disable_content_enhancement")
{
if disable_enhancement == "true" {
println!(
"โ
disable_content_enhancement is set to true"
);
} else {
println!(
"โ ๏ธ disable_content_enhancement is not set to true"
);
}
} else {
println!(
"โ ๏ธ disable_content_enhancement parameter not found"
);
}
} else {
println!("โ ๏ธ extra_parameters not found");
}
break;
}
}
}
if !found_queryrs {
println!(
"โ QueryRs haystack not found in Rust Engineer role"
);
}
} else {
println!("โ haystacks is not an array");
}
} else {
println!("โ haystacks not found in Rust Engineer role");
}
if let Some(llm_auto_summarize) =
rust_engineer.get("llm_auto_summarize")
{
if llm_auto_summarize == true {
println!("โ
llm_auto_summarize is enabled");
} else {
println!("โ ๏ธ llm_auto_summarize is disabled");
}
} else {
println!("โ ๏ธ llm_auto_summarize not found in configuration");
}
if let Some(llm_provider) = rust_engineer.get("llm_provider") {
println!("โ
llm_provider: {}", llm_provider);
} else {
println!("โ ๏ธ llm_provider not found in configuration");
}
} else {
println!("โ Rust Engineer role not found in configuration");
}
} else {
println!("โ roles not found in configuration");
}
}
Err(e) => {
println!("โ Configuration file is not valid JSON: {}", e);
}
}
}
Err(e) => {
println!("โ Configuration file not found: {}", e);
}
}
println!("\nโ
Server configuration test completed");
}