orama-js-pool 0.4.3

Create a pool of JavaScript engines to invoke JavaScript code concurrently.
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
use std::collections::HashMap;

use deno_core::ModuleCodeString;
use serde::de::DeserializeOwned;
use tracing::warn;

use crate::orama_extension::SharedCache;
use crate::runtime::ModuleName;

use super::{
    options::{DomainPermission, ExecOptions, MaxExecutions},
    parameters::TryIntoFunctionParameters,
    runtime::{Runtime, RuntimeError},
};

use std::sync::Arc;

/// Metadata about a loaded module
struct ModuleInfo {
    code: Arc<str>,
}

/// Worker that can execute multiple modules with a shared runtime
pub struct Worker {
    runtime: Option<Runtime>,
    modules: HashMap<String, ModuleInfo>,
    cache: SharedCache,
    domain_permission: DomainPermission,
    evaluation_timeout: std::time::Duration,
    execution_timeout: std::time::Duration,
    max_executions: MaxExecutions,
    execution_count: u64,
    version: u64,
}

impl Worker {
    /// Create a new worker with the given cache, and settings
    pub(crate) fn new(
        cache: SharedCache,
        domain_permission: DomainPermission,
        evaluation_timeout: std::time::Duration,
        execution_timeout: std::time::Duration,
        max_executions: MaxExecutions,
        version: u64,
    ) -> Self {
        Self {
            runtime: None,
            modules: HashMap::new(),
            cache,
            domain_permission,
            evaluation_timeout,
            execution_timeout,
            max_executions,
            execution_count: 0,
            version,
        }
    }

    /// Get the version of this worker
    pub fn version(&self) -> u64 {
        self.version
    }

    pub fn builder() -> WorkerBuilder {
        WorkerBuilder::default()
    }

    /// Add a module to this worker
    pub async fn add_module<Code>(
        &mut self,
        name: impl Into<String>,
        code: Code,
    ) -> Result<(), RuntimeError>
    where
        Code: Into<ModuleCodeString> + Send + 'static,
    {
        let name_string = name.into();
        let validated_name = ModuleName::new(&name_string)?;
        let code_string: ModuleCodeString = code.into();
        let (runtime_code, module_code) = code_string.into_cheap_copy();

        let runtime = self.get_runtime().await?;
        runtime.load_module(validated_name, runtime_code).await?;

        self.modules.insert(
            name_string,
            ModuleInfo {
                code: module_code.as_str().into(),
            },
        );

        Ok(())
    }

    /// Remove a module from this worker and recreate the runtime
    /// This helps free memory by recreating the runtime without the removed module
    pub async fn remove_module(&mut self, name: &str) -> Result<(), RuntimeError> {
        if !self.modules.contains_key(name) {
            return Err(RuntimeError::MissingModule(name.to_string()));
        }

        self.modules.remove(name);

        // Rebuild the runtime to recreate it without the new module
        self.rebuild_runtime().await?;

        Ok(())
    }

    /// Execute a function in a module
    pub async fn exec<Input, Output>(
        &mut self,
        module_name: &str,
        function_name: &str,
        params: &Input,
        exec_options: ExecOptions,
    ) -> Result<Output, RuntimeError>
    where
        Input: TryIntoFunctionParameters + Send + Sync + 'static + ?Sized,
        Output: DeserializeOwned + Send + 'static,
    {
        if !self.modules.contains_key(module_name) {
            return Err(RuntimeError::MissingModule(module_name.to_string()));
        }

        // Check if we need to invalidate the runtime due to execution limit
        if self.max_executions.is_exceeded(self.execution_count) {
            warn!("Worker reached max executions limit, invalidating runtime");
            self.runtime = None;
            self.execution_count = 0;
        }

        let domain_permission = exec_options
            .domain_permission
            .unwrap_or_else(|| self.domain_permission.clone());

        let timeout = exec_options.timeout.unwrap_or(self.execution_timeout);

        let runtime = self.get_runtime().await?;

        let params_tuple = params.try_into_function_parameter()?;
        let params_value = serde_json::to_value(params_tuple.0)?;

        let result: serde_json::Value = runtime
            .exec(
                module_name,
                function_name.to_string(),
                &params_value,
                exec_options.stdout_sender,
                domain_permission,
                timeout,
            )
            .await?;

        self.execution_count += 1;

        let output: Output = serde_json::from_value(result)?;
        Ok(output)
    }

