use tailtales::lua_console::ConsoleLine;
use tailtales::lua_engine::LuaEngine;
use tailtales::state::TuiState;
#[test]
fn test_lua_print_output_redirection() {
println!("Testing Lua print output redirection");
let mut engine = LuaEngine::new().unwrap();
engine.initialize().unwrap();
let mut state = TuiState::new().unwrap();
state.lua_console.output_history.clear();
let test_script = r#"
print("This is a test print statement")
print("Another print statement")
print("Script executed successfully")
"#;
let result = engine.execute_script_string_with_state(test_script, &mut state);
match result {
Ok(output) => {
println!("✓ Script executed successfully");
println!("Captured output: '{}'", output);
assert!(output.contains("This is a test print statement"));
assert!(output.contains("Another print statement"));
assert!(output.contains("Script executed successfully"));
let lines: Vec<&str> = output.split('\n').collect();
assert_eq!(lines.len(), 3, "Should have 3 lines of output");
println!("✓ Print output was captured correctly");
}
Err(e) => {
panic!("Script execution failed: {}", e);
}
}
}
#[test]
fn test_bytes_count_processor_print_output() {
println!("Testing bytes count processor print output");
let mut engine = LuaEngine::new().unwrap();
engine.initialize().unwrap();
let mut state = TuiState::new().unwrap();
state.lua_console.output_history.clear();
let processor_script = r#"
-- Add a processor function to the record_processors array
table.insert(record_processors, function(record)
-- Calculate the byte count of the original line
local bytes_count = #record.original
-- Return a table with the new attribute
return {
bytes_count = tostring(bytes_count)
}
end)
print("Bytes count processor added to record_processors")
print("Each new record will now have a 'bytes_count' attribute with the line size in bytes")
"#;
let result = engine.execute_script_string_with_state(processor_script, &mut state);
match result {
Ok(output) => {
println!("✓ Processor script executed successfully");
println!("Captured output: '{}'", output);
assert!(output.contains("Bytes count processor added to record_processors"));
assert!(output.contains("Each new record will now have a 'bytes_count' attribute"));
println!("✓ Processor script print output was captured correctly");
}
Err(e) => {
panic!("Processor script execution failed: {}", e);
}
}
}
#[test]
fn test_lua_print_vs_return_value() {
println!("Testing Lua print vs return value handling");
let mut engine = LuaEngine::new().unwrap();
engine.initialize().unwrap();
let mut state = TuiState::new().unwrap();
state.lua_console.output_history.clear();
let test_script = r#"
print("This is printed output")
print("More printed output")
return "This is the return value"
"#;
let result = engine.execute_script_string_with_state(test_script, &mut state);
match result {
Ok(output) => {
println!("✓ Script with print and return executed successfully");
println!("Captured output: '{}'", output);
assert!(output.contains("This is printed output"));
assert!(output.contains("More printed output"));
assert!(output.contains("This is the return value"));
let lines: Vec<&str> = output.split('\n').collect();
assert!(lines.len() >= 3, "Should have at least 3 lines of output");
println!("✓ Both print output and return value were captured correctly");
}
Err(e) => {
panic!("Script execution failed: {}", e);
}
}
}
#[test]
fn test_external_script_print_output_integration() {
println!("Testing external script print output integration");
let mut engine = LuaEngine::new().unwrap();
engine.initialize().unwrap();
let mut state = TuiState::new().unwrap();
state.lua_console.output_history.clear();
let external_script = r#"
print("External script loaded")
print("Setting up record processors")
-- Add a processor function
table.insert(record_processors, function(record)
return {
test_field = "test_value"
}
end)
print("Record processor added successfully")
"#;
let result = engine.execute_script_string_with_state(external_script, &mut state);
match result {
Ok(output) => {
println!("✓ External script executed successfully");
println!("Captured output: '{}'", output);
assert!(output.contains("External script loaded"));
assert!(output.contains("Setting up record processors"));
assert!(output.contains("Record processor added successfully"));
if !output.is_empty() {
state.lua_console.add_output(
"Script 'test_script.lua' output:".to_string(),
state.visible_width,
);
let output_lines: Vec<String> =
output.lines().map(|line| format!(" {}", line)).collect();
for line in output_lines {
state.lua_console.add_output(line, state.visible_width);
}
}
state.lua_console.add_output(
"Script 'test_script.lua' executed successfully.".to_string(),
state.visible_width,
);
assert!(!state.lua_console.output_history.is_empty());
let history_text: String = state
.lua_console
.output_history
.iter()
.map(|line| match line {
ConsoleLine::Stdout(msg) => msg.clone(),
ConsoleLine::Stderr(msg) => msg.clone(),
})
.collect::<Vec<String>>()
.join("\n");
assert!(history_text.contains("External script loaded"));
assert!(history_text.contains("Setting up record processors"));
assert!(history_text.contains("Record processor added successfully"));
assert!(history_text.contains("Script 'test_script.lua' executed successfully"));
println!("✓ External script print output was captured and added to REPL history");
}
Err(e) => {
panic!("External script execution failed: {}", e);
}
}
}
#[test]
fn test_lua_script_error_handling() {
println!("Testing Lua script error handling");
let mut engine = LuaEngine::new().unwrap();
engine.initialize().unwrap();
let mut state = TuiState::new().unwrap();
state.lua_console.output_history.clear();
let error_script = r#"
print("This will work")
-- Syntax error: missing end
if true then
print("This won't be reached")
"#;
let result = engine.execute_script_string_with_state(error_script, &mut state);
match result {
Ok(_) => {
panic!("Script should have failed with syntax error");
}
Err(e) => {
println!("✓ Script failed as expected with error: {}", e);
state.lua_console.add_error(
format!("Error executing script 'test_error.lua': {}", e),
state.visible_width,
);
assert!(!state.lua_console.output_history.is_empty());
let history_text: String = state
.lua_console
.output_history
.iter()
.map(|line| match line {
ConsoleLine::Stdout(msg) => msg.clone(),
ConsoleLine::Stderr(msg) => msg.clone(),
})
.collect::<Vec<String>>()
.join("\n");
assert!(history_text.contains("Error executing script"));
assert!(history_text.contains("test_error.lua"));
println!("✓ Error message was added to REPL history");
}
}
}