#!perl
use Cassandane::Tiny;

sub test_qresync
    :min_version_3_9
{
    my ($self) = @_;

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

    xlog $self, "Deliver a message";
    my $msg = $self->{gen}->generate(subject => "Message 1");
    $self->{instance}->deliver($msg);

    $imaptalk->select("INBOX");
    my $uidvalidity = $imaptalk->get_response_code('uidvalidity');

    xlog $self, "ENABLE QRESYNC";
    my $res = $imaptalk->_imap_cmd('ENABLE', 0, 'enabled', 'QRESYNC');
    $self->assert_num_equals(1, $res->{qresync});

    xlog "QResync mailbox with message sequence map";
    $imaptalk->unselect();
    $imaptalk->select("INBOX", "(QRESYNC ($uidvalidity 0 1 (1 1)))" => 1);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $res = $imaptalk->get_response_code('fetch');
    $self->assert_not_null($res);
    $self->assert(exists $res->{'1'}{'flags'});
    $self->assert(exists $res->{'1'}{'modseq'});
    $self->assert(exists $res->{'1'}{'uid'});

    xlog $self, "ENABLE UIDONLY";
    $res = $imaptalk->_imap_cmd('ENABLE', 0, 'enabled', 'UIDONLY');
    $self->assert_num_equals(1, $res->{uidonly});

    xlog "attempt to QResync mailbox with message sequence map";
    $imaptalk->unselect();
    $imaptalk->select("INBOX", "(QRESYNC ($uidvalidity 0 1 (1 1)))" => 1);
    $self->assert_str_equals('bad', $imaptalk->get_last_completion_response());

    xlog "QResync mailbox";
    my %fetched = $self->uidonly_cmd($imaptalk, 'SELECT',
                                     "INBOX", "(QRESYNC ($uidvalidity 0 1))");
    $self->assert(exists $fetched{'1'});
    # make sure UID isn't in the response
    $self->assert(not exists $fetched{'1'}->{uid});
    $self->assert(exists $fetched{'1'}->{flags});
    $self->assert(exists $fetched{'1'}->{modseq});
}

1;
