use strict;
use warnings;

use Cwd qw(getcwd);
use Leek::IntegrationTest;

with_server {
    my $response;

    my $cwd = getcwd();
    $response = send_command "LOAD 'lua' FROM '$cwd/target/extension/lua.so'";

    $response = send_command "CREATE JOURNAL 'txs'";
    assert_response_size $response, 1;
    assert_response_part $response, 0, 2, '';

    $response = send_command "APPEND ONTO 'txs' ENTRY '+8'";
    assert_response_size $response, 1;
    assert_response_part $response, 0, 2, '';

    $response = send_command "APPEND ONTO 'txs' ENTRY '+4'";
    assert_response_size $response, 1;
    assert_response_part $response, 0, 2, '';

    $response = send_command "APPEND ONTO 'txs' ENTRY '-5'";
    assert_response_size $response, 1;
    assert_response_part $response, 0, 2, '';

    $response = send_command <<'EOF';
        FOLDL 'txs'
        USING '
            accumulator = 0
            function step(entry)
                accumulator = accumulator + entry
            end
            function finalize()
                return accumulator
            end
        '
        FROM 'lua'
EOF
    assert_response_size $response, 2;
    assert_response_part $response, 0, 0, '7';
    assert_response_part $response, 1, 2, '';
};
