use jstime_core as jstime;
mod common;
#[test]
fn test_pooled_headers_in_fetch() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
// Mock fetch to test header handling
(async function() {
// This test ensures that header vectors are properly pooled
// and don't interfere with each other between requests
let results = [];
// Simulate multiple fetches (would need a test server in reality)
// For now, we just test that the runtime doesn't crash with pooling
for (let i = 0; i < 10; i++) {
results.push(i);
}
return results.length;
})();
"#;
let result = jstime.run_script(script, "test_pooled_headers.js");
assert!(result.is_ok());
}
#[test]
fn test_pool_initialization() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
// Simple test to ensure pools don't interfere with basic operations
const arr = [1, 2, 3];
arr.reduce((a, b) => a + b, 0);
"#;
let result = jstime.run_script(script, "test_pool_init.js");
assert_eq!(result.unwrap(), "6");
}
#[test]
fn test_header_vector_reuse() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
// Test multiple header arrays in the same script
function test1() {
const headers1 = [['Content-Type', 'application/json']];
return headers1.length;
}
function test2() {
const headers2 = [['Authorization', 'Bearer token'], ['Accept', 'application/json']];
return headers2.length;
}
test1() + test2();
"#;
let result = jstime.run_script(script, "test_headers.js");
assert_eq!(result.unwrap(), "3");
}
#[test]
fn test_pool_stress() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
let total = 0;
for (let i = 0; i < 1000; i++) {
const arr = new Array(10).fill(i);
total += arr[0];
}
total;
"#;
let result = jstime.run_script(script, "test_pool_stress.js");
assert_eq!(result.unwrap(), "499500");
}
#[test]
fn test_timer_vector_pooling() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
let timerCount = 0;
// Create and clear multiple timers
for (let i = 0; i < 50; i++) {
const timerId = setTimeout(() => {}, i);
timerCount++;
// Clear some timers immediately
if (i % 2 === 0) {
clearTimeout(timerId);
}
}
timerCount.toString();
"#;
let result = jstime.run_script(script, "test_timer_operations.js");
assert!(result.is_ok());
assert_eq!(result.unwrap(), "50");
}
#[test]
fn test_interval_vector_pooling() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
let count = 0;
const intervals = [];
// Create multiple intervals
for (let i = 0; i < 20; i++) {
const intervalId = setInterval(() => {
count++;
}, 1);
intervals.push(intervalId);
}
// Clear all intervals immediately
intervals.forEach(id => clearInterval(id));
// Create more intervals
for (let i = 0; i < 10; i++) {
const intervalId = setInterval(() => {
count++;
}, 1);
clearInterval(intervalId);
}
"success";
"#;
let result = jstime.run_script(script, "test_interval_operations.js");
assert!(result.is_ok(), "Script execution should succeed");
assert_eq!(result.unwrap(), "success");
}
#[test]
fn test_mixed_timer_pooling() {
let _setup_guard = common::setup();
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
let script = r#"
let operations = 0;
// Create a mix of timers and clear them
for (let i = 0; i < 10; i++) {
const timeoutId = setTimeout(() => {}, i);
clearTimeout(timeoutId);
operations++;
const intervalId = setInterval(() => {}, 1);
clearInterval(intervalId);
operations++;
}
operations.toString();
"#;
let result = jstime.run_script(script, "test_mixed_timers.js");
assert!(result.is_ok(), "Script execution should succeed");
let output = result.unwrap();
assert_eq!(output, "20", "Expected 20 operations, got {}", output);
}