use crate::node::{InputStreams, Node};
use crate::nodes::string::StringSliceNode;
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, AnySender, InputStreams) {
let (config_tx, config_rx) = mpsc::channel(10);
let (in_tx, in_rx) = mpsc::channel(10);
let (start_tx, start_rx) = mpsc::channel(10);
let (end_tx, end_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(
"in".to_string(),
Box::pin(ReceiverStream::new(in_rx)) as crate::node::InputStream,
);
inputs.insert(
"start".to_string(),
Box::pin(ReceiverStream::new(start_rx)) as crate::node::InputStream,
);
inputs.insert(
"end".to_string(),
Box::pin(ReceiverStream::new(end_rx)) as crate::node::InputStream,
);
(config_tx, in_tx, start_tx, end_tx, inputs)
}
#[tokio::test]
async fn test_string_slice_node_creation() {
let node = StringSliceNode::new("test_slice".to_string());
assert_eq!(node.name(), "test_slice");
assert!(node.has_input_port("configuration"));
assert!(node.has_input_port("in"));
assert!(node.has_input_port("start"));
assert!(node.has_input_port("end"));
assert!(node.has_output_port("out"));
assert!(node.has_output_port("error"));
}
#[tokio::test]
async fn test_string_slice_basic() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello World".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(0usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(5usize) 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");
}
#[tokio::test]
async fn test_string_slice_middle() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello World".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(6usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(11usize) 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], "World");
}
#[tokio::test]
async fn test_string_slice_empty_result() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(2usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(2usize) 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_slice_with_i32_indices() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(1i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(4i32) 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], "ell");
}
#[tokio::test]
async fn test_string_slice_invalid_start_gt_end() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(4usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(2usize) 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("start") && errors[0].contains("end"));
}
#[tokio::test]
async fn test_string_slice_out_of_bounds_start() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(10usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(15usize) 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("out of bounds"));
}
#[tokio::test]
async fn test_string_slice_out_of_bounds_end() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(0usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(10usize) 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("out of bounds"));
}
#[tokio::test]
async fn test_string_slice_non_string_input() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new(42i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(0usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(2usize) 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("input must be String"));
}
#[tokio::test]
async fn test_string_slice_negative_index() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let _ = in_tx
.send(Arc::new("Hello".to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(-1i32) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(2usize) 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("cannot be negative"));
}
#[tokio::test]
async fn test_string_slice_unicode_characters() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let test_str = "Unicode: 🚀".to_string();
let _ = in_tx
.send(Arc::new(test_str.clone()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(9usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(10usize) 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_slice_unicode_middle_of_multibyte() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let test_str = "Hello 🚀 World".to_string();
let _ = in_tx
.send(Arc::new(test_str.clone()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(6usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(7usize) 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_slice_unicode_multiple_chars() {
let node = StringSliceNode::new("test_slice".to_string());
let (_config_tx, in_tx, start_tx, end_tx, inputs) = create_input_streams();
let outputs_future = node.execute(inputs);
let mut outputs = outputs_future.await.unwrap();
let test_str = "🚀🌍".to_string();
let _ = in_tx
.send(Arc::new(test_str.clone()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = start_tx
.send(Arc::new(0usize) as Arc<dyn Any + Send + Sync>)
.await;
let _ = end_tx
.send(Arc::new(2usize) 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], "🚀🌍");
}