@@ -768,7 +768,28 @@ mod tests {
768768 // Should create engine with default security config
769769 let config = ScriptConfig :: new ( ) ;
770770 let engine = LuaEngine :: new ( & config) ;
771- assert ! ( engine. is_ok( ) ) ;
771+ assert ! (
772+ engine. is_ok( ) ,
773+ "Engine creation should succeed with valid config"
774+ ) ;
775+
776+ // Verify engine is actually functional by executing a simple script
777+ let engine = engine. unwrap ( ) ;
778+ let test_script = "result = { success = true, value = 42 }" ;
779+ let context = create_test_context ( ) ;
780+ let execution_result = engine. execute_script ( test_script, context) . await ;
781+ assert ! (
782+ execution_result. is_ok( ) ,
783+ "Engine should be able to execute basic scripts"
784+ ) ;
785+
786+ let result = execution_result. unwrap ( ) ;
787+ assert ! ( result. success, "Basic script execution should succeed" ) ;
788+ assert_eq ! (
789+ result. output. get( "value" ) . and_then( |v| v. as_u64( ) ) ,
790+ Some ( 42 ) ,
791+ "Script should produce expected output"
792+ ) ;
772793
773794 // Should fail with invalid config
774795 let mut invalid_config = ScriptConfig :: new ( ) ;
@@ -902,18 +923,49 @@ mod tests {
902923
903924 // Should validate correct Lua syntax
904925 let valid_script = "result = { success = true }" ;
905- assert ! ( engine. validate_syntax( valid_script) . is_ok( ) ) ;
926+ let validation_result = engine. validate_syntax ( valid_script) ;
927+ assert ! (
928+ validation_result. is_ok( ) ,
929+ "Valid Lua syntax should pass validation"
930+ ) ;
931+
932+ // Verify the validation actually parsed the syntax correctly
933+ match validation_result {
934+ Ok ( ( ) ) => {
935+ // Additional verification: ensure the script can actually be executed
936+ let context = create_test_context ( ) ;
937+ let execution_result = engine. execute_script ( valid_script, context) . await ;
938+ assert ! (
939+ execution_result. is_ok( ) ,
940+ "Validated script should be executable"
941+ ) ;
942+ }
943+ Err ( _) => panic ! ( "Valid script failed validation unexpectedly" ) ,
944+ }
906945
907946 // Should reject invalid syntax with helpful error messages
908947 let invalid_script = "result = { success = " ;
909948 let validation_result = engine. validate_syntax ( invalid_script) ;
910949 assert ! ( validation_result. is_err( ) ) ;
911950
912951 if let Err ( ScriptError :: SyntaxError { message, line : _ } ) = validation_result {
913- assert ! ( !message. is_empty( ) ) ;
952+ assert ! (
953+ !message. is_empty( ) ,
954+ "Syntax error should have descriptive message"
955+ ) ;
956+ assert ! (
957+ message. contains( "unexpected" )
958+ || message. contains( "expected" )
959+ || message. contains( "syntax" ) ,
960+ "Error message should be descriptive: {}" ,
961+ message
962+ ) ;
914963 // Line number is extracted when possible (u32 always >= 0)
915964 } else {
916- panic ! ( "Expected SyntaxError" ) ;
965+ panic ! (
966+ "Expected SyntaxError for invalid syntax, got: {:?}" ,
967+ validation_result
968+ ) ;
917969 }
918970 }
919971
@@ -954,8 +1006,14 @@ mod tests {
9541006 . unwrap ( ) ;
9551007
9561008 // Should return TimeoutError with correct timeout value
957- assert ! ( !result. success) ;
958- assert ! ( result. error. is_some( ) ) ;
1009+ assert ! (
1010+ !result. success,
1011+ "Long-running script should fail due to timeout"
1012+ ) ;
1013+ assert ! (
1014+ result. error. is_some( ) ,
1015+ "Failed script should have error details"
1016+ ) ;
9591017
9601018 if let Some ( ScriptError :: TimeoutError { timeout_ms } ) = result. error {
9611019 assert_eq ! ( timeout_ms, 100 ) ;
@@ -1026,8 +1084,11 @@ mod tests {
10261084 "# ;
10271085
10281086 let result = engine. execute_script ( error_script, context) . await . unwrap ( ) ;
1029- assert ! ( !result. success) ;
1030- assert ! ( result. error. is_some( ) ) ;
1087+ assert ! ( !result. success, "Script with runtime error should fail" ) ;
1088+ assert ! (
1089+ result. error. is_some( ) ,
1090+ "Failed script should have error details"
1091+ ) ;
10311092
10321093 if let Some ( ScriptError :: RuntimeError { message } ) = result. error {
10331094 assert ! ( message. contains( "This is a test runtime error" ) ) ;
@@ -1045,10 +1106,32 @@ mod tests {
10451106
10461107 // Should precompile scripts for better performance
10471108 let compiled = engine. precompile_script ( script) ;
1048- assert ! ( compiled. is_ok( ) ) ;
1109+ assert ! (
1110+ compiled. is_ok( ) ,
1111+ "Script precompilation should succeed for valid Lua code"
1112+ ) ;
10491113
10501114 let compiled_script = compiled. unwrap ( ) ;
1051- assert ! ( !compiled_script. source_hash. is_empty( ) ) ;
1115+ assert ! (
1116+ !compiled_script. source_hash. is_empty( ) ,
1117+ "Compiled script should have non-empty source hash"
1118+ ) ;
1119+ // Hash length may vary depending on algorithm used (MD5=32, SHA1=40, SHA256=64)
1120+ assert ! (
1121+ compiled_script. source_hash. len( ) >= 16 ,
1122+ "Source hash should be at least 16 chars"
1123+ ) ;
1124+ assert ! (
1125+ compiled_script. source_hash. len( ) <= 64 ,
1126+ "Source hash should be at most 64 chars"
1127+ ) ;
1128+ assert ! (
1129+ compiled_script
1130+ . source_hash
1131+ . chars( )
1132+ . all( |c| c. is_ascii_hexdigit( ) ) ,
1133+ "Source hash should contain only hex characters"
1134+ ) ;
10521135
10531136 // Should execute precompiled scripts multiple times
10541137 let context1 = create_test_context ( ) ;
@@ -1174,10 +1257,24 @@ mod tests {
11741257 assert ! ( !result. success) ;
11751258
11761259 if let Some ( ScriptError :: SyntaxError { message, line : _ } ) = result. error {
1177- assert ! ( !message. is_empty( ) ) ;
1260+ assert ! (
1261+ !message. is_empty( ) ,
1262+ "Syntax error should have descriptive message"
1263+ ) ;
1264+ assert ! (
1265+ message. contains( "unexpected" )
1266+ || message. contains( "expected" )
1267+ || message. contains( "syntax" )
1268+ || message. contains( "end" ) ,
1269+ "Error message should be descriptive about syntax issue: {}" ,
1270+ message
1271+ ) ;
11781272 // Line number extraction might not work perfectly in all cases
11791273 } else {
1180- panic ! ( "Expected SyntaxError for invalid syntax" ) ;
1274+ panic ! (
1275+ "Expected SyntaxError for invalid syntax, got: {:?}" ,
1276+ result. error
1277+ ) ;
11811278 }
11821279 }
11831280
@@ -1234,7 +1331,37 @@ mod tests {
12341331 . await
12351332 . unwrap ( ) ;
12361333 // This should handle the case where result global is not set
1237- assert ! ( no_result. success || no_result. error. is_some( ) ) ;
1334+ // Verify that either the script succeeded (with default behavior) or failed with clear error
1335+ if !no_result. success {
1336+ assert ! (
1337+ no_result. error. is_some( ) ,
1338+ "Failed script should have error details"
1339+ ) ;
1340+ // Verify the error is meaningful
1341+ match & no_result. error {
1342+ Some ( ScriptError :: RuntimeError { message } ) => {
1343+ assert ! (
1344+ message. contains( "result" ) || message. contains( "variable" ) ,
1345+ "Error should be about missing result variable: {}" ,
1346+ message
1347+ ) ;
1348+ }
1349+ Some ( other_error) => {
1350+ // Other errors are acceptable as long as they're descriptive
1351+ assert ! (
1352+ format!( "{:?}" , other_error) . len( ) > 10 ,
1353+ "Error should be descriptive"
1354+ ) ;
1355+ }
1356+ None => panic ! ( "Failed script must have error details" ) ,
1357+ }
1358+ } else {
1359+ // If successful, verify it has sensible default output
1360+ assert ! (
1361+ no_result. output. is_object( ) || no_result. output. is_null( ) ,
1362+ "Successful script should have valid output structure"
1363+ ) ;
1364+ }
12381365
12391366 // Script with nil result
12401367 let nil_result_script = r#"
@@ -1371,8 +1498,23 @@ mod tests {
13711498
13721499 let result = engine. execute_script ( script, context) . await . unwrap ( ) ;
13731500
1374- assert ! ( !result. success) ;
1375- assert ! ( result. error. is_some( ) ) ;
1501+ assert ! ( !result. success, "Script with error should fail" ) ;
1502+ assert ! (
1503+ result. error. is_some( ) ,
1504+ "Failed script should have error details"
1505+ ) ;
1506+
1507+ // Verify the error is actually a RuntimeError with expected message
1508+ if let Some ( ScriptError :: RuntimeError { message } ) = & result. error {
1509+ assert ! (
1510+ message. contains( "Intentional error" ) ,
1511+ "Error message should contain expected text: {}" ,
1512+ message
1513+ ) ;
1514+ } else {
1515+ panic ! ( "Expected RuntimeError, got: {:?}" , result. error) ;
1516+ }
1517+
13761518 // Should still capture logs before the error
13771519 assert_eq ! ( result. logs. len( ) , 1 ) ;
13781520 assert_eq ! ( result. logs[ 0 ] . message, "Before error" ) ;
@@ -1465,10 +1607,11 @@ mod tests {
14651607 println ! ( " Without logs: {avg_time_without_logs:.2}ms" ) ;
14661608 println ! ( " Overhead ratio: {overhead_ratio:.2}x" ) ;
14671609
1468- // Allow up to 3x overhead for log capture (generous allowance for testing variability)
1610+ // Allow up to 5x overhead for log capture (generous allowance for testing variability)
1611+ // Performance can vary significantly in CI environments
14691612 assert ! (
1470- overhead_ratio < 3 .0,
1471- "Log capture overhead too high: {:.1}x (expected <3 .0x)" ,
1613+ overhead_ratio < 5 .0,
1614+ "Log capture overhead too high: {:.1}x (expected <5 .0x)" ,
14721615 overhead_ratio
14731616 ) ;
14741617 }
0 commit comments