camel-processor 0.22.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
use super::*;
use camel_api::{Exchange, Message, Value};
use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;
use tower::ServiceExt;

/// In-memory test repository backed by `Mutex<HashMap>`.
#[derive(Debug)]
struct TestRepo {
    name: String,
    keys: Mutex<HashMap<String, Message>>,
    stacks: Mutex<HashMap<String, VecDeque<Message>>>,
}

impl TestRepo {
    fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            keys: Mutex::new(HashMap::new()),
            stacks: Mutex::new(HashMap::new()),
        }
    }
}

#[async_trait::async_trait]
impl ClaimCheckRepository for TestRepo {
    fn name(&self) -> &str {
        &self.name
    }

    async fn set(&self, key: &str, payload: Message) -> Result<(), CamelError> {
        self.keys
            .lock()
            .expect("mutex poisoned")
            .insert(key.to_string(), payload);
        Ok(())
    }

    async fn get(&self, key: &str) -> Result<Message, CamelError> {
        self.keys
            .lock()
            .expect("mutex poisoned")
            .get(key)
            .cloned()
            .ok_or_else(|| CamelError::RouteError(format!("Claim check key not found: {key}")))
    }

    async fn get_and_remove(&self, key: &str) -> Result<Message, CamelError> {
        self.keys
            .lock()
            .expect("mutex poisoned")
            .remove(key)
            .ok_or_else(|| CamelError::RouteError(format!("Claim check key not found: {key}")))
    }

    async fn remove(&self, key: &str) -> Result<(), CamelError> {
        self.keys.lock().expect("mutex poisoned").remove(key);
        Ok(())
    }

    async fn push(&self, key: &str, payload: Message) -> Result<(), CamelError> {
        self.stacks
            .lock()
            .expect("mutex poisoned")
            .entry(key.to_string())
            .or_default()
            .push_back(payload);
        Ok(())
    }

    async fn pop(&self, key: &str) -> Result<Message, CamelError> {
        self.stacks
            .lock()
            .expect("mutex poisoned")
            .get_mut(key)
            .and_then(|s| s.pop_back())
            .ok_or_else(|| {
                CamelError::RouteError(format!("Claim check stack empty for key: {key}"))
            })
    }
}

fn make_key_expr(key: &str) -> KeyExpression {
    let k = key.to_string();
    Arc::new(move |_ex: &Exchange| Ok(k.clone()))
}

fn make_exchange(body: Body) -> Exchange {
    Exchange::new(Message::new(body))
}

#[tokio::test]
async fn set_moves_body_to_repo() {
    let repo = Arc::new(TestRepo::new("test"));
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Set, make_key_expr("mykey"));
    let exchange = make_exchange(Body::Text("secret-data".to_string()));

    let result = svc.oneshot(exchange).await.unwrap();
    assert_eq!(result.input.body, Body::Text("mykey".to_string()));

    let stashed = repo.get("mykey").await.unwrap();
    assert_eq!(stashed.body, Body::Text("secret-data".to_string()));
}

#[tokio::test]
async fn get_restores_body() {
    let repo = Arc::new(TestRepo::new("test"));
    repo.set("k1", Message::new(Body::Text("stashed-body".to_string())))
        .await
        .unwrap();

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k1"));
    let exchange = make_exchange(Body::Empty);

    let result = svc.oneshot(exchange).await.unwrap();
    assert_eq!(result.input.body, Body::Text("stashed-body".to_string()));
}

#[tokio::test]
async fn get_and_remove_restores_and_deletes() {
    let repo = Arc::new(TestRepo::new("test"));
    repo.set(
        "k2",
        Message::new(Body::Text("will-be-removed".to_string())),
    )
    .await
    .unwrap();

    let svc = ClaimCheckService::new(
        repo.clone(),
        ClaimCheckOp::GetAndRemove,
        make_key_expr("k2"),
    );
    let exchange = make_exchange(Body::Empty);

    let result = svc.oneshot(exchange).await.unwrap();
    assert_eq!(result.input.body, Body::Text("will-be-removed".to_string()));

    let err = repo.get("k2").await.unwrap_err();
    assert!(
        matches!(&err, CamelError::RouteError(msg) if msg.contains("not found")),
        "expected RouteError with 'not found', got: {err:?}"
    );
}

