From e2e58e86e872ffd05a200809fd057089142860b9 Mon Sep 17 00:00:00 2001 From: Abdulla Abdurakhmanov Date: Thu, 15 Aug 2024 12:33:10 +0200 Subject: [PATCH] Clipboard tests and fixes --- src/file_systems/clipboard.rs | 82 ++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/file_systems/clipboard.rs b/src/file_systems/clipboard.rs index ac21221..af0b7e2 100644 --- a/src/file_systems/clipboard.rs +++ b/src/file_systems/clipboard.rs @@ -57,7 +57,7 @@ impl<'a> FileSystemConnection<'a> for ClipboardFileSystem<'a> { Ok(( FileSystemRef { relative_path: format!("{}.png", filename).into(), - media_type: Some(mime::TEXT_PLAIN), + media_type: Some(mime::IMAGE_PNG), file_size: Some(png_image_bytes.len() as u64), }, Box::new(futures::stream::iter(vec![Ok(bytes::Bytes::from( @@ -166,3 +166,83 @@ impl<'a> FileSystemConnection<'a> for ClipboardFileSystem<'a> { } } } + +#[allow(unused_imports)] +mod tests { + use super::*; + use crate::file_systems::DetectFileSystem; + use console::Term; + use image::RgbaImage; + + #[tokio::test] + async fn upload_download_text_test() -> Result<(), Box> { + let term = Term::stdout(); + let reporter: AppReporter = AppReporter::from(&term); + + let mut fs = DetectFileSystem::open(&format!("clipboard://"), &reporter).await?; + + let test_content = "Test content"; + + fs.upload( + futures::stream::iter(vec![Ok(bytes::Bytes::from(test_content))]), + Some(&FileSystemRef { + relative_path: "temp_file.txt".into(), + media_type: Some(mime::TEXT_PLAIN), + file_size: Some(13), + }), + ) + .await?; + + let (file_ref, stream) = fs.download(None).await?; + + let downloaded_bytes: Vec = stream.try_collect().await?; + let flattened_bytes = downloaded_bytes.concat(); + let downloaded_content = std::str::from_utf8(&flattened_bytes)?; + assert_eq!(downloaded_content, test_content); + assert_eq!(file_ref.media_type, Some(mime::TEXT_PLAIN)); + assert_eq!(file_ref.file_size, Some(test_content.len() as u64)); + + fs.close().await?; + + Ok(()) + } + + #[tokio::test] + async fn upload_download_image_test() -> Result<(), Box> { + let term = Term::stdout(); + let reporter: AppReporter = AppReporter::from(&term); + + let mut fs = DetectFileSystem::open(&format!("clipboard://"), &reporter).await?; + + let test_content: image::RgbaImage = RgbaImage::new(100, 100); + let mut writer = std::io::Cursor::new(Vec::new()); + test_content.write_to(&mut writer, ImageFormat::Png)?; + let png_image_bytes = writer.into_inner(); + let png_images_bytes_len = png_image_bytes.len() as u64; + + fs.upload( + futures::stream::iter(vec![Ok(bytes::Bytes::from(png_image_bytes))]), + Some(&FileSystemRef { + relative_path: "temp_file.png".into(), + media_type: Some(mime::IMAGE_PNG), + file_size: Some(png_images_bytes_len), + }), + ) + .await?; + + let (file_ref, stream) = fs.download(None).await?; + + let downloaded_bytes: Vec = stream.try_collect().await?; + let flattened_bytes = downloaded_bytes.concat(); + let downloaded_content = + image::load_from_memory_with_format(&flattened_bytes, ImageFormat::Png)?; + assert_eq!(downloaded_content.width(), 100); + assert_eq!(downloaded_content.height(), 100); + assert_eq!(file_ref.media_type, Some(mime::IMAGE_PNG)); + assert_eq!(file_ref.file_size, Some(png_images_bytes_len)); + + fs.close().await?; + + Ok(()) + } +}