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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! # Match Node Test Suite
//!
//! Comprehensive test suite for the MatchNode implementation.

use crate::node::{InputStreams, Node};
use crate::nodes::common::TestSender;
use crate::nodes::match_node::{
  MatchConfig, MatchNode, match_config, match_exact_string, match_numeric_range, match_regex,
};
use regex::Regex;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};

/// Helper to create input streams from channels
fn create_input_streams() -> (TestSender, TestSender, InputStreams) {
  let (config_tx, config_rx) = mpsc::channel(10);
  let (data_tx, data_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(data_rx)) as crate::node::InputStream,
  );

  (config_tx, data_tx, inputs)
}

#[tokio::test]
async fn test_match_node_creation() {
  let node = MatchNode::new("test_match".to_string(), 3);
  assert_eq!(node.name(), "test_match");
  assert_eq!(node.max_branches(), 3);
  assert!(!node.has_config());
  assert!(node.has_input_port("configuration"));
  assert!(node.has_input_port("in"));
  assert!(node.has_output_port("out_0"));
  assert!(node.has_output_port("out_1"));
  assert!(node.has_output_port("out_2"));
  assert!(node.has_output_port("default"));
  assert!(node.has_output_port("error"));
}

#[tokio::test]
async fn test_match_node_numeric_range() {
  let node = MatchNode::new("test_match".to_string(), 2);

  // Create a config that routes based on number ranges
  // Negative -> out_0, 0-10 -> out_1, 10+ -> default
  let config: MatchConfig = match_numeric_range(|n: i32| async move {
    if n < 0 {
      Ok(Some(0))
    } else if n < 10 {
      Ok(Some(1))
    } else {
      Ok(None) // Route to default
    }
  });

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data: negative, small positive, large positive
  let _ = data_tx
    .send(Arc::new(-5i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(15i32) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check out_0 (negative numbers)
  let out_0_stream = outputs.remove("out_0").unwrap();
  let mut out_0_results = Vec::new();
  let mut stream = out_0_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_i32) = item.downcast::<i32>() {
            out_0_results.push(*arc_i32);
            if !out_0_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_0_results.len(), 1);
  assert_eq!(out_0_results[0], -5);

  // Check out_1 (0-10 range)
  let out_1_stream = outputs.remove("out_1").unwrap();
  let mut out_1_results = Vec::new();
  let mut stream = out_1_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_i32) = item.downcast::<i32>() {
            out_1_results.push(*arc_i32);
            if !out_1_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_1_results.len(), 1);
  assert_eq!(out_1_results[0], 5);

  // Check default (10+)
  let default_stream = outputs.remove("default").unwrap();
  let mut default_results = Vec::new();
  let mut stream = default_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_i32) = item.downcast::<i32>() {
            default_results.push(*arc_i32);
            if !default_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(default_results.len(), 1);
  assert_eq!(default_results[0], 15);
}