    // Checks if the runtime is healthy otherwise it recreate it.
    async fn get_runtime(&mut self) -> Result<&mut Runtime, RuntimeError> {
        let needs_rebuild = !matches!(&self.runtime, Some(rt) if rt.is_alive());

        if needs_rebuild {
            warn!("Runtime not alive or missing, rebuilding...");
            self.rebuild_runtime().await?;
        }

        self.runtime.as_mut().ok_or(RuntimeError::Terminated)
    }

    /// Rebuild the runtime with all currently registered modules
    async fn rebuild_runtime(&mut self) -> Result<(), RuntimeError> {
        let mut runtime = Runtime::new(
            self.domain_permission.clone(),
            self.evaluation_timeout,
            self.cache.clone(),
        )
        .await?;

        for (name, info) in &self.modules {
            let validated_name = ModuleName::new(name.clone())?;
            runtime
                .load_module(validated_name, info.code.clone())
                .await?;
        }

        self.runtime = Some(runtime);

        Ok(())
    }

    /// Check if the worker is alive
    pub fn is_alive(&self) -> bool {
        self.runtime.as_ref().is_some_and(|rt| rt.is_alive())
    }

    /// Get the shared cache
    pub fn cache(&self) -> &SharedCache {
        &self.cache
    }
}

/// Builder for creating a Worker
pub struct WorkerBuilder {
    modules: Vec<(String, ModuleCodeString)>,
    cache: Option<SharedCache>,
    domain_permission: Option<DomainPermission>,
    evaluation_timeout: Option<std::time::Duration>,
    execution_timeout: Option<std::time::Duration>,
    max_executions: MaxExecutions,
    version: u64,
}

impl WorkerBuilder {
    /// Create a new WorkerBuilder
    pub fn new() -> Self {
        Self {
            modules: Vec::new(),
            cache: None,
            domain_permission: None,
            evaluation_timeout: None,
            execution_timeout: None,
            max_executions: MaxExecutions::default(),
            version: 0,
        }
    }

    /// Add a module to the worker
    pub fn add_module<Code: Into<ModuleCodeString>>(
        mut self,
        name: impl Into<String>,
        code: Code,
    ) -> Self {
        let code: ModuleCodeString = code.into();
        self.modules.push((name.into(), code));
        self
    }

    /// Set the cache for the worker
    pub fn with_cache(mut self, cache: SharedCache) -> Self {
        self.cache = Some(cache);
        self
    }

    /// Set the domain permission for all modules
    pub fn with_domain_permission(mut self, permission: DomainPermission) -> Self {
        self.domain_permission = Some(permission);
        self
    }

    /// Set the evaluation timeout for module loading
    pub fn with_evaluation_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.evaluation_timeout = Some(timeout);
        self
    }

    /// Set the execution timeout for function execution
    pub fn with_execution_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.execution_timeout = Some(timeout);
        self
    }

    /// Set the maximum number of executions before recycling the runtime.
    pub fn with_max_executions(mut self, max: MaxExecutions) -> Self {
        self.max_executions = max;
        self
    }

    /// Set the version for the worker
    pub fn with_version(mut self, version: u64) -> Self {
        self.version = version;
        self
    }

    /// Build the worker
    pub async fn build(self) -> Result<Worker, RuntimeError> {
        let cache = self.cache.unwrap_or_default();
        let domain_permission = self.domain_permission.unwrap_or_default();
        let evaluation_timeout = self
            .evaluation_timeout
            .unwrap_or(std::time::Duration::from_secs(5));
        let execution_timeout = self
            .execution_timeout
            .unwrap_or(std::time::Duration::from_secs(30));

        let mut worker = Worker::new(
            cache,
            domain_permission,
            evaluation_timeout,
            execution_timeout,
            self.max_executions,
            self.version,
        );

        for (name, code) in self.modules {
            worker.add_module(name, code).await?;
        }

        Ok(worker)
    }
}

