onetool 0.0.1-alpha.9

Sandboxed Lua REPL for LLM tool use
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
use crate::runtime::{self, output, sandbox};
use std::sync::{Arc, Mutex};

/// Errors that can occur during REPL operations.
#[derive(Debug)]
pub enum ReplError {
    /// Error from the Lua runtime
    Lua(mlua::Error),
    /// The runtime lock was poisoned (panic in another thread while holding lock)
    LockPoisoned,
}

impl std::fmt::Display for ReplError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReplError::Lua(e) => write!(f, "Lua error: {}", e),
            ReplError::LockPoisoned => write!(f, "Runtime lock poisoned"),
        }
    }
}

impl std::error::Error for ReplError {}

impl From<mlua::Error> for ReplError {
    fn from(err: mlua::Error) -> Self {
        ReplError::Lua(err)
    }
}

/// Main interface for evaluating Lua code in a sandboxed environment.
///
/// `Repl` manages a Lua runtime and captures output from `print()` calls separately
/// from expression return values. State (variables, functions) persists between
/// evaluations.
///
/// # Thread Safety
///
/// `Repl` is `Send + Sync` and can be safely shared across threads. The Lua runtime
/// is wrapped in a `Mutex` to provide interior mutability and thread-safe concurrent
/// access. This is enabled by mlua's `send` feature flag, which makes the underlying
/// Lua VM thread-safe.
///
/// You can safely call methods on the same `Repl` from multiple threads. The mutex
/// ensures that evaluations are serialized and state remains consistent.
///
/// # Example
///
/// ```
/// use onetool::Repl;
///
/// # fn example() -> Result<(), mlua::Error> {
/// let repl = Repl::new()?;
///
/// // State persists across evaluations
/// repl.eval("x = 42")?;
/// let outcome = repl.eval("return x * 2")?;
///
/// assert_eq!(outcome.result.unwrap()[0], "84");
/// # Ok(())
/// # }
/// ```
pub struct Repl {
    runtime: Mutex<mlua::Lua>,
}

/// Result of evaluating Lua code.
///
/// Contains both the return values (or error) from the evaluation and any output
/// captured from `print()` calls during execution.
pub struct EvalOutcome {
    /// Evaluation result: `Ok(values)` for successful execution with formatted return
    /// values, or `Err(message)` for runtime/syntax/callback errors.
    pub result: Result<Vec<String>, String>,
    /// Lines captured from `print()` calls during execution. Each element includes
    /// a trailing newline.
    pub output: Vec<String>,
}

