fmt
Some checks failed
CI / Check / Test / Build (push) Has been cancelled

This commit is contained in:
2026-05-13 23:38:57 +02:00
parent 7415b91e23
commit 19171806b9
142 changed files with 4140 additions and 2025 deletions

View File

@@ -18,7 +18,9 @@ struct MockPublisher {
impl MockPublisher {
fn new() -> Arc<Self> {
Arc::new(Self { emitted: Mutex::new(vec![]) })
Arc::new(Self {
emitted: Mutex::new(vec![]),
})
}
fn emitted(&self) -> Vec<String> {
@@ -42,10 +44,8 @@ async fn emits_image_stored_for_unconverted_keys() {
keys: vec!["avatars/u1".into(), "posters/m1".into()],
});
let publisher = MockPublisher::new();
let job = ConversionBackfillJob::new(
image_ref,
Arc::clone(&publisher) as Arc<dyn EventPublisher>,
);
let job =
ConversionBackfillJob::new(image_ref, Arc::clone(&publisher) as Arc<dyn EventPublisher>);
job.run().await.unwrap();
@@ -64,10 +64,8 @@ async fn skips_already_converted_keys() {
],
});
let publisher = MockPublisher::new();
let job = ConversionBackfillJob::new(
image_ref,
Arc::clone(&publisher) as Arc<dyn EventPublisher>,
);
let job =
ConversionBackfillJob::new(image_ref, Arc::clone(&publisher) as Arc<dyn EventPublisher>);
job.run().await.unwrap();
@@ -78,10 +76,8 @@ async fn skips_already_converted_keys() {
async fn empty_keys_emits_nothing() {
let image_ref = Arc::new(MockImageRef { keys: vec![] });
let publisher = MockPublisher::new();
let job = ConversionBackfillJob::new(
image_ref,
Arc::clone(&publisher) as Arc<dyn EventPublisher>,
);
let job =
ConversionBackfillJob::new(image_ref, Arc::clone(&publisher) as Arc<dyn EventPublisher>);
job.run().await.unwrap();

View File

@@ -3,18 +3,24 @@ use super::*;
#[test]
fn disabled_by_default() {
assert!(ConversionConfig::from_vars(None, None).unwrap().is_none());
assert!(ConversionConfig::from_vars(Some("false"), None).unwrap().is_none());
assert!(ConversionConfig::from_vars(Some("false"), None)
.unwrap()
.is_none());
}
#[test]
fn enabled_avif() {
let cfg = ConversionConfig::from_vars(Some("true"), Some("avif")).unwrap().unwrap();
let cfg = ConversionConfig::from_vars(Some("true"), Some("avif"))
.unwrap()
.unwrap();
assert_eq!(cfg.format, Format::Avif);
}
#[test]
fn enabled_webp() {
let cfg = ConversionConfig::from_vars(Some("true"), Some("webp")).unwrap().unwrap();
let cfg = ConversionConfig::from_vars(Some("true"), Some("webp"))
.unwrap()
.unwrap();
assert_eq!(cfg.format, Format::Webp);
}

View File

@@ -1,7 +1,7 @@
use super::*;
use std::sync::Mutex;
use object_store::memory::InMemory;
use image_storage::ImageStorageAdapter;
use object_store::memory::InMemory;
use std::sync::Mutex;
struct MockImageRef {
swaps: Mutex<Vec<(String, String)>>,
@@ -9,7 +9,9 @@ struct MockImageRef {
impl MockImageRef {
fn new() -> Arc<Self> {
Arc::new(Self { swaps: Mutex::new(vec![]) })
Arc::new(Self {
swaps: Mutex::new(vec![]),
})
}
fn swaps(&self) -> Vec<(String, String)> {
@@ -31,9 +33,7 @@ fn in_memory_storage() -> Arc<ImageStorageAdapter> {
fn tiny_jpeg() -> Vec<u8> {
use image::{DynamicImage, ImageBuffer, Rgb};
let img = DynamicImage::ImageRgb8(
ImageBuffer::from_pixel(4, 4, Rgb([200u8, 100, 50])),
);
let img = DynamicImage::ImageRgb8(ImageBuffer::from_pixel(4, 4, Rgb([200u8, 100, 50])));
let mut buf = std::io::Cursor::new(Vec::new());
img.write_to(&mut buf, image::ImageFormat::Jpeg).unwrap();
buf.into_inner()
@@ -49,9 +49,12 @@ async fn ignores_non_image_stored_events() {
Format::Avif,
);
handler.handle(&DomainEvent::UserUpdated {
user_id: domain::value_objects::UserId::from_uuid(uuid::Uuid::new_v4()),
}).await.unwrap();
handler
.handle(&DomainEvent::UserUpdated {
user_id: domain::value_objects::UserId::from_uuid(uuid::Uuid::new_v4()),
})
.await
.unwrap();
assert!(image_ref.swaps().is_empty());
}
@@ -59,7 +62,10 @@ async fn ignores_non_image_stored_events() {
#[tokio::test]
async fn skips_already_converted_avif_key() {
let storage = in_memory_storage();
storage.store("avatars/u1.avif", &tiny_jpeg()).await.unwrap();
storage
.store("avatars/u1.avif", &tiny_jpeg())
.await
.unwrap();
let image_ref = MockImageRef::new();
let handler = ImageConversionHandler::new(
Arc::clone(&storage) as Arc<dyn ImageStorage>,
@@ -67,7 +73,12 @@ async fn skips_already_converted_avif_key() {
Format::Avif,
);
handler.handle(&DomainEvent::ImageStored { key: "avatars/u1.avif".into() }).await.unwrap();
handler
.handle(&DomainEvent::ImageStored {
key: "avatars/u1.avif".into(),
})
.await
.unwrap();
assert!(image_ref.swaps().is_empty());
}
@@ -75,7 +86,10 @@ async fn skips_already_converted_avif_key() {
#[tokio::test]
async fn skips_already_converted_webp_key() {
let storage = in_memory_storage();
storage.store("posters/m1.webp", &tiny_jpeg()).await.unwrap();
storage
.store("posters/m1.webp", &tiny_jpeg())
.await
.unwrap();
let image_ref = MockImageRef::new();
let handler = ImageConversionHandler::new(
Arc::clone(&storage) as Arc<dyn ImageStorage>,
@@ -83,7 +97,12 @@ async fn skips_already_converted_webp_key() {
Format::Webp,
);
handler.handle(&DomainEvent::ImageStored { key: "posters/m1.webp".into() }).await.unwrap();
handler
.handle(&DomainEvent::ImageStored {
key: "posters/m1.webp".into(),
})
.await
.unwrap();
assert!(image_ref.swaps().is_empty());
}
@@ -99,9 +118,17 @@ async fn converts_jpeg_to_avif_and_swaps_key() {
Format::Avif,
);
handler.handle(&DomainEvent::ImageStored { key: "avatars/u1".into() }).await.unwrap();
handler
.handle(&DomainEvent::ImageStored {
key: "avatars/u1".into(),
})
.await
.unwrap();
assert_eq!(image_ref.swaps(), vec![("avatars/u1".into(), "avatars/u1.avif".into())]);
assert_eq!(
image_ref.swaps(),
vec![("avatars/u1".into(), "avatars/u1.avif".into())]
);
assert!(storage.get("avatars/u1.avif").await.is_ok());
assert!(storage.get("avatars/u1").await.is_err());
}
@@ -117,9 +144,17 @@ async fn converts_jpeg_to_webp_and_swaps_key() {
Format::Webp,
);
handler.handle(&DomainEvent::ImageStored { key: "avatars/u1".into() }).await.unwrap();
handler
.handle(&DomainEvent::ImageStored {
key: "avatars/u1".into(),
})
.await
.unwrap();
assert_eq!(image_ref.swaps(), vec![("avatars/u1".into(), "avatars/u1.webp".into())]);
assert_eq!(
image_ref.swaps(),
vec![("avatars/u1".into(), "avatars/u1.webp".into())]
);
assert!(storage.get("avatars/u1.webp").await.is_ok());
assert!(storage.get("avatars/u1").await.is_err());
}