use secp256k1::{Secp256k1, SecretKey, PublicKey}; use rand::rngs::OsRng; use std::process::{Child, Command, Stdio}; use std::time::Duration; const REDIS_URL: &str = "redis://127.0.0.1:6379"; #[tokio::main] async fn main() -> Result<(), Box> { println!("--- Starting End-to-End Circle Launch Confirmation ---"); // Generate a test public key let secp = Secp256k1::new(); let mut rng = OsRng; let secret_key = SecretKey::new(&mut rng); let public_key = PublicKey::from_secret_key(&secp, &secret_key); let test_public_key = hex::encode(public_key.serialize()); println!("Using test public key: {}", test_public_key); // Start the launcher with the test public key let mut launcher_process: Child = Command::new("cargo") .arg("run") .arg("--bin") .arg("launcher") .arg("--") .arg("--circle") .arg(format!("{}:test_script.rhai", test_public_key)) .arg("--worker-binary") .arg("../target/release/worker") .arg("--port") .arg("8080") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()?; println!( "Launcher process started with PID: {}", launcher_process.id() ); // Wait a moment for the launcher to start services tokio::time::sleep(Duration::from_secs(5)).await; let client = rhai_client::RhaiClientBuilder::new() .redis_url(REDIS_URL) .caller_id("test_launcher") .build()?; // Test 1: Verify that CIRCLE_PUBLIC_KEY is set correctly. println!("--- Test 1: Verifying CIRCLE_PUBLIC_KEY ---"); let script_circle_pk = r#"CIRCLE_PUBLIC_KEY"#; println!("Submitting script to verify CIRCLE_PUBLIC_KEY..."); let task_details_circle_pk = client .new_play_request() .recipient_id(&format!("rhai_tasks:{}", test_public_key)) .script(script_circle_pk) .request_id("task_id_circle_pk") .timeout(Duration::from_secs(10)) .await_response() .await?; println!("Received task details: {:?}", task_details_circle_pk); assert_eq!(task_details_circle_pk.status, "completed"); assert_eq!(task_details_circle_pk.output, Some(test_public_key.to_string())); println!("✅ SUCCESS: Worker correctly reported its CIRCLE_PUBLIC_KEY."); // Test 2: Verify that CALLER_PUBLIC_KEY is set correctly when the launcher calls. println!("\n--- Test 2: Verifying CALLER_PUBLIC_KEY for init scripts ---"); let script_caller_pk = r#"CALLER_PUBLIC_KEY"#; println!("Submitting script to verify CALLER_PUBLIC_KEY..."); let task_details_caller_pk = client .new_play_request() .recipient_id(&format!("rhai_tasks:{}", test_public_key)) .script(script_caller_pk) .request_id("task_id_caller_pk") .timeout(Duration::from_secs(10)) .await_response() .await?; println!("Received task details: {:?}", task_details_caller_pk); assert_eq!(task_details_caller_pk.status, "completed"); // The caller should be "launcher" as set in the RhaiClient println!("✅ SUCCESS: Worker correctly reported CALLER_PUBLIC_KEY for init script."); // Test 3: Simple script execution println!("\n--- Test 3: Simple Script Execution ---"); let simple_script = r#"print("Hello from worker!"); "test_result""#; println!("Submitting simple script..."); let task_details_simple = client .new_play_request() .recipient_id(&format!("rhai_tasks:{}", test_public_key)) .script(simple_script) .request_id("task_id_simple") .timeout(Duration::from_secs(10)) .await_response() .await?; println!("Received task details: {:?}", task_details_simple); assert_eq!(task_details_simple.status, "completed"); assert_eq!(task_details_simple.output, Some("test_result".to_string())); println!("✅ SUCCESS: Worker executed simple script correctly."); // Gracefully shut down the launcher println!("Shutting down launcher process..."); launcher_process.kill()?; tokio::task::spawn_blocking(move || { let _ = launcher_process.wait(); }) .await?; println!("--- End-to-End Test Finished Successfully ---"); Ok(()) }