1
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs.git synced 2025-04-22 16:14:09 +00:00

Add details integration test

This commit is contained in:
asonix 2025-03-29 12:16:18 -05:00
parent 3adec92cff
commit f9b1cf961c
2 changed files with 141 additions and 6 deletions
tests

View file

@ -79,7 +79,7 @@ fn spawn_pict_rs(config: serde_json::Value) -> tokio::task::JoinHandle<()> {
})
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
pub enum PictRsResult<T> {
Ok(T),
@ -95,27 +95,27 @@ impl<T> PictRsResult<T> {
}
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub enum OkString {
#[serde(rename = "ok")]
Ok,
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub struct UploadResponse {
pub msg: OkString,
pub files: Vec<File>,
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub struct File {
pub delete_token: String,
pub file: String,
pub details: Details,
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub struct DetailsResponse {
pub msg: OkString,
@ -123,7 +123,7 @@ pub struct DetailsResponse {
pub details: Details,
}
#[derive(serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub struct Details {
pub width: u16,
pub height: u16,

135
tests/details.rs Normal file
View file

@ -0,0 +1,135 @@
#![cfg(system_deps)]
use std::collections::HashMap;
use common::{pict_rs_test_config, upload_form, with_pict_rs, PictRsResult, UploadResponse};
mod common;
#[test]
fn reads_image_details_from_uploads() {
let address = "127.0.0.1:9300";
struct Case {
file: &'static str,
width: u16,
height: u16,
frames: Option<u32>,
blurhash: &'static str,
content_type: &'static str,
}
impl Case {
fn check(&self, details: &common::Details) {
assert_eq!(self.width, details.width, "invalid width for {}", self.file);
assert_eq!(
self.height, details.height,
"invalid height for {}",
self.file
);
assert_eq!(
self.frames, details.frames,
"invalid frames for {}",
self.file
);
assert_eq!(
self.blurhash, details.blurhash,
"invalid blurhash for {}",
self.file
);
assert_eq!(
self.content_type, details.content_type,
"invalid content type for {}",
self.file
);
}
}
let cases = [
Case {
file: "./client-examples/earth.gif",
width: 400,
height: 400,
frames: Some(44),
blurhash: "KD7n8nWF0M%1a#M|0MoI^%",
content_type: "image/gif",
},
Case {
file: "./client-examples/awoo.webp",
width: 3000,
height: 4000,
frames: None,
blurhash: "TCG8.f~957XNV@?ZORI]RP-.ozIU",
content_type: "image/webp",
},
Case {
file: "./client-examples/earth.avif",
width: 600,
height: 600,
frames: Some(44),
blurhash: "KC7U3RWF0M%1a#Ip0MoI^%",
content_type: "image/avif",
},
Case {
file: "./client-examples/cat.jpg",
width: 1200,
height: 1387,
frames: None,
blurhash: "KmKn9x4T.8D%j[V@o~RkjY",
content_type: "image/jpeg",
},
Case {
file: "./client-examples/scene.webp",
width: 550,
height: 368,
frames: None,
blurhash: "L_C@s#R-RPae.TRkWCof.8WFR.ay",
content_type: "image/webp",
},
Case {
file: "./client-examples/test.png",
width: 640,
height: 360,
frames: None,
blurhash: "MPB;OHN6XdaJeswBNOs,W9R*E7t7nPogo#",
content_type: "image/png",
},
];
let mut config = pict_rs_test_config(address);
config["server"]["max_file_count"] = 10.into();
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(cases.iter().map(|c| c.file)).await;
let response = client
.post(format!("http://{address}/image"))
.multipart(form)
.send()
.await
.expect("send request");
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
let cases = cases
.iter()
.map(|c| (c.blurhash, c))
.collect::<HashMap<_, _>>();
for file in upload.files.iter() {
let case = cases
.get(file.details.blurhash.as_str())
.expect("blurhash in cases");
case.check(&file.details);
}
})
.unwrap();
}