use anyhow::Result;
use lumin::view::{FileContents, ViewOptions, view_file};
use std::path::Path;
#[test]
fn test_view_text_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions::default();
let result = view_file(file_path, &options)?;
assert_eq!(result.file_path, file_path);
assert!(result.file_type.starts_with("text/"));
match result.contents {
FileContents::Text { content, metadata } => {
assert!(content.contains("This is a sample text file"));
assert!(content.contains("PATTERN"));
assert_eq!(metadata.line_count, 6);
assert!(metadata.char_count > 0);
}
_ => panic!("Expected text content"),
}
Ok(())
}
#[test]
fn test_view_binary_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/binary_files/binary.bin");
let options = ViewOptions::default();
let result = view_file(file_path, &options)?;
assert_eq!(result.file_path, file_path);
match result.contents {
FileContents::Binary { message, metadata } => {
assert!(message.contains("Binary file detected"));
assert!(metadata.binary);
assert!(metadata.size_bytes > 0);
}
_ => panic!("Expected binary content"),
}
Ok(())
}
#[test]
fn test_view_image_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/binary_files/sample.jpg");
let options = ViewOptions::default();
let result = view_file(file_path, &options)?;
assert_eq!(result.file_path, file_path);
match result.contents {
FileContents::Image { message, metadata } => {
assert!(message.contains("Image file detected"));
assert!(metadata.binary);
assert!(metadata.size_bytes > 0);
assert_eq!(metadata.media_type, "image");
}
FileContents::Binary { message, metadata } => {
assert!(message.contains("Binary file detected"));
assert!(metadata.binary);
assert!(metadata.size_bytes > 0);
}
_ => panic!("Expected image or binary content"),
}
Ok(())
}
#[test]
fn test_view_with_size_limit() -> Result<()> {
let tiny_limit = 10; let file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions {
max_size: Some(tiny_limit),
line_from: None,
line_to: None,
};
let result = view_file(file_path, &options);
assert!(result.is_err());
let error_message = format!("{:?}", result.err().unwrap());
assert!(error_message.contains("File is too large"));
Ok(())
}
#[test]
fn test_view_nonexistent_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/does_not_exist.txt");
let options = ViewOptions::default();
let result = view_file(file_path, &options);
assert!(result.is_err());
let error_message = format!("{:?}", result.err().unwrap());
assert!(error_message.contains("File not found"));
Ok(())
}
#[test]
fn test_view_markdown_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/markdown.md");
let options = ViewOptions::default();
let result = view_file(file_path, &options)?;
assert!(result.file_type.starts_with("text/"));
match result.contents {
FileContents::Text {
content,
metadata: _,
} => {
assert!(content.contains("# Sample Markdown"));
assert!(content.contains("```rust"));
assert!(content.contains("fn main()"));
}
_ => panic!("Expected text content for markdown file"),
}
Ok(())
}
#[test]
fn test_view_toml_config_file() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/config.toml");
let options = ViewOptions::default();
let result = view_file(file_path, &options)?;
assert!(result.file_type.starts_with("text/"));
match result.contents {
FileContents::Text {
content,
metadata: _,
} => {
assert!(content.contains("[server]"));
assert!(content.contains("port = 8080"));
assert!(content.contains("[database]"));
assert!(content.contains("[logging]"));
}
_ => panic!("Expected text content for toml file"),
}
Ok(())
}
#[test]
fn test_view_with_line_filtering() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions {
max_size: None,
line_from: Some(2), line_to: Some(4), };
let view_result = view_file(file_path, &options)?;
match &view_result.contents {
FileContents::Text { content, metadata } => {
assert_eq!(content.line_contents.len(), 3);
assert_eq!(content.line_contents[0].line_number, 2);
assert_eq!(content.line_contents[2].line_number, 4);
assert!(metadata.line_count > 3); }
_ => panic!("Expected text content"),
}
Ok(())
}
#[test]
fn test_view_with_out_of_range_line_filtering() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions {
max_size: None,
line_from: Some(100),
line_to: Some(200),
};
let view_result = view_file(file_path, &options)?;
match &view_result.contents {
FileContents::Text { content, metadata } => {
assert!(content.line_contents.is_empty());
assert_eq!(metadata.line_count, 6);
}
_ => panic!("Expected text content"),
}
let options = ViewOptions {
max_size: None,
line_from: Some(5),
line_to: Some(10),
};
let view_result = view_file(file_path, &options)?;
match &view_result.contents {
FileContents::Text { content, metadata } => {
assert_eq!(content.line_contents.len(), 2);
assert_eq!(content.line_contents[0].line_number, 5);
assert_eq!(content.line_contents[1].line_number, 6);
assert_eq!(metadata.line_count, 6);
}
_ => panic!("Expected text content"),
}
let options = ViewOptions {
max_size: None,
line_from: Some(4),
line_to: Some(2),
};
let view_result = view_file(file_path, &options)?;
match &view_result.contents {
FileContents::Text { content, metadata } => {
assert!(content.line_contents.is_empty());
assert_eq!(metadata.line_count, 6);
}
_ => panic!("Expected text content"),
}
Ok(())
}
#[test]
fn test_total_line_num_field() -> Result<()> {
let text_file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions::default();
let text_result = view_file(text_file_path, &options)?;
assert_eq!(text_result.total_line_num, Some(6));
let binary_file_path = Path::new("tests/fixtures/binary_files/binary.bin");
let binary_result = view_file(binary_file_path, &options)?;
assert_eq!(binary_result.total_line_num, None);
let filtered_options = ViewOptions {
max_size: None,
line_from: Some(2),
line_to: Some(4),
};
let filtered_result = view_file(text_file_path, &filtered_options)?;
assert_eq!(filtered_result.total_line_num, Some(6));
Ok(())
}
#[test]
fn test_no_trailing_newlines() -> Result<()> {
let text_file_path = Path::new("tests/fixtures/text_files/sample.txt");
let options = ViewOptions::default();
let text_result = view_file(text_file_path, &options)?;
match &text_result.contents {
FileContents::Text { content, .. } => {
for line_content in &content.line_contents {
assert!(
!line_content.line.ends_with('\n'),
"Line content contains trailing newline: {:?}",
line_content.line
);
}
}
_ => panic!("Expected text content"),
}
let filtered_options = ViewOptions {
max_size: None,
line_from: Some(2),
line_to: Some(4),
};
let filtered_result = view_file(text_file_path, &filtered_options)?;
match &filtered_result.contents {
FileContents::Text { content, .. } => {
assert_eq!(content.line_contents.len(), 3);
for line_content in &content.line_contents {
assert!(
!line_content.line.ends_with('\n'),
"Filtered line contains trailing newline: {:?}",
line_content.line
);
}
}
_ => panic!("Expected text content"),
}
Ok(())
}
#[test]
fn test_size_check_with_line_filters() -> Result<()> {
let file_path = Path::new("tests/fixtures/text_files/sample.txt");
let regular_options = ViewOptions {
max_size: Some(10), line_from: None,
line_to: None,
};
let regular_result = view_file(file_path, ®ular_options);
assert!(regular_result.is_err());
assert!(format!("{:?}", regular_result.unwrap_err()).contains("File is too large"));
let filter_options = ViewOptions {
max_size: Some(10), line_from: Some(1), line_to: Some(1),
};
let filter_result = view_file(file_path, &filter_options);
match filter_result {
Ok(result) => {
match &result.contents {
FileContents::Text { content, .. } => {
assert_eq!(content.line_contents.len(), 1);
assert_eq!(content.line_contents[0].line_number, 1);
}
_ => panic!("Expected text content"),
};
}
Err(e) => {
let err_msg = format!("{:?}", e);
assert!(err_msg.contains("Filtered content is too large"));
}
}
let test_dir = tempfile::tempdir()?;
let test_file_path = test_dir.path().join("tiny_test.txt");
std::fs::write(&test_file_path, "Line1\nLine2\nLine3\n")?;
let tiny_options = ViewOptions {
max_size: Some(6), line_from: Some(1),
line_to: Some(1),
};
let tiny_result = view_file(&test_file_path, &tiny_options)?;
match &tiny_result.contents {
FileContents::Text { content, .. } => {
assert_eq!(content.line_contents.len(), 1);
assert_eq!(content.line_contents[0].line_number, 1);
assert_eq!(content.line_contents[0].line, "Line1");
}
_ => panic!("Expected text content"),
}
let too_small_options = ViewOptions {
max_size: Some(6), line_from: Some(1),
line_to: Some(2), };
let too_small_result = view_file(&test_file_path, &too_small_options);
assert!(too_small_result.is_err());
assert!(
format!("{:?}", too_small_result.unwrap_err()).contains("Filtered content is too large")
);
Ok(())
}