streamweave 0.10.1

Composable, async, stream-first computation in pure Rust
Documentation
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! # Node Trait Test Suite - Stream-Based
//!
//! Comprehensive test suite for the [`Node`] trait with stream-based architecture,
//! including name management, port queries, and stream execution.
//!
//! ## Test Coverage
//!
//! This test suite covers:
//!
//! - **Name Management**: Getting and setting node names
//! - **Port Queries**: Querying input and output port names
//! - **Port Validation**: Checking if ports exist by name
//! - **Stream Execution**: Executing nodes with input streams and producing output streams

use crate::node::{InputStreams, Node, NodeExecutionError, OutputStreams};
use async_trait::async_trait;
use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tokio_stream::{Stream, StreamExt};

// ============================================================================
// Mock Node Implementations for Testing
// ============================================================================

/// Mock producer node (0 inputs, 1 output)
struct MockProducerNode {
  name: String,
  output_port_names: Vec<String>,
}

impl MockProducerNode {
  fn new(name: String) -> Self {
    Self {
      name,
      output_port_names: vec!["out".to_string()],
    }
  }
}

#[async_trait]
impl Node for MockProducerNode {
  fn name(&self) -> &str {
    &self.name
  }

  fn set_name(&mut self, name: &str) {
    self.name = name.to_string();
  }

  fn input_port_names(&self) -> &[String] {
    &[]
  }

  fn output_port_names(&self) -> &[String] {
    &self.output_port_names
  }

  fn has_input_port(&self, _name: &str) -> bool {
    false
  }

  fn has_output_port(&self, name: &str) -> bool {
    self.output_port_names.contains(&name.to_string())
  }

  fn execute(
    &self,
    _inputs: InputStreams,
  ) -> Pin<
    Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
  > {
    Box::pin(async move {
      let (tx, rx) = mpsc::channel(10);

      // Send some test data
      tokio::spawn(async move {
        for i in 0..3 {
          let _ = tx.send(Arc::new(i) as Arc<dyn Any + Send + Sync>).await;
        }
      });

      let stream: OutputStreams = {
        let mut map = HashMap::new();
        map.insert(
          "out".to_string(),
          Box::pin(ReceiverStream::new(rx))
            as Pin<Box<dyn Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
        );
        map
      };

      Ok(stream)
    })
  }
}

/// Mock transform node (1 input, 1 output)
struct MockTransformNode {
  name: String,
  input_port_names: Vec<String>,
  output_port_names: Vec<String>,
}

impl MockTransformNode {
  fn new(name: String) -> Self {
    Self {
      name,
      input_port_names: vec!["in".to_string()],
      output_port_names: vec!["out".to_string()],
    }
  }
}

#[async_trait]
impl Node for MockTransformNode {
  fn name(&self) -> &str {
    &self.name
  }

  fn set_name(&mut self, name: &str) {
    self.name = name.to_string();
  }

  fn input_port_names(&self) -> &[String] {
    &self.input_port_names
  }

  fn output_port_names(&self) -> &[String] {
    &self.output_port_names
  }

  fn has_input_port(&self, name: &str) -> bool {
    self.input_port_names.contains(&name.to_string())
  }

  fn has_output_port(&self, name: &str) -> bool {
    self.output_port_names.contains(&name.to_string())
  }

  fn execute(
    &self,
    mut inputs: InputStreams,
  ) -> Pin<
    Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
  > {
    Box::pin(async move {
      let input_stream = inputs.remove("in").ok_or("Missing 'in' input")?;

      let output_stream: OutputStreams = {
        let mut map = HashMap::new();
        map.insert(
          "out".to_string(),
          Box::pin(async_stream::stream! {
            let mut input = input_stream;
            while let Some(item) = input.next().await {
              // Double the value if it's an i32
              if let Ok(arc_i32) = item.clone().downcast::<i32>() {
                yield Arc::new(*arc_i32 * 2) as Arc<dyn Any + Send + Sync>;
              } else {
                yield item;
              }
            }
          }) as Pin<Box<dyn Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
        );
        map
      };

      Ok(output_stream)
    })
  }
}

/// Mock sink node (1 input, 0 outputs)
struct MockSinkNode {
  name: String,
  input_port_names: Vec<String>,
  received: Arc<tokio::sync::Mutex<Vec<Arc<dyn Any + Send + Sync>>>>,
}

impl MockSinkNode {
  fn new(name: String) -> Self {
    Self {
      name,
      input_port_names: vec!["in".to_string()],
      received: Arc::new(tokio::sync::Mutex::new(Vec::new())),
    }
  }

  #[allow(dead_code)]
  async fn get_received(&self) -> Vec<Arc<dyn Any + Send + Sync>> {
    self.received.lock().await.clone()
  }
}

#[async_trait]
impl Node for MockSinkNode {
  fn name(&self) -> &str {
    &self.name
  }

  fn set_name(&mut self, name: &str) {
    self.name = name.to_string();
  }

  fn input_port_names(&self) -> &[String] {
    &self.input_port_names
  }

  fn output_port_names(&self) -> &[String] {
    &[]
  }

  fn has_input_port(&self, name: &str) -> bool {
    self.input_port_names.contains(&name.to_string())
  }

  fn has_output_port(&self, _name: &str) -> bool {
    false
  }

