Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rust-plugins/auto-import/playground-vue/src/types/auto_import.d.ts
Original file line number Diff line number Diff line change
@@ -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']
}
45 changes: 45 additions & 0 deletions rust-plugins/compress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
51 changes: 51 additions & 0 deletions rust-plugins/dsv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&param);
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(&param);
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(&param);
assert!(reader.is_err());
}
}
37 changes: 37 additions & 0 deletions rust-plugins/image/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
37 changes: 37 additions & 0 deletions rust-plugins/mdx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
21 changes: 21 additions & 0 deletions rust-plugins/modular-import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
59 changes: 59 additions & 0 deletions rust-plugins/strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,62 @@ fn create_first_pass_regex(first_pass: &[String]) -> Result<Regex, Box<dyn Error
let regex = Regex::new(&format!(r"\b(?:{})", joined_patterns))?;
Ok(regex)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_plugin_name() {
let config = Config::default();
let options = r#"{}"#.to_string();
let plugin = FarmPulginStrip::new(&config, options);
assert_eq!(plugin.name(), PLUGIN_NAME);
}

#[test]
fn test_options_deserialization() {
let json = r#"{"labels":["TEST"],"functions":["console.log"],"debugger":true}"#;
let options: Options = serde_json::from_str(json).unwrap();
assert_eq!(options.labels.as_ref().unwrap().len(), 1);
assert_eq!(options.functions.as_ref().unwrap().len(), 1);
assert_eq!(options.debugger, Some(true));
}

#[test]
fn test_create_regex_from_list() {
let functions = vec!["console.log".to_string(), "console.debug".to_string()];
let regex = create_regex_from_list(&functions);
assert!(regex.is_ok());
let regex = regex.unwrap();
assert!(regex.is_match("console.log"));
assert!(regex.is_match("console.debug"));
assert!(!regex.is_match("console.error"));
}

#[test]
fn test_flatten_member_expression() {
use farmfe_core::swc_common::DUMMY_SP;
use farmfe_core::swc_common::SyntaxContext;

// Test simple identifier (should return None)
let ident_expr = Expr::Ident(Ident {
sym: "test".into(),
span: DUMMY_SP,
optional: false,
ctxt: SyntaxContext::empty(),
});
assert_eq!(flatten(&ident_expr), None);
}

#[test]
fn test_void_expr_creation() {
let expr = void_expr();
match *expr {
Expr::Ident(ref ident) => {
assert_eq!(ident.sym.as_ref(), "(void 0)");
}
_ => panic!("Expected Ident expression"),
}
}
}
61 changes: 61 additions & 0 deletions rust-plugins/url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions rust-plugins/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading