- Add `.gitignore` entries for `webmeta.json` and `.vscode` - Improve collection scanning logging for better debugging - Improve error handling in collection methods for robustness
65 lines
2.0 KiB
Rust
65 lines
2.0 KiB
Rust
#[cfg(test)]
|
|
mod tests {
|
|
use crate::error::WebBuilderError;
|
|
use crate::ipfs::{calculate_blake_hash, upload_file};
|
|
use std::fs;
|
|
use tempfile::TempDir;
|
|
|
|
#[test]
|
|
fn test_upload_file_missing_file() {
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let file_path = temp_dir.path().join("nonexistent.txt");
|
|
|
|
let result = upload_file(&file_path);
|
|
|
|
assert!(result.is_err());
|
|
assert!(matches!(
|
|
result.unwrap_err(),
|
|
WebBuilderError::MissingFile(_)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn test_calculate_blake_hash() {
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let file_path = temp_dir.path().join("test.txt");
|
|
fs::write(&file_path, "test content").unwrap();
|
|
|
|
let result = calculate_blake_hash(&file_path).unwrap();
|
|
|
|
// The hash should start with "blake3-"
|
|
assert!(result.starts_with("blake3-"));
|
|
|
|
// The hash should be 64 characters long after the prefix
|
|
assert_eq!(result.len(), "blake3-".len() + 64);
|
|
|
|
// The hash should be the same for the same content
|
|
let file_path2 = temp_dir.path().join("test2.txt");
|
|
fs::write(&file_path2, "test content").unwrap();
|
|
|
|
let result2 = calculate_blake_hash(&file_path2).unwrap();
|
|
assert_eq!(result, result2);
|
|
|
|
// The hash should be different for different content
|
|
let file_path3 = temp_dir.path().join("test3.txt");
|
|
fs::write(&file_path3, "different content").unwrap();
|
|
|
|
let result3 = calculate_blake_hash(&file_path3).unwrap();
|
|
assert_ne!(result, result3);
|
|
}
|
|
|
|
#[test]
|
|
fn test_calculate_blake_hash_missing_file() {
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let file_path = temp_dir.path().join("nonexistent.txt");
|
|
|
|
let result = calculate_blake_hash(&file_path);
|
|
|
|
assert!(result.is_err());
|
|
assert!(matches!(
|
|
result.unwrap_err(),
|
|
WebBuilderError::MissingFile(_)
|
|
));
|
|
}
|
|
}
|