xberg 1.1.5

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
Documentation
//! Regression tests for #830: extraction_timeout_secs silently ignored in single-file paths.

mod helpers;
use helpers::{extract_bytes_document, extract_uri_document};

#[cfg(feature = "tokio-runtime")]
use std::time::Instant;
use xberg::XbergError;
use xberg::core::config::ExtractionConfig;

/// A timeout of 0 seconds should fire immediately, before any real work is done.
/// We use plain-text content so the test doesn't require external binaries (Tesseract, PDF extractor).
#[cfg(feature = "tokio-runtime")]
#[tokio::test]
async fn test_extract_bytes_zero_timeout_returns_timeout_error() {
    let config = ExtractionConfig {
        extraction_timeout_secs: Some(0),
        ..Default::default()
    };

    let content = b"Hello world, this is a plain-text document.";
    let result = extract_bytes_document(content, "text/plain", &config).await;

    match result {
        Err(XbergError::Timeout { limit_ms, .. }) => {
            assert_eq!(limit_ms, 0, "limit_ms should reflect the configured 0-second timeout");
        }
        Ok(_) => {}
        Err(e) => panic!("Expected Ok or Timeout, got: {e:?}"),
    }
}

/// Same check for extract_uri_document.
#[cfg(feature = "tokio-runtime")]
#[tokio::test]
async fn test_extract_file_zero_timeout_returns_timeout_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("hello.txt");
    std::fs::write(&path, b"Hello world").expect("write");

    let config = ExtractionConfig {
        extraction_timeout_secs: Some(0),
        ..Default::default()
    };

    let result = extract_uri_document(&path, None, &config).await;

    match result {
        Err(XbergError::Timeout { limit_ms, .. }) => {
            assert_eq!(limit_ms, 0);
        }
        Ok(_) => {}
        Err(e) => panic!("Expected Ok or Timeout, got: {e:?}"),
    }
}

/// When no timeout is configured, extraction should succeed normally.
#[cfg(feature = "tokio-runtime")]
#[tokio::test]
async fn test_extract_bytes_no_timeout_succeeds() {
    let config = ExtractionConfig::default();
    let content = b"No timeout configured.";
    let result = extract_bytes_document(content, "text/plain", &config).await;
    assert!(result.is_ok(), "extraction without timeout should succeed: {result:?}");
}

/// When no timeout is configured, file extraction should succeed normally.
#[cfg(feature = "tokio-runtime")]
#[tokio::test]
async fn test_extract_file_no_timeout_succeeds() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("test.txt");
    std::fs::write(&path, b"No timeout configured.").expect("write");

    let config = ExtractionConfig::default();
    let result = extract_uri_document(&path, None, &config).await;
    assert!(result.is_ok(), "extraction without timeout should succeed: {result:?}");
}

/// Elapsed time reported in the error must be <= limit_ms for reasonable timeouts.
#[cfg(feature = "tokio-runtime")]
#[tokio::test]
async fn test_extract_bytes_timeout_elapsed_is_plausible() {
    let config = ExtractionConfig {
        extraction_timeout_secs: Some(0),
        ..Default::default()
    };
    let content = b"timing check";
    let start = Instant::now();
    let _ = extract_bytes_document(content, "text/plain", &config).await;
    let wall_ms = start.elapsed().as_millis() as u64;
    assert!(
        wall_ms < 1000,
        "single-file extraction with 0s timeout took too long: {wall_ms}ms"
    );
}

/// When no tokio-runtime is available, setting a timeout should return a Validation error.
#[cfg(not(feature = "tokio-runtime"))]
#[tokio::test]
async fn test_extract_bytes_timeout_without_tokio_returns_validation_error() {
    let config = ExtractionConfig {
        extraction_timeout_secs: Some(5),
        ..Default::default()
    };
    let content = b"testing";
    let result = extract_bytes_document(content, "text/plain", &config).await;
    match result {
        Err(XbergError::Validation { message, .. }) => {
            assert!(message.contains("requires the 'tokio-runtime' feature"));
        }
        other => panic!("Expected Validation error, got {other:?}"),
    }
}

/// When no tokio-runtime is available, setting a timeout should return a Validation error.
#[cfg(not(feature = "tokio-runtime"))]
#[tokio::test]
async fn test_extract_file_timeout_without_tokio_returns_validation_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("test.txt");
    std::fs::write(&file_path, b"testing").unwrap();

    let config = ExtractionConfig {
        extraction_timeout_secs: Some(5),
        ..Default::default()
    };
    let result = extract_uri_document(&file_path, Some("text/plain"), &config).await;
    match result {
        Err(XbergError::Validation { message, .. }) => {
            assert!(message.contains("requires the 'tokio-runtime' feature"));
        }
        other => panic!("Expected Validation error, got {other:?}"),
    }
}