LD-Converter/src-tauri/tests/office_engine_tests.rs

99 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Yeni ofis motoru: zengin AST, DOCX/HTML/PDF çıktıları.
use std::fs;
use std::path::{Path, PathBuf};
use app_lib::services::office_model::Block;
use app_lib::services::OfficeEngine;
fn fixture() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures_bicim.docx")
}
fn temp_dir(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("ld_office_{}_{}", name, uuid::Uuid::new_v4()));
fs::create_dir_all(&dir).unwrap();
dir
}
fn document_xml(path: &Path) -> String {
let file = fs::File::open(path).unwrap();
let mut archive = zip::ZipArchive::new(file).unwrap();
let mut xml = String::new();
{
use std::io::Read;
archive.by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap();
}
xml
}
#[test]
fn docx_reader_captures_structure() {
let document = app_lib::services::office_docx::read_docx(&fixture()).expect("DOCX okunamadı");
let mut bold = false;
let mut italic = false;
let mut heading = false;
let mut cells = 0usize;
for block in &document.blocks {
match block {
Block::Paragraph(paragraph) => {
heading |= paragraph.style.heading.is_some();
for run in &paragraph.runs {
bold |= run.bold;
italic |= run.italic;
}
}
Block::Table(table) => {
cells += table.rows.iter().map(|row| row.len()).sum::<usize>();
}
_ => {}
}
}
assert!(heading, "başlık algılanmadı");
assert!(bold, "kalın run algılanmadı");
assert!(italic, "italik run algılanmadı");
assert_eq!(cells, 4, "tablo hücreleri okunamadı");
}
#[test]
fn docx_to_docx_keeps_formatting() {
let dir = temp_dir("docx");
let output = OfficeEngine::convert(&fixture().to_string_lossy(), "docx", &dir)
.expect("DOCX -> DOCX başarısız");
let xml = document_xml(&output);
assert!(xml.contains("<w:b/>"), "kalın korunmadı");
assert!(xml.contains("<w:i/>"), "italik korunmadı");
assert!(xml.contains("Heading1"), "başlık stili korunmadı");
assert!(xml.contains("<w:tbl>"), "tablo korunmadı");
let _ = fs::remove_dir_all(dir);
}
#[test]
fn docx_to_pdf_is_vector_and_searchable() {
let dir = temp_dir("pdf");
let output = OfficeEngine::convert(&fixture().to_string_lossy(), "pdf", &dir)
.expect("DOCX -> PDF başarısız");
let bytes = fs::read(&output).unwrap();
assert!(bytes.starts_with(b"%PDF"));
let text = app_lib::services::pdf_text::extract_pages(&output.to_string_lossy())
.expect("PDF metni çıkarılamadı")
.join("\n");
eprintln!("PDF_TEXT_LEN={}", text.len());
assert!(text.contains("12345"), "metin katmanı yok");
let _ = fs::remove_dir_all(dir);
}
#[test]
fn docx_to_html_keeps_semantics() {
let dir = temp_dir("html");
let output = OfficeEngine::convert(&fixture().to_string_lossy(), "html", &dir)
.expect("DOCX -> HTML başarısız");
let html = fs::read_to_string(&output).unwrap();
assert!(html.contains("<h1>"), "başlık HTML'e girmedi");
assert!(html.contains("<strong>"), "kalın HTML'e girmedi");
assert!(html.contains("<table>"), "tablo HTML'e girmedi");
let _ = fs::remove_dir_all(dir);
}