#[cfg(test)]
mod tests {
use metadata_gen::async_extract_metadata_from_file;
use metadata_gen::utils::escape_html;
#[test]
fn test_string_escaping() {
let input = "<script>alert('test');</script>";
let escaped = escape_html(input);
let expected =
"<script>alert('test');</script>"; assert_eq!(escaped, expected, "The escaped HTML entities should match the expected result.");
}
#[test]
fn test_invalid_input_handling() {
let input = "";
let result = escape_html(input);
assert_eq!(
result, "",
"Empty string should return empty result"
);
}
#[test]
fn test_escape_html() {
let input = "Hello, <world> & \"friends\"!";
let expected =
"Hello, <world> & "friends"!";
assert_eq!(escape_html(input), expected);
}
#[tokio::test]
async fn test_async_extract_metadata_from_file() {
use tempfile::tempdir;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
let temp_dir = tempdir().unwrap();
let file_path = temp_dir.path().join("test.md");
let mut file = File::create(&file_path).await.unwrap();
let content = r#"---
title: Test Page
description: A test page for metadata extraction
keywords: test, metadata, extraction
---
# Test Content
This is a test file for metadata extraction."#;
file.write_all(content.as_bytes()).await.unwrap();
let result = async_extract_metadata_from_file(
file_path.to_str().unwrap(),
)
.await;
assert!(result.is_ok());
let (metadata, keywords, meta_tags) = result.unwrap();
assert_eq!(
metadata.get("title"),
Some(&"Test Page".to_string())
);
assert_eq!(
metadata.get("description"),
Some(&"A test page for metadata extraction".to_string())
);
assert_eq!(keywords, vec!["test", "metadata", "extraction"]);
assert!(!meta_tags.primary.is_empty());
}
}