use newts::server::kernel::{PythonKernel, NodeKernel, RustKernel, CKernel, CppKernel, GoKernel, Kernel};
#[test]
fn test_python_kernel_persistence() {
let mut kernel = PythonKernel::new(None).expect("Failed to init kernel");
let resp1 = kernel.execute("x = 42".to_string(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
let resp2 = kernel.execute("print(x)".to_string(), None, None, None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_node_kernel_persistence() {
let mut kernel = NodeKernel::new().expect("Failed to init kernel");
let resp1 = kernel.execute("var x = 42;".to_string(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
let resp2 = kernel.execute("console.log(x);".to_string(), None, None, None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_rust_kernel_persistence() {
let mut kernel = RustKernel::new();
let code1 = "struct Foo { x: i32 }".to_string();
let resp1 = kernel.execute(code1.clone(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
let code2 = "let f = Foo { x: 42 }; println!(\"{}\", f.x);".to_string();
let resp2 = kernel.execute(code2, None, Some(vec![code1]), None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_python_kernel_isolation() {
let mut kernel = PythonKernel::new(None).expect("Failed to init kernel");
let resp1 = kernel.execute("x = 100".to_string(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
let mut kernel2 = PythonKernel::new(None).expect("Failed to init kernel");
let resp2 = kernel2.execute("print(x)".to_string(), None, None, None).expect("Exec failed");
assert_ne!(resp2.status, Some(0));
}
#[test]
fn test_c_kernel_persistence() {
let mut kernel = CKernel::new();
let code1 = "int x = 42;".to_string();
let resp1 = kernel.execute(code1.clone(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
let code2 = "printf(\"%d\", x);".to_string();
let resp2 = kernel.execute(code2, None, Some(vec![code1]), None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_cpp_kernel_persistence() {
let mut kernel = CppKernel::new();
let code1 = "class Foo { public: int x; };".to_string();
let resp1 = kernel.execute(code1.clone(), None, None, None).expect("Exec failed");
if resp1.status != Some(0) {
println!("Cpp Exec 1 failed: {}", resp1.stderr);
}
assert_eq!(resp1.status, Some(0));
let code2 = "Foo f; f.x = 42; std::cout << f.x;".to_string();
let resp2 = kernel.execute(code2, None, Some(vec![code1]), None).expect("Exec failed");
if resp2.status != Some(0) {
println!("Cpp Exec 2 failed: {}", resp2.stderr);
}
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_go_kernel_persistence() {
let mut kernel = GoKernel::new();
let code1 = "type Foo struct { X int }".to_string();
let resp1 = kernel.execute(code1.clone(), None, None, None).expect("Exec failed");
if resp1.status != Some(0) {
println!("Go Exec 1 failed: {}", resp1.stderr);
}
assert_eq!(resp1.status, Some(0));
let code2 = "f := Foo{X: 42}; fmt.Println(f.X)".to_string();
let resp2 = kernel.execute(code2, None, Some(vec![code1]), None).expect("Exec failed");
if resp2.status != Some(0) {
println!("Go Exec 2 failed: {}", resp2.stderr);
}
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_typescript_kernel_persistence() {
let mut kernel = NodeKernel::new().expect("Failed to init kernel");
let code1 = "let x: number = 42;".to_string();
let resp1 = kernel.execute(code1, Some("typescript".to_string()), None, None).expect("Exec failed");
if resp1.status != Some(0) {
println!("TS Exec 1 failed (likely missing typescript): {}", resp1.stderr);
return;
}
assert_eq!(resp1.status, Some(0));
let code2 = "console.log(x);".to_string();
let resp2 = kernel.execute(code2, Some("typescript".to_string()), None, None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "42");
}
#[test]
fn test_python_performance_persistence() {
let mut kernel = PythonKernel::new(None).expect("Failed to init kernel");
let start = std::time::Instant::now();
let code1 = "x = [i for i in range(5000000)]".to_string();
let resp1 = kernel.execute(code1, None, None, None).expect("Exec failed");
let duration1 = start.elapsed();
assert_eq!(resp1.status, Some(0));
println!("Python Cell 1 (Allocation) duration: {:?}", duration1);
let start2 = std::time::Instant::now();
let code2 = "print(len(x))".to_string();
let resp2 = kernel.execute(code2, None, None, None).expect("Exec failed");
let duration2 = start2.elapsed();
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "5000000");
println!("Python Cell 2 (Access) duration: {:?}", duration2);
assert!(duration2.as_millis() < 100, "Accessing existing memory should be instant");
}
#[test]
fn test_node_performance_persistence() {
let mut kernel = NodeKernel::new().expect("Failed to init kernel");
let code1 = r#"
var x = new Array(5000000).fill(0).map((_, i) => i);
"#.to_string();
let start = std::time::Instant::now();
let resp1 = kernel.execute(code1, None, None, None).expect("Exec failed");
let duration1 = start.elapsed();
assert_eq!(resp1.status, Some(0));
println!("Node Cell 1 (Allocation) duration: {:?}", duration1);
let start2 = std::time::Instant::now();
let code2 = "console.log(x.length);".to_string();
let resp2 = kernel.execute(code2, None, None, None).expect("Exec failed");
let duration2 = start2.elapsed();
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "5000000");
println!("Node Cell 2 (Access) duration: {:?}", duration2);
assert!(duration2.as_millis() < 100, "Accessing existing memory should be instant");
}
#[test]
fn test_python_input_override() {
let mut kernel = PythonKernel::new(None).expect("Failed to init kernel");
let code = "import builtins; print(builtins.input.__name__)".to_string();
let resp = kernel.execute(code, None, None, None).expect("Exec failed");
assert_eq!(resp.status, Some(0));
assert_eq!(resp.stdout.trim(), "_newt_input");
}
#[test]
fn test_rust_kernel_no_output_accumulation() {
let mut kernel = RustKernel::new();
let code1 = r#"
let s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2);
"#.to_string();
let resp1 = kernel.execute(code1.clone(), None, None, None).expect("Exec failed");
assert_eq!(resp1.status, Some(0));
assert_eq!(resp1.stdout.trim(), "hello and hello");
let code2 = r#"
let mut s = String::from("hello");
let r1 = &mut s;
r1.push_str(", world");
println!("{}", r1);
"#.to_string();
let resp2 = kernel.execute(code2, None, Some(vec![code1]), None).expect("Exec failed");
assert_eq!(resp2.status, Some(0));
assert_eq!(resp2.stdout.trim(), "hello, world");
}