diff --git a/crates/zx_document_core/src/epub.rs b/crates/zx_document_core/src/epub.rs index ec01ffb..737121c 100644 --- a/crates/zx_document_core/src/epub.rs +++ b/crates/zx_document_core/src/epub.rs @@ -792,4 +792,42 @@ mod tests { assert_eq!(texts.len(), 1); assert_eq!(texts[0].text, "No HTML tags, just plain text."); } + + // ── Path construction variants ── + + #[test] + fn test_extract_epub_text_with_full_path_chapter() { + // chapter_id already contains "/" → used as-is + let data = epub_with_chapter_text("Path", &[ + ("OEBPS/ch1.xhtml", "

Full path chapter.

"), + ]); + let path = write_temp(&data, "fullpath.epub"); + let texts = extract_epub_chapter_texts(&path).unwrap(); + assert_eq!(texts.len(), 1); + assert_eq!(texts[0].text, "Full path chapter."); + } + + #[test] + fn test_extract_epub_text_with_html_suffix() { + // chapter_id with .html suffix → used as-is + let data = epub_with_chapter_text("Html", &[ + ("OEBPS/ch1.html", "

HTML suffix chapter.

"), + ]); + let path = write_temp(&data, "html_suffix.epub"); + let texts = extract_epub_chapter_texts(&path).unwrap(); + assert_eq!(texts.len(), 1); + assert_eq!(texts[0].text, "HTML suffix chapter."); + } + + #[test] + fn test_extract_epub_text_bare_id_fallback() { + // Simple chapter ID (no /, .html, .xhtml) → OEBPS/{id}.xhtml + let data = epub_with_chapter_text("Bare", &[ + ("OEBPS/ch1.xhtml", "

Bare ID fallback.

"), + ]); + let path = write_temp(&data, "bare_id.epub"); + let texts = extract_epub_chapter_texts(&path).unwrap(); + assert_eq!(texts.len(), 1); + assert_eq!(texts[0].text, "Bare ID fallback."); + } } diff --git a/crates/zx_document_core/src/pdf.rs b/crates/zx_document_core/src/pdf.rs index b825605..5bced7d 100644 --- a/crates/zx_document_core/src/pdf.rs +++ b/crates/zx_document_core/src/pdf.rs @@ -452,4 +452,41 @@ mod tests { assert_eq!(meta.page_count, 1); } } + + // ── Error handling: corrupted / non-PDF files ── + + #[test] + fn test_read_pdf_metadata_garbage_file() { + let data = b"this is not a pdf file at all"; + let path = write_temp(data, "garbage.pdf"); + // Should not panic — may return metadata with page_count=0 or 1 + let meta = read_pdf_metadata(&path).unwrap(); + // file_size should match actual size + assert_eq!(meta.file_size, data.len() as u64); + } + + #[test] + fn test_read_pdf_metadata_empty_file() { + let data = b""; + let path = write_temp(data, "empty.pdf"); + let result = read_pdf_metadata(&path); + assert!(result.is_ok()); + } + + #[test] + fn test_read_pdf_metadata_truncated_pdf() { + let data = b"%PDF-1.4\n1 0 obj\n<< /Type /Catalog"; + let path = write_temp(data, "truncated.pdf"); + let result = read_pdf_metadata(&path); + assert!(result.is_ok()); + } + + #[test] + fn test_extract_pdf_text_garbage_file() { + let data = b"not a pdf"; + let path = write_temp(data, "garbage_txt.pdf"); + let result = extract_pdf_text(&path); + assert!(result.is_ok()); + assert!(result.unwrap().is_empty()); + } }