camel-component-wasm 0.24.0

WASM plugin component 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
use serde_json::Value;
use wasmtime::component::{Access, Accessor, Linker};

use crate::runtime::WasmHostState;

// Host marker trait must stay on WasmHostState (not HasSelf<WasmHostState>):
// the add_to_linker bound `for<'a> D::Data<'a>: Host` resolves through the
// `impl<T: Host> Host for &mut T` blanket impl, which requires the concrete
// type inside &mut (_) to implement Host.
// Sync `Host` trait impl: under the per-function import config the sync host
// functions (get-property, set-property, host-store, host-load) live in
// `HostWithStore` (taking an `Access`); `Host` remains an empty marker trait
// required by the linker's where-clause.
impl crate::bindings::camel::plugin::host::Host for WasmHostState {}

impl crate::bindings::camel::plugin::host::HostWithStore<WasmHostState>
    for wasmtime::component::HasSelf<WasmHostState>
{
    async fn camel_call(
        store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        uri: String,
        payload: String,
    ) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
        WasmHostState::camel_call_impl(store, uri, payload).await
    }

    async fn camel_poll(
        store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        uri: String,
        timeout_ms: u32,
    ) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
        WasmHostState::camel_poll_impl(store, uri, timeout_ms).await
    }

    fn get_property(
        mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        key: String,
    ) -> Option<String> {
        store.get().get_property_impl(key)
    }

    fn set_property(
        mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        key: String,
        value: String,
    ) {
        store.get().set_property_impl(key, value)
    }

    fn host_store(
        mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        key: String,
        value: String,
    ) -> Result<(), crate::bindings::camel::plugin::types::WasmError> {
        WasmHostState::host_store_impl(store.get(), key, value)
    }

    fn host_load(
        mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        key: String,
    ) -> Result<Option<String>, crate::bindings::camel::plugin::types::WasmError> {
        WasmHostState::host_load_impl(store.get(), key)
    }
}

pub fn add_to_linker(linker: &mut Linker<WasmHostState>) -> Result<(), wasmtime::Error> {
    // D = HasSelf<WasmHostState> uses wasmtime's built-in HasData impl
    // (Data<'a> = &'a mut WasmHostState). The HostWithStore and Host trait
    // impls target HasSelf<WasmHostState> and WasmHostState respectively.
    crate::bindings::camel::plugin::host::add_to_linker::<
        WasmHostState,
        wasmtime::component::HasSelf<WasmHostState>,
    >(linker, |state| state)
}

// Async helpers: the actual producer / poller logic. Pulled out of the trait
// impls so they can also be invoked from the per-binding macro arms and from
// tests (where we don't have an Accessor in scope).

async fn run_async_call(
    registry: std::sync::Arc<std::sync::Mutex<camel_core::Registry>>,
    uri: String,
    payload: String,
) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
    use tower::ServiceExt;
    let scheme = uri.split(':').next().unwrap_or("").to_string();
    if scheme.is_empty() {
        return Err(
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "invalid URI (no scheme): {}",
                uri
            )),
        );
    }

    let component = {
        let guard = registry.lock().map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "registry lock poisoned: {}",
                e
            ))
        })?;
        guard.get(&scheme).ok_or_else(|| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "component not found for scheme: {}",
                scheme
            ))
        })?
    };

    let endpoint = component
        .create_endpoint(&uri, &camel_component_api::NoOpComponentContext)
        .map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "create_endpoint failed: {}",
                e
            ))
        })?;

    let rt: std::sync::Arc<dyn camel_component_api::RuntimeObservability> =
        std::sync::Arc::new(camel_component_api::NoOpComponentContext);
    let producer = endpoint
        .create_producer(rt, &camel_api::ProducerContext::new())
        .map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "create_producer failed: {}",
                e
            ))
        })?;

    let exchange =
        camel_api::Exchange::new(camel_api::Message::new(camel_api::Body::Text(payload)));

    let result = producer.oneshot(exchange).await.map_err(|e| {
        crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
            "endpoint call failed: {}",
            e
        ))
    })?;

    let body_str = match &result.output {
        Some(msg) => match &msg.body {
            camel_api::Body::Text(s) => s.clone(),
            camel_api::Body::Json(v) => v.to_string(),
            camel_api::Body::Bytes(b) => String::from_utf8_lossy(b).to_string(),
            camel_api::Body::Xml(s) => s.clone(),
            camel_api::Body::Empty => String::new(),
            camel_api::Body::Stream(_) => "<stream>".to_string(),
        },
        None => match &result.input.body {
            camel_api::Body::Text(s) => s.clone(),
            camel_api::Body::Json(v) => v.to_string(),
            camel_api::Body::Bytes(b) => String::from_utf8_lossy(b).to_string(),
            camel_api::Body::Xml(s) => s.clone(),
            camel_api::Body::Empty => String::new(),
            camel_api::Body::Stream(_) => "<stream>".to_string(),
        },
    };

    Ok(body_str)
}