#[tokio::test]
async fn push_pop_lifo() {
    let repo = Arc::new(TestRepo::new("test"));
    let first = Body::Text("first".to_string());
    let second = Body::Text("second".to_string());

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Push, make_key_expr("stack-key"));
    svc.oneshot(make_exchange(first)).await.unwrap();

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Push, make_key_expr("stack-key"));
    svc.oneshot(make_exchange(second.clone())).await.unwrap();

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Pop, make_key_expr("stack-key"));
    let result = svc.oneshot(make_exchange(Body::Empty)).await.unwrap();
    assert_eq!(result.input.body, second);

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Pop, make_key_expr("stack-key"));
    let result = svc.oneshot(make_exchange(Body::Empty)).await.unwrap();
    assert_eq!(result.input.body, Body::Text("first".to_string()));
}

#[tokio::test]
async fn get_missing_propagates_error() {
    let repo = Arc::new(TestRepo::new("test"));
    let svc = ClaimCheckService::new(
        repo.clone(),
        ClaimCheckOp::Get,
        make_key_expr("nonexistent"),
    );
    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await;
    assert!(
        matches!(&result, Err(CamelError::RouteError(msg)) if msg.contains("not found")),
        "expected Err with 'not found', got: {result:?}"
    );
}

#[tokio::test]
async fn set_overwrites_existing() {
    let repo = Arc::new(TestRepo::new("test"));
    repo.set("k", Message::new(Body::Text("old".to_string())))
        .await
        .unwrap();

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Set, make_key_expr("k"));
    let exchange = make_exchange(Body::Text("new".to_string()));
    svc.oneshot(exchange).await.unwrap();

    let stashed = repo.get("k").await.unwrap();
    assert_eq!(stashed.body, Body::Text("new".to_string()));
}

#[tokio::test]
async fn pop_empty_stack_propagates_error() {
    let repo = Arc::new(TestRepo::new("test"));
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Pop, make_key_expr("empty"));
    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await;
    assert!(
        matches!(&result, Err(CamelError::RouteError(msg)) if msg.contains("empty")),
        "expected Err with 'empty', got: {result:?}"
    );
}

// --- merge tests with filter ---

#[tokio::test]
async fn get_with_filter_body_only_restores_body() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Text("stashed".into()),
        headers: [("X-Old".into(), Value::String("old".into()))].into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("body").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let current_msg = Message {
        body: Body::Empty,
        headers: [("X-Current".into(), Value::String("cur".into()))].into(),
    };
    let exchange = Exchange::new(current_msg);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("stashed".into()));
    assert_eq!(
        result.input.headers.get("X-Current"),
        Some(&Value::String("cur".into()))
    );
    assert_eq!(result.input.headers.get("X-Old"), None);
}

#[tokio::test]
async fn get_without_filter_backward_compat() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message::new(Body::Text("stashed".into()));
    repo.set("k", stashed_msg).await.unwrap();

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"));
    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("stashed".into()));
}

#[tokio::test]
async fn get_with_header_wildcard_merges_matching_headers() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Empty,
        headers: [
            ("X-Foo".into(), Value::String("foo".into())),
            ("X-Bar".into(), Value::String("bar".into())),
            ("Other".into(), Value::String("other".into())),
        ]
        .into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("header:X-*").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(
        result.input.headers.get("X-Foo"),
        Some(&Value::String("foo".into()))
    );
    assert_eq!(
        result.input.headers.get("X-Bar"),
        Some(&Value::String("bar".into()))
    );
    assert_eq!(result.input.headers.get("Other"), None);
}

#[tokio::test]
async fn get_with_remove_body_sets_empty() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message::new(Body::Text("stashed".into()));
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("--body").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Text("current".into()));
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Empty);
}

#[tokio::test]
async fn set_preserves_headers_on_exchange() {
    let repo = Arc::new(TestRepo::new("test"));
    let mut msg = Message::new(Body::Text("body".into()));
    msg.set_header("X-Some", Value::String("header".into()));
    let exchange = Exchange::new(msg);

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Set, make_key_expr("k"));
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("k".into()));
    assert_eq!(
        result.input.headers.get("X-Some"),
        Some(&Value::String("header".into()))
    );

    let stashed = repo.get("k").await.unwrap();
    assert_eq!(stashed.body, Body::Text("body".into()));
    assert_eq!(
        stashed.headers.get("X-Some"),
        Some(&Value::String("header".into()))
    );
}

