// 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
const std = @import("std");
const testing = std.testing;
const kreuzberg = @import("kreuzberg");

// Suppress C++ global destructor aborts that break zig's --listen=- IPC
extern "c" fn signal(sig: i32, handler: usize) usize;
var _abort_handler_installed: bool = false;
fn suppress_abort() void {
    if (!_abort_handler_installed) {
        // SIGABRT = 6 on POSIX; SIG_IGN = 1
        _ = signal(6, 1);
        _abort_handler_installed = true;
    }
}

// E2e tests for category: smoke

test "ocr_image_png" {
    // OCR: PNG image extraction with OCR enabled. In WASM this exercises the Uint8Array bridge parameter and Promise await in the generated OcrBackend bridge.
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const content_bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "images/test_hello_world.png", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = try kreuzberg.extract_bytes_sync(content_bytes, "image/png", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("image/png", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 1);
    try testing.expect(
        std.mem.indexOf(u8, result.object.get("content").?.string, "Hello") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "World") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "hello") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "world") != null
    );
}

test "smoke_docx_basic" {
    // Smoke test: DOCX with formatted text
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("docx/fake.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("application/vnd.openxmlformats-officedocument.wordprocessingml.document", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 20);
    try testing.expect(
        std.mem.indexOf(u8, result.object.get("content").?.string, "Lorem") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "ipsum") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "document") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "text") != null
    );
}

test "smoke_html_basic" {
    // Smoke test: HTML table extraction
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("html/simple_table.html", "text/html", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("text/html", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 10);
    try testing.expect(
        std.mem.indexOf(u8, result.object.get("content").?.string, "Sample Data Table") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "Laptop") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "Electronics") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "Product") != null
    );
}

test "smoke_image_png" {
    // Smoke test: PNG image (without OCR, metadata only)
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("images/sample.png", null, "{\"disable_ocr\":true}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("image/png", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
}

test "smoke_json_basic" {
    // Smoke test: JSON file extraction
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("json/simple.json", "application/json", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("application/json", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 5);
}

test "smoke_pdf_basic" {
    // Smoke test: PDF with simple text extraction
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("pdf/fake_memo.pdf", "application/pdf", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("application/pdf", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 50);
    try testing.expect(
        std.mem.indexOf(u8, result.object.get("content").?.string, "May 5, 2023") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "To Whom it May Concern") != null
    );
}

test "smoke_txt_basic" {
    // Smoke test: Plain text file
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("text/report.txt", "text/plain", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("text/plain", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 5);
}

test "smoke_xlsx_basic" {
    // Smoke test: XLSX with basic spreadsheet data including tables
    suppress_abort();
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    const _result_json = try kreuzberg.extract_file_sync("xlsx/stanley_cups.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "{}");
    defer std.heap.c_allocator.free(_result_json);
    var _parsed = try std.json.parseFromSlice(std.json.Value, allocator, _result_json, .{});
    defer _parsed.deinit();
    const result = &_parsed.value;
    try testing.expectEqualStrings("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", std.mem.trim(u8, result.object.get("mime_type").?.string, " \n\r\t"));
    try testing.expect(result.object.get("content").?.string.len >= 100);
    {
        const _jva0 = result.object.get("content").?;
        const _jsa0 = if (_jva0 == .string) _jva0.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva0, .{});
        defer if (_jva0 != .string) std.heap.c_allocator.free(_jsa0);
        try testing.expect(std.mem.indexOf(u8, _jsa0, "Team") != null);
    }
    {
        const _jva1 = result.object.get("content").?;
        const _jsa1 = if (_jva1 == .string) _jva1.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva1, .{});
        defer if (_jva1 != .string) std.heap.c_allocator.free(_jsa1);
        try testing.expect(std.mem.indexOf(u8, _jsa1, "Location") != null);
    }
    {
        const _jva2 = result.object.get("content").?;
        const _jsa2 = if (_jva2 == .string) _jva2.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva2, .{});
        defer if (_jva2 != .string) std.heap.c_allocator.free(_jsa2);
        try testing.expect(std.mem.indexOf(u8, _jsa2, "Stanley Cups") != null);
    }
    {
        const _jva3 = result.object.get("content").?;
        const _jsa3 = if (_jva3 == .string) _jva3.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva3, .{});
        defer if (_jva3 != .string) std.heap.c_allocator.free(_jsa3);
        try testing.expect(std.mem.indexOf(u8, _jsa3, "Blues") != null);
    }
    {
        const _jva4 = result.object.get("content").?;
        const _jsa4 = if (_jva4 == .string) _jva4.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva4, .{});
        defer if (_jva4 != .string) std.heap.c_allocator.free(_jsa4);
        try testing.expect(std.mem.indexOf(u8, _jsa4, "Flyers") != null);
    }
    {
        const _jva5 = result.object.get("content").?;
        const _jsa5 = if (_jva5 == .string) _jva5.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva5, .{});
        defer if (_jva5 != .string) std.heap.c_allocator.free(_jsa5);
        try testing.expect(std.mem.indexOf(u8, _jsa5, "Maple Leafs") != null);
    }
    {
        const _jva6 = result.object.get("content").?;
        const _jsa6 = if (_jva6 == .string) _jva6.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva6, .{});
        defer if (_jva6 != .string) std.heap.c_allocator.free(_jsa6);
        try testing.expect(std.mem.indexOf(u8, _jsa6, "STL") != null);
    }
    {
        const _jva7 = result.object.get("content").?;
        const _jsa7 = if (_jva7 == .string) _jva7.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva7, .{});
        defer if (_jva7 != .string) std.heap.c_allocator.free(_jsa7);
        try testing.expect(std.mem.indexOf(u8, _jsa7, "PHI") != null);
    }
    {
        const _jva8 = result.object.get("content").?;
        const _jsa8 = if (_jva8 == .string) _jva8.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jva8, .{});
        defer if (_jva8 != .string) std.heap.c_allocator.free(_jsa8);
        try testing.expect(std.mem.indexOf(u8, _jsa8, "TOR") != null);
    }
    // skipped: field 'tables' not available on result type
    // skipped: field 'metadata.format.excel.sheet_count' not available on result type
    // skipped: field 'metadata.format.excel.sheet_names' not available on result type
}
