#!perl
use Cassandane::Tiny;

#
# Test the cyrus annotations
#
sub test_shared
{
    my ($self) = @_;

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

    xlog $self, "reading read_only Cyrus annotations";
    my $res = $imaptalk->getmetadata('INBOX', {depth => 'infinity'}, '/shared');
    my $r = $res->{INBOX};
    $self->assert_not_null($r);

    xlog $self, "checking specific entries";
    # Note: lastupdate will be a time string close within the
    # last second, but I'm too lazy to check that properly
    $self->assert_not_null($r->{'/shared/vendor/cmu/cyrus-imapd/lastupdate'});
    delete $r->{'/shared/vendor/cmu/cyrus-imapd/lastupdate'};
    # Note: uniqueid will be a hash of some information that
    # we can't entirely predict
    $self->assert_not_null($r->{'/shared/vendor/cmu/cyrus-imapd/uniqueid'});
    delete $r->{'/shared/vendor/cmu/cyrus-imapd/uniqueid'};
    my %specific_entries = (
            '/shared/vendor/cmu/cyrus-imapd/squat' => undef,
            '/shared/vendor/cmu/cyrus-imapd/size' => '0',
            '/shared/vendor/cmu/cyrus-imapd/sieve' => undef,
            '/shared/vendor/cmu/cyrus-imapd/sharedseen' => 'false',
            '/shared/vendor/cmu/cyrus-imapd/pop3showafter' => undef,
            '/shared/vendor/cmu/cyrus-imapd/pop3newuidl' => 'true',
            '/shared/vendor/cmu/cyrus-imapd/partition' => 'default',
            '/shared/vendor/cmu/cyrus-imapd/news2mail' => undef,
            '/shared/vendor/cmu/cyrus-imapd/lastpop' => undef,
            '/shared/vendor/cmu/cyrus-imapd/expire' => undef,
            '/shared/vendor/cmu/cyrus-imapd/duplicatedeliver' => 'false',
            '/shared/vendor/cmu/cyrus-imapd/userrawquota' => undef,
            '/shared/specialuse' => undef,
            '/shared/thread' => undef,
            '/shared/sort' => undef,
            '/shared/specialuse' => undef,
            '/shared/comment' => undef,
            '/shared/checkperiod' => undef,
            '/shared/check' => undef,
            '/shared' => undef,
    );
    # Note: annotsize/synccrcs new in 3.0
    my ($maj, $min, $rev) = Cassandane::Instance->get_version();
    if ($maj >= 3) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/annotsize'} = '0';
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/synccrcs'} = '0 0';
    }
    # We introduced vendor/cmu/cyrus-imapd/{archive,delete} in 3.1.0
    if ($maj > 3 or ($maj == 3 and $min >= 1)) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/archive'} = undef;
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/delete'} = undef;
    }
    # We introduced vendor/cmu/cyrus-imapd/sortorder in 3.1.3
    if ($maj > 3 or ($maj == 3 and ($min > 1 or ($min == 1 and $rev >= 3)))) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/sortorder'} = undef;
    }
    # synccrcs got a new default in 3.1.7, and hasalarms got added
    # XXX Not sure how useful it is to keep subdividing our 3.1 tests, we
    # XXX expect this unstable series to be a moving target.  Once 3.2 forks
    # XXX I think we def should collapse all these 3.1s into a single 3.1
    if ($maj > 3 or ($maj == 3 and ($min > 1 or ($min == 1 and $rev >= 7)))) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/synccrcs'} =
            '0 12345678';
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/hasalarms'} = 'false';
    }
    # foldermodsseq was added in 3.2.0
    if ($maj > 3 or ($maj == 3 and ($min > 1))) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/foldermodseq'} = 4;
    }
    # We introduced vendor/cmu/cyrus-imapd/search-fuzzy-always in 3.3.0
    if ($maj > 3 or ($maj == 3 and $min >= 3)) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/search-fuzzy-always'} = undef;
    }

    # We introduced vendor/cmu/cyrus-imapd/noexpire_until in 3.9.0
    if ($maj > 3 or ($maj == 3 and $min >= 9)) {
        $specific_entries{'/shared/vendor/cmu/cyrus-imapd/noexpire_until'} = undef;
    }

    $self->assert_deep_equals(\%specific_entries, $r);

    # individual item fetch:
    my $part = $imaptalk->getmetadata('INBOX', "/shared/vendor/cmu/cyrus-imapd/partition");
    $self->assert_str_equals('default', $part->{INBOX}{"/shared/vendor/cmu/cyrus-imapd/partition"});

    # duplicate deliver should be false
    $self->assert_str_equals('false', $res->{INBOX}{"/shared/vendor/cmu/cyrus-imapd/duplicatedeliver"});

    # set duplicate deliver (as admin)
    my $admintalk = $self->{adminstore}->get_client();
    $admintalk->setmetadata('user.cassandane', "/shared/vendor/cmu/cyrus-imapd/duplicatedeliver", 'true');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    # and make sure the change sticks
    my $dup = $imaptalk->getmetadata('INBOX', "/shared/vendor/cmu/cyrus-imapd/duplicatedeliver");
    $self->assert_str_equals('true', $dup->{INBOX}{"/shared/vendor/cmu/cyrus-imapd/duplicatedeliver"});
}
