use crate::node::{InputStreams, Node};
use crate::nodes::string::StringConcatNode;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
type AnySender = mpsc::Sender<Arc<dyn Any + Send + Sync>>;
fn create_input_streams() -> (AnySender, AnySender, AnySender, InputStreams) {
let (config_tx, config_rx) = mpsc::channel(10);
let (in1_tx, in1_rx) = mpsc::channel(10);
let (in2_tx, in2_rx) = mpsc::channel(10);
let mut inputs = HashMap::new();
inputs.insert(
"configuration".to_string(),
Box::pin(ReceiverStream::new(config_rx)) as crate::node::InputStream,
);
inputs.insert(
"in1".to_string(),
Box::pin(ReceiverStream::new(in1_rx)) as crate::node::InputStream,
);
inputs.insert(
"in2".to_string(),
Box::pin(ReceiverStream::new(in2_rx)) as crate::node::InputStream,
);
(config_tx, in1_tx, in2_tx, inputs)
}
#[tokio::test]
async fn test_string_concat_node_creation() {
let node = StringConcatNode::new("test_concat".to_string());
assert_eq!(node.name(), "test_concat");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in1"));
assert!(node.has_input_port("in2"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_string_concat_basic() {
let node = StringConcatNode::new("test_concat".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new("World".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = item.downcast::<String>() {
results.push(arc_str.clone());
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(*results[0], "HelloWorld");
}
#[tokio::test]
async fn test_string_concat_empty_strings() {
let node = StringConcatNode::new("test_concat".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new("".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = item.downcast::<String>() {
results.push(arc_str.clone());
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(*results[0], "");
}
#[tokio::test]
async fn test_string_concat_with_spaces() {
let node = StringConcatNode::new("test_concat".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("Hello ".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new("World".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = item.downcast::<String>() {
results.push(arc_str.clone());
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 1);
assert_eq!(*results[0], "Hello World");
}
#[tokio::test]
async fn test_string_concat_non_string_input() {
let node = StringConcatNode::new("test_concat".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new(42i32) as Arc<dyn Any + Send + Sync>)
.await;
let error_stream = outputs.remove("error").unwrap();
let mut errors = Vec::new();
let mut stream = error_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(200));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = item.downcast::<String>() {
errors.push(arc_str.clone());
break;
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(errors.len(), 1);
assert!(errors[0].contains("Unsupported types for string concatenation"));
}
#[tokio::test]
async fn test_string_concat_multiple_pairs() {
let node = StringConcatNode::new("test_concat".to_string());
let (_config_tx, in1_tx, in2_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in1_tx
.send(Arc::new("A".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new("B".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in1_tx
.send(Arc::new("C".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = in2_tx
.send(Arc::new("D".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let out_stream = outputs.remove("out").unwrap();
let mut results = Vec::new();
let mut stream = out_stream;
let timeout = tokio::time::sleep(tokio::time::Duration::from_millis(500));
tokio::pin!(timeout);
loop {
tokio::select! {
result = stream.next() => {
if let Some(item) = result {
if let Ok(arc_str) = item.downcast::<String>() {
results.push(arc_str.clone());
if results.len() >= 2 {
break;
}
}
} else {
break;
}
}
_ = &mut timeout => break,
}
}
assert_eq!(results.len(), 2);
assert_eq!(*results[0], "AB");
assert_eq!(*results[1], "CD");
}