use std::time::Duration;
use tokio::time::timeout;
#[tokio::test]
async fn test_api_execute_returns_200_not_404() {
let result = timeout(Duration::from_secs(5), test_real_api_call()).await;
match result {
Ok(success) => {
assert!(success, "API call should succeed");
}
Err(_) => {
panic!("Test timed out - server not responding");
}
}
}
async fn test_real_api_call() -> bool {
use std::process::Command;
let mut server = Command::new("cargo")
.args(&["run", "--bin", "ruchy", "--", "notebook", "--port", "8890"])
.spawn()
.expect("Failed to start server");
tokio::time::sleep(Duration::from_secs(2)).await;
let output = Command::new("curl")
.args(&[
"-s",
"-w",
"%{http_code}",
"-X",
"POST",
"http://localhost:8890/api/execute",
"-H",
"Content-Type: application/json",
"-d",
r#"{"code":"2 + 2","cell_id":"test","session_id":"test"}"#,
])
.output()
.expect("Failed to run curl");
let _ = server.kill();
let response = String::from_utf8_lossy(&output.stdout);
println!("API Response: {}", response);
!response.contains("404")
}
#[test]
fn test_route_registration_in_code() {
let source = std::fs::read_to_string("ruchy-notebook/src/server/mod.rs")
.expect("Could not read server module");
assert!(
source.contains("/api/execute"),
"Route /api/execute not found in server code!"
);
assert!(
source.contains("post(execute_code_handler)"),
"POST handler for execute_code_handler not found!"
);
println!("✅ Route registration found in source code");
}