async fn run_async_poll(
    registry: std::sync::Arc<std::sync::Mutex<camel_core::Registry>>,
    uri: String,
    timeout_ms: u32,
) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
    let scheme = uri.split(':').next().unwrap_or("").to_string();
    if scheme.is_empty() {
        return Err(
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "invalid URI (no scheme): {}",
                uri
            )),
        );
    }

    let component = {
        let guard = registry.lock().map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "registry lock poisoned: {}",
                e
            ))
        })?;
        guard.get(&scheme).ok_or_else(|| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "component not found for scheme: {}",
                scheme
            ))
        })?
    };

    let endpoint = component
        .create_endpoint(&uri, &camel_component_api::NoOpComponentContext)
        .map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "create_endpoint failed: {}",
                e
            ))
        })?;

    let mut poller = endpoint.polling_consumer().ok_or_else(|| {
        crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
            "camel_poll requires a component that supports polling consumers (scheme: {})",
            scheme
        ))
    })?;

    let exchange = poller
        .receive(std::time::Duration::from_millis(timeout_ms as u64))
        .await
        .map_err(|e| {
            crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                "poll failed: {}",
                e
            ))
        })?;

    let body_str = match exchange {
        Some(ex) => {
            let bytes = ex
                .input
                .body
                .into_bytes(10 * 1024 * 1024)
                .await
                .map_err(|e| {
                    crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                        "body read failed: {}",
                        e
                    ))
                })?;
            String::from_utf8_lossy(&bytes).to_string()
        }
        None => {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                    "no message received within {}ms timeout",
                    timeout_ms
                )),
            );
        }
    };

    Ok(body_str)
}

// Macro that implements the same `HostWithStore` trait for the bean and
// security-policy bindings modules. Those bindings define an equivalent
// `host` interface (separate types but identical shape), so the host impl is
// duplicated against each module's generated `HostWithStore` trait.
macro_rules! impl_host_for_binding {
    ($bindings_mod:ident) => {
        // Host marker stays on WasmHostState for the `impl<T: Host> Host for &mut T`
        // blanket bound on D::Data<'a> inside add_to_linker.
        impl crate::$bindings_mod::camel::plugin::host::Host for WasmHostState {}

        // HostWithStore targets HasSelf<WasmHostState> (wasmtime's built-in HasData)
        // instead of a hand-rolled HasData impl on WasmHostState.
        impl crate::$bindings_mod::camel::plugin::host::HostWithStore<WasmHostState>
            for wasmtime::component::HasSelf<WasmHostState>
        {
            async fn camel_call(
                store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                uri: String,
                payload: String,
            ) -> Result<String, crate::$bindings_mod::camel::plugin::types::WasmError> {
                WasmHostState::camel_call_impl(store, uri, payload)
                    .await
                    .map_err(map_plugin_wasm_error_to)
            }

            async fn camel_poll(
                store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                uri: String,
                timeout_ms: u32,
            ) -> Result<String, crate::$bindings_mod::camel::plugin::types::WasmError> {
                WasmHostState::camel_poll_impl(store, uri, timeout_ms)
                    .await
                    .map_err(map_plugin_wasm_error_to)
            }

            fn get_property(
                mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                key: String,
            ) -> Option<String> {
                store.get().get_property_impl(key)
            }

            fn set_property(
                mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                key: String,
                value: String,
            ) {
                store.get().set_property_impl(key, value)
            }

            fn host_store(
                mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                key: String,
                value: String,
            ) -> Result<(), crate::$bindings_mod::camel::plugin::types::WasmError> {
                WasmHostState::host_store_impl(store.get(), key, value)
                    .map_err(map_plugin_wasm_error_to)
            }

            fn host_load(
                mut store: Access<'_, WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
                key: String,
            ) -> Result<Option<String>, crate::$bindings_mod::camel::plugin::types::WasmError> {
                WasmHostState::host_load_impl(store.get(), key).map_err(map_plugin_wasm_error_to)
            }
        }
    };
}

