From 4226dcd89f7a0954276c36de6de39c1ee890acb2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 16:21:04 +0000 Subject: [PATCH 1/2] Initial plan From a6ae2ceb6e44d0f3c4b4d6051f55a75207705735 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 16:40:06 +0000 Subject: [PATCH 2/2] Add unit tests to all Rust plugins without tests Co-authored-by: Maidang1 <50993231+Maidang1@users.noreply.github.com> --- .../playground-vue/src/types/auto_import.d.ts | 16 +++++ rust-plugins/compress/src/lib.rs | 45 ++++++++++++++ rust-plugins/dsv/src/lib.rs | 51 +++++++++++++++ rust-plugins/image/src/lib.rs | 37 +++++++++++ rust-plugins/mdx/src/lib.rs | 37 +++++++++++ rust-plugins/modular-import/src/lib.rs | 21 +++++++ rust-plugins/strip/src/lib.rs | 59 ++++++++++++++++++ rust-plugins/url/src/lib.rs | 61 ++++++++++++++++++ rust-plugins/wasm/src/lib.rs | 18 ++++++ rust-plugins/worker/src/lib.rs | 44 +++++++++++++ rust-plugins/yaml/src/lib.rs | 62 +++++++++++++++++++ 11 files changed, 451 insertions(+) create mode 100644 rust-plugins/auto-import/playground-vue/src/types/auto_import.d.ts diff --git a/rust-plugins/auto-import/playground-vue/src/types/auto_import.d.ts b/rust-plugins/auto-import/playground-vue/src/types/auto_import.d.ts new file mode 100644 index 00000000..f807fe07 --- /dev/null +++ b/rust-plugins/auto-import/playground-vue/src/types/auto_import.d.ts @@ -0,0 +1,16 @@ +/* generated by farmfe_plugin_auto_import */ +export {} +declare global { + const getName: typeof import('./../apis/index.ts')['getName'] + const name: typeof import('./../apis/name.ts')['default'] + const useOutletContext: typeof import('react-router')['useOutletContext'] + const useHref: typeof import('react-router')['useHref'] + const useInRouterContext: typeof import('react-router')['useInRouterContext'] + const useLocation: typeof import('react-router')['useLocation'] + const useNavigationType: typeof import('react-router')['useNavigationType'] + const useNavigate: typeof import('react-router')['useNavigate'] + const useOutlet: typeof import('react-router')['useOutlet'] + const useParams: typeof import('react-router')['useParams'] + const useResolvedPath: typeof import('react-router')['useResolvedPath'] + const useRoutes: typeof import('react-router')['useRoutes'] +} diff --git a/rust-plugins/compress/src/lib.rs b/rust-plugins/compress/src/lib.rs index 267f292f..1a461d79 100644 --- a/rust-plugins/compress/src/lib.rs +++ b/rust-plugins/compress/src/lib.rs @@ -161,3 +161,48 @@ impl Plugin for FarmfePluginCompress { Ok(None) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_compress_algorithm_default() { + let algo: CompressAlgorithm = Default::default(); + assert!(matches!(algo, CompressAlgorithm::Brotli)); + } + + #[test] + fn test_default_filter() { + let filter = default_filter(); + assert_eq!(filter, "\\.(js|mjs|json|css|html)$"); + } + + #[test] + fn test_default_level() { + assert_eq!(default_level(), 6); + } + + #[test] + fn test_default_threshold() { + assert_eq!(default_threshold(), 1024); + } + + #[test] + fn test_options_deserialization() { + let json = r#"{"algorithm":"gzip","level":9,"threshold":2048,"filter":"\\.(js|css)$"}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert!(matches!(options.algorithm, CompressAlgorithm::Gzip)); + assert_eq!(options.level, 9); + assert_eq!(options.threshold, 2048); + assert_eq!(options.filter, "\\.(js|css)$"); + } + + #[test] + fn test_plugin_creation() { + let config = Config::default(); + let options = r#"{"algorithm":"brotli","level":6,"threshold":1024}"#.to_string(); + let plugin = FarmfePluginCompress::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginCompress"); + } +} diff --git a/rust-plugins/dsv/src/lib.rs b/rust-plugins/dsv/src/lib.rs index a5919aa5..c1da5a6b 100644 --- a/rust-plugins/dsv/src/lib.rs +++ b/rust-plugins/dsv/src/lib.rs @@ -103,3 +103,54 @@ impl Plugin for FarmPluginDsv { })) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{"include":[],"exclude":[]}"#.to_string(); + let plugin = FarmPluginDsv::new(&config, options); + assert_eq!(plugin.name(), "FarmPluginDsv"); + } + + #[test] + fn test_options_deserialization() { + let json = r#"{"include":[],"exclude":[]}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert!(options.include.is_some()); + assert!(options.exclude.is_some()); + } + + #[test] + fn test_csv_parsing() { + let param = Param { + module_id: "test.csv".to_string(), + content: "name,age\nJohn,30\nJane,25".to_string(), + }; + let reader = get_reader(¶m); + assert!(reader.is_ok()); + } + + #[test] + fn test_tsv_parsing() { + let param = Param { + module_id: "test.tsv".to_string(), + content: "name\tage\nJohn\t30\nJane\t25".to_string(), + }; + let reader = get_reader(¶m); + assert!(reader.is_ok()); + } + + #[test] + fn test_unsupported_file_type() { + let param = Param { + module_id: "test.txt".to_string(), + content: "some content".to_string(), + }; + let reader = get_reader(¶m); + assert!(reader.is_err()); + } +} diff --git a/rust-plugins/image/src/lib.rs b/rust-plugins/image/src/lib.rs index 58d2110d..7685a157 100644 --- a/rust-plugins/image/src/lib.rs +++ b/rust-plugins/image/src/lib.rs @@ -74,3 +74,40 @@ impl Plugin for FarmfePluginImage { Ok(None) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmfePluginImage::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginImage"); + } + + #[test] + fn test_options_deserialization_default() { + let json = r#"{}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert_eq!(options.dom, None); + assert!(options.include.is_none()); + assert!(options.exclude.is_none()); + } + + #[test] + fn test_options_deserialization_with_dom() { + let json = r#"{"dom":true}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert_eq!(options.dom, Some(true)); + } + + #[test] + fn test_options_deserialization_with_filters() { + let json = r#"{"include":[".*\\.png$"],"exclude":[".*\\.svg$"]}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert!(options.include.is_some()); + assert!(options.exclude.is_some()); + } +} diff --git a/rust-plugins/mdx/src/lib.rs b/rust-plugins/mdx/src/lib.rs index 933f28dc..e47583a8 100644 --- a/rust-plugins/mdx/src/lib.rs +++ b/rust-plugins/mdx/src/lib.rs @@ -136,3 +136,40 @@ impl Plugin for FarmPluginMdx { return Ok(None); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmPluginMdx::new(&config, options); + assert_eq!(plugin.name(), "FarmPluginMdx"); + } + + #[test] + fn test_is_mdx_file() { + assert!(is_mdx_file(&"test.md".to_string())); + assert!(is_mdx_file(&"test.mdx".to_string())); + assert!(!is_mdx_file(&"test.js".to_string())); + assert!(!is_mdx_file(&"test.txt".to_string())); + } + + #[test] + fn test_options_deserialization_defaults() { + let json = r#"{}"#; + let options: FarmPluginMdxOptions = serde_json::from_str(json).unwrap(); + assert_eq!(options.development, None); + assert_eq!(options.jsx, None); + } + + #[test] + fn test_options_deserialization_with_values() { + let json = r#"{"development":true,"jsx":true}"#; + let options: FarmPluginMdxOptions = serde_json::from_str(json).unwrap(); + assert_eq!(options.development, Some(true)); + assert_eq!(options.jsx, Some(true)); + } +} diff --git a/rust-plugins/modular-import/src/lib.rs b/rust-plugins/modular-import/src/lib.rs index 6e8cd398..ff270f23 100644 --- a/rust-plugins/modular-import/src/lib.rs +++ b/rust-plugins/modular-import/src/lib.rs @@ -41,3 +41,24 @@ impl Plugin for FarmfePluginComponent { transform(&self.options, param) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmfePluginComponent::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginComponent"); + } + + #[test] + fn test_options_deserialization() { + let json = r#"{}"#; + let options: Options = serde_json::from_str(json).unwrap(); + // Just verify it can be deserialized + drop(options); + } +} diff --git a/rust-plugins/strip/src/lib.rs b/rust-plugins/strip/src/lib.rs index 48881d16..ea525679 100644 --- a/rust-plugins/strip/src/lib.rs +++ b/rust-plugins/strip/src/lib.rs @@ -281,3 +281,62 @@ fn create_first_pass_regex(first_pass: &[String]) -> Result { + assert_eq!(ident.sym.as_ref(), "(void 0)"); + } + _ => panic!("Expected Ident expression"), + } + } +} diff --git a/rust-plugins/url/src/lib.rs b/rust-plugins/url/src/lib.rs index c819f436..5a8b888f 100644 --- a/rust-plugins/url/src/lib.rs +++ b/rust-plugins/url/src/lib.rs @@ -181,3 +181,64 @@ impl Plugin for FarmfePluginUrl { Ok(None) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmfePluginUrl::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginUrl"); + } + + #[test] + fn test_options_deserialization_defaults() { + let json = r#"{}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert_eq!(options.limit, None); + assert_eq!(options.public_path, None); + assert_eq!(options.emit_files, None); + } + + #[test] + fn test_options_deserialization_with_values() { + let json = r#"{"limit":8192,"publicPath":"/assets/","emitFiles":true}"#; + let options: Options = serde_json::from_str(json).unwrap(); + assert_eq!(options.limit, Some(8192)); + assert_eq!(options.public_path, Some("/assets/".to_string())); + assert_eq!(options.emit_files, Some(true)); + } + + #[test] + fn test_get_file_size_nonexistent() { + let size = get_file_size("/nonexistent/file.txt"); + assert_eq!(size, 0); + } + + #[test] + fn test_copy_file_function() { + use std::fs::{self, File}; + use std::io::Write; + + // Create a temp directory and file + let temp_dir = std::env::temp_dir().join("farm_url_test"); + let _ = fs::create_dir_all(&temp_dir); + let src = temp_dir.join("test_src.txt"); + let dst = temp_dir.join("subdir").join("test_dst.txt"); + + // Write test content + let mut file = File::create(&src).unwrap(); + file.write_all(b"test content").unwrap(); + + // Test copy + let result = copy_file(&src, &dst); + assert!(result.is_ok()); + assert!(dst.exists()); + + // Cleanup + let _ = fs::remove_dir_all(&temp_dir); + } +} diff --git a/rust-plugins/wasm/src/lib.rs b/rust-plugins/wasm/src/lib.rs index 87ee1690..8245b339 100644 --- a/rust-plugins/wasm/src/lib.rs +++ b/rust-plugins/wasm/src/lib.rs @@ -172,3 +172,21 @@ impl Plugin for FarmfePluginWasm { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = String::new(); + let plugin = FarmfePluginWasm::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginWasm"); + } + + #[test] + fn test_wasm_helper_constant() { + assert_eq!(WASM_HELPER_ID_FARM, "farm/wasm-helper.js"); + } +} diff --git a/rust-plugins/worker/src/lib.rs b/rust-plugins/worker/src/lib.rs index 449e8496..58d0a8b0 100644 --- a/rust-plugins/worker/src/lib.rs +++ b/rust-plugins/worker/src/lib.rs @@ -486,3 +486,47 @@ impl Plugin for FarmfePluginWorker { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmfePluginWorker::new(&config, options); + assert_eq!(plugin.name(), "FarmfePluginWorker"); + } + + #[test] + fn test_plugin_priority() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmfePluginWorker::new(&config, options); + assert_eq!(plugin.priority(), 105); + } + + #[test] + fn test_worker_regex_patterns() { + let re = JsRegex::new(WORKER_OR_SHARED_WORKER_RE).unwrap(); + assert!(re.find("test.js?worker").is_some()); + assert!(re.find("test.js?sharedworker").is_some()); + assert!(re.find("test.js?worker&inline").is_some()); + assert!(re.find("test.js").is_none()); + } + + #[test] + fn test_worker_import_meta_url_regex() { + let re = JsRegex::new(WORKER_IMPORT_META_URL_RE).unwrap(); + let test_code = r#"new Worker(new URL('./worker.js', import.meta.url))"#; + assert!(re.find(test_code).is_some()); + } + + #[test] + fn test_options_deserialization() { + let json = r#"{"isBuild":true}"#; + let options: Value = serde_json::from_str(json).unwrap(); + assert_eq!(options.get("isBuild").and_then(|x| x.as_bool()), Some(true)); + } +} diff --git a/rust-plugins/yaml/src/lib.rs b/rust-plugins/yaml/src/lib.rs index 6f3d13ab..f86c591d 100644 --- a/rust-plugins/yaml/src/lib.rs +++ b/rust-plugins/yaml/src/lib.rs @@ -153,3 +153,65 @@ impl Plugin for FarmPluginYaml { })) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plugin_name() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmPluginYaml::new(&config, options); + assert_eq!(plugin.name(), "FarmPluginYaml"); + } + + #[test] + fn test_is_yaml_file() { + assert!(is_yaml_file(&"test.yaml".to_string())); + assert!(is_yaml_file(&"test.yml".to_string())); + assert!(!is_yaml_file(&"test.json".to_string())); + assert!(!is_yaml_file(&"test.js".to_string())); + } + + #[test] + fn test_options_deserialization_defaults() { + let json = r#"{}"#; + let options: FarmPluginYamlOptions = serde_json::from_str(json).unwrap(); + assert!(options.document_mode.is_none()); + assert_eq!(options.include, None); + assert_eq!(options.exclude, None); + } + + #[test] + fn test_options_deserialization_with_values() { + let json = r#"{"documentMode":"multi","include":".*\\.yaml$","exclude":"node_modules"}"#; + let options: FarmPluginYamlOptions = serde_json::from_str(json).unwrap(); + assert!(options.document_mode.is_some()); + assert_eq!(options.include, Some(".*\\.yaml$".to_string())); + assert_eq!(options.exclude, Some("node_modules".to_string())); + } + + #[test] + fn test_should_process_path() { + let config = Config::default(); + let options = r#"{"include":".*\\.yaml$"}"#.to_string(); + let plugin = FarmPluginYaml::new(&config, options); + + assert!(plugin.should_process_path("test.yaml")); + assert!(!plugin.should_process_path("test.txt")); + } + + #[test] + fn test_yaml_to_js_simple() { + let config = Config::default(); + let options = r#"{}"#.to_string(); + let plugin = FarmPluginYaml::new(&config, options); + + let yaml_content = "key: value"; + let result = plugin.yaml_to_js(yaml_content); + assert!(result.is_ok()); + let js_code = result.unwrap(); + assert!(js_code.contains("export default")); + } +}