#!perl
use Cassandane::Tiny;

#
# An admin-set /vendor/cmu/cyrus-imapd/accountid on a user's INBOX is the
# ACCOUNTID of every mailbox the user owns, wherever OBJECTID+ reports
# one, and SELECT by identifier accepts it.
#
sub test_objectidplus_accountid_annot
    :AltNamespace :CompactIdsPerUser
    ($self)
{
    my $entry = '/shared/vendor/cmu/cyrus-imapd/accountid';
    # created with compact ids, which only the owner's userid scopes
    $self->{instance}->create_user("other", subdirs => [ 'box' ]);
    my $admintalk = $self->{adminstore}->get_client();
    $admintalk->setacl("user.other.box", "cassandane", "lrs");
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    $admintalk->setmetadata('user.cassandane', $entry, 'u1a48e8e3');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
    $admintalk->setmetadata('user.other', $entry, 'u2b59f9f4');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    my $talk = $self->{store}->get_client();
    my $other = 'Other Users.other.box';

    xlog $self, "CREATE reports the annotated ACCOUNTID";
    $talk->enable('OBJECTID+');
    $talk->create('original');
    my $code = $talk->get_response_code('objectid');
    my %created = split ' ', $code =~ tr/()//dr;
    $self->assert_str_equals('u1a48e8e3', $created{ACCOUNTID});
    my $mailboxid = $created{MAILBOXID};

    xlog $self, "STATUS and LIST report it for our own and other's mailboxes";
    my %list;
    my %handlers = (list => sub ($, $data) { $list{$data->[2]} = $data });
    $talk->_imap_cmd('LIST', 0, \%handlers, '', '*', 'RETURN', ['OBJECTID']);
    $self->assert_str_equals('ok', $talk->get_last_completion_response());
    for my $name ('INBOX', 'original', $other) {
        my $want = $name eq $other ? 'u2b59f9f4' : 'u1a48e8e3';
        my %status = @{$talk->status($name, '(objectid)')->{objectid}};
        $self->assert_str_equals($want, $status{ACCOUNTID});
        my %ext = @{$list{$name}[3]};
        $self->assert_str_equals($want, { @{$ext{OBJECTID}} }->{ACCOUNTID});
    }

    xlog $self, "SELECT reports it";
    $talk->select('INBOX');
    $code = $talk->get_response_code('objectid');
    $self->assert_str_equals('u1a48e8e3',
                             { split ' ', $code =~ tr/()//dr }->{ACCOUNTID});
    $talk->unselect();

    xlog $self, "SELECT by identifier finds a renamed mailbox";
    $talk->rename('original', 'renamed');
    $self->assert_str_equals('ok', $talk->get_last_completion_response());
    for my $ids ("MAILBOXID $mailboxid ACCOUNTID u1a48e8e3",
                 "MAILBOXID $mailboxid")
    {
        $talk->clear_response_code('objectid');
        $talk->select('original', "(OBJECTID ($ids))");
        $self->assert_str_equals('ok', $talk->get_last_completion_response());
        $code = $talk->get_response_code('objectid');
        $self->assert_cmp_deeply(
            { MAILBOXID => $mailboxid, ACCOUNTID => 'u1a48e8e3' },
            { split ' ', $code =~ tr/()//dr });
        $talk->unselect();
    }

    xlog $self, "and another account's renamed mailbox";
    my %boxids = @{$talk->status($other, '(objectid)')->{objectid}};
    $self->assert_does_not_match(qr/^[0-9a-f-]{36}$/, $boxids{MAILBOXID});
    $admintalk->rename('user.other.box', 'user.other.moved');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
    $talk->clear_response_code('objectid');
    $talk->select($other, "(OBJECTID (MAILBOXID $boxids{MAILBOXID} "
                        . "ACCOUNTID u2b59f9f4))");
    $self->assert_str_equals('ok', $talk->get_last_completion_response());
    $code = $talk->get_response_code('objectid');
    $self->assert_cmp_deeply(\%boxids, { split ' ', $code =~ tr/()//dr });
    $talk->unselect();

    xlog $self, "the userid is no longer our ACCOUNTID";
    $talk->select('original',
                  "(OBJECTID (MAILBOXID $mailboxid ACCOUNTID cassandane))");
    $self->assert_str_equals('no', $talk->get_last_completion_response());

    xlog $self, "removing the annotation restores the userid at once";
    $admintalk->setmetadata('user.cassandane', $entry, undef);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
    my %status = @{$talk->status('renamed', '(objectid)')->{objectid}};
    $self->assert_str_equals('cassandane', $status{ACCOUNTID});
}
