#!perl
use Cassandane::Tiny;

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

    xlog $self, "test setting X-ANNOTATION-STORAGE quota resource after";
    xlog $self, "per-mailbox annotations are added";

    $self->_set_quotaroot('user.cassandane');
    my $talk = $self->{store}->get_client();

    xlog $self, "set ourselves a basic limit";
    $self->_set_limits($self->res_annot_storage => 100000);
    $self->_check_usages($self->res_annot_storage => 0);

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

    xlog $self, "store annotations";
    my %expecteds = ();
    my $expected = 0;
    foreach my $folder ("INBOX", "INBOX.sub")
    {
        $expecteds{$folder} = 0;
        $self->{store}->set_folder($folder);
        my $data = '';
        while ($expecteds{$folder} <= 60*1024)
        {
            my $moredata = $self->make_random_data(5);
            $data .= $moredata;
            $talk->setmetadata($self->{store}->{folder}, '/private/comment', { Quote => $data });
            $self->assert_str_equals('ok', $talk->get_last_completion_response());
            $expecteds{$folder} += length($moredata);
            $expected += length($moredata);
            xlog $self, "EXPECTING $expected on $folder";
            $self->_check_usages($self->res_annot_storage => int($expected/1024));
        }
    }

    # delete subfolder
    xlog $self, "Deleting a folder";
    $talk->delete("INBOX.sub") || die "Failed to delete subfolder";
    $expected -= delete($expecteds{"INBOX.sub"});
    $self->_check_usages($self->res_annot_storage => int($expected/1024));

    # delete remaining annotations
    $self->{store}->set_folder("INBOX");
    $talk->setmetadata($self->{store}->{folder}, '/private/comment', undef);
    $self->assert_str_equals('ok', $talk->get_last_completion_response());
    $expected -= delete($expecteds{"INBOX"});
    $self->assert_num_equals(0, $expected);
    $self->_check_usages($self->res_annot_storage => int($expected/1024));
}
