#!perl
use Cassandane::Tiny;

sub _convdb_msgids
{
    my ($self) = @_;

    my $outfile = $self->{instance}->{basedir} . "/convdb-dump.txt";
    $self->{instance}->run_command(
        { cyrus => 1, redirects => { stdout => $outfile } },
        'ctl_conversationsdb', '-d', 'cassandane',
    );

    # dump format is "key\tvalue", and msgid records are the ones keyed
    # by the literal Message-ID, angle brackets and all
    return [ grep { m/^</ } split /\n/, slurp_file($outfile) ];
}

sub _check_cyr_expire_prune
{
    my ($self) = @_;

    # prune everything we find, on age, so we don't have to arrange for
    # the conversations themselves to go away as well
    $self->{instance}->{config}->set('conversations_keep_existing' => 'no');
    $self->{instance}->{config}->set('conversations_expire_after' => '0d');
    $self->_start_instances();

    $self->{store}->get_client();

    xlog $self, "generating some messages";
    $self->make_message("Message $_") for (1..5);

    my $msgids = $self->_convdb_msgids();
    $self->assert_num_equals(5, scalar @{$msgids});

    xlog $self, "pruning with a batchsize smaller than the work to do";
    $self->{instance}->run_command({ cyrus => 1 },
                                   'cyr_expire', '-E', '1', '-b', '2');

    $msgids = $self->_convdb_msgids();
    $self->assert_num_equals(0, scalar @{$msgids});

    xlog $self, "the messages themselves are still there";
    my $talk = $self->{store}->get_client();
    $talk->select('INBOX');
    $self->assert_num_equals(5, $talk->get_response_code('exists'));
}

# cyr_expire prunes the msgid records a -b batchsize at a time, on a
# backend with no fetchnext, so each batch restarts the scan
sub test_cyr_expire_prune
    :NoStartInstances
    ($self)
{
    $self->{instance}->{config}->set('conversations_db' => 'skiplist');
    $self->_check_cyr_expire_prune();
}

# ... and on a backend which can resume an iteration with fetchnext
sub test_cyr_expire_prune_chunked
    :NoStartInstances
    ($self)
{
    $self->{instance}->{config}->set('conversations_db' => 'twom');
    $self->_check_cyr_expire_prune();
}

# with conversations_keep_existing, prune drops the msgid records of
# conversations that are gone, and keeps the rest whatever their age
sub test_cyr_expire_prune_keep_existing
    :NoStartInstances
    ($self)
{
    $self->{instance}->{config}->set('conversations_keep_existing' => 'yes');
    $self->{instance}->{config}->set('conversations_expire_after' => '0d');
    $self->_start_instances();

    my $talk = $self->{store}->get_client();

    xlog $self, "generating some messages, each its own conversation";
    my @msgs = map { $self->make_message("Message $_") } (1..5);
    $self->assert_num_equals(5, scalar @{$self->_convdb_msgids()});

    xlog $self, "expunging the second and fourth";
    $talk->select('INBOX');
    $talk->store('2,4', '+flags', '(\\Deleted)');
    $talk->expunge();
    $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-X', '0');

    $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-E', '1');

    my @kept = sort map { (split /\t/)[0] } @{$self->_convdb_msgids()};
    my @want = sort map { $msgs[$_]->get_header('message-id') } (0, 2, 4);
    $self->assert_cmp_deeply(\@want, \@kept);
}
