// 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: format_specific

test "format_docx_standalone" {
    // Standalone DOCX extraction using extract_bytes_sync
    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, "docx/fake.docx", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = try kreuzberg.extract_bytes_sync(content_bytes, "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.expect(result.object.get("content").?.string.len >= 20);
}

test "format_hwpx_standalone" {
    // Standalone HWPX extraction using extract_bytes_sync
    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, "hwpx/simple.hwpx", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = try kreuzberg.extract_bytes_sync(content_bytes, "application/haansofthwpx", "{}");
    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.expect(result.object.get("content").?.string.len >= 20);
    {
        const _jv = result.object.get("content").?;
        const _js = if (_jv == .string) _jv.string else try std.json.Stringify.valueAlloc(std.heap.c_allocator, _jv, .{});
        defer if (_jv != .string) std.heap.c_allocator.free(_js);
        try testing.expect(std.mem.indexOf(u8, _js, "Hello from HWPX") != null);
    }
}

test "format_pdf_text" {
    // Standalone PDF text extraction using extract_bytes_sync
    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, "pdf/fake_memo.pdf", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = try kreuzberg.extract_bytes_sync(content_bytes, "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.expect(result.object.get("content").?.string.len >= 50);
    try testing.expect(
        std.mem.indexOf(u8, result.object.get("content").?.string, "Mallori") != null or
        std.mem.indexOf(u8, result.object.get("content").?.string, "May") != null
    );
}

test "format_pptx" {
    // PPTX presentation extraction using extract_file_sync
    suppress_abort();
    const _result_json = try kreuzberg.extract_file_sync("pptx/simple.pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "{}");
    defer std.heap.c_allocator.free(_result_json);
}

test "format_xlsx" {
    // XLSX spreadsheet extraction using extract_file_sync
    suppress_abort();
    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);
}
