use super::*;
const PNG_1024X768: &[u8] = &[
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 4, 0, 0, 0, 3, 0, 8, 6, 0, 0, 0, 0, 0, 0, 0, ];
const JPEG_SOF0_800X600: &[u8] = &[
255, 216, 255, 192, 0, 17, 8, 2, 88, 3, 32, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 255, 217, ];
const JPEG_APP0_SOF2_640X480: &[u8] = &[
255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 194, 0, 17, 8, 1, 224, 2, 128, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 255, 217, ];
#[test]
fn test_png_dimensions_reads_ihdr_width_and_height() {
assert_eq!(png_dimensions(PNG_1024X768), Some((1024, 768)));
}
#[test]
fn test_png_dimensions_rejects_bad_signature() {
let mut bad = PNG_1024X768.to_vec();
bad[0] = 0x00;
assert_eq!(png_dimensions(&bad), None);
}
#[test]
fn test_png_dimensions_rejects_truncated_input() {
assert_eq!(png_dimensions(&PNG_1024X768[..10]), None);
}
#[test]
fn test_png_dimensions_rejects_missing_ihdr_tag() {
let mut bad = PNG_1024X768.to_vec();
bad[12..16].copy_from_slice(b"IDAT");
assert_eq!(png_dimensions(&bad), None);
}
#[test]
fn test_jpeg_dimensions_reads_sof0_baseline() {
assert_eq!(jpeg_dimensions(JPEG_SOF0_800X600), Some((800, 600)));
}
#[test]
fn test_jpeg_dimensions_skips_leading_app0_and_reads_sof2_progressive() {
assert_eq!(jpeg_dimensions(JPEG_APP0_SOF2_640X480), Some((640, 480)));
}
#[test]
fn test_jpeg_dimensions_rejects_bad_soi() {
let mut bad = JPEG_SOF0_800X600.to_vec();
bad[0] = 0x00;
assert_eq!(jpeg_dimensions(&bad), None);
}
#[test]
fn test_jpeg_dimensions_rejects_truncated_input() {
assert_eq!(jpeg_dimensions(&JPEG_SOF0_800X600[..4]), None);
}
#[test]
fn test_jpeg_dimensions_returns_none_without_a_sof_marker() {
assert_eq!(jpeg_dimensions(&[255, 216, 255, 217]), None);
}
#[test]
fn test_jpeg_dimensions_rejects_a_non_0xff_byte_mid_stream() {
assert_eq!(jpeg_dimensions(&[255, 216, 0x00, 0x00]), None);
}
#[test]
fn test_jpeg_dimensions_rejects_a_segment_length_under_two() {
assert_eq!(jpeg_dimensions(&[255, 216, 255, 0xE0, 0x00, 0x01]), None);
}
#[test]
fn test_image_token_estimator_png_cost_is_pixels_over_750_ceiling() {
let bytes_b64 = "irrelevant-when-dimensions-are-known";
let cost = ImageTokenEstimator::estimate("image/png", PNG_1024X768, bytes_b64);
assert_eq!(cost, 786_432_u64.div_ceil(750));
}
#[test]
fn test_image_token_estimator_jpeg_cost_is_pixels_over_750_ceiling() {
let bytes_b64 = "irrelevant-when-dimensions-are-known";
let cost = ImageTokenEstimator::estimate("image/jpeg", JPEG_SOF0_800X600, bytes_b64);
assert_eq!(cost, 640);
}
#[test]
fn test_image_token_estimator_falls_back_to_text_estimate_for_unsupported_mime() {
let bytes_b64 = "AAAA";
let cost = ImageTokenEstimator::estimate("image/gif", b"whatever bytes", bytes_b64);
assert_eq!(cost, HeuristicEstimator.estimate(bytes_b64));
}
#[test]
fn test_image_token_estimator_falls_back_to_text_estimate_for_unreadable_png_header() {
let bytes_b64 = "not-a-real-header-payload-here";
let cost = ImageTokenEstimator::estimate("image/png", b"too short", bytes_b64);
assert_eq!(cost, HeuristicEstimator.estimate(bytes_b64));
}
#[test]
fn test_image_token_estimator_falls_back_to_text_estimate_for_unreadable_jpeg_header() {
let bytes_b64 = "another-fallback-b64-string";
let cost = ImageTokenEstimator::estimate("image/jpeg", b"too short", bytes_b64);
assert_eq!(cost, HeuristicEstimator.estimate(bytes_b64));
}
#[test]
fn test_image_token_estimator_is_deterministic() {
let a = ImageTokenEstimator::estimate("image/png", PNG_1024X768, "x");
let b = ImageTokenEstimator::estimate("image/png", PNG_1024X768, "x");
assert_eq!(a, b);
}
const PNG_0X768: &[u8] = &[
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 0, 0, 0, 3, 0, 8, 6, 0, 0, 0, 0, 0, 0, 0, ];
const JPEG_SOF0_800X0: &[u8] = &[
255, 216, 255, 192, 0, 17, 8, 0, 0, 3, 32, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 255, 217, ];
#[test]
fn test_zero_dimension_png_falls_back_to_overcounting_text_estimate() {
let bytes_b64 = "a-large-payload-stand-in";
let cost = ImageTokenEstimator::estimate("image/png", PNG_0X768, bytes_b64);
assert_eq!(
cost,
HeuristicEstimator.estimate(bytes_b64),
"a forged zero dimension must never price a payload at 0 tokens"
);
assert!(cost > 0);
}
#[test]
fn test_zero_dimension_jpeg_falls_back_to_overcounting_text_estimate() {
let bytes_b64 = "another-large-payload-stand-in";
let cost = ImageTokenEstimator::estimate("image/jpeg", JPEG_SOF0_800X0, bytes_b64);
assert_eq!(cost, HeuristicEstimator.estimate(bytes_b64));
assert!(cost > 0);
}