camel-processor 0.29.0

Message processors for rust-camel
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use tokio::task::JoinSet;
use tower::Service;
use tower::ServiceExt;

use camel_api::endpoint_pipeline::{CAMEL_SLIP_ENDPOINT, EndpointPipelineConfig};
use camel_api::recipient_list::RecipientListConfig;
use camel_api::{Body, CamelError, Exchange, Value};

use crate::endpoint_pipeline::EndpointPipelineService;

#[derive(Clone)]
pub struct RecipientListService {
    config: RecipientListConfig,
    pipeline: EndpointPipelineService,
}

impl RecipientListService {
    pub fn new(
        config: RecipientListConfig,
        endpoint_resolver: camel_api::EndpointResolver,
    ) -> Result<Self, CamelError> {
        config.validate()?;
        let pipeline_config = EndpointPipelineConfig {
            cache_size: EndpointPipelineConfig::from_signed(1000),
            ignore_invalid_endpoints: false,
        };
        Ok(Self {
            config,
            pipeline: EndpointPipelineService::new(endpoint_resolver, pipeline_config),
        })
    }
}

impl Service<Exchange> for RecipientListService {
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, mut exchange: Exchange) -> Self::Future {
        let config = self.config.clone();
        let pipeline = self.pipeline.clone();

        Box::pin(async move {
            let uris_raw = (config.expression)(&exchange);
            if uris_raw.is_empty() {
                return Ok(exchange);
            }

            // H13 Batch 1: cap the resolved-URI list BEFORE any endpoint
            // resolution. A malicious expression yielding millions of URIs
            // would otherwise allocate a Vec of millions of &str references
            // and resolve each one (multicast) or call each one (sequential).
            // The default cap is 1_000 (camel-api::recipient_list).
            let cap = config.max_recipients;
            let uris: Vec<&str> = uris_raw
                .split(&config.delimiter)
                .map(|s| s.trim())
                .filter(|s| !s.is_empty())
                .take(cap)
                .collect();
            if uris.is_empty() {
                return Ok(exchange);
            }

            if config.parallel {
                let original_for_aggregate = exchange.clone();
                let mut endpoints_to_call = Vec::with_capacity(uris.len());
                for uri in &uris {
                    if let Some(endpoint) = pipeline.resolve(uri)? {
                        endpoints_to_call.push((uri.to_string(), endpoint));
                    }
                }

                let mut results: Vec<Exchange> = Vec::with_capacity(endpoints_to_call.len());
                let mut join_set = JoinSet::new();
                let mut iter = endpoints_to_call.into_iter();
                let raw_limit = config.parallel_limit.unwrap_or(results.capacity());
                let limit = raw_limit.max(1).min(results.capacity().max(1));
                let mut last_parallel_error: Option<CamelError> = None;

                for _ in 0..limit {
                    if let Some((uri, mut endpoint)) = iter.next() {
                        let mut cloned = original_for_aggregate.clone();
                        cloned.set_property(CAMEL_SLIP_ENDPOINT, Value::String(uri));
                        join_set.spawn(async move { endpoint.ready().await?.call(cloned).await });
                    }
                }

                while let Some(result) = join_set.join_next().await {
                    match result {
                        Ok(Ok(ex)) => results.push(ex),
                        Ok(Err(e)) if config.stop_on_exception => {
                            join_set.abort_all();
                            return Err(e);
                        }
                        Ok(Err(e)) => {
                            // stop_on_exception=false: track the representative
                            // error — the last failing task to complete via
                            // join_next order (ADR-0058). Pending tasks continue.
                            last_parallel_error = Some(e);
                        }
                        Err(join_err) if join_err.is_panic() => {
                            // A recipient task panicked. ADR-0058: a panic is
                            // zero-success attempted work and MUST NOT launder to
                            // Ok(original); convert to a representative error so
                            // the zero-success guard fires. (Cancellation is
                            // handled separately below — it is often self-induced
                            // by stop_on_exception's abort_all.)
                            last_parallel_error = Some(CamelError::ProcessorError(format!(
                                "recipient task panicked: {join_err}"
                            )));
                        }
                        Err(_) => {} // Cancellation (JoinSet abort); ignore.
                    }

                    if let Some((uri, mut endpoint)) = iter.next() {
                        let mut cloned = original_for_aggregate.clone();
                        cloned.set_property(CAMEL_SLIP_ENDPOINT, Value::String(uri));
                        join_set.spawn(async move { endpoint.ready().await?.call(cloned).await });
                    }
                }

                // ADR-0058: zero-success operational failure. At least one
                // recipient was called and zero returned Ok — report the
                // representative error instead of laundering to Ok(original).
                let zero_success_error = if results.is_empty() {
                    last_parallel_error
                } else {
                    None
                };
                if let Some(err) = zero_success_error {
                    return Err(err);
                }

                exchange = aggregate_results(config.strategy, original_for_aggregate, results);
            } else {
                let mut results: Vec<Exchange> = Vec::new();
                let mut last_error: Option<CamelError> = None;
                let original_for_aggregate = exchange.clone();
                for uri in &uris {
                    let endpoint = match pipeline.resolve(uri)? {
                        Some(e) => e,
                        None => continue,
                    };
                    exchange.set_property(CAMEL_SLIP_ENDPOINT, Value::String(uri.to_string()));
                    let mut endpoint = endpoint;
                    let result = endpoint.ready().await?.call(exchange.clone()).await;
                    match result {
                        Ok(ex) => {
                            results.push(ex.clone());
                            exchange = ex;
                        }
                        Err(e) if config.stop_on_exception => return Err(e),
                        Err(e) => {
                            // stop_on_exception=false: track the iteration-last
                            // error (ADR-0058) and continue to remaining recipients.
                            last_error = Some(e);
                            continue;
                        }
                    }
                }
                // ADR-0058: zero-success operational failure. At least one
                // recipient was called and zero returned Ok — report the
                // iteration-last error instead of laundering to Ok(original),
                // which would poison an outer cache write-back with the inbound body.
                let zero_success_error = if results.is_empty() { last_error } else { None };
                if let Some(err) = zero_success_error {
                    return Err(err);
                }
                exchange = aggregate_results(config.strategy, original_for_aggregate, results);
            }

            Ok(exchange)
        })
    }
}

