#!perl
use Cassandane::Tiny;

sub test_storage_convquota_immediate
    :min_version_3_3 :Conversations :ConversationsQuota :ImmediateDelete
{
    my ($self) = @_;

    xlog $self, "test increasing usage of the STORAGE quota resource as messages are added";
    $self->_set_quotaroot('user.cassandane');
    xlog $self, "set ourselves a basic limit";
    $self->_set_limits(storage => 100000);
    $self->_check_usages(storage => 0);
    my $talk = $self->{store}->get_client();

    my $KEY = "/shared/vendor/cmu/cyrus-imapd/userrawquota";

    $talk->create("INBOX.sub") || die "Failed to create subfolder";

    # append some messages
    $self->{store}->set_folder("INBOX");
    my $msg = $self->make_message("Message 1",
                                  extra_lines => 10 + rand(5000));
    my $size1 = length($msg->as_string());

    $self->{store}->set_folder("INBOX.sub");
    my $msg2 = $self->make_message("Message 2",
                                  extra_lines => 10 + rand(5000));
    my $size2 = length($msg2->as_string());

    my $data1 = $talk->getmetadata("INBOX", $KEY);
    my ($rawusage1) = $data1->{'INBOX'}{$KEY} =~ m/STORAGE (\d+)/;

    $self->_check_usages(storage => int(($size1+$size2)/1024));
    $self->assert_num_equals(int(($size1+$size2)/1024), $rawusage1);

    $talk->select("INBOX");
    $talk->copy("1", "INBOX.sub");

    my $data2 = $talk->getmetadata("INBOX", $KEY);
    my ($rawusage2) = $data2->{'INBOX'}{$KEY} =~ m/STORAGE (\d+)/;

    # quota usage hasn't changed, because we don't get double-charged
    $self->_check_usages(storage => int(($size1+$size2)/1024));
    # but raw usage has gone up by another copy of message 1
    $self->assert_num_equals(int(($size1+$size2+$size1)/1024), $rawusage2);

    $talk->delete("INBOX.sub");

    my $data3 = $talk->getmetadata("INBOX", $KEY);
    my ($rawusage3) = $data3->{'INBOX'}{$KEY} =~ m/STORAGE (\d+)/;

    # we just lost all copies of message2
    $self->_check_usages(storage => int($size1/1024));
    # and also the second copy of message1, so just size1 left
    $self->assert_num_equals(int($size1/1024), $rawusage3);
}