impl Repl {
    /// Creates a new sandboxed Lua REPL.
    ///
    /// The runtime has dangerous operations blocked (file I/O, code loading, OS commands)
    /// while preserving safe Lua standard library functions.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use onetool::Repl;
    ///
    /// # fn example() -> Result<(), mlua::Error> {
    /// let repl = Repl::new()?;
    /// let outcome = repl.eval("return math.sqrt(16)")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new() -> Result<Self, mlua::Error> {
        Self::new_with(runtime::default()?)
    }

    /// Creates a REPL with a custom Lua runtime.
    ///
    /// Useful when you need to register custom functions or globals.
    /// Note that sandboxing is NOT automatically applied to the provided runtime.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use onetool::{Repl, runtime};
    ///
    /// # fn example() -> Result<(), mlua::Error> {
    /// let lua = mlua::Lua::new();
    ///
    /// // Apply sandboxing FIRST (it clears globals)
    /// runtime::sandbox::apply(&lua)?;
    ///
    /// // Register custom functions AFTER sandboxing
    /// lua.globals().set("custom_value", 42)?;
    ///
    /// let repl = Repl::new_with(lua)?;
    /// let outcome = repl.eval("return custom_value")?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Security Warning
    ///
    /// When using `new_with`, you are responsible for sandboxing the Lua runtime BEFORE
    /// registering custom functions. The sandbox uses `globals.clear()` which will destroy
    /// any globals registered before sandboxing is applied.
    ///
    /// **Recommended pattern**:
    /// 1. Create Lua runtime
    /// 2. Apply sandboxing with `runtime::sandbox::apply()`
    /// 3. Register custom functions AFTER sandboxing
    /// 4. Create REPL with `new_with()`
    ///
    /// **Not recommended**:
    /// 1. Create Lua runtime
    /// 2. Register custom functions ← These will be destroyed by sandboxing!
    /// 3. Apply sandboxing
    /// 4. Create REPL
    pub fn new_with(runtime: mlua::Lua) -> Result<Self, mlua::Error> {
        let runtime = Mutex::new(runtime);

        Ok(Self { runtime })
    }

    /// Creates a sandboxed REPL with a custom access control policy.
    ///
    /// This constructor automatically creates a fresh Lua runtime and applies policy-based
    /// sandboxing, giving you fine-grained control over which unsafe operations are allowed.
    ///
    /// # When to Use Each Constructor
    ///
    /// - [`new()`](Repl::new) - Default sandboxing (blocks all unsafe operations)
    /// - **`new_with_policy()`** - Custom policy for selective unsafe operation control
    /// - [`new_with()`](Repl::new_with) - Manual sandboxing and custom functions
    ///
    /// Use this constructor when you need to allow specific unsafe operations while blocking
    /// others. For example, you might allow filesystem reads but block writes, or allow
    /// specific system commands while denying others.
    ///
    /// # Parameters
    ///
    /// * `policy` - An [`Arc`]-wrapped policy implementing [`Policy`](sandbox::policy::Policy).
    ///              The policy's `check_access()` method is called for each unsafe operation.
    ///
    /// # Example: Custom Policy
    ///
    /// ```
    /// use onetool::{Repl, runtime::sandbox::policy::{Policy, Action, Decision}};
    /// use std::sync::Arc;
    ///
    /// # fn example() -> Result<(), mlua::Error> {
    /// // Policy that only allows string.upper but blocks other unsafe operations
    /// struct SelectivePolicy;
    ///
    /// impl Policy for SelectivePolicy {
    ///     fn check_access(&self, action: &Action) -> Decision {
    ///         match action {
    ///             Action::CallFunction { name, .. } if name == "string.upper" => {
    ///                 Decision::Allow
    ///             }
    ///             _ => Decision::Deny("Not in allowlist".to_string())
    ///         }
    ///     }
    /// }
    ///
    /// let repl = Repl::new_with_policy(Arc::new(SelectivePolicy))?;
    ///
    /// // Allowed operation succeeds
    /// let outcome = repl.eval(r#"return string.upper("hello")"#)?;
    /// assert!(outcome.result.unwrap()[0].contains("HELLO"));
    ///
    /// // Blocked operation returns nil
    /// let outcome = repl.eval(r#"return io.open("file.txt")"#)?;
    /// assert!(outcome.result.unwrap()[0].to_lowercase().contains("nil"));
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Combining with Custom Functions
    ///
    /// You can use [`with_runtime()`](Repl::with_runtime) to add custom Rust functions
    /// after creating a policy-based REPL:
    ///
    /// ```
    /// use onetool::{Repl, runtime::sandbox::policy::DenyAllPolicy};
    /// use std::sync::Arc;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let repl = Repl::new_with_policy(Arc::new(DenyAllPolicy))?;
    ///
    /// // Add custom functions after sandboxing
    /// repl.with_runtime(|lua| {
    ///     let greet = lua.create_function(|_, name: String| {
    ///         Ok(format!("Hello, {}!", name))
    ///     })?;
    ///     lua.globals().set("greet", greet)?;
    ///     Ok(())
    /// })?;
    ///
    /// let outcome = repl.eval(r#"return greet("World")"#)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [`Policy`](sandbox::policy::Policy) trait - Implement custom access control
    /// - [`DenyAllPolicy`](sandbox::policy::DenyAllPolicy) - Built-in restrictive policy
    /// - [`DangerousAllowAllPolicy`](sandbox::policy::DangerousAllowAllPolicy) - Built-in permissive policy (use with caution)
    /// - [`runtime::sandbox::apply_with_policy()`] - Low-level policy application API
    /// - [`with_runtime()`](Repl::with_runtime) - Add custom functions post-sandboxing
    pub fn new_with_policy<P: sandbox::policy::Policy + 'static>(
        policy: Arc<P>,
    ) -> Result<Self, mlua::Error> {
        let runtime = mlua::Lua::new();
        sandbox::apply_with_policy(&runtime, policy, None)?;
        Self::new_with(runtime)
    }

    /// Evaluates Lua code and captures output.
    ///
    /// Returns both the expression return values and any output from `print()` calls.
    /// State persists between calls, so variables and functions remain available.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use onetool::Repl;
    ///
    /// # fn example() -> Result<(), mlua::Error> {
    /// let repl = Repl::new()?;
    ///
    /// let outcome = repl.eval(r#"
    ///     print("debug message")
    ///     return 1, 2, 3
    /// "#)?;
    ///
    /// assert_eq!(outcome.output, vec!["debug message\n"]);
    /// assert_eq!(outcome.result.unwrap(), vec!["1", "2", "3"]);
    /// # Ok(())
    /// # }
    /// ```
    pub fn eval(&self, code: &str) -> Result<EvalOutcome, mlua::Error> {
        let runtime = self.runtime.lock().unwrap();

        let (eval_result, output) = output::with_output_capture(&runtime, |runtime| {
            runtime.load(code).eval::<mlua::MultiValue>()
        })?;

        let result = match eval_result {
            Ok(values) => Ok(values
                .iter()
                .map(|v| format!("{:#?}", v))
                .collect::<Vec<_>>()),
            Err(e) => Err(Self::format_lua_error(&e)),
        };

        Ok(EvalOutcome { result, output })
    }

    /// Provides temporary access to the underlying Lua runtime.
    ///
    /// This method allows you to perform advanced operations that aren't exposed
    /// by the REPL's main API, such as registering custom Rust functions or
    /// modifying global state.
    ///
    /// # Note on Thread Safety
    ///
    /// The Lua VM is thread-safe (enabled by mlua's `send` feature). The Mutex
    /// provides interior mutability and ensures thread-safe concurrent access.
    /// Multiple threads can safely call this method on the same `Repl` instance.
    ///
    /// # Parameters
    ///
    /// * `f` - A closure that receives an immutable reference to the Lua runtime
    ///         and can return any type wrapped in `Result<T, mlua::Error>`
    ///
    /// # Returns
    ///
    /// Returns whatever the closure returns, or a `ReplError` if the lock is poisoned
    /// or the Lua operation fails.
    ///
    /// # Examples
    ///
    /// ## Registering a Custom Rust Function
    ///
    /// ```
    /// use onetool::Repl;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let repl = Repl::new()?;
    ///
    /// // Register a function with captured state
    /// let multiplier = 10;
    /// repl.with_runtime(|lua| {
    ///     let func = lua.create_function(move |_, x: i32| {
    ///         Ok(x * multiplier)
    ///     })?;
    ///     lua.globals().set("multiply", func)?;
    ///     Ok(())
    /// })?;
    ///
    /// let result = repl.eval("return multiply(5)")?;
    /// assert_eq!(result.result.unwrap()[0], "50");
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Setting Global Variables
    ///
    /// ```
    /// use onetool::Repl;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let repl = Repl::new()?;
    ///
    /// // Set a global variable from Rust
    /// repl.with_runtime(|lua| {
    ///     lua.globals().set("API_KEY", "secret-key-123")?;
    ///     lua.globals().set("MAX_RETRIES", 3)?;
    ///     Ok(())
    /// })?;
    ///
    /// let result = repl.eval("return API_KEY, MAX_RETRIES")?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Extracting Values from Lua
    ///
    /// ```
    /// use onetool::Repl;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let repl = Repl::new()?;
    /// repl.eval("counter = 42")?;
    ///
    /// // Extract a value from Lua to Rust
    /// let counter: i32 = repl.with_runtime(|lua| {
    ///     lua.globals().get("counter")
    /// })?;
    ///
    /// assert_eq!(counter, 42);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Registering Custom Modules
    ///
    /// ```
    /// use onetool::{Repl, runtime::docs};
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let repl = Repl::new()?;
    ///
    /// repl.with_runtime(|lua| {
    ///     // Create a module table
    ///     let utils = lua.create_table()?;
    ///
    ///     // Add functions to the module
    ///     let double = lua.create_function(|_, x: i32| Ok(x * 2))?;
    ///     utils.set("double", double)?;
    ///
    ///     // Register the module
    ///     lua.globals().set("utils", utils)?;
    ///
    ///     // Register documentation
    ///     docs::register(lua, &docs::LuaDoc {
    ///         name: "utils".to_string(),
    ///         typ: docs::LuaDocTyp::Scope,
    ///         description: "Utility functions".to_string(),
    ///     })?;
    ///
    ///     Ok(())
    /// })?;
    ///
    /// let result = repl.eval("return utils.double(5)")?;
    /// assert_eq!(result.result.unwrap()[0], "10");
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## See Also
    ///
    /// - [`new_with()`](Repl::new_with) for pre-sandboxing extension
    /// - [`runtime::docs::register()`] for making functions discoverable
    /// - `examples/custom-functions.rs` for complete patterns
    pub fn with_runtime<F, R>(&self, f: F) -> Result<R, ReplError>
    where
        F: FnOnce(&mlua::Lua) -> Result<R, mlua::Error>,
    {
        let runtime = self.runtime.lock().map_err(|_| ReplError::LockPoisoned)?;
        f(&runtime).map_err(ReplError::from)
    }

    fn format_lua_error(error: &mlua::Error) -> String {
        match error {
            mlua::Error::RuntimeError(msg) => format!("RuntimeError: {}", msg),
            mlua::Error::SyntaxError { message, .. } => format!("SyntaxError: {}", message),
            mlua::Error::MemoryError(msg) => format!("MemoryError: {}", msg),
            mlua::Error::CallbackError { traceback, cause } => {
                format!("CallbackError: {}\nTraceback:\n{}", cause, traceback)
            }
            _ => format!("{}", error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Helper function to create a new REPL instance
    fn create_repl() -> Repl {
        Repl::new().expect("Failed to create REPL")
    }

    // Helper function to assert that a result contains an error with expected substring
    fn assert_error_contains(result: &Result<Vec<String>, String>, expected: &str) {
        assert!(result.is_err(), "Expected error but got success");
        let error = result.as_ref().unwrap_err();
        assert!(
            error.contains(expected),
            "Expected error to contain '{}', but got: {}",
            expected,
            error
        );
    }

    fn assert_send_sync<T: Send + Sync>() {}

    #[test]
    fn repl_is_send_sync() {
        assert_send_sync::<Repl>();
    }

    // === A. Initialization Tests ===

    #[test]
    fn test_new_creates_repl_successfully() {
        let result = Repl::new();
        assert!(result.is_ok(), "Failed to create REPL: {:?}", result.err());
    }

    #[test]
    fn test_new_with_custom_runtime() {
        let lua = mlua::Lua::new();
        // Set a global variable
        lua.globals().set("test_var", 42).unwrap();

        let repl = Repl::new_with(lua).unwrap();
        let eval = repl.eval("return test_var").unwrap();

        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "42");
    }

    #[test]
    fn test_new_applies_sandboxing() {
        let repl = create_repl();
        let eval = repl.eval("return io.open('test.txt', 'r')").unwrap();

        // With policy-based sandbox, io.open returns nil (denied by DenyAllPolicy)
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].to_lowercase().contains("nil"));
    }

    // === B. Successful Evaluation Tests ===

    #[test]
    fn test_eval_simple_expression() {
        let repl = create_repl();
        let eval = repl.eval("1 + 1").unwrap();

        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "2");
        assert!(eval.output.is_empty());
    }

    #[test]
    fn test_eval_string_expression() {
        let repl = create_repl();
        let eval = repl.eval(r#"return "hello""#).unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert_eq!(result.len(), 1);
        assert!(result[0].contains("hello"));
    }

    #[test]
    fn test_eval_multiple_return_values() {
        let repl = create_repl();
        let eval = repl.eval("return 1, 2, 3").unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0], "1");
        assert_eq!(result[1], "2");
        assert_eq!(result[2], "3");
    }

    #[test]
    fn test_eval_nil_value() {
        let repl = create_repl();
        let eval = repl.eval("return nil").unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert_eq!(result.len(), 1);
        // Debug format uses "Nil" in mlua
        assert!(result[0].to_lowercase().contains("nil"));
    }

    #[test]
    fn test_eval_boolean_values() {
        let repl = create_repl();
        let eval_true = repl.eval("return true").unwrap();
        let eval_false = repl.eval("return false").unwrap();

        assert!(eval_true.result.is_ok());
        let result_true = eval_true.result.unwrap();
        assert!(result_true[0].contains("true"));

        assert!(eval_false.result.is_ok());
        let result_false = eval_false.result.unwrap();
        assert!(result_false[0].contains("false"));
    }

    #[test]
    fn test_eval_table_expression() {
        let repl = create_repl();
        let eval = repl.eval("return {x=1, y=2}").unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        // Tables have a representation like "Table(...)" or similar
        assert!(!result.is_empty());
    }

    #[test]
    fn test_eval_function_return() {
        let repl = create_repl();
        let eval = repl.eval(r#"return string.upper("hello")"#).unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].contains("HELLO"));
    }

    #[test]
    fn test_eval_empty_code() {
        let repl = create_repl();
        let eval = repl.eval("").unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result.is_empty());
        assert!(eval.output.is_empty());
    }

    #[test]
    fn test_eval_assignment_no_return() {
        let repl = create_repl();
        let eval = repl.eval("x = 42").unwrap();

        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result.is_empty());
    }

    // === C. Output Capture Tests ===

    #[test]
    fn test_eval_captures_print_output() {
        let repl = create_repl();
        let eval = repl.eval(r#"print("test")"#).unwrap();

        assert_eq!(eval.output, vec!["test\n"]);
        assert!(eval.result.is_ok());
        assert!(eval.result.unwrap().is_empty());
    }

    #[test]
    fn test_eval_captures_multiple_prints() {
        let repl = create_repl();
        let eval = repl
            .eval(
                r#"
            print("line1")
            print("line2")
            print("line3")
        "#,
            )
            .unwrap();

        assert_eq!(eval.output, vec!["line1\n", "line2\n", "line3\n"]);
    }

    #[test]
    fn test_eval_captures_print_with_multiple_args() {
        let repl = create_repl();
        let eval = repl.eval(r#"print("a", "b", "c")"#).unwrap();

        assert_eq!(eval.output, vec!["a\tb\tc\n"]);
    }

    #[test]
    fn test_eval_print_and_return_separate() {
        let repl = create_repl();
        let eval = repl
            .eval(
                r#"
            print("output")
            return 42
        "#,
            )
            .unwrap();

        assert_eq!(eval.output, vec!["output\n"]);
        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "42");
    }

    #[test]
    fn test_eval_print_various_types() {
        let repl = create_repl();
        let eval = repl.eval(r#"print(42, nil, true, false)"#).unwrap();

        assert_eq!(eval.output, vec!["42\tnil\ttrue\tfalse\n"]);
    }

    #[test]
    fn test_eval_output_not_accumulated() {
        let repl = create_repl();

        let eval1 = repl.eval(r#"print("first")"#).unwrap();
        assert_eq!(eval1.output, vec!["first\n"]);

        let eval2 = repl.eval(r#"print("second")"#).unwrap();
        assert_eq!(eval2.output, vec!["second\n"]);
    }

    // === D. Error Handling Tests ===

    #[test]
    fn test_eval_syntax_error() {
        let repl = create_repl();
        let eval = repl.eval("function end").unwrap();

        assert_error_contains(&eval.result, "SyntaxError:");
    }

    #[test]
    fn test_eval_runtime_error() {
        let repl = create_repl();
        let eval = repl.eval(r#"error("test error")"#).unwrap();

        assert_error_contains(&eval.result, "RuntimeError:");
        assert_error_contains(&eval.result, "test error");
    }

    #[test]
    fn test_eval_undefined_variable_error() {
        let repl = create_repl();
        // In Lua, accessing undefined variables returns nil, not an error.
        // To get an error, we need to call a nil value or access a field on nil.
        let eval = repl.eval("undefined_var()").unwrap();

        assert_error_contains(&eval.result, "RuntimeError:");
    }

    #[test]
    fn test_eval_type_error() {
        let repl = create_repl();
        let eval = repl.eval(r#"return "string" + 1"#).unwrap();

        assert!(eval.result.is_err());
    }

    #[test]
    fn test_eval_callback_error() {
        let lua = mlua::Lua::new();

        // Create Rust function that errors
        let error_fn = lua
            .create_function(|_, ()| -> mlua::Result<()> {
                Err(mlua::Error::RuntimeError("callback failed".to_string()))
            })
            .unwrap();
        lua.globals().set("error_fn", error_fn).unwrap();

        let repl = Repl::new_with(lua).unwrap();
        let eval = repl.eval("error_fn()").unwrap();

        assert_error_contains(&eval.result, "CallbackError:");
        assert_error_contains(&eval.result, "callback failed");
    }

    #[test]
    fn test_eval_blocked_function_error() {
        let repl = create_repl();
        let eval = repl.eval(r#"return io.open("file.txt")"#).unwrap();

        // Now returns nil instead of erroring
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].to_lowercase().contains("nil"));
    }

    #[test]
    fn test_eval_error_preserves_output() {
        let repl = create_repl();
        let eval = repl
            .eval(
                r#"
            print("before error")
            error("test error")
        "#,
            )
            .unwrap();

        assert_eq!(eval.output, vec!["before error\n"]);
        assert_error_contains(&eval.result, "RuntimeError:");
    }

    // === E. State Persistence Tests ===

    #[test]
    fn test_eval_state_persists_between_calls() {
        let repl = create_repl();

        let eval1 = repl.eval("x = 42").unwrap();
        assert!(eval1.result.is_ok());

        let eval2 = repl.eval("return x").unwrap();
        assert!(eval2.result.is_ok());
        assert_eq!(eval2.result.unwrap()[0], "42");
    }

    #[test]
    fn test_eval_function_definition_persists() {
        let repl = create_repl();

        let eval1 = repl.eval("function double(n) return n * 2 end").unwrap();
        assert!(eval1.result.is_ok());

        let eval2 = repl.eval("return double(21)").unwrap();
        assert!(eval2.result.is_ok());
        assert_eq!(eval2.result.unwrap()[0], "42");
    }

    #[test]
    fn test_eval_global_table_persists() {
        let repl = create_repl();

        let eval1 = repl.eval("my_table = {x = 10}").unwrap();
        assert!(eval1.result.is_ok());

        let eval2 = repl.eval("return my_table.x").unwrap();
        assert!(eval2.result.is_ok());
        assert_eq!(eval2.result.unwrap()[0], "10");
    }

    #[test]
    fn test_eval_table_modification_persists() {
        let repl = create_repl();

        repl.eval("my_table = {x = 10}").unwrap();
        repl.eval("my_table.x = 20").unwrap();

        let eval = repl.eval("return my_table.x").unwrap();
        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "20");
    }

    // === F. Integration Tests ===

    #[test]
    fn test_integration_with_safe_os_functions() {
        let repl = create_repl();

        // os.time should work
        let eval = repl.eval("return os.time()").unwrap();
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(!result.is_empty());
        // Should be a number (timestamp)
        assert!(result[0].parse::<i64>().is_ok());
    }

    #[test]
    fn test_integration_math_functions() {
        let repl = create_repl();

        let eval = repl.eval("return math.sqrt(16)").unwrap();
        assert!(eval.result.is_ok());
        // Lua may format as "4" or "4.0" depending on the value
        let result = eval.result.unwrap()[0].clone();
        assert!(result == "4" || result == "4.0");
    }

    #[test]
    fn test_integration_string_functions() {
        let repl = create_repl();

        let eval = repl.eval(r#"return string.upper("test")"#).unwrap();
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].contains("TEST"));
    }

    #[test]
    fn test_integration_table_functions() {
        let repl = create_repl();

        let eval = repl
            .eval(r#"return table.concat({"a", "b", "c"}, ",")"#)
            .unwrap();
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].contains("a,b,c"));
    }

    // === G. Runtime Access Tests ===

    #[test]
    fn test_with_runtime_set_global_variable() {
        let repl = create_repl();

        let result = repl.with_runtime(|lua| {
            lua.globals().set("custom_var", 42)?;
            Ok(())
        });

        assert!(result.is_ok());

        let eval = repl.eval("return custom_var").unwrap();
        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "42");
    }

    #[test]
    fn test_with_runtime_register_rust_function() {
        let repl = create_repl();

        let result = repl.with_runtime(|lua| {
            let greet = lua.create_function(|_, name: String| Ok(format!("Hello, {}!", name)))?;
            lua.globals().set("greet", greet)?;
            Ok(())
        });

        assert!(result.is_ok());

        let eval = repl.eval(r#"return greet("World")"#).unwrap();
        assert!(eval.result.is_ok());
        let result = eval.result.unwrap();
        assert!(result[0].contains("Hello, World!"));
    }

    #[test]
    fn test_with_runtime_closure_captures_state() {
        let repl = create_repl();

        let multiplier = 10;
        let result = repl.with_runtime(|lua| {
            let func = lua.create_function(move |_, x: i32| Ok(x * multiplier))?;
            lua.globals().set("multiply", func)?;
            Ok(())
        });

        assert!(result.is_ok());

        let eval = repl.eval("return multiply(5)").unwrap();
        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "50");
    }

    #[test]
    fn test_with_runtime_extract_value_from_lua() {
        let repl = create_repl();
        repl.eval("x = 42").unwrap();

        let value: i32 = repl.with_runtime(|lua| lua.globals().get("x")).unwrap();

        assert_eq!(value, 42);
    }

    #[test]
    fn test_with_runtime_extract_string_from_lua() {
        let repl = create_repl();
        repl.eval(r#"name = "Alice""#).unwrap();

        let value: String = repl.with_runtime(|lua| lua.globals().get("name")).unwrap();

        assert_eq!(value, "Alice");
    }

    #[test]
    fn test_with_runtime_returns_custom_type() {
        let repl = create_repl();
        repl.eval("a = 10; b = 20").unwrap();

        let sum: i32 = repl
            .with_runtime(|lua| {
                let a: i32 = lua.globals().get("a")?;
                let b: i32 = lua.globals().get("b")?;
                Ok(a + b)
            })
            .unwrap();

        assert_eq!(sum, 30);
    }

    #[test]
    fn test_with_runtime_error_propagation() {
        let repl = create_repl();

        let result: Result<(), ReplError> = repl.with_runtime(|lua| {
            // Try to get a non-existent global as a number (will fail)
            let _val: i32 = lua.globals().get("nonexistent")?;
            Ok(())
        });

        assert!(result.is_err());
        match result {
            Err(ReplError::Lua(_)) => {}
            _ => panic!("Expected Lua error"),
        }
    }

    #[test]
    fn test_with_runtime_multiple_operations() {
        let repl = create_repl();

        repl.with_runtime(|lua| {
            lua.globals().set("a", 1)?;
            lua.globals().set("b", 2)?;
            lua.globals().set("c", 3)?;
            Ok(())
        })
        .unwrap();

        let eval = repl.eval("return a + b + c").unwrap();
        assert!(eval.result.is_ok());
        assert_eq!(eval.result.unwrap()[0], "6");
    }

    #[test]
    fn test_with_runtime_state_persists_after_call() {
        let repl = create_repl();

        // First call sets a global
        repl.with_runtime(|lua| {
            lua.globals().set("persistent", 99)?;
            Ok(())
        })
        .unwrap();

        // Second call should see the same global
        let value: i32 = repl
            .with_runtime(|lua| lua.globals().get("persistent"))
            .unwrap();

        assert_eq!(value, 99);
    }
}