fn aggregate_results(
    strategy: camel_api::MulticastStrategy,
    original: Exchange,
    results: Vec<Exchange>,
) -> Exchange {
    match strategy {
        camel_api::MulticastStrategy::LastWins => results.into_iter().last().unwrap_or(original),
        camel_api::MulticastStrategy::CollectAll => {
            let bodies: Vec<Value> = results
                .iter()
                .map(|ex| match &ex.input.body {
                    Body::Text(s) => Value::String(s.clone()),
                    Body::Json(v) => v.clone(),
                    Body::Xml(s) => Value::String(s.clone()),
                    Body::Bytes(b) => Value::String(String::from_utf8_lossy(b).into_owned()),
                    Body::Stream(s) => serde_json::json!({
                        "_stream": {
                            "origin": s.metadata.origin,
                            "placeholder": true,
                            "hint": "Materialize exchange body with .into_bytes() before recipient-list aggregation"
                        }
                    }),
                    // Empty and future variants contribute no extractable value.
                    _ => Value::Null,
                })
                .collect();
            let mut result = results.into_iter().last().unwrap_or(original);
            result.input.body = camel_api::Body::from(Value::Array(bodies));
            result
        }
        camel_api::MulticastStrategy::Custom(fn_) => {
            results.into_iter().fold(original, |acc, ex| fn_(acc, ex))
        }
        // Original and any future variant return the original exchange.
        _ => original,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use camel_api::MulticastStrategy;
    use camel_api::{BoxProcessor, BoxProcessorExt, CamelError, Message};
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::{Duration, Instant};
    use tokio::sync::Mutex;
    use tokio::time::sleep;

    fn mock_resolver() -> camel_api::EndpointResolver {
        Arc::new(|uri: &str| {
            if uri.starts_with("mock:") {
                Some(BoxProcessor::from_fn(|ex| Box::pin(async move { Ok(ex) })))
            } else {
                None
            }
        })
    }

    #[tokio::test]
    async fn recipient_list_single_destination() {
        let call_count = Arc::new(AtomicUsize::new(0));
        let count_clone = call_count.clone();

        let resolver = Arc::new(move |uri: &str| {
            if uri == "mock:a" {
                let count = count_clone.clone();
                Some(BoxProcessor::from_fn(move |ex| {
                    count.fetch_add(1, Ordering::SeqCst);
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| "mock:a".to_string()));

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(result.is_ok());
        assert_eq!(call_count.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn recipient_list_multiple_destinations() {
        let call_count = Arc::new(AtomicUsize::new(0));
        let count_clone = call_count.clone();

        let resolver = Arc::new(move |uri: &str| {
            if uri.starts_with("mock:") {
                let count = count_clone.clone();
                Some(BoxProcessor::from_fn(move |ex| {
                    count.fetch_add(1, Ordering::SeqCst);
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c".to_string()
        }));

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(result.is_ok());
        assert_eq!(call_count.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn recipient_list_empty_expression() {
        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| String::new()));

        let mut svc = RecipientListService::new(config, mock_resolver()).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn recipient_list_invalid_endpoint_error() {
        let config =
            RecipientListConfig::new(Arc::new(|_ex: &Exchange| "invalid:endpoint".to_string()));

        let mut svc = RecipientListService::new(config, mock_resolver()).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid endpoint"));
    }

    #[tokio::test]
    async fn recipient_list_custom_delimiter() {
        use std::sync::Mutex;

        let order: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

        let resolver = {
            let order = order.clone();
            Arc::new(move |uri: &str| {
                let order = order.clone();
                let uri = uri.to_string();
                Some(BoxProcessor::from_fn(move |ex| {
                    order.lock().unwrap().push(uri.clone());
                    Box::pin(async move { Ok(ex) })
                }))
            })
        };

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:x|mock:y|mock:z".to_string()
        }))
        .delimiter("|");

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        svc.ready().await.unwrap().call(ex).await.unwrap();

        let order = order.lock().unwrap();
        assert_eq!(*order, vec!["mock:x", "mock:y", "mock:z"]);
    }

    #[tokio::test]
    async fn recipient_list_expression_evaluated_once() {
        let expr_count = Arc::new(AtomicUsize::new(0));
        let expr_count_clone = expr_count.clone();

        let config = RecipientListConfig::new(Arc::new(move |_ex: &Exchange| {
            expr_count_clone.fetch_add(1, Ordering::SeqCst);
            "mock:a,mock:b".to_string()
        }));

        let mut svc = RecipientListService::new(config, mock_resolver()).unwrap();
        let ex = Exchange::new(Message::new("test"));
        svc.ready().await.unwrap().call(ex).await.unwrap();

        assert_eq!(
            expr_count.load(Ordering::SeqCst),
            1,
            "Expression must be evaluated exactly once"
        );
    }

    #[tokio::test]
    async fn recipient_list_ignores_empty_uri_tokens() {
        let call_count = Arc::new(AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        let resolver = Arc::new(move |uri: &str| {
            if uri.starts_with("mock:") {
                let count = call_count_clone.clone();
                Some(BoxProcessor::from_fn(move |ex| {
                    count.fetch_add(1, Ordering::SeqCst);
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            " ,mock:a, ,mock:b,, ".to_string()
        }));

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;
        assert!(result.is_ok());
        assert_eq!(call_count.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn recipient_list_mutation_between_steps() {
        let resolver = Arc::new(|uri: &str| {
            if uri == "mock:mutate" {
                Some(BoxProcessor::from_fn(|mut ex| {
                    ex.input.body = camel_api::Body::Text("mutated".to_string());
                    Box::pin(async move { Ok(ex) })
                }))
            } else if uri == "mock:verify" {
                Some(BoxProcessor::from_fn(|ex| {
                    let body = ex.input.body.as_text().unwrap_or("").to_string();
                    assert_eq!(body, "mutated");
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:mutate,mock:verify".to_string()
        }));

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("original"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn recipient_list_parallel_executes_concurrently() {
        let records: Arc<Mutex<Vec<(String, Instant, Instant)>>> = Arc::new(Mutex::new(Vec::new()));

        let resolver = {
            let records = records.clone();
            Arc::new(move |uri: &str| {
                if uri.starts_with("mock:") {
                    let records = records.clone();
                    let uri = uri.to_string();
                    Some(BoxProcessor::from_fn(move |ex| {
                        let records = records.clone();
                        let uri = uri.clone();
                        Box::pin(async move {
                            let start = Instant::now();
                            sleep(Duration::from_millis(100)).await;
                            let end = Instant::now();
                            records.lock().await.push((uri, start, end));
                            Ok(ex)
                        })
                    }))
                } else {
                    None
                }
            })
        };

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c".to_string()
        }))
        .parallel(true);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        svc.ready().await.unwrap().call(ex).await.unwrap();

        let records = records.lock().await;
        assert_eq!(records.len(), 3);

        let mut overlap_found = false;
        for i in 0..records.len() {
            for j in (i + 1)..records.len() {
                let (_, a_start, a_end) = records[i];
                let (_, b_start, b_end) = records[j];
                if a_start < b_end && b_start < a_end {
                    overlap_found = true;
                    break;
                }
            }
            if overlap_found {
                break;
            }
        }

        assert!(overlap_found);
    }

    #[tokio::test]
    async fn recipient_list_parallel_stop_on_exception_returns_error() {
        let resolver = Arc::new(|uri: &str| {
            if uri == "mock:err" {
                Some(BoxProcessor::from_fn(|_ex| {
                    Box::pin(async { Err(CamelError::ProcessorError("boom".to_string())) })
                }))
            } else if uri.starts_with("mock:") {
                Some(BoxProcessor::from_fn(|ex| Box::pin(async move { Ok(ex) })))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:err,mock:c".to_string()
        }))
        .parallel(true)
        .stop_on_exception(true);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let result = svc.ready().await.unwrap().call(ex).await;
        assert!(matches!(result, Err(CamelError::ProcessorError(msg)) if msg == "boom"));
    }

    #[tokio::test]
    async fn recipient_list_parallel_limit_respects_limit() {
        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c,mock:d".to_string()
        }))
        .parallel(true)
        .parallel_limit(2);

        let resolver = Arc::new(|uri: &str| {
            if uri.starts_with("mock:") {
                Some(BoxProcessor::from_fn(|ex| {
                    Box::pin(async move {
                        sleep(Duration::from_millis(100)).await;
                        Ok(ex)
                    })
                }))
            } else {
                None
            }
        });

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("test"));
        let start = Instant::now();
        svc.ready().await.unwrap().call(ex).await.unwrap();
        let elapsed = start.elapsed();

        assert!(elapsed >= Duration::from_millis(180));
        assert!(elapsed < Duration::from_millis(350));
    }

    #[tokio::test]
    async fn recipient_list_collect_all_strategy() {
        let resolver = Arc::new(|uri: &str| {
            if uri == "mock:a" {
                Some(BoxProcessor::from_fn(|mut ex| {
                    ex.input.body = Body::Text("a".to_string());
                    Box::pin(async move { Ok(ex) })
                }))
            } else if uri == "mock:b" {
                Some(BoxProcessor::from_fn(|mut ex| {
                    ex.input.body = Body::Text("b".to_string());
                    Box::pin(async move { Ok(ex) })
                }))
            } else if uri == "mock:c" {
                Some(BoxProcessor::from_fn(|mut ex| {
                    ex.input.body = Body::Text("c".to_string());
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c".to_string()
        }))
        .strategy(MulticastStrategy::CollectAll);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("seed"));
        let result = svc.ready().await.unwrap().call(ex).await.unwrap();

        assert_eq!(
            result.input.body,
            Body::from(Value::Array(vec![
                Value::String("a".to_string()),
                Value::String("b".to_string()),
                Value::String("c".to_string()),
            ]))
        );
    }

    #[tokio::test]
    async fn recipient_list_original_strategy() {
        let resolver = Arc::new(|uri: &str| {
            if uri.starts_with("mock:") {
                let label = uri.to_string();
                Some(BoxProcessor::from_fn(move |mut ex| {
                    let label = label.clone();
                    ex.input.body = Body::Text(format!("mutated-{label}"));
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c".to_string()
        }))
        .strategy(MulticastStrategy::Original);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("original"));
        let result = svc.ready().await.unwrap().call(ex).await.unwrap();

        assert_eq!(result.input.body.as_text(), Some("original"));
    }

    // ── H13 Batch 1: cap resolved-URI count ──────────────────────────

    /// H13: an expression yielding millions of URIs is truncated to
    /// `max_recipients` before endpoint resolution. The test uses a
    /// cap of 4 to keep the test fast; the principle (cap the list) is
    /// what Batch 1 enforces. The default cap is 1_000 in camel-api.
    #[tokio::test]
    async fn test_huge_recipient_list_is_capped() {
        let call_count = Arc::new(AtomicUsize::new(0));
        let count_clone = call_count.clone();

        let resolver = Arc::new(move |uri: &str| {
            if uri.starts_with("mock:") {
                let count = count_clone.clone();
                Some(BoxProcessor::from_fn(move |ex| {
                    count.fetch_add(1, Ordering::SeqCst);
                    Box::pin(async move { Ok(ex) })
                }))
            } else {
                None
            }
        });

        // Build the untrusted payload: 1_000_000 URIs as one string.
        let mut many = String::with_capacity(8 * 1_000_000);
        for i in 0..1_000_000 {
            if i > 0 {
                many.push(',');
            }
            many.push_str(&format!("mock:k{i}"));
        }

        // Disposition-5 pattern: the untrusted data flows FROM the exchange
        // (a header on the inbound message), NOT from a captured variable.
        // The expression reads it off the passed `&Exchange` — this is what
        // makes the cap an untrusted-data-validation control, not a local
        // limit.
        let config = RecipientListConfig::new(Arc::new(|ex: &Exchange| {
            ex.input
                .header("CamelRecipients")
                .and_then(|v| v.as_str().map(|s| s.to_string()))
                .unwrap_or_default()
        }))
        .max_recipients(4);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let mut ex = Exchange::new(Message::new("test"));
        ex.input.set_header("CamelRecipients", Value::String(many));
        let result = svc.ready().await.unwrap().call(ex).await;
        assert!(result.is_ok(), "capped execution should still succeed");
        assert_eq!(
            call_count.load(Ordering::SeqCst),
            4,
            "must resolve at most max_recipients (4) endpoints"
        );
    }

    #[tokio::test]
    async fn recipient_list_last_wins_strategy() {
        let payloads: Arc<HashMap<String, String>> = Arc::new(HashMap::from([
            ("mock:a".to_string(), "first".to_string()),
            ("mock:b".to_string(), "second".to_string()),
            ("mock:c".to_string(), "third".to_string()),
        ]));

        let resolver = {
            let payloads = payloads.clone();
            Arc::new(move |uri: &str| {
                if let Some(payload) = payloads.get(uri) {
                    let payload = payload.clone();
                    Some(BoxProcessor::from_fn(move |mut ex| {
                        let payload = payload.clone();
                        ex.input.body = Body::Text(payload);
                        Box::pin(async move { Ok(ex) })
                    }))
                } else {
                    None
                }
            })
        };

        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:a,mock:b,mock:c".to_string()
        }))
        .strategy(MulticastStrategy::LastWins);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("seed"));
        let result = svc.ready().await.unwrap().call(ex).await.unwrap();

        assert_eq!(result.input.body.as_text(), Some("third"));
    }

    // ── ADR-0058: zero-success operational failure must not launder to Ok(original) ─

    fn err_resolver(uri_to_err: Vec<(&'static str, CamelError)>) -> camel_api::EndpointResolver {
        Arc::new(move |uri: &str| {
            for (pattern, err) in &uri_to_err {
                if uri == *pattern {
                    let err = err.clone();
                    return Some(BoxProcessor::from_fn(move |_ex| {
                        let err = err.clone();
                        Box::pin(async move { Err(err) })
                    }));
                }
            }
            None
        })
    }

    #[tokio::test]
    async fn recipient_list_sequential_all_failed_returns_err() {
        // ADR-0058: zero-success sequential. One recipient errors; zero Ok.
        // MUST return Err, not Ok(original) (which would poison an outer cache).
        let resolver = err_resolver(vec![(
            "mock:a",
            CamelError::Config(String::from("seq-all-failed")),
        )]);
        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| "mock:a".to_string()))
            .strategy(MulticastStrategy::LastWins);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let mut ex = Exchange::new(Message::new("timer:t tick #1"));
        ex.input.body = Body::Text(String::from("timer:t tick #1"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(
            result.is_err(),
            "zero-success recipient_list must return Err, not Ok(original)"
        );
        assert!(
            matches!(result, Err(CamelError::Config(m)) if m == "seq-all-failed"),
            "returned error must carry the iteration-last error"
        );
    }

    #[tokio::test]
    async fn recipient_list_parallel_all_failed_returns_err() {
        // ADR-0058: zero-success parallel. Two recipients error; zero Ok.
        // MUST return a representative Err, not Ok(original).
        let resolver = err_resolver(vec![
            ("mock:a", CamelError::Config(String::from("par-err-a"))),
            ("mock:b", CamelError::Config(String::from("par-err-b"))),
        ]);
        let config =
            RecipientListConfig::new(Arc::new(|_ex: &Exchange| "mock:a,mock:b".to_string()))
                .strategy(MulticastStrategy::LastWins)
                .parallel(true);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("inbound"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(
            result.is_err(),
            "zero-success parallel recipient_list must return Err, not Ok(original)"
        );
    }

    #[tokio::test]
    async fn recipient_list_parallel_last_error_is_join_next_order() {
        // ADR-0058 last-error determinism: the representative error is the one
        // from the task returned by the last `JoinSet::join_next` that completed
        // with an error. mock:a errors immediately; mock:b awaits a oneshot
        // signal then errors. The test sends the signal after a brief yield so
        // mock:a completes first → join_next order yields mock:b's error last.
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();
        let rx = Arc::new(tokio::sync::Mutex::new(Some(rx)));
        let resolver: camel_api::EndpointResolver = Arc::new(move |uri: &str| {
            if uri == "mock:a" {
                Some(BoxProcessor::from_fn(|_ex| {
                    Box::pin(async move { Err(CamelError::Config(String::from("par-err-a"))) })
                }))
            } else if uri == "mock:b" {
                let rx = rx.clone();
                Some(BoxProcessor::from_fn(move |_ex| {
                    let rx = rx.clone();
                    Box::pin(async move {
                        // Wait for the test's signal before completing.
                        let mut lock = rx.lock().await;
                        if let Some(rx) = lock.take() {
                            let _ = rx.await;
                        }
                        Err(CamelError::Config(String::from("par-err-b")))
                    })
                }))
            } else {
                None
            }
        });
        let config =
            RecipientListConfig::new(Arc::new(|_ex: &Exchange| "mock:a,mock:b".to_string()))
                .strategy(MulticastStrategy::LastWins)
                .parallel(true);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("inbound"));

        // Drive the call concurrently; release mock:b after mock:a has had a
        // chance to error first.
        let join = tokio::spawn(async move { svc.ready().await.unwrap().call(ex).await });
        // Yield the runtime so mock:a (synchronous Err) completes before mock:b.
        for _ in 0..10 {
            tokio::task::yield_now().await;
        }
        let _ = tx.send(());
        let result = join.await.unwrap();

        assert!(
            matches!(result, Err(CamelError::Config(ref m)) if m == "par-err-b"),
            "representative error must be the last failing task to complete (mock:b), got: {result:?}"
        );
    }

    #[tokio::test]
    async fn recipient_list_partial_success_aggregates_and_returns_ok() {
        // ADR-0058: partial success (>=1 Ok) MUST aggregate over successes and
        // return Ok. The invariant fires only on ZERO successes.
        let call_count = Arc::new(AtomicUsize::new(0));
        let ok_count = call_count.clone();
        let resolver: camel_api::EndpointResolver = Arc::new(move |uri: &str| {
            if uri == "mock:ok" {
                let c = ok_count.clone();
                Some(BoxProcessor::from_fn(move |mut ex| {
                    c.fetch_add(1, Ordering::SeqCst);
                    ex.input.body = Body::Text(String::from("ok-body"));
                    Box::pin(async move { Ok(ex) })
                }))
            } else if uri == "mock:fail" {
                Some(BoxProcessor::from_fn(|_ex| {
                    Box::pin(async move { Err(CamelError::Config(String::from("partial-fail"))) })
                }))
            } else {
                None
            }
        });
        let config =
            RecipientListConfig::new(Arc::new(|_ex: &Exchange| "mock:fail,mock:ok".to_string()))
                .strategy(MulticastStrategy::LastWins);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("inbound"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(
            result.is_ok(),
            "partial success must return Ok, got: {result:?}"
        );
        assert_eq!(call_count.load(Ordering::SeqCst), 1);
        assert_eq!(result.unwrap().input.body.as_text(), Some("ok-body"));
    }

    #[tokio::test]
    async fn recipient_list_parallel_all_panic_returns_err() {
        // ADR-0058 (e_gpt review gap): a parallel recipient_list where every
        // spawned task PANICS produces only JoinError(panic) results. These
        // MUST NOT launder to Ok(original); convert to a representative error
        // so the zero-success guard fires. Cancels (self-induced abort) stay
        // ignored.
        let resolver: camel_api::EndpointResolver = Arc::new(|uri: &str| {
            if uri.starts_with("mock:panic") {
                Some(BoxProcessor::from_fn(|_ex| {
                    Box::pin(async move {
                        panic!("recipient panicked");
                    })
                }))
            } else {
                None
            }
        });
        let config = RecipientListConfig::new(Arc::new(|_ex: &Exchange| {
            "mock:panic1,mock:panic2".to_string()
        }))
        .strategy(MulticastStrategy::LastWins)
        .parallel(true);

        let mut svc = RecipientListService::new(config, resolver).unwrap();
        let ex = Exchange::new(Message::new("inbound"));
        let result = svc.ready().await.unwrap().call(ex).await;

        assert!(
            result.is_err(),
            "all-panic parallel recipient_list must return Err, not Ok(original); got: {result:?}"
        );
        assert!(
            matches!(result, Err(CamelError::ProcessorError(_))),
            "panic must surface as a ProcessorError representative"
        );
    }
}