#[tokio::test]
async fn get_and_remove_with_filter() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Text("data".into()),
        headers: [("K".into(), Value::String("v".into()))].into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("body,header:K").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::GetAndRemove, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("data".into()));
    assert_eq!(
        result.input.headers.get("K"),
        Some(&Value::String("v".into()))
    );

    let err = repo.get("k").await.unwrap_err();
    assert!(matches!(&err, CamelError::RouteError(msg) if msg.contains("not found")));
}

#[tokio::test]
async fn exclude_header_merges_non_matching() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Empty,
        headers: [
            ("Secret".into(), Value::String("hidden".into())),
            ("Public".into(), Value::String("visible".into())),
        ]
        .into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("-header:Secret*").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.headers.get("Secret"), None);
    assert_eq!(
        result.input.headers.get("Public"),
        Some(&Value::String("visible".into()))
    );
}

#[tokio::test]
async fn remove_headers_deletes_from_current() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message::new(Body::Empty);
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("--header:ToRemove").unwrap();
    let mut current_msg = Message::new(Body::Empty);
    current_msg.set_header("ToRemove", Value::String("remove-me".into()));
    current_msg.set_header("Keep", Value::String("keep-me".into()));

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = Exchange::new(current_msg);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.headers.get("ToRemove"), None);
    assert_eq!(
        result.input.headers.get("Keep"),
        Some(&Value::String("keep-me".into()))
    );
}

#[tokio::test]
async fn remove_all_headers_clears_current() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message::new(Body::Empty);
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("--headers").unwrap();
    let mut current_msg = Message::new(Body::Empty);
    current_msg.set_header("X-One", Value::String("one".into()));
    current_msg.set_header("X-Two", Value::String("two".into()));

    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = Exchange::new(current_msg);
    let result = svc.oneshot(exchange).await.unwrap();

    assert!(result.input.headers.is_empty());
}

#[tokio::test]
async fn round_trip_set_then_get_with_filter() {
    let repo = Arc::new(TestRepo::new("test"));

    let mut msg = Message::new(Body::Text("payload".into()));
    msg.set_header("X-Route", Value::String("camel".into()));
    let exchange = Exchange::new(msg);

    let set_svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Set, make_key_expr("k"));
    set_svc.oneshot(exchange).await.unwrap();

    let filter = ClaimCheckFilter::parse("body,header:X-*").unwrap();
    let get_svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = get_svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("payload".into()));
    assert_eq!(
        result.input.headers.get("X-Route"),
        Some(&Value::String("camel".into()))
    );
}

#[tokio::test]
async fn pop_with_filter_merges_headers() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Text("popped".into()),
        headers: [("K".into(), Value::String("v".into()))].into(),
    };
    repo.push("s", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("body,header:K").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Pop, make_key_expr("s"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("popped".into()));
    assert_eq!(
        result.input.headers.get("K"),
        Some(&Value::String("v".into()))
    );
}

// --- parser tests ---

#[test]
fn parse_body_only() {
    let f = ClaimCheckFilter::parse("body").unwrap();
    assert_eq!(f.body, FilterAction::Include);
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Exclude));
}

#[test]
fn parse_exclude_body() {
    let f = ClaimCheckFilter::parse("-body").unwrap();
    assert_eq!(f.body, FilterAction::Exclude);
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Include));
}

#[test]
fn parse_body_and_header_wildcard() {
    let f = ClaimCheckFilter::parse("body,header:foo*").unwrap();
    assert_eq!(f.body, FilterAction::Include);
    match &f.headers_action {
        HeadersAction::ByPattern {
            include,
            exclude,
            remove,
        } => {
            assert_eq!(include.len(), 1);
            assert!(matches!(&include[0], HeaderPattern::Prefix(p) if p == "foo"));
            assert!(exclude.is_empty());
            assert!(remove.is_empty());
        }
        other => panic!("expected ByPattern, got {other:?}"),
    }
}

#[test]
fn parse_remove_headers_wildcard() {
    let f = ClaimCheckFilter::parse("--headers:bar*").unwrap();
    assert_eq!(f.body, FilterAction::Include);
    match &f.headers_action {
        HeadersAction::ByPattern {
            include,
            exclude,
            remove,
        } => {
            assert!(include.is_empty());
            assert!(exclude.is_empty());
            assert_eq!(remove.len(), 1);
            assert!(matches!(&remove[0], HeaderPattern::Prefix(p) if p == "bar"));
        }
        other => panic!("expected ByPattern, got {other:?}"),
    }
}

#[test]
fn parse_mixed_include_exclude_rejected() {
    let err = ClaimCheckFilter::parse("header:foo*,-header:bar*").unwrap_err();
    assert!(matches!(err, FilterParseError::MixedIncludeExclude));
}

