// This file is auto-generated by alef — DO NOT EDIT.
// alef:hash:4e15143f4af1ae8bafbdb1506ef057da924484c66a19483966333558ad437e75
// To regenerate: alef generate
// To verify freshness: alef verify --exit-code
// Issues & docs: https://github.com/kreuzberg-dev/alef
package dev.kreuzberg.e2e

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertFailsWith
import kotlinx.coroutines.runBlocking
import dev.kreuzberg.Kreuzberg
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import dev.kreuzberg.ExtractionConfig

/** E2e tests for category: contract. */
class ContractTest {

    companion object {
        private val MAPPER = ObjectMapper().registerModule(Jdk8Module()).registerKotlinModule().setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE)
    }
    @Test
    fun testApiBatchBytesAsync() = runBlocking {
        // Tests async batch bytes extraction API (batch_extract_bytes)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, ExtractionConfig())
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        assertTrue(result.content.contains("May 5, 2023") || result.content.contains("Mallori"), "expected to contain at least one of the specified values")
    }

    @Test
    fun testApiBatchBytesWithConfigsAsync() = runBlocking {
        // Tests async batch bytes extraction with per-file configs (batch_extract_bytes with file_configs parameter)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, "{\"output_format\":\"markdown\"}")
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'metadata.output_format' not available on result type
    }

    @Test
    fun testApiBatchFileAsync() = runBlocking {
        // Tests async batch file extraction API (batch_extract_file)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, ExtractionConfig())
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        assertTrue(result.content.contains("May 5, 2023") || result.content.contains("Mallori"), "expected to contain at least one of the specified values")
    }

    @Test
    fun testApiBatchFileWithConfigsAsync() = runBlocking {
        // Tests async batch file extraction with per-file configs (batch_extract_files with file_configs parameter)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, "{\"output_format\":\"markdown\"}")
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'metadata.output_format' not available on result type
    }

    @Test
    fun testApiExtractBytesAsync() = runBlocking {
        // Tests async bytes extraction API (extract_bytes)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, ExtractionConfig())
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        assertTrue(result.content.contains("May 5, 2023") || result.content.contains("Mallori"), "expected to contain at least one of the specified values")
    }

    @Test
    fun testApiExtractFileAsync() = runBlocking {
        // Tests async file extraction API (extract_file)
        val result = Kreuzberg.extractFile("pdf/fake_memo.pdf", null, ExtractionConfig())
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        assertTrue(result.content.contains("May 5, 2023") || result.content.contains("Mallori"), "expected to contain at least one of the specified values")
    }

    @Test
    fun testConfigChunkingPrependHeadingContext() = runBlocking {
        // Tests markdown chunker prepends heading hierarchy to chunk content
        val config = MAPPER.readValue("{\"chunking\":{\"chunker_type\":\"markdown\",\"max_chars\":300,\"max_overlap\":50,\"prepend_heading_context\":true}}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("markdown/extraction_test.md", null, config)
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'chunks' not available on result type
        // skipped: field 'chunks_have_content' not available on result type
        // skipped: field 'chunks_have_heading_context' not available on result type
        // skipped: field 'first_chunk_starts_with_heading' not available on result type
    }

    @Test
    fun testConfigDocumentStructureWithHeadings() = runBlocking {
        // Tests document structure with DOCX heading-driven nesting
        val config = MAPPER.readValue("{\"include_document_structure\":true}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("docx/fake.docx", null, config)
        assertEquals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", result.mimeType.trim())
        // skipped: field 'document' not available on result type
        // skipped: field 'document.nodes' not available on result type
    }

    @Test
    fun testConfigElementTypes() = runBlocking {
        // Tests element-based result format with element type assertions on DOCX
        val config = MAPPER.readValue("{\"result_format\":\"element_based\"}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("docx/unit_test_headers.docx", null, config)
        assertTrue(result.mimeType.contains("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), "expected to contain at least one of the specified values")
        // skipped: field 'elements' not available on result type
    }

    @Test
    fun testConfigExtractionTimeout() = runBlocking {
        // Tests that extraction_timeout_secs config field is accepted and does not affect fast extractions
        val config = MAPPER.readValue("{\"extraction_timeout_secs\":300}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("pdf/fake_memo.pdf", null, config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
    }

    @Test
    fun testConfigKeywords() = runBlocking {
        // Tests keyword extraction via YAKE algorithm
        val config = MAPPER.readValue("{\"keywords\":{\"algorithm\":\"yake\",\"max_keywords\":10}}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("pdf/fake_memo.pdf", null, config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'keywords' not available on result type
        // skipped: field 'keywords' not available on result type
    }

    @Test
    fun testConfigPages() = runBlocking {
        // Tests page extraction and page marker configuration
        val config = MAPPER.readValue("{\"pages\":{\"extract_pages\":true,\"insert_page_markers\":true}}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("pdf/fake_memo.pdf", null, config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        assertTrue(result.content.contains("PAGE"), "expected to contain at least one of the specified values")
    }

    @Test
    fun testConfigQualityEnabled() = runBlocking {
        // Tests quality scoring produces a score value in [0.0, 1.0]
        val config = MAPPER.readValue("{\"enable_quality_processing\":true}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("pdf/fake_memo.pdf", null, config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'quality_score' not available on result type
        // skipped: field 'quality_score' not available on result type
        // skipped: field 'quality_score' not available on result type
    }

    @Test
    fun testConfigSecurityLimits() = runBlocking {
        // Tests archive extraction with custom security limits
        val config = MAPPER.readValue("{\"security_limits\":{\"max_archive_size\":104857600,\"max_compression_ratio\":50,\"max_files_in_archive\":100}}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("archives/documents.zip", null, config)
        assertTrue(result.mimeType.contains("application/zip") || result.mimeType.contains("application/x-zip-compressed"), "expected to contain at least one of the specified values")
        assertTrue(result.content.length >= 10, "expected length >= 10")
    }

    @Test
    fun testConfigTreeSitter() = runBlocking {
        // Tests tree-sitter configuration round-trip
        val config = MAPPER.readValue("{\"tree_sitter\":{\"groups\":[\"web\"],\"languages\":[\"python\",\"rust\"],\"process\":{\"comments\":false,\"diagnostics\":false,\"docstrings\":false,\"exports\":true,\"imports\":true,\"structure\":true,\"symbols\":false}}}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("code/hello.py", null, config)
        assertEquals("text/x-source-code", result.mimeType.trim())
        assertTrue(result.content.length >= 5, "expected length >= 5")
    }

    @Test
    fun testOutputFormatBytesMarkdown() = runBlocking {
        // Tests markdown output format via bytes extraction API
        val config = MAPPER.readValue("{\"output_format\":\"markdown\"}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractBytesSync("pdf/fake_memo.pdf", "application/pdf", config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'metadata.output_format' not available on result type
    }

    @Test
    fun testOutputFormatMarkdown() = runBlocking {
        // Tests Markdown output format
        val config = MAPPER.readValue("{\"output_format\":\"markdown\"}", ExtractionConfig::class.java)
        val result = Kreuzberg.extractFileSync("pdf/fake_memo.pdf", null, config)
        assertEquals("application/pdf", result.mimeType.trim())
        assertTrue(result.content.length >= 10, "expected length >= 10")
        // skipped: field 'metadata.output_format' not available on result type
    }

}