  fn execute(
    &self,
    mut inputs: InputStreams,
  ) -> Pin<
    Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
  > {
    let received = Arc::clone(&self.received);
    Box::pin(async move {
      let input_stream = inputs.remove("in").ok_or("Missing 'in' input")?;

      // Consume the stream
      tokio::spawn(async move {
        let mut input = input_stream;
        while let Some(item) = input.next().await {
          received.lock().await.push(item);
        }
      });

      Ok(HashMap::new())
    })
  }
}

// ============================================================================
// Name Management Tests
// ============================================================================

#[tokio::test]
async fn test_node_name_get() {
  let node = MockProducerNode::new("test_node".to_string());
  assert_eq!(node.name(), "test_node");
}

#[tokio::test]
async fn test_node_name_set() {
  let mut node = MockProducerNode::new("old_name".to_string());
  node.set_name("new_name");
  assert_eq!(node.name(), "new_name");
}

// ============================================================================
// Port Query Tests
// ============================================================================

#[tokio::test]
async fn test_producer_node_ports() {
  let node = MockProducerNode::new("producer".to_string());
  assert_eq!(node.input_port_names().len(), 0);
  assert_eq!(node.output_port_names().len(), 1);
  assert_eq!(node.output_port_names()[0], "out");
}

#[tokio::test]
async fn test_transform_node_ports() {
  let node = MockTransformNode::new("transform".to_string());
  assert_eq!(node.input_port_names().len(), 1);
  assert_eq!(node.input_port_names()[0], "in");
  assert_eq!(node.output_port_names().len(), 1);
  assert_eq!(node.output_port_names()[0], "out");
}

#[tokio::test]
async fn test_sink_node_ports() {
  let node = MockSinkNode::new("sink".to_string());
  assert_eq!(node.input_port_names().len(), 1);
  assert_eq!(node.input_port_names()[0], "in");
  assert_eq!(node.output_port_names().len(), 0);
}

// ============================================================================
// Port Validation Tests
// ============================================================================

#[tokio::test]
async fn test_has_input_port() {
  let node = MockTransformNode::new("transform".to_string());
  assert!(node.has_input_port("in"));
  assert!(!node.has_input_port("out"));
  assert!(!node.has_input_port("invalid"));
}

#[tokio::test]
async fn test_has_output_port() {
  let node = MockTransformNode::new("transform".to_string());
  assert!(node.has_output_port("out"));
  assert!(!node.has_output_port("in"));
  assert!(!node.has_output_port("invalid"));
}

// ============================================================================
// Stream Execution Tests
// ============================================================================

#[tokio::test]
async fn test_producer_node_execution() {
  let node = MockProducerNode::new("producer".to_string());
  let inputs = HashMap::new();
  let mut outputs = node.execute(inputs).await.unwrap();

  assert_eq!(outputs.len(), 1);
  assert!(outputs.contains_key("out"));

  // Consume the output stream
  let mut stream = outputs.remove("out").unwrap();
  let mut count = 0;
  while let Some(_item) = stream.next().await {
    count += 1;
  }
  assert_eq!(count, 3);
}

#[tokio::test]
async fn test_transform_node_execution() {
  let node = MockTransformNode::new("transform".to_string());

  // Create input stream
  let (tx, rx) = mpsc::channel(10);
  let mut inputs = HashMap::new();
  inputs.insert(
    "in".to_string(),
    Box::pin(ReceiverStream::new(rx))
      as Pin<Box<dyn Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
  );

  // Send test data
  tokio::spawn(async move {
    for i in 1..=3 {
      let _ = tx.send(Arc::new(i) as Arc<dyn Any + Send + Sync>).await;
    }
  });

  // Execute node
  let mut outputs = node.execute(inputs).await.unwrap();
  assert_eq!(outputs.len(), 1);
  assert!(outputs.contains_key("out"));

  // Consume output stream and verify transformation
  let mut stream = outputs.remove("out").unwrap();
  let mut results = Vec::new();
  while let Some(item) = stream.next().await {
    if let Ok(arc_i32) = item.downcast::<i32>() {
      results.push(*arc_i32);
    }
  }

  assert_eq!(results, vec![2, 4, 6]);
}

#[tokio::test]
async fn test_sink_node_execution() {
  let node = MockSinkNode::new("sink".to_string());

  // Create input stream
  let (tx, rx) = mpsc::channel(10);
  let mut inputs = HashMap::new();
  inputs.insert(
    "in".to_string(),
    Box::pin(ReceiverStream::new(rx))
      as Pin<Box<dyn Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
  );

  // Send test data
  tokio::spawn(async move {
    for i in 1..=3 {
      let _ = tx.send(Arc::new(i) as Arc<dyn Any + Send + Sync>).await;
    }
  });

  // Execute node
  let outputs = node.execute(inputs).await.unwrap();
  assert_eq!(outputs.len(), 0);

  // Wait a bit for processing
  tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

  // Check received data
  let received = node.get_received().await;
  assert_eq!(received.len(), 3);
}

#[tokio::test]
async fn test_node_execution_missing_input() {
  let node = MockTransformNode::new("transform".to_string());
  let inputs = HashMap::new(); // Missing "in" input

  let result = node.execute(inputs).await;
  assert!(result.is_err());
  // Just verify it's an error - we can't format the error due to Debug trait bounds
  // The error type contains streams which don't implement Debug
}