#[tokio::test]
async fn test_match_node_regex() {
  let node = MatchNode::new("test_match".to_string(), 2);

  // Create a config that routes based on regex patterns
  // Email -> out_0, URL -> out_1, others -> default
  let patterns = vec![
    (
      Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(),
      0,
    ),
    (Regex::new(r"^https?://").unwrap(), 1),
  ];
  let config: MatchConfig = match_regex(patterns);

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data: email, URL, other string
  let _ = data_tx
    .send(Arc::new("user@example.com".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new("https://example.com".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new("plain text".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check out_0 (email)
  let out_0_stream = outputs.remove("out_0").unwrap();
  let mut out_0_results = Vec::new();
  let mut stream = out_0_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>() {
            out_0_results.push((*arc_str).clone());
            if !out_0_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_0_results.len(), 1);
  assert_eq!(out_0_results[0], "user@example.com");

  // Check out_1 (URL)
  let out_1_stream = outputs.remove("out_1").unwrap();
  let mut out_1_results = Vec::new();
  let mut stream = out_1_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>() {
            out_1_results.push(arc_str.clone());
            if !out_1_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_1_results.len(), 1);
  assert_eq!(&*out_1_results[0], "https://example.com");

  // Check default (other strings)
  let default_stream = outputs.remove("default").unwrap();
  let mut default_results = Vec::new();
  let mut stream = default_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>() {
            default_results.push(arc_str.clone());
            if !default_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(default_results.len(), 1);
  assert_eq!(&*default_results[0], "plain text");
}

#[tokio::test]
async fn test_match_node_exact() {
  let node = MatchNode::new("test_match".to_string(), 2);

  // Create a config that routes based on exact string matching
  // "error" -> out_0, "warning" -> out_1, others -> default
  let patterns = vec![("error", 0), ("warning", 1)];
  let config: MatchConfig = match_exact_string(patterns);

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data: error, warning, info
  let _ = data_tx
    .send(Arc::new("error".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new("warning".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new("info".to_string()) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check out_0 (error)
  let out_0_stream = outputs.remove("out_0").unwrap();
  let mut out_0_results: Vec<String> = Vec::new();
  let mut stream = out_0_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>() {
            out_0_results.push((*arc_str).clone());
            if !out_0_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_0_results.len(), 1);
  assert_eq!(&*out_0_results[0], "error");

  // Check out_1 (warning)
  let out_1_stream = outputs.remove("out_1").unwrap();
  let mut out_1_results = Vec::new();
  let mut stream = out_1_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>() {
            out_1_results.push(arc_str.clone());
            if !out_1_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_1_results.len(), 1);
  assert_eq!(&*out_1_results[0], "warning");

  // Check default (info)
  let default_stream = outputs.remove("default").unwrap();
  let mut default_results = Vec::new();
  let mut stream = default_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>() {
            default_results.push(arc_str.clone());
            if !default_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(default_results.len(), 1);
  assert_eq!(&*default_results[0], "info");
}

#[tokio::test]
async fn test_match_node_custom_pattern() {
  let node = MatchNode::new("test_match".to_string(), 3);

  // Create a custom config using match_config directly
  // Even numbers -> out_0, odd numbers -> out_1, zero -> out_2, negative -> default
  let config: MatchConfig = match_config(|value| async move {
    if let Ok(arc_i32) = value.downcast::<i32>() {
      let n = *arc_i32;
      if n == 0 {
        Ok(Some(2))
      } else if n % 2 == 0 {
        Ok(Some(0))
      } else if n > 0 {
        Ok(Some(1))
      } else {
        Ok(None) // Negative -> default
      }
    } else {
      Err("Expected i32".to_string())
    }
  });

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data: even, odd, zero, negative
  let _ = data_tx
    .send(Arc::new(4i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(0i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(-3i32) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check out_0 (even)
  let out_0_stream = outputs.remove("out_0").unwrap();
  let mut out_0_results = Vec::new();
  let mut stream = out_0_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_i32) = item.downcast::<i32>() {
            out_0_results.push(*arc_i32);
            if !out_0_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_0_results.len(), 1);
  assert_eq!(out_0_results[0], 4);

  // Check out_1 (odd)
  let out_1_stream = outputs.remove("out_1").unwrap();
  let mut out_1_results = Vec::new();
  let mut stream = out_1_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_i32) = item.downcast::<i32>() {
            out_1_results.push(*arc_i32);
            if !out_1_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_1_results.len(), 1);
  assert_eq!(out_1_results[0], 5);

  // Check out_2 (zero)
  let out_2_stream = outputs.remove("out_2").unwrap();
  let mut out_2_results = Vec::new();
  let mut stream = out_2_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_i32) = item.downcast::<i32>() {
            out_2_results.push(*arc_i32);
            if !out_2_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_2_results.len(), 1);
  assert_eq!(out_2_results[0], 0);

  // Check default (negative)
  let default_stream = outputs.remove("default").unwrap();
  let mut default_results = Vec::new();
  let mut stream = default_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_i32) = item.downcast::<i32>() {
            default_results.push(*arc_i32);
            if !default_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(default_results.len(), 1);
  assert_eq!(default_results[0], -3);
}

#[tokio::test]
async fn test_match_node_error_handling() {
  let node = MatchNode::new("test_match".to_string(), 2);

  // Create a config that returns an error for certain inputs
  let config: MatchConfig = match_config(|value| async move {
    if let Ok(arc_i32) = value.downcast::<i32>() {
      let n = *arc_i32;
      if n < 0 {
        Err("Negative numbers not allowed".to_string())
      } else if n < 10 {
        Ok(Some(0))
      } else {
        Ok(Some(1))
      }
    } else {
      Err("Expected i32".to_string())
    }
  });

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data: valid, error case, valid
  let _ = data_tx
    .send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(-5i32) as Arc<dyn Any + Send + Sync>)
    .await;
  let _ = data_tx
    .send(Arc::new(15i32) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check error output
  let error_stream = outputs.remove("error").unwrap();
  let mut error_results: Vec<String> = 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>() {
            error_results.push((*arc_str).clone());
            if !error_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(error_results.len(), 1);
  assert_eq!(&*error_results[0], "Negative numbers not allowed");

  // Check that valid items still route correctly
  let out_0_stream = outputs.remove("out_0").unwrap();
  let mut out_0_results = Vec::new();
  let mut stream = out_0_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_i32) = item.downcast::<i32>() {
            out_0_results.push(*arc_i32);
            if !out_0_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(out_0_results.len(), 1);
  assert_eq!(out_0_results[0], 5);
}

#[tokio::test]
async fn test_match_node_no_config() {
  let node = MatchNode::new("test_match".to_string(), 2);

  let (_config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send data without configuration
  let _ = data_tx
    .send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check error output
  let error_stream = outputs.remove("error").unwrap();
  let mut error_results: Vec<String> = 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>() {
            error_results.push((*arc_str).clone());
            if !error_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(error_results.len(), 1);
  assert!(error_results[0].contains("No configuration set"));
}

#[tokio::test]
async fn test_match_node_branch_index_out_of_range() {
  let node = MatchNode::new("test_match".to_string(), 2); // Only out_0 and out_1

  // Create a config that returns branch index 5 (out of range)
  let config: MatchConfig = match_config(|value| async move {
    if let Ok(_arc_i32) = value.downcast::<i32>() {
      Ok(Some(5)) // Out of range - should route to default
    } else {
      Err("Expected i32".to_string())
    }
  });

  let (config_tx, data_tx, inputs) = create_input_streams();

  // Execute the node
  let outputs_future = node.execute(inputs);
  let mut outputs = outputs_future.await.unwrap();

  // Send configuration
  let _ = config_tx
    .send(Arc::new(config) as Arc<dyn Any + Send + Sync>)
    .await;
  tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

  // Send data
  let _ = data_tx
    .send(Arc::new(5i32) as Arc<dyn Any + Send + Sync>)
    .await;

  tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

  // Check default (out of range branch should route to default)
  let default_stream = outputs.remove("default").unwrap();
  let mut default_results = Vec::new();
  let mut stream = default_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_i32) = item.downcast::<i32>() {
            default_results.push(*arc_i32);
            if !default_results.is_empty() {
              break;
            }
          }
        } else {
          break;
        }
      }
      _ = &mut timeout => break,
    }
  }

  assert_eq!(default_results.len(), 1);
  assert_eq!(default_results[0], 5);
}