// Helper: re-map a canonical bindings::camel::plugin::types::WasmError to
// the same shape produced by a different bindgen invocation (bean /
// authorization-policy). All variants have identical structure, so this is
// a straight field-for-field clone.
fn map_plugin_wasm_error_to<E: FromWasmErrorVariant>(
    e: crate::bindings::camel::plugin::types::WasmError,
) -> E {
    match e {
        crate::bindings::camel::plugin::types::WasmError::ProcessorError(s) => {
            E::from_processor_error(s)
        }
        crate::bindings::camel::plugin::types::WasmError::TypeConversion(s) => {
            E::from_type_conversion(s)
        }
        crate::bindings::camel::plugin::types::WasmError::Io(s) => E::from_io(s),
        crate::bindings::camel::plugin::types::WasmError::Timeout => E::from_timeout(),
    }
}

trait FromWasmErrorVariant {
    fn from_processor_error(s: String) -> Self;
    fn from_type_conversion(s: String) -> Self;
    fn from_io(s: String) -> Self;
    fn from_timeout() -> Self;
}

impl FromWasmErrorVariant for crate::bindings::camel::plugin::types::WasmError {
    fn from_processor_error(s: String) -> Self {
        Self::ProcessorError(s)
    }
    fn from_type_conversion(s: String) -> Self {
        Self::TypeConversion(s)
    }
    fn from_io(s: String) -> Self {
        Self::Io(s)
    }
    fn from_timeout() -> Self {
        Self::Timeout
    }
}

impl FromWasmErrorVariant for crate::bean_bindings::camel::plugin::types::WasmError {
    fn from_processor_error(s: String) -> Self {
        Self::ProcessorError(s)
    }
    fn from_type_conversion(s: String) -> Self {
        Self::TypeConversion(s)
    }
    fn from_io(s: String) -> Self {
        Self::Io(s)
    }
    fn from_timeout() -> Self {
        Self::Timeout
    }
}

impl FromWasmErrorVariant for crate::security_policy_bindings::camel::plugin::types::WasmError {
    fn from_processor_error(s: String) -> Self {
        Self::ProcessorError(s)
    }
    fn from_type_conversion(s: String) -> Self {
        Self::TypeConversion(s)
    }
    fn from_io(s: String) -> Self {
        Self::Io(s)
    }
    fn from_timeout() -> Self {
        Self::Timeout
    }
}

pub fn add_bean_to_linker(linker: &mut Linker<WasmHostState>) -> Result<(), wasmtime::Error> {
    crate::bean_bindings::camel::plugin::host::add_to_linker::<
        WasmHostState,
        wasmtime::component::HasSelf<WasmHostState>,
    >(linker, |state| state)
}

impl_host_for_binding!(bean_bindings);

pub fn add_security_policy_to_linker(
    linker: &mut Linker<WasmHostState>,
) -> Result<(), wasmtime::Error> {
    crate::security_policy_bindings::camel::plugin::host::add_to_linker::<
        WasmHostState,
        wasmtime::component::HasSelf<WasmHostState>,
    >(linker, |state| state)
}

