use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn pxl_binary() -> PathBuf {
let release = Path::new("target/release/pxl");
if release.exists() {
return release.to_path_buf();
}
let debug = Path::new("target/debug/pxl");
if debug.exists() {
return debug.to_path_buf();
}
panic!("pxl binary not found. Run 'cargo build' first.");
}
fn get_image_dimensions(path: &Path) -> (u32, u32) {
let img = image::open(path).expect("Failed to open output image");
(img.width(), img.height())
}
#[test]
fn test_scale_default() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_default.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("-o")
.arg(&output_path)
.output()
.expect("Failed to execute pxl");
assert!(output.status.success(), "Render failed: {}", String::from_utf8_lossy(&output.stderr));
let (w, h) = get_image_dimensions(&output_path);
assert_eq!((w, h), (1, 1), "Default scale should produce 1x1 output");
}
#[test]
fn test_scale_2x() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_2x.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--scale")
.arg("2")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render with --scale 2 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let (w, h) = get_image_dimensions(&output_path);
assert_eq!((w, h), (2, 2), "Scale 2 should produce 2x2 from 1x1");
}
#[test]
fn test_scale_4x() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_4x.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/include_palette.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--scale")
.arg("4")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render with --scale 4 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let (w, h) = get_image_dimensions(&output_path);
assert_eq!((w, h), (8, 8), "Scale 4 should produce 8x8 from 2x2");
}
#[test]
fn test_scale_8x() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_8x.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--scale")
.arg("8")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render with --scale 8 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let (w, h) = get_image_dimensions(&output_path);
assert_eq!((w, h), (8, 8), "Scale 8 should produce 8x8 from 1x1");
}
#[test]
fn test_scale_max_16x() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_16x.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--scale")
.arg("16")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render with --scale 16 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let (w, h) = get_image_dimensions(&output_path);
assert_eq!((w, h), (16, 16), "Scale 16 should produce 16x16 from 1x1");
}
#[test]
fn test_scale_invalid_too_high() {
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("--scale")
.arg("17")
.output()
.expect("Failed to execute pxl");
assert!(!output.status.success(), "Scale 17 should be rejected");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("17") || stderr.contains("invalid") || stderr.contains("error"),
"Expected error message about invalid scale value, got: {stderr}"
);
}
#[test]
fn test_scale_invalid_zero() {
let output = Command::new(pxl_binary())
.arg("render")
.arg("tests/fixtures/valid/minimal_dot.jsonl")
.arg("--scale")
.arg("0")
.output()
.expect("Failed to execute pxl");
assert!(!output.status.success(), "Scale 0 should be rejected");
}
#[test]
fn test_scale_with_spritesheet() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_spritesheet.png");
let output = Command::new(pxl_binary())
.arg("render")
.arg("examples/walk_cycle.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--spritesheet")
.arg("--scale")
.arg("2")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render spritesheet with --scale 2 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let (w, h) = get_image_dimensions(&output_path);
assert_eq!(h, 16, "Scaled spritesheet height should be 16 (8x8 frames * scale 2)");
assert!(w > 16, "Scaled spritesheet width should be wider than single frame");
}
#[test]
fn test_scale_with_gif() {
let output_dir = std::env::temp_dir().join("pxl_scale_test");
fs::create_dir_all(&output_dir).ok();
let output_path = output_dir.join("scale_test.gif");
let output = Command::new(pxl_binary())
.arg("render")
.arg("examples/walk_cycle.jsonl")
.arg("-o")
.arg(&output_path)
.arg("--gif")
.arg("--scale")
.arg("2")
.output()
.expect("Failed to execute pxl");
assert!(
output.status.success(),
"Render GIF with --scale 2 failed: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(output_path.exists(), "GIF output file should exist at {output_path:?}");
}