#!perl
use Cassandane::Tiny;

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

    xlog $self, "testing if we could /private/specialuse twice on a folder";

    my $imaptalk = $self->{store}->get_client();
    my $entry = '/private/specialuse';
    my $sentry = '/shared/specialuse';
    my $folder1 = 'INBOX.bar';
    my $folder2 = 'INBOX.foo';
    my $specialuse1 = '\Sent \Trash';
    my $specialuse2 = '\Sent \Trash \Junk';
    my $specialuse3 = '\Sent';
    my $specialuse4 = '\Drafts';
    my $specialuse5 = '\Junk \Archive';
    my $specialuse6 = '\Drafts \Archive';
    my $res;

    xlog $self, "Create a folder $folder1";
    $imaptalk->create($folder1)
        or die "Cannot create mailbox $folder1: $@";

    xlog $self, "Create a folder $folder2";
    $imaptalk->create($folder2)
        or die "Cannot create mailbox $folder2: $@";

    $res = $imaptalk->getmetadata($folder1, $entry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    delete $res->{$sentry};
    $self->assert_deep_equals({
        $folder1 => { $entry => undef }
        }, $res);


    xlog $self, "Set $folder1 to be $specialuse1";
    $imaptalk->setmetadata($folder1, $entry, $specialuse1);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder2 to be $specialuse4";
    $imaptalk->setmetadata($folder2, $entry, $specialuse4);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse2, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse2);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse1, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse1);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse3, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse2);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse1, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse1);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse4, and it should not work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse4);
    $self->assert_str_equals('no', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder1 to $specialuse5, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse5);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Set $folder2 to be $specialuse1";
    $imaptalk->setmetadata($folder2, $entry, $specialuse1);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());

    xlog $self, "Now set $folder1 to $specialuse6, and it should work.";
    $imaptalk->setmetadata($folder1, $entry, $specialuse6);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
}
