#!perl
use Cassandane::Tiny;

sub test_squatter_attachextract_empty_part
    :SearchAttachmentExtractor
    ($self)
{
    my $instance = $self->{instance};
    my $imap = $self->{store}->get_client();

    my $tracedir = tempdir(DIR => $instance->{basedir} . "/tmp");
    $self->start_echo_extractor(tracedir => $tracedir);

    xlog "Append email with a zero-byte attachment";
    $self->make_message("msg1",
        mime_type => "multipart/related",
        mime_boundary => "123456789abcdef",
        body => ""
        ."\r\n--123456789abcdef\r\n"
        ."Content-Type: text/plain\r\n"
        ."\r\n"
        ."bodyterm"
        ."\r\n--123456789abcdef\r\n"
        ."Content-Type: application/pdf\r\n"
        ."\r\n"
        ."\r\n--123456789abcdef--\r\n"
    ) || die;

    my $cachedir = tempdir(DIR => $instance->{basedir} . "/tmp");

    xlog "Run squatter with cachedir $cachedir";
    $self->{instance}->run_command({cyrus => 1},
        'squatter', "--attachextract-cache-dir=$cachedir");

    xlog "Assert text body is indexed";
    my $uids = $imap->search('fuzzy', 'body', 'bodyterm');
    $self->assert_cmp_deeply([1], $uids);

    xlog "Assert extractor never got called";
    my @tracefiles = glob($tracedir."/*");
    $self->assert_num_equals(0, scalar @tracefiles);

    xlog "Assert cache is empty";
    my @files = glob($cachedir."/*");
    $self->assert_num_equals(0, scalar @files);
}
