#!perl
use Cassandane::Tiny;

sub test_one_doc_per_message
    :SearchEngineSquat :min_version_3_4
    ($self)
{
    my $imap = $self->{store}->get_client();

    # make some messages where the only indexed field is the body
    foreach my $body (qw(term1 term1 term2 term4)) {
        $self->make_message(undef,
                            from => undef,
                            to => undef,
                            body => $body) || die;
    }

    # make enough other messages such that an incremental reindex
    # will need to realloc doc_ID_map
    for (1..50) {
        $self->make_message() || die;
    }

    $self->run_squatter();

    my @tests = ({
        search => ['body', 'term1'],
        wantUids => [1,2],
    }, {
        search => ['body', 'term2'],
        wantUids => [3],
    }, {
        search => ['body', 'term3'],
        wantUids => [],
    }, {
        search => ['body', 'term4'],
        wantUids => [4],
    });

    foreach (@tests) {
        $self->{instance}->getsyslog();

        my $uids = $imap->search(@{$_->{search}}) || die;
        $self->assert_deep_equals($_->{wantUids}, $uids);

        $self->assert_syslog_matches($self->{instance}, qr/Squat run/);
    }

    # make some more messages
    foreach my $body (qw(term5 term6 term6 term8)) {
        $self->make_message(undef,
                            from => undef,
                            to => undef,
                            body => $body) || die;
    }

    # incremental reindex
    my (undef, $err) = $self->run_squatter('-i', '-v');
    $self->assert_matches(qr{indexed 4 messages}, $err);

    push @tests, {
        search => ['body', 'term5'],
        wantUids => [55],
    }, {
        search => ['body', 'term6'],
        wantUids => [56, 57],
    }, {
        search => ['body', 'term7'],
        wantUids => [],
    }, {
        search => ['body', 'term8'],
        wantUids => [58],
    };

    # better not be any off-by-one errors in search results!
    foreach (@tests) {
        $self->{instance}->getsyslog();

        my $uids = $imap->search(@{$_->{search}}) || die;
        $self->assert_deep_equals($_->{wantUids}, $uids);

        $self->assert_syslog_matches($self->{instance}, qr/Squat run/);
    }
}