impl Default for WorkerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    #[tokio::test]
    async fn test_module_evaluation_timeout() {
        let _ = tracing_subscriber::fmt::try_init();

        let expensive_code = r#"
            await new Promise(resolve => setTimeout(resolve, 10000));
            function getValue() {
                return 42;
            }
            export default { getValue };
        "#;

        let not_expensive_code = r#"
            function add(a, b) { return a + b; }
            export default { add };
        "#;

        let result = Worker::builder()
            .with_evaluation_timeout(Duration::from_millis(100))
            .add_module("expensive", expensive_code.to_string())
            .build()
            .await;

        assert!(result.is_err(), "Module evaluation should timeout");
        assert!(matches!(result, Err(RuntimeError::InitTimeout)));

        let mut worker = Worker::builder()
            .with_evaluation_timeout(Duration::from_millis(100))
            .add_module("not_expensive", not_expensive_code.to_string())
            .build()
            .await
            .unwrap();

        // also on adding a module
        let result = worker
            .add_module("expensive", expensive_code.to_string())
            .await;

        assert!(result.is_err(), "Module evaluation should timeout");
        assert!(matches!(result, Err(RuntimeError::InitTimeout)));

        worker
            .add_module("not_expensive_2", not_expensive_code.to_string())
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_module_evaluation_domain_permission() {
        let _ = tracing_subscriber::fmt::try_init();

        let not_allowed_code = r#"
            let res = await fetch("http://foo.test");
            let value = await res.text();

            function getValue() {
                return value;
            }
            export default { getValue };
        "#;

        let result = Worker::builder()
            .with_domain_permission(DomainPermission::DenyAll)
            .add_module("net_call", not_allowed_code.to_string())
            .build()
            .await;

        assert!(
            result.is_err(),
            "Module evaluation should fail due to network deny"
        );
        match result {
            Err(RuntimeError::InitializationError(e)) => {
                let error_msg = e.to_string();
                assert!(
                    error_msg.contains("network access is denied"),
                    "Error should contain network deny information, got: {error_msg}",
                );
            }
            _ => panic!("Expected InitializationError with network deny information"),
        }
    }

    #[tokio::test]
    async fn test_module_override() {
        let _ = tracing_subscriber::fmt::try_init();

        let original_code = r#"
            function getValue() { return 42; }
            export default { getValue };
        "#;

        let override_code = r#"
            function getValue() { return 100; }
            export default { getValue };
        "#;

        let mut worker = Worker::builder()
            .add_module("test", original_code.to_string())
            .build()
            .await
            .unwrap();

        let result: i32 = worker
            .exec("test", "getValue", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, 42);

        // Override the module
        worker
            .add_module("test", override_code.to_string())
            .await
            .unwrap();

        let result: i32 = worker
            .exec("test", "getValue", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, 100, "Function should return overridden value");
    }

    #[tokio::test]
    async fn test_max_executions() {
        let _ = tracing_subscriber::fmt::try_init();

        let counter_code = r#"
            let callCount = 0;
            function increment() {
                callCount++;
                return callCount;
            }
            export default { increment };
        "#;

        let mut worker = Worker::builder()
            .add_module("counter", counter_code.to_string())
            .with_max_executions(MaxExecutions::Limited(3))
            .build()
            .await
            .unwrap();

        // First 3 executions should increment the counter
        let result1: i32 = worker
            .exec("counter", "increment", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result1, 1);

        let result2: i32 = worker
            .exec("counter", "increment", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result2, 2);

        let result3: i32 = worker
            .exec("counter", "increment", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result3, 3);

        // After 3 executions, the runtime should be recycled
        // and the counter should reset to 1
        let result4: i32 = worker
            .exec("counter", "increment", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(
            result4, 1,
            "Counter should reset after max_executions is reached"
        );
    }

    #[tokio::test]
    async fn test_runtime_error_on_invalid_function_code() {
        let _ = tracing_subscriber::fmt::try_init();

        // This code has a syntax error in the function body that will only be
        // triggered when the function is executed (not during module evaluation)
        let code_with_runtime_syntax_error = r#"
            function badFunction() {
                // This will cause a syntax error during dynamic code generation
                eval('this is not valid javascript!!!');
                return 42;
            }
            export default { badFunction };
        "#;

        let mut worker = Worker::builder()
            .add_module("test", code_with_runtime_syntax_error.to_string())
            .build()
            .await
            .unwrap();

        let result: Result<i32, RuntimeError> = worker
            .exec("test", "badFunction", &(), ExecOptions::default())
            .await;

        // Should return an error, not panic
        assert!(
            result.is_err(),
            "Should return error for runtime syntax error"
        );
        assert!(
            matches!(result.unwrap_err(), RuntimeError::ErrorThrown(_)),
            "Should return ErrorThrown variant"
        );
    }

    #[tokio::test]
    async fn test_runtime_error_on_eval_failure() {
        let _ = tracing_subscriber::fmt::try_init();

        // This code will fail during the eval phase (after load_side_es_module_from_code)
        // by throwing an error during async function execution
        let code_with_async_error = r#"
            async function throwingFunction() {
                // Force an error during execution that affects the eval phase
                await Promise.reject(new Error("Async execution failed"));
                return 42;
            }
            export default { throwingFunction };
        "#;

        let mut worker = Worker::builder()
            .add_module("test", code_with_async_error.to_string())
            .build()
            .await
            .unwrap();

        let result: Result<i32, RuntimeError> = worker
            .exec("test", "throwingFunction", &(), ExecOptions::default())
            .await;

        // Should return an error, not panic
        assert!(
            result.is_err(),
            "Should return error for async execution failure"
        );
        assert!(
            matches!(result.unwrap_err(), RuntimeError::ErrorThrown(_)),
            "Should return ErrorThrown variant"
        );
    }

    #[tokio::test]
    async fn test_invalid_module_name() {
        let _ = tracing_subscriber::fmt::try_init();

        // Test that invalid module names are caught early
        let code = r#"
            function test() { return 42; }
            export default { test };
        "#;

        let mut worker = Worker::builder().build().await.unwrap();

        // Empty module name should fail
        let result = worker.add_module("", code.to_string()).await;
        assert!(result.is_err(), "Empty module name should fail");
        assert!(
            matches!(result.unwrap_err(), RuntimeError::InvalidModuleName(_, _)),
            "Should return InvalidModuleName error"
        );
    }

    #[tokio::test]
    async fn test_remove_module() {
        let _ = tracing_subscriber::fmt::try_init();

        let code1 = r#"
            function getValue() { return 42; }
            export default { getValue };
        "#;

        let code2 = r#"
            function add(a, b) { return a + b; }
            export default { add };
        "#;

        let mut worker = Worker::builder()
            .add_module("module1", code1.to_string())
            .add_module("module2", code2.to_string())
            .build()
            .await
            .unwrap();

        let result1: i32 = worker
            .exec("module1", "getValue", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result1, 42);

        let result2: i32 = worker
            .exec("module2", "add", &(5, 3), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result2, 8);

        worker.remove_module("module1").await.unwrap();

        let result: Result<i32, RuntimeError> = worker
            .exec("module1", "getValue", &(), ExecOptions::default())
            .await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            RuntimeError::MissingModule(name) if name == "module1"
        ));

        let result2: i32 = worker
            .exec("module2", "add", &(10, 5), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result2, 15);
    }

    #[tokio::test]
    async fn test_remove_nonexistent_module() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut worker = Worker::builder().build().await.unwrap();

        // Removing a module that doesn't exist should fail
        let result = worker.remove_module("nonexistent").await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            RuntimeError::MissingModule(name) if name == "nonexistent"
        ));
    }

    #[tokio::test]
    async fn test_function_without_return() {
        let code = r#"
            function noReturn() {}
            function explicitUndefined() { return undefined; }
            function explicitNull() { return null; }
            export default { noReturn, explicitUndefined, explicitNull };
        "#;

        let mut worker = Worker::builder()
            .add_module("test", code.to_string())
            .build()
            .await
            .unwrap();

        let result: serde_json::Value = worker
            .exec("test", "noReturn", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Null);

        let result: serde_json::Value = worker
            .exec("test", "explicitUndefined", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Null);

        let result: serde_json::Value = worker
            .exec("test", "explicitNull", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Null);

        let result: Option<i32> = worker
            .exec("test", "noReturn", &(), ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, None);

        let result: Result<String, RuntimeError> = worker
            .exec("test", "noReturn", &(), ExecOptions::default())
            .await;
        assert!(result.is_err());

        let result: Result<i32, RuntimeError> = worker
            .exec("test", "noReturn", &(), ExecOptions::default())
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_domain_permission_deny_ip_wildcard() {
        let _ = tracing_subscriber::fmt::try_init();

        let fetch_code = r#"
            async function fetchUrl(url) {
                // Don't catch errors - let them propagate to Rust
                const res = await fetch(url);
                return { success: true, status: res.status };
            }
            export default { fetchUrl };
        "#;

        let mut worker = Worker::builder()
            .with_domain_permission(DomainPermission::Deny(vec![
                "10.0.0.*".to_string(),
                "192.168.*.*".to_string(),
            ]))
            .add_module("fetch_test", fetch_code.to_string())
            .build()
            .await
            .unwrap();

        // Test that 10.0.0.* is blocked
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://10.0.0.1/test",
                ExecOptions::default(),
            )
            .await;

        assert!(result.is_err(), "Should block fetch to 10.0.0.1");
        match result.unwrap_err() {
            RuntimeError::NetworkPermissionDenied(msg) => {
                assert!(msg.contains("Domain not allowed"));
                assert!(msg.contains("10.0.0.1"));
            }
            e => panic!("Expected NetworkPermissionDenied, got: {e:?}"),
        }

        // Test that 192.168.*.* is blocked
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://192.168.1.100/test",
                ExecOptions::default(),
            )
            .await;

        assert!(result.is_err(), "Should block fetch to 192.168.1.100");

        // We just want to verify it's not blocked by permission
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://10.0.1.1/test",
                ExecOptions::default(),
            )
            .await;

        // Should not fail with NetworkPermissionDenied
        // It might fail with other errors (connection refused, etc.)
        if let Err(RuntimeError::NetworkPermissionDenied(_)) = result {
            panic!("Should not block 10.0.1.1 - it's not in deny list");
        }
    }

    #[tokio::test]
    async fn test_domain_permission_allow_ip_wildcard() {
        let _ = tracing_subscriber::fmt::try_init();

        let fetch_code = r#"
            async function fetchUrl(url) {
                // Don't catch errors - let them propagate to Rust
                const res = await fetch(url);
                return { success: true, status: res.status };
            }
            export default { fetchUrl };
        "#;

        let mut worker = Worker::builder()
            .with_domain_permission(DomainPermission::Allow(vec!["10.0.0.*".to_string()]))
            .add_module("fetch_test", fetch_code.to_string())
            .build()
            .await
            .unwrap();

        // Test that only 10.0.0.* is allowed
        // Other IPs should be blocked
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://192.168.1.1/test",
                ExecOptions::default(),
            )
            .await;

        assert!(
            result.is_err(),
            "Should block fetch to 192.168.1.1 (not in allow list)"
        );
        match result.unwrap_err() {
            RuntimeError::NetworkPermissionDenied(msg) => {
                assert!(msg.contains("Domain not allowed"));
            }
            e => panic!("Expected NetworkPermissionDenied, got: {e:?}"),
        }

        // Test that IPs outside range are blocked
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://8.8.8.8/test",
                ExecOptions::default(),
            )
            .await;

        assert!(result.is_err(), "Should block fetch to 8.8.8.8");
        match result.unwrap_err() {
            RuntimeError::NetworkPermissionDenied(msg) => {
                assert!(msg.contains("Domain not allowed"));
            }
            e => panic!("Expected NetworkPermissionDenied, got: {e:?}"),
        }

        // Verify that allowed IPs are not blocked by permission
        let result: Result<serde_json::Value, RuntimeError> = worker
            .exec(
                "fetch_test",
                "fetchUrl",
                &"http://10.0.0.1/test",
                ExecOptions::default(),
            )
            .await;

        if let Err(RuntimeError::NetworkPermissionDenied(_)) = result {
            panic!("Should not block 10.0.0.1 - it's in the allow list");
        }
    }

    #[tokio::test]
    async fn test_worker_execution_timeout_priority() {
        let _ = tracing_subscriber::fmt::try_init();

        let slow_code = r#"
            async function slowCode(delay) {
                await new Promise(resolve => setTimeout(resolve, delay));
                return "completed";
            }
            export default { slowCode };
        "#;

        // Worker has 5 second timeout
        let mut worker = Worker::builder()
            .with_execution_timeout(Duration::from_secs(5))
            .add_module("slow", slow_code.to_string())
            .build()
            .await
            .unwrap();

        // Case 1: No ExecOptions timeout - uses worker timeout (5 seconds)
        let result: String = worker
            .exec("slow", "slowCode", &100, ExecOptions::default())
            .await
            .unwrap();
        assert_eq!(result, "completed");

        // Case 2: ExecOptions timeout set - overrides worker timeout
        let result: Result<String, RuntimeError> = worker
            .exec(
                "slow",
                "slowCode",
                &200,
                ExecOptions::default().with_timeout(Duration::from_millis(50)),
            )
            .await;
        assert!(matches!(result.unwrap_err(), RuntimeError::ExecTimeout));

        // Case 3: ExecOptions with longer timeout - overrides worker timeout
        let result: String = worker
            .exec(
                "slow",
                "slowCode",
                &1000,
                ExecOptions::default().with_timeout(Duration::from_secs(3)),
            )
            .await
            .unwrap();
        assert_eq!(result, "completed");
    }
}