impl_host_for_binding!(security_policy_bindings);

// RAII recursion guard: increments `call_depth` on construction and
// decrements on drop. The drop runs even if the future is cancelled,
// which is the whole point: a manual inc/dec around an `.await` leaks
// the increment forever if the future is dropped between the two sites.
// Returns `None` from `new` if the depth was already > 0 (a nested
// call is in flight) so the caller can return the recursion error.
struct DepthGuard<'a> {
    depth: &'a std::sync::atomic::AtomicUsize,
}

impl<'a> DepthGuard<'a> {
    fn new(depth: &'a std::sync::atomic::AtomicUsize) -> Option<Self> {
        let prev = depth.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        if prev > 0 {
            depth.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
            return None;
        }
        Some(DepthGuard { depth })
    }
}

impl Drop for DepthGuard<'_> {
    fn drop(&mut self) {
        self.depth.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
    }
}

// Inherent methods on `WasmHostState` that implement the host function
// logic. The trait impls above are thin shims that call these. Splitting
// the logic this way gives the test suite direct access to the same code
// paths without needing an `Accessor<WasmHostState, HasSelf<WasmHostState>>` (which
// can only be constructed inside a `run_concurrent` scope).
impl WasmHostState {
    pub(crate) async fn camel_call_impl(
        store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        uri: String,
        payload: String,
    ) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
        // Capability gate (H4): check scheme against per-world allowlist.
        let scheme = uri.split(':').next().unwrap_or("").to_string();
        let can_call = store.with(|mut view| view.get().capabilities.can_call(&scheme));
        if !can_call {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                    "camel_call denied: scheme '{}' not in capability allowlist",
                    scheme
                )),
            );
        }

        // Snapshot the registry (cloned Arc — cheap) for the async work
        // so we don't borrow `store` across the await.
        let registry = store.with(|mut view| view.get().registry.clone());

        // Recursion guard. The RAII `DepthGuard` decrements on drop, so
        // even if the `run_async_call` future is cancelled mid-await the
        // counter returns to 0 — otherwise every subsequent call would
        // fail with "recursive wasm calls not supported".
        //
        // Clone the Arc<AtomicUsize> from store state — no unsafe needed.
        // The guard borrows the Arc's inner AtomicUsize for the call scope.
        let call_depth = store.with(|mut view| view.get().call_depth.clone());
        let _depth_guard = match DepthGuard::new(call_depth.as_ref()) {
            Some(g) => g,
            None => {
                return Err(
                    crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                        "recursive wasm calls not supported".to_string(),
                    ),
                );
            }
        };

        run_async_call(registry, uri, payload).await
    }

    pub(crate) async fn camel_poll_impl(
        store: &Accessor<WasmHostState, wasmtime::component::HasSelf<WasmHostState>>,
        uri: String,
        timeout_ms: u32,
    ) -> Result<String, crate::bindings::camel::plugin::types::WasmError> {
        let scheme = uri.split(':').next().unwrap_or("").to_string();
        let can_call = store.with(|mut view| view.get().capabilities.can_call(&scheme));
        if !can_call {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                    "camel_poll denied: scheme '{}' not in capability allowlist",
                    scheme
                )),
            );
        }

        // Recursion guard (RAII — see camel_call_impl for rationale).
        let registry = store.with(|mut view| view.get().registry.clone());
        let call_depth = store.with(|mut view| view.get().call_depth.clone());
        let _depth_guard = match DepthGuard::new(call_depth.as_ref()) {
            Some(g) => g,
            None => {
                return Err(
                    crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                        "recursive wasm calls not supported".to_string(),
                    ),
                );
            }
        };

        run_async_poll(registry, uri, timeout_ms).await
    }

    pub(crate) fn get_property_impl(&self, key: String) -> Option<String> {
        self.properties.get(&key).map(|v| match v {
            Value::String(s) => s.clone(),
            other => other.to_string(),
        })
    }

    pub(crate) fn set_property_impl(&mut self, key: String, value: String) {
        let parsed = serde_json::from_str::<Value>(&value).unwrap_or(Value::String(value));
        self.properties.insert(key, parsed);
    }

    pub(crate) fn host_store_impl(
        state: &mut WasmHostState,
        key: String,
        value: String,
    ) -> Result<(), crate::bindings::camel::plugin::types::WasmError> {
        if !state.capabilities.host_kv {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                    "host_store denied: host_kv capability not granted".to_string(),
                ),
            );
        }
        state
            .state_store
            .store(&key, &value)
            .map_err(crate::bindings::camel::plugin::types::WasmError::Io)
    }

    pub(crate) fn host_load_impl(
        state: &mut WasmHostState,
        key: String,
    ) -> Result<Option<String>, crate::bindings::camel::plugin::types::WasmError> {
        if !state.capabilities.host_kv {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                    "host_load denied: host_kv capability not granted".to_string(),
                ),
            );
        }
        state
            .state_store
            .load(&key)
            .map_err(crate::bindings::camel::plugin::types::WasmError::Io)
    }
}

