refactor(gateway): share shard helpers and test fixtures (#3381)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-10 23:04:30 +02:00
1 parent f22261f218
commit e7b13fd19f
25 files changed
+101 -2021

No files matched your search

-60
View File
@@ -1,60 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pub use crate::constants::{clamp_size, parse_image_size};
#[cfg(test)]
mod tests {
use super::*;
use crate::constants::{AssetKind, DEFAULT_IMAGE_SIZE, IMAGE_SIZES};
#[test]
fn unparsable_sizes_fall_back_to_the_default() {
assert_eq!(DEFAULT_IMAGE_SIZE, parse_image_size(None));
assert_eq!(DEFAULT_IMAGE_SIZE, parse_image_size(Some("")));
assert_eq!(DEFAULT_IMAGE_SIZE, parse_image_size(Some("-1")));
assert_eq!(DEFAULT_IMAGE_SIZE, parse_image_size(Some("99999999999")));
assert_eq!(DEFAULT_IMAGE_SIZE, parse_image_size(Some("not-a-number")));
}
#[test]
fn parsable_sizes_snap_up_to_the_next_rung() {
assert_eq!(16, parse_image_size(Some("0")));
assert_eq!(640, parse_image_size(Some("640")));
assert_eq!(1024, parse_image_size(Some("641")));
assert_eq!(1024, parse_image_size(Some("777")));
assert_eq!(1024, parse_image_size(Some("1000")));
assert_eq!(16384, parse_image_size(Some("99999")));
for size in IMAGE_SIZES {
assert_eq!(*size, parse_image_size(Some(&size.to_string())));
}
assert_eq!(16384, parse_image_size(Some("16384")));
}
#[test]
fn an_off_ladder_size_never_serves_fewer_pixels_than_requested() {
for requested in 1..=4096u32 {
let served = parse_image_size(Some(&requested.to_string()));
assert!(
served >= requested,
"size={requested} served {served}, a silent downscale"
);
}
}
#[test]
fn snapping_is_idempotent() {
for raw in [0u32, 1, 17, 641, 777, 1000, 4097, 99999] {
let once = parse_image_size(Some(&raw.to_string()));
let twice = parse_image_size(Some(&once.to_string()));
assert_eq!(once, twice, "size={raw} did not settle");
}
}
#[test]
fn clamping_respects_kind_bounds() {
assert_eq!(128, clamp_size(1, AssetKind::Avatar));
assert_eq!(1024, clamp_size(4096, AssetKind::Avatar));
assert_eq!(480, clamp_size(16, AssetKind::Banner));
assert_eq!(512, clamp_size(4096, AssetKind::Emoji));
}
}
+3
View File
@@ -157,6 +157,9 @@ mod tests {
assert_eq!(16, parse_image_size(Some("0")));
assert_eq!(16384, parse_image_size(Some("99999")));
assert_eq!(128, parse_image_size(Some("not-a-number")));
assert_eq!(128, parse_image_size(Some("")));
assert_eq!(128, parse_image_size(Some("-1")));
assert_eq!(128, parse_image_size(Some("99999999999")));
}
#[test]
-1
View File
@@ -2,7 +2,6 @@
mod aggregate_error;
pub mod asset_hash;
mod asset_size;
pub mod aws_sigv4;
mod byte_budget;
mod byte_cache;
+2 -5
View File
@@ -1,9 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{
asset_size,
constants::{AssetExtension, AssetKind},
};
use crate::constants::{self, AssetExtension, AssetKind};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum OutputFormat {
@@ -97,7 +94,7 @@ pub fn select_url_variant(input: Input) -> OutputSelection {
format: OutputFormat::coerce_from_extension(requested),
size: input
.requested_size
.map(|size| asset_size::clamp_size(size, input.kind)),
.map(|size| constants::clamp_size(size, input.kind)),
reason: if is_output_format_supported(requested) {
"url"
} else {
+1 -2
View File
@@ -6,7 +6,6 @@ pub(in crate::server) mod response;
mod tests;
use crate::{
asset_size,
coalescer::CoalescerError,
config::PolicyMode,
constants::{self, AssetExtension, AssetKind},
@@ -65,7 +64,7 @@ pub(in crate::server) async fn serve_asset_image(
let range_header = headers.get(header::RANGE).and_then(|v| v.to_str().ok());
let requested_download = bool_param(params, "download", false);
let asset_filename = asset_filename_hint(&asset);
let size = asset_size::parse_image_size(params.get("size").map(String::as_str));
let size = constants::parse_image_size(params.get("size").map(String::as_str));
let requested_manual_format = asset_manual_format_override(params, asset.original_ext);
let manual_format_override = asset.forced_output_format.or(requested_manual_format);
let selected = output_format::select_url_variant(output_format::Input {
@@ -70,7 +70,7 @@ fn test_transform_cache() -> crate::transform_cache::TransformCache {
#[test]
fn asset_size_query_is_clamped_by_kind() {
let params = HashMap::from([("size".to_owned(), "4096".to_owned())]);
let size = asset_size::parse_image_size(params.get("size").map(String::as_str));
let size = constants::parse_image_size(params.get("size").map(String::as_str));
let selected = output_format::select_url_variant(output_format::Input {
kind: AssetKind::Avatar,
original: AssetExtension::Webp,
@@ -339,7 +339,7 @@ async fn an_asset_image_source_that_vanishes_mid_request_is_not_found() {
}
fn avatar_cache_key_for_requested_size(raw: &str) -> String {
let size = asset_size::parse_image_size(Some(raw));
let size = constants::parse_image_size(Some(raw));
let selected = output_format::select_url_variant(output_format::Input {
kind: AssetKind::Avatar,
original: AssetExtension::Webp,