#[test]
fn parse_invalid_regex_rejected() {
    let err = ClaimCheckFilter::parse("header:[invalid").unwrap_err();
    assert!(matches!(err, FilterParseError::InvalidPattern(_)));
}

#[test]
fn parse_attachments_accepted_as_noop() {
    let f = ClaimCheckFilter::parse("attachments").unwrap();
    assert_eq!(f.body, FilterAction::Include);
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Include));
}

#[test]
fn parse_all_headers() {
    let f = ClaimCheckFilter::parse("headers").unwrap();
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Include));
}

#[test]
fn parse_exclude_all_headers() {
    let f = ClaimCheckFilter::parse("-headers").unwrap();
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Exclude));
}

#[test]
fn parse_remove_all_headers() {
    let f = ClaimCheckFilter::parse("--headers").unwrap();
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Remove));
}

#[test]
fn parse_remove_body() {
    let f = ClaimCheckFilter::parse("--body").unwrap();
    assert_eq!(f.body, FilterAction::Remove);
}

#[test]
fn parse_only_header_pattern_defaults_omitted_to_exclude() {
    let f = ClaimCheckFilter::parse("header:foo*").unwrap();
    assert_eq!(f.body, FilterAction::Exclude);
}

#[test]
fn parse_all_negative_tokens_yield_exclude() {
    let f = ClaimCheckFilter::parse("-body,-headers").unwrap();
    assert_eq!(f.body, FilterAction::Exclude);
    assert_eq!(f.headers_action, HeadersAction::All(FilterAction::Exclude));
}

#[test]
fn parse_regex_pattern() {
    let f = ClaimCheckFilter::parse("header:^x-").unwrap();
    match &f.headers_action {
        HeadersAction::ByPattern { include, .. } => {
            assert!(matches!(&include[0], HeaderPattern::Regex(_)));
        }
        other => panic!("expected ByPattern, got {other:?}"),
    }
}

#[test]
fn parse_exact_header() {
    let f = ClaimCheckFilter::parse("header:Content-Type").unwrap();
    match &f.headers_action {
        HeadersAction::ByPattern { include, .. } => {
            assert!(matches!(&include[0], HeaderPattern::Exact(s) if s == "Content-Type"));
        }
        other => panic!("expected ByPattern, got {other:?}"),
    }
}

#[test]
fn parse_all_wildcard_matches_everything() {
    let p = HeaderPattern::compile("*").unwrap();
    assert!(p.matches("anything"));
}

#[test]
fn prefix_pattern_matches_start() {
    let p = HeaderPattern::compile("X-Foo*").unwrap();
    assert!(p.matches("X-Foo-Bar"));
    assert!(!p.matches("X-Bar"));
}

// --- coverage gap: HeadersAction::All(Include) merge ---

#[tokio::test]
async fn get_with_headers_all_include_merges_all_stashed_headers() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Text("data".into()),
        headers: [
            ("A".into(), Value::String("1".into())),
            ("B".into(), Value::String("2".into())),
        ]
        .into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    let filter = ClaimCheckFilter::parse("body,headers").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(result.input.body, Body::Text("data".into()));
    assert_eq!(
        result.input.headers.get("A"),
        Some(&Value::String("1".into()))
    );
    assert_eq!(
        result.input.headers.get("B"),
        Some(&Value::String("2".into()))
    );
}

// --- coverage gap: include-then-remove ordering ---

#[tokio::test]
async fn include_then_remove_specific_header() {
    let repo = Arc::new(TestRepo::new("test"));
    let stashed_msg = Message {
        body: Body::Empty,
        headers: [
            ("X-Keep".into(), Value::String("keep".into())),
            ("X-Remove".into(), Value::String("remove".into())),
        ]
        .into(),
    };
    repo.set("k", stashed_msg).await.unwrap();

    // include all X-* headers but then remove X-Remove explicitly
    let filter = ClaimCheckFilter::parse("header:X-*,--header:X-Remove").unwrap();
    let svc = ClaimCheckService::new(repo.clone(), ClaimCheckOp::Get, make_key_expr("k"))
        .with_filter(filter);

    let exchange = make_exchange(Body::Empty);
    let result = svc.oneshot(exchange).await.unwrap();

    assert_eq!(
        result.input.headers.get("X-Keep"),
        Some(&Value::String("keep".into()))
    );
    assert_eq!(result.input.headers.get("X-Remove"), None);
}