// Inherent (non-async) versions of the impl methods for the test suite.
// The trait methods are async + store-bound, but the unit tests want to
// exercise the gating logic synchronously against a plain `&mut
// WasmHostState`. Both layers share the same recursion / capability /
// host_kv gates.
//
// `dead_code` allow: the `*_gate` methods are referenced only from
// `#[cfg(test)]` code in this same module, but the rustc dead-code lint
// analyses test code separately and reports them as unused. The
// #[allow(dead_code)] silences the false positive.
#[allow(dead_code)]
impl WasmHostState {
    /// Same gating as `camel_call_impl` but synchronous and against a
    /// plain `&mut self`. Returns the same error variant on gate failure.
    /// Async work is NOT run — this is purely for unit-testing the gate
    /// logic in isolation.
    pub(crate) fn camel_call_gate(
        &mut self,
        uri: String,
    ) -> Result<(), crate::bindings::camel::plugin::types::WasmError> {
        let scheme = uri.split(':').next().unwrap_or("").to_string();
        if !self.capabilities.can_call(&scheme) {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(format!(
                    "camel_call denied: scheme '{}' not in capability allowlist",
                    scheme
                )),
            );
        }
        if self.call_depth.load(std::sync::atomic::Ordering::Relaxed) > 0 {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                    "recursive wasm calls not supported".to_string(),
                ),
            );
        }
        self.call_depth
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Ok(())
    }

    pub(crate) fn release_call_depth(&self) {
        self.call_depth
            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
    }

    pub(crate) fn host_store_gate(
        &self,
        key: String,
        value: String,
    ) -> Result<(), crate::bindings::camel::plugin::types::WasmError> {
        if !self.capabilities.host_kv {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                    "host_store denied: host_kv capability not granted".to_string(),
                ),
            );
        }
        self.state_store
            .store(&key, &value)
            .map_err(crate::bindings::camel::plugin::types::WasmError::Io)
    }

    pub(crate) fn host_load_gate(
        &self,
        key: String,
    ) -> Result<Option<String>, crate::bindings::camel::plugin::types::WasmError> {
        if !self.capabilities.host_kv {
            return Err(
                crate::bindings::camel::plugin::types::WasmError::ProcessorError(
                    "host_load denied: host_kv capability not granted".to_string(),
                ),
            );
        }
        self.state_store
            .load(&key)
            .map_err(crate::bindings::camel::plugin::types::WasmError::Io)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use camel_core::Registry;
    use std::collections::HashMap;
    use std::sync::Arc;

    fn make_state(call_depth: usize) -> WasmHostState {
        WasmHostState {
            table: wasmtime::component::ResourceTable::new(),
            wasi: wasmtime_wasi::WasiCtxBuilder::new()
                .inherit_stderr()
                .build(),
            properties: HashMap::new(),
            registry: Arc::new(std::sync::Mutex::new(Registry::new())),
            call_depth: Arc::new(std::sync::atomic::AtomicUsize::new(call_depth)),
            limits: wasmtime::StoreLimits::default(),
            state_store: crate::state_store::StateStore::new(),
            capabilities: crate::capabilities::WasmCapabilities::default(),
        }
    }

    #[test]
    fn test_recursion_guard_blocks_nested_calls() {
        let state = make_state(1);
        assert!(state.call_depth.load(std::sync::atomic::Ordering::Relaxed) > 0);
    }

    #[test]
    fn test_recursion_guard_allows_initial_call() {
        let state = make_state(0);
        assert_eq!(
            state.call_depth.load(std::sync::atomic::Ordering::Relaxed),
            0
        );
    }

    #[test]
    fn test_get_property_string_value() {
        let mut state = make_state(0);
        state
            .properties
            .insert("key".to_string(), Value::String("value".to_string()));
        assert_eq!(
            state.get_property_impl("key".to_string()),
            Some("value".to_string())
        );
    }

    #[test]
    fn test_get_property_missing_key() {
        let state = make_state(0);
        assert!(!state.properties.contains_key("missing"));
    }

    #[test]
    fn test_set_property_json_value() {
        let mut state = make_state(0);
        state.set_property_impl("json_key".to_string(), "{\"nested\":true}".to_string());
        assert!(state.properties.get("json_key").unwrap().is_object());
    }

    #[test]
    fn test_uri_scheme_parsing() {
        assert_eq!("direct".split(':').next().unwrap_or(""), "direct");
        assert_eq!("log:info".split(':').next().unwrap_or(""), "log");
        assert_eq!("noscheme".split(':').next().unwrap_or(""), "noscheme");
        assert_eq!("".split(':').next().unwrap_or(""), "");
    }

    #[test]
    fn test_camel_call_does_not_panic_inside_tokio_runtime() {
        let mut state = make_state(0);
        let result = state.camel_call_gate("noscheme".to_string());
        assert!(
            result.is_err(),
            "should return error for empty scheme, not panic"
        );
    }

    // Capability gating (H4 / H5)
    // -----------------------------------------------------------------------
    // Default capabilities are fail-closed: empty scheme allowlist, host_kv
    // disabled. Tests below verify that the gates fire BEFORE any side effect
    // (registry lookup, state store mutation) so a denied call cannot leak
    // information or persist data.

    #[test]
    fn test_camel_call_denied_without_capability() {
        let mut state = make_state(0);
        // Default capabilities: empty allowlist
        let result = state.camel_call_gate("log:info".to_string());
        let err = result.unwrap_err();
        let msg = format!("{:?}", err);
        assert!(
            msg.contains("denied"),
            "expected 'denied' in error, got: {msg}"
        );
    }

    #[test]
    fn test_camel_poll_denied_without_capability() {
        let state = make_state(0);
        // The poll gate mirrors the call gate's scheme check.
        let can_call = state.capabilities.can_call("file:foo");
        assert!(!can_call, "default capabilities must deny 'file' scheme");
    }

    #[test]
    fn test_host_store_denied_without_capability() {
        let state = make_state(0);
        let result = state.host_store_gate("key".to_string(), "val".to_string());
        let err = result.unwrap_err();
        let msg = format!("{:?}", err);
        assert!(
            msg.contains("host_kv"),
            "expected 'host_kv' in error, got: {msg}"
        );
    }

    #[test]
    fn test_host_load_denied_without_capability() {
        let state = make_state(0);
        let result = state.host_load_gate("key".to_string());
        let err = result.unwrap_err();
        let msg = format!("{:?}", err);
        assert!(
            msg.contains("host_kv"),
            "expected 'host_kv' in error, got: {msg}"
        );
    }

    #[test]
    fn test_camel_call_allowed_with_capability_passes_scheme_check() {
        // Granting the scheme means the gate no longer denies — but with an
        // empty registry, the next stage returns "component not found". This
        // proves the gate runs BEFORE the registry lookup.
        let mut state = make_state(0);
        state.capabilities = crate::capabilities::WasmCapabilities::from_scheme_list("noscheme");
        // The scheme check now passes; the gate increments call_depth.
        let result = state.camel_call_gate("noscheme:foo".to_string());
        assert!(
            result.is_ok(),
            "gate should pass when scheme is allowed; got: {result:?}"
        );
        state.release_call_depth();
    }

    #[test]
    fn test_denied_capabilities_block_host_kv_via_field() {
        // H5: policy worlds (denied caps) must have host_kv disabled — the
        // gate is a single field check, not a per-key check.
        let mut state = make_state(0);
        state.capabilities = crate::capabilities::WasmCapabilities::denied();
        assert!(!state.capabilities.host_kv);
        // host_store and host_load both go through the same gate
        assert!(
            state
                .host_store_gate("k".to_string(), "v".to_string())
                .is_err()
        );
        assert!(state.host_load_gate("k".to_string()).is_err());
    }

    // ── DepthGuard: RAII recursion counter (C2, I2) ──────────────────────
    //
    // The guard increments `call_depth` on `new` and decrements on `Drop`.
    // The three tests below cover the three behaviours the production code
    // depends on: increment, drop-decrement, and recursion rejection.
    //
    // Per I2: the spec explicitly notes that "if testing through `Accessor`
    // is too complex for unit tests, at minimum test the `DepthGuard`
    // struct directly". The trait method signatures now use
    // `&Accessor<WasmHostState, HasSelf<WasmHostState>>` (switched from
    // hand-rolled HasData to wasmtime's built-in HasSelf). Accessor
    // construction still requires `run_concurrent`, so DepthGuard unit
    // tests remain the most practical verification path for the RAII
    // recursion counter behaviour.

    #[test]
    fn test_depth_guard_increments_and_decrements() {
        // new() must increment; Drop must decrement back to the original value.
        let state = make_state(0);
        let depth = &state.call_depth;
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 0);
        {
            let _g = DepthGuard::new(depth).expect("first guard must succeed");
            assert_eq!(
                depth.load(std::sync::atomic::Ordering::SeqCst),
                1,
                "guard must increment to 1"
            );
        }
        assert_eq!(
            depth.load(std::sync::atomic::Ordering::SeqCst),
            0,
            "Drop must decrement back to 0"
        );
    }

    #[test]
    fn test_depth_guard_blocks_recursion() {
        // A second guard while the first is alive must return None and must
        // not leak an increment (the failed new() must roll back).
        let state = make_state(0);
        let depth = &state.call_depth;
        let first = DepthGuard::new(depth).expect("first guard must succeed");
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 1);
        let second = DepthGuard::new(depth);
        assert!(second.is_none(), "nested guard must be rejected");
        // Drop the second (None) and verify the counter is still 1.
        drop(second);
        assert_eq!(
            depth.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "rejected guard must not leak an increment"
        );
        drop(first);
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    #[test]
    fn test_depth_guard_rolls_back_on_rejection() {
        // If new() is called when depth > 0, it must return None AND restore
        // the counter (otherwise the increment would leak forever).
        let state = make_state(0);
        let depth = &state.call_depth;
        let guard = DepthGuard::new(depth).expect("first guard");
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 1);
        // The second attempt increments-then-decrements; verify the transient
        // bump does not affect the observed value after rejection.
        let _rejected = DepthGuard::new(depth);
        assert_eq!(
            depth.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "after rejection, counter must be unchanged"
        );
        drop(guard);
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    #[test]
    fn test_depth_guard_decrements_on_explicit_drop() {
        // Explicit drop() must decrement. This is the test the original
        // manual inc/dec pattern was missing: a future that gets dropped
        // between inc and dec would leak the counter.
        let state = make_state(0);
        let depth = &state.call_depth;
        let guard = DepthGuard::new(depth).expect("first guard");
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 1);
        drop(guard);
        assert_eq!(depth.load(std::sync::atomic::Ordering::SeqCst), 0);
    }
}