Repository navigation
Expand file tree
/
Copy pathtest_response.rs
More file actions
120 lines (105 loc) · 4.62 KB
/
Copy pathtest_response.rs
File metadata and controls
120 lines (105 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Test to verify iFlow response handling
use futures::stream::StreamExt;
use iflow_cli_sdk_rust::{IFlowClient, IFlowOptions, Message};
use iflow_cli_sdk_rust::error::IFlowError;
use std::time::Duration;
use tokio::time::timeout;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging with environment variable support
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
println!("🧪 Testing iFlow response handling...");
// Use LocalSet for spawn_local compatibility
let local = tokio::task::LocalSet::new();
local
.run_until(async {
// Configure client options with auto-start enabled for stdio mode
let options = IFlowOptions::new().with_timeout(30.0).with_process_config(
iflow_cli_sdk_rust::types::ProcessConfig::new()
.enable_auto_start()
.stdio_mode(),
);
let mut client = IFlowClient::new(Some(options));
println!("🔗 Connecting to iFlow...");
client.connect().await?;
println!("✅ Connected to iFlow");
// Send a simple test message
let prompt = "Hello! Please reply with 'Hello back!' to confirm you're working.";
println!("📤 Sending: {}", prompt);
// Handle the send_message result to catch timeout errors
match client.send_message(prompt, None).await {
Ok(()) => {
println!("✅ Message sent successfully");
}
Err(IFlowError::Timeout(msg)) => {
eprintln!("⏰ Timeout error occurred: {}", msg);
eprintln!("This may be due to processing delays.");
eprintln!("Consider increasing the timeout or checking the iFlow process.");
}
Err(e) => {
eprintln!("❌ Error sending message: {}", e);
return Err(e.into());
}
}
// Wait for response with timeout
println!("⏳ Waiting for response...");
let mut message_stream = client.messages();
let result = timeout(Duration::from_secs(10), async {
let mut response_received = false;
while let Some(message) = message_stream.next().await {
match message {
Message::Assistant { content } => {
println!("📝 Received assistant response: {}", content);
if content.contains("Hello back") || content.contains("hello") {
println!("✅ SUCCESS: Received expected response!");
response_received = true;
break;
}
}
Message::User { content } => {
println!("👤 User message echo: {}", content);
if content.contains("Hello!") {
println!("ℹ️ Received our own message echo");
}
}
Message::TaskFinish { reason } => {
println!("🏁 Task finished: {:?}", reason);
break;
}
Message::Error {
code,
message,
details: _,
} => {
println!("❌ Error {}: {}", code, message);
break;
}
_ => {
println!("📨 Other message type: {:?}", message);
}
}
}
response_received
})
.await;
match result {
Ok(true) => {
println!("🎉 TEST PASSED: Received expected response from iFlow!");
}
Ok(false) => {
println!("⚠️ TEST INCONCLUSIVE: No matching response received");
}
Err(_) => {
println!("⏰ TEST FAILED: Timeout waiting for response");
}
}
// Disconnect
println!("\n🔌 Disconnecting...");
client.disconnect().await?;
println!("👋 Disconnected from iFlow");
Ok(())
})
.await
}