#!perl
use Cassandane::Tiny;

# Expunging a message leaves a hole in the UID sequence, because UIDs are
# never reused, while the sequence numbers of the surviving messages close up
# behind it.  Check that the message store's read path -- the engine behind
# check_messages() -- still finds every message once the two have diverged,
# and that the server renumbers the survivors as it should.
sub test_uid_gaps_after_expunge ($self)
{
    my $store = $self->{store};
    my $talk = $store->get_client();

    $store->set_fetch_attributes(qw(uid));
    $store->_select();

    xlog $self, "Append five messages";
    my %msg;
    $msg{$_} = $self->make_message("Message $_") for 1..5;

    xlog $self, "Expunge the first and the third of them";
    # At this point sequence numbers and UIDs still agree, so this works
    # whichever of the two the client is speaking in.
    $talk->store('1,3', '+flags', '(\\Deleted)');
    $talk->expunge();
    delete $msg{1};
    delete $msg{3};

    xlog $self, "The survivors keep their UIDs but move down in sequence";
    $msg{2}->set_attributes(uid => 2);
    $msg{4}->set_attributes(uid => 4);
    $msg{5}->set_attributes(uid => 5);

    $self->check_messages(\%msg);
    $self->assert_seq_uids($talk, [ 2, 4, 5 ]);
}
