From 97cedae689b23e36259e9e1c32c7c89eb70ab237 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:09:31 +0000 Subject: [PATCH] test(stdlib): add unit tests for register_image and std.image module Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/image.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/stdlib/src/image.rs b/stdlib/src/image.rs index 2bd1f219..10b37d99 100644 --- a/stdlib/src/image.rs +++ b/stdlib/src/image.rs @@ -103,3 +103,128 @@ impl StdlibRegistry { ); } } + +#[cfg(test)] +mod tests { + use super::*; + use techscript_runtime::context::{RuntimeConfig, RuntimeContext}; + + #[test] + fn test_register_image() { + let mut registry = StdlibRegistry::default(); + registry.register_image(); + + assert!(registry.has_module("std.image")); + let module = registry.get_module("std.image").unwrap(); + assert_eq!(module.name, "std.image"); + assert_eq!(module.version, "1.0.0"); + assert!(module.required_capabilities.is_empty()); + + assert_eq!(module.exports.len(), 3); + assert_eq!(module.exports.get("open").unwrap().arity(), 1); + assert_eq!(module.exports.get("resize").unwrap().arity(), 3); + assert_eq!(module.exports.get("grayscale").unwrap().arity(), 1); + } + + #[test] + fn test_image_open_and_errors() { + let mut registry = StdlibRegistry::default(); + registry.register_image(); + let module = registry.get_module("std.image").unwrap(); + let open_fn = module.exports.get("open").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + + // Error path: file does not exist + let err_res = open_fn.call( + &mut ctx, + vec![RuntimeValue::Str("non_existent_file_9999.png".to_string())], + ); + assert!(err_res.is_err()); + + // Success path: create a temporary 10x20 image + let temp_dir = std::env::temp_dir(); + let test_img_path = temp_dir.join("techscript_test_open.png"); + let img = image::RgbImage::new(10, 20); + img.save(&test_img_path).expect("failed to save temp image"); + + let res = open_fn.call( + &mut ctx, + vec![RuntimeValue::Str( + test_img_path.to_str().unwrap().to_string(), + )], + ); + assert!(res.is_ok()); + if let Ok(RuntimeValue::Str(val)) = res { + assert_eq!(val, "image 10x20"); + } else { + panic!("expected RuntimeValue::Str"); + } + + let _ = std::fs::remove_file(test_img_path); + } + + #[test] + fn test_image_resize() { + let mut registry = StdlibRegistry::default(); + registry.register_image(); + let module = registry.get_module("std.image").unwrap(); + let resize_fn = module.exports.get("resize").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + + let temp_dir = std::env::temp_dir(); + let test_img_path = temp_dir.join("techscript_test_resize.png"); + let img = image::RgbImage::new(20, 20); + img.save(&test_img_path).expect("failed to save temp image"); + + let res = resize_fn.call( + &mut ctx, + vec![ + RuntimeValue::Str(test_img_path.to_str().unwrap().to_string()), + RuntimeValue::Int(10), + RuntimeValue::Int(15), + ], + ); + assert!(res.is_ok()); + if let Ok(RuntimeValue::Str(out_path)) = res { + assert!(std::path::Path::new(&out_path).exists()); + let _ = std::fs::remove_file(&out_path); + } else { + panic!("expected RuntimeValue::Str"); + } + + let _ = std::fs::remove_file(test_img_path); + } + + #[test] + fn test_image_grayscale() { + let mut registry = StdlibRegistry::default(); + registry.register_image(); + let module = registry.get_module("std.image").unwrap(); + let grayscale_fn = module.exports.get("grayscale").unwrap(); + + let mut ctx = RuntimeContext::new(RuntimeConfig::default()); + + let temp_dir = std::env::temp_dir(); + let test_img_path = temp_dir.join("techscript_test_gray.png"); + let img = image::RgbImage::new(20, 20); + img.save(&test_img_path).expect("failed to save temp image"); + + let res = grayscale_fn.call( + &mut ctx, + vec![RuntimeValue::Str( + test_img_path.to_str().unwrap().to_string(), + )], + ); + assert!(res.is_ok()); + if let Ok(RuntimeValue::Str(out_path)) = res { + assert!(std::path::Path::new(&out_path).exists()); + let _ = std::fs::remove_file(&out_path); + } else { + panic!("expected RuntimeValue::Str"); + } + + let _ = std::fs::remove_file(test_img_path); + } +}