test: fill DOC coverage gaps - epub path fallback + PDF error handling

- epub: full-path chapter_id, .html suffix, bare ID→OEBPS fallback (3)
- pdf: garbage file, empty file, truncated PDF metadata (3)
- pdf: garbage file text extraction returns empty (1)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
wangdl 2026-06-18 13:47:46 +08:00
parent fd9c157702
commit 433b1ad7c0
2 changed files with 75 additions and 0 deletions

View File

@ -792,4 +792,42 @@ mod tests {
assert_eq!(texts.len(), 1); assert_eq!(texts.len(), 1);
assert_eq!(texts[0].text, "No HTML tags, just plain text."); 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", "<html><p>Full path chapter.</p></html>"),
]);
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><p>HTML suffix chapter.</p></html>"),
]);
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", "<html><p>Bare ID fallback.</p></html>"),
]);
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.");
}
} }

View File

@ -452,4 +452,41 @@ mod tests {
assert_eq!(meta.page_count, 1); 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());
}
} }