pict-rs/tests/common/mod.rs
asonix a71812930e Add tests with system deps
These new tests each spin up their own instance of pict-rs. This allows
for configuring pict-rs differently depending on the test that needs to
be run. It also opens the possibility of writing tests with object
storage and postgres requirements in the future.

So far there's pretty minimal test coverage. Just uploading,
downloading, deleting, and checking validation errors for images and
animations. In the future, tests need to be added for videos, many
variations of the process endpoint, and the admin endpoints.

The new tests are locked behind a configuration option (not a feature)
called `system_deps`. They can be enabled by passing

`RUSTFLAGS='--cfg system_deps'`

This is to prevent tests from running and failing in environments where
the exiftool, imagemagick, and ffmpeg binaries are not present.

This builds imagemagick from source for a few reasons
1. binaries for imagemagick 7 are not available for debian 12
2. the imagemagick appimage does not support avif files
3. the imagemagick appimage is limited to x86_64
2025-03-28 10:30:03 -05:00

135 lines
3.2 KiB
Rust

#![allow(dead_code)]
use std::future::Future;
pub fn with_pict_rs<F, Fut>(config: serde_json::Value, callback: F) -> color_eyre::Result<()>
where
F: Fn() -> Fut,
Fut: Future<Output = ()>,
{
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(async move {
tokio::task::LocalSet::new()
.run_until(async move {
let mut pict_rs_handle = spawn_pict_rs(config);
// give time to spin up
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
tokio::select! {
_ = (callback)() => {
pict_rs_handle.abort();
let _ = pict_rs_handle.await;
}
res = &mut pict_rs_handle => {
res.unwrap();
}
}
Ok(())
})
.await
})
}
pub async fn upload_form<I>(files: I) -> reqwest::multipart::Form
where
I: IntoIterator<Item = &'static str>,
{
let mut form = reqwest::multipart::Form::new();
for file in files {
form = form.file("images[]", file).await.expect("read file");
}
form
}
pub fn pict_rs_test_config(address: &str) -> serde_json::Value {
let directory = format!("/tmp/pict-rs-test/{}", uuid::Uuid::now_v7());
serde_json::json!({
"server": {
"address": address,
"temporary_directory": format!("{directory}/tmp")
},
"repo": {
"type": "sled",
"path": format!("{directory}/sled-repo")
},
"store": {
"type": "filesystem",
"path": format!("{directory}/files")
}
})
}
fn spawn_pict_rs(config: serde_json::Value) -> tokio::task::JoinHandle<()> {
tokio::task::spawn_local(async move {
pict_rs::ConfigSource::memory(config)
.init::<String>(None)
.expect("init pict-rs config")
.install_crypto_provider()
.run()
.await
.expect("run pict-rs")
})
}
#[derive(serde::Deserialize)]
#[serde(untagged)]
pub enum PictRsResult<T> {
Ok(T),
Err { msg: String, code: String },
}
impl<T> PictRsResult<T> {
pub fn unwrap(self) -> T {
match self {
Self::Ok(t) => t,
Self::Err { msg, code } => panic!("{code}: {msg}"),
}
}
}
#[derive(serde::Deserialize)]
pub enum OkString {
#[serde(rename = "ok")]
Ok,
}
#[derive(serde::Deserialize)]
pub struct UploadResponse {
pub msg: OkString,
pub files: Vec<File>,
}
#[derive(serde::Deserialize)]
pub struct File {
pub delete_token: String,
pub file: String,
pub details: Details,
}
#[derive(serde::Deserialize)]
pub struct DetailsResponse {
pub msg: OkString,
#[serde(flatten)]
pub details: Details,
}
#[derive(serde::Deserialize)]
pub struct Details {
pub width: u16,
pub height: u16,
pub frames: Option<u32>,
pub blurhash: String,
pub content_type: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: time::OffsetDateTime,
}