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

test "async_extract_bytes" {
    // Async extract_bytes call on PDF document
    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.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);
}

test "async_extract_bytes_empty_mime" {
    // extract_bytes empty MIME async
    suppress_abort();
    const content_bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "text/plain.txt", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = kreuzberg.extract_bytes_sync(content_bytes, "", "{}") catch {
        try testing.expect(true); // Error occurred as expected
        return;
    };
    _ = _result_json;
}

test "async_extract_bytes_invalid_mime" {
    // extract_bytes unsupported MIME async
    suppress_abort();
    const content_bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "text/plain.txt", std.heap.c_allocator, .unlimited);
    defer std.heap.c_allocator.free(content_bytes);
    const _result_json = kreuzberg.extract_bytes_sync(content_bytes, "application/x-nonexistent", "{}") catch {
        try testing.expect(true); // Error occurred as expected
        return;
    };
    _ = _result_json;
}
