#!perl
use Cassandane::Tiny;

sub test_annotation_selected
    :needs_component_idled
    ($self)
{
    $self->{instance}->add_start(name => 'idled', argv => [ 'idled' ]);
    $self->{instance}->start();

    my $svc = $self->{instance}->get_service('imap');
    my $store = $svc->create_store();
    my $talk = $store->get_client();
    my $othertalk = $svc->create_store()->get_client();

    $othertalk->append('INBOX', "()",
                       $self->{gen}->generate(subject => "A")->as_string())
        or die "append: $@";

    $talk->select('INBOX');
    $talk->_imap_cmd('NOTIFY', 0, "", "SET",
                     "(SELECTED (MessageNew MessageExpunge AnnotationChange))");
    $self->assert_str_equals('ok', $talk->get_last_completion_response());

    my @got;
    my %handlers = (fetch => sub { push @got, $_[1] });

    $othertalk->select('INBOX');
    $othertalk->store('1', 'annotation',
                      ['/comment', ['value.shared', 'hello world']]);
    $self->assert_str_equals('ok', $othertalk->get_last_completion_response());

    # RFC 5465 5.1: an unsolicited FETCH with UID and ANNOTATION
    $self->assert($store->idle_response(\%handlers, 10), "got FETCH");
    $self->assert_cmp_deeply(
        [ superhashof({ uid => 1, annotation => { '/comment' => undef } }) ],
        \@got);
}
