Files
rust_browser/tests/main_cli_tests.rs
2026-01-29 01:08:41 -05:00

279 lines
8.3 KiB
Rust

//! Integration tests for the main CLI application.
//!
//! These tests verify the command-line interface behavior, including
//! the --render and --png flags.
use std::fs;
use std::path::PathBuf;
use std::process::Command;
fn get_binary_path() -> PathBuf {
// Build the binary first
let output = Command::new("cargo")
.args(["build", "-p", "app_browser"])
.output()
.expect("Failed to build binary");
assert!(output.status.success(), "Build failed");
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("debug")
.join("app_browser")
}
fn create_temp_dir() -> PathBuf {
let temp_dir = std::env::temp_dir().join(format!("rust_browser_test_{}", std::process::id()));
fs::create_dir_all(&temp_dir).expect("Failed to create temp dir");
temp_dir
}
#[test]
#[ignore] // Filesystem-dependent test
fn test_render_mode_basic() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
// Create a simple HTML file
let input_html = temp_dir.join("test.html");
fs::write(&input_html, "<div>Test</div>").expect("Failed to write HTML");
// Run with --render
let output = Command::new(&binary)
.args(["--render", input_html.to_str().unwrap()])
.output()
.expect("Failed to execute binary");
assert!(output.status.success(), "Command failed: {:?}", output);
// Check output files were created
let layout_file = input_html.with_extension("").with_extension("layout.txt");
let dl_file = input_html.with_extension("").with_extension("dl.txt");
assert!(layout_file.exists(), "Layout file not created");
assert!(dl_file.exists(), "Display list file not created");
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}
#[test]
#[ignore] // Filesystem-dependent test
fn test_render_mode_with_output_dir() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
let output_dir = temp_dir.join("output");
// Create a simple HTML file
let input_html = temp_dir.join("test.html");
fs::write(&input_html, "<div>Test</div>").expect("Failed to write HTML");
// Run with --render and --output-dir
let output = Command::new(&binary)
.args([
"--render",
input_html.to_str().unwrap(),
"--output-dir",
output_dir.to_str().unwrap(),
])
.output()
.expect("Failed to execute binary");
assert!(output.status.success(), "Command failed: {:?}", output);
// Check output files were created in the specified directory
assert!(output_dir.exists(), "Output directory not created");
assert!(
output_dir.join("test.layout.txt").exists(),
"Layout file not in output dir"
);
assert!(
output_dir.join("test.dl.txt").exists(),
"Display list file not in output dir"
);
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}
#[test]
#[ignore] // Requires specific runtime environment setup
fn test_render_mode_with_png_flag() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
// Create a simple HTML file with color
let input_html = temp_dir.join("test.html");
fs::write(
&input_html,
r#"<div style="background-color: red; width: 100px; height: 100px;">Red Box</div>"#,
)
.expect("Failed to write HTML");
// Run with --render and --png
let output = Command::new(&binary)
.args(["--render", input_html.to_str().unwrap(), "--png"])
.output()
.expect("Failed to execute binary");
if !output.status.success() {
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
}
assert!(output.status.success(), "Command failed");
// Check PNG file was created
let png_file = input_html.with_extension("png");
assert!(png_file.exists(), "PNG file not created");
// Verify PNG is valid by checking magic bytes
let png_data = fs::read(&png_file).expect("Failed to read PNG");
assert_eq!(
&png_data[0..8],
&[137, 80, 78, 71, 13, 10, 26, 10],
"Invalid PNG magic bytes"
);
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}
#[test]
fn test_render_mode_missing_input_file() {
let binary = get_binary_path();
// Try to render a non-existent file
let output = Command::new(&binary)
.args(["--render", "/tmp/nonexistent_file_12345.html"])
.output()
.expect("Failed to execute binary");
// Should fail gracefully
assert!(!output.status.success(), "Should fail for missing file");
}
#[test]
fn test_render_mode_no_args() {
let binary = get_binary_path();
// Run --render without specifying a file
let output = Command::new(&binary)
.args(["--render"])
.output()
.expect("Failed to execute binary");
// Should exit with error code
assert!(
!output.status.success(),
"Should fail when no file specified"
);
// Should print usage message
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("Usage"), "Should show usage message");
}
#[test]
#[ignore] // Filesystem-dependent test
fn test_render_mode_empty_html() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
// Create an empty HTML file
let input_html = temp_dir.join("empty.html");
fs::write(&input_html, "").expect("Failed to write HTML");
// Run with --render
let output = Command::new(&binary)
.args(["--render", input_html.to_str().unwrap()])
.output()
.expect("Failed to execute binary");
assert!(output.status.success(), "Should handle empty HTML");
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}
#[test]
#[ignore] // Filesystem-dependent test
fn test_render_mode_malformed_html() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
// Create malformed HTML
let input_html = temp_dir.join("malformed.html");
fs::write(&input_html, "<div><p>Unclosed tags").expect("Failed to write HTML");
// Run with --render - should still work (HTML is forgiving)
let output = Command::new(&binary)
.args(["--render", input_html.to_str().unwrap()])
.output()
.expect("Failed to execute binary");
assert!(
output.status.success(),
"Should handle malformed HTML gracefully"
);
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}
#[test]
#[ignore] // Requires specific runtime environment setup
fn test_render_mode_complex_document() {
let binary = get_binary_path();
let temp_dir = create_temp_dir();
// Create a complex HTML document
let input_html = temp_dir.join("complex.html");
fs::write(
&input_html,
r#"
<html>
<head>
<style>
body { margin: 20px; padding: 10px; }
.container { width: 500px; height: 300px; }
#header { background-color: #333; color: white; padding: 10px; }
.item { margin: 5px; padding: 5px; }
</style>
</head>
<body>
<div id="header">Header</div>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
"#,
)
.expect("Failed to write HTML");
// Run with --render and --png
let output = Command::new(&binary)
.args(["--render", input_html.to_str().unwrap(), "--png"])
.output()
.expect("Failed to execute binary");
if !output.status.success() {
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
}
assert!(output.status.success(), "Should handle complex HTML");
// Check all output files
let png_file = input_html.with_extension("png");
assert!(png_file.exists(), "PNG file should be created");
// Check PNG size is reasonable (should be 800x600 based on viewport)
let png_data = fs::read(&png_file).expect("Failed to read PNG");
assert!(png_data.len() > 1000, "PNG should have substantial data");
// Cleanup
fs::remove_dir_all(&temp_dir).ok();
}