#!perl
use Cassandane::Tiny;

sub test_message
    :needs_component_idled :Conversations
    ($self)
{
    xlog $self, "Message test of the NOTIFY command (idled required)";

    $self->{instance}->{config}->set(imapidlepoll => '2');
    $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 $otherstore = $svc->create_store();
    my $othertalk = $otherstore->get_client();

    xlog $self, "The server should report the NOTIFY capability";
    $self->assert($talk->capability()->{notify});

    xlog $self, "Create two mailboxes";
    $talk->create("INBOX.foo");
    $talk->create("INBOX.bar");

    xlog $self, "Deliver a message";
    my $msg = $self->{gen}->generate(subject => "Message 1");
    $self->{instance}->deliver($msg);

    xlog $self, "Examine INBOX.foo";
    $talk->examine("INBOX.foo");

    xlog $self, "Enable Notify";
    my $res = $talk->_imap_cmd('NOTIFY', 0, 'STATUS', 'SET', 'STATUS',
                               "(SELECTED (MessageNew" .
                               " (UID EMAILID MAILBOXIDS BODY.PEEK[HEADER.FIELDS (From Subject)])" .
                               " MessageExpunge FlagChange))",
                               "(PERSONAL (MessageNew MessageExpunge))");

    # Should get STATUS responses for unselected mailboxes
    my $status = $talk->get_response_code('status');
    $self->assert_num_equals(1, $status->{'INBOX'}{messages});
    $self->assert_num_equals(2, $status->{'INBOX'}{uidnext});
    $self->assert_num_equals(0, $status->{'INBOX.bar'}{messages});
    $self->assert_num_equals(1, $status->{'INBOX.bar'}{uidnext});

    xlog $self, "Deliver a message";
    $msg = $self->{gen}->generate(subject => "Message 2");
    $self->{instance}->deliver($msg);

    # Should get STATUS response for INBOX
    $res = $store->idle_response('STATUS', 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no more unsolicited responses");

    $status = $talk->get_response_code('status');
    $self->assert_num_equals(2, $status->{'INBOX'}{messages});
    $self->assert_num_equals(3, $status->{'INBOX'}{uidnext});

    xlog $self, "EXPUNGE message from INBOX in other session";
    $othertalk->select("INBOX");
    $res = $othertalk->store('1', '+flags', '(\\Deleted)');
    $res = $othertalk->expunge();

    # Should get STATUS response for INBOX
    $res = $store->idle_response('STATUS', 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no more unsolicited responses");

    $status = $talk->get_response_code('status');
    $self->assert_num_equals(1, $status->{'INBOX'}{messages});
    $self->assert_num_equals(3, $status->{'INBOX'}{uidnext});

    xlog $self, "Select INBOX";
    $talk->examine("INBOX");

    xlog $self, "Deliver a message";
    $msg = $self->{gen}->generate(subject => "Message 3");
    $self->{instance}->deliver($msg);

    # Should get EXISTS, RECENT, FETCH response
    $res = $store->idle_response({}, 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response('FETCH', 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no more unsolicited responses");

    $self->assert_num_equals(2, $talk->get_response_code('exists'));
    $self->assert_num_equals(1, $talk->get_response_code('recent'));

    my $fetch = $talk->get_response_code('fetch');
    $self->assert_num_equals(3, $fetch->{2}{uid});
    $self->assert_str_equals('Message 3', $fetch->{2}{headers}{subject}[0]);
    $self->assert_not_null($fetch->{2}{headers}{from});
    $self->assert_not_null($fetch->{2}{emailid}[0]);
    $self->assert_not_null($fetch->{2}{mailboxids}[0]);

    xlog $self, "DELETE message from INBOX in other session";
    $res = $othertalk->store('1', '+flags', '(\\Deleted)');

    # Should get FETCH response for INBOX
    $res = $store->idle_response('FETCH', 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no more unsolicited responses");

    $fetch = $talk->get_response_code('fetch');
    $self->assert_num_equals(2, $fetch->{1}{uid});
    $self->assert_str_equals('\\Deleted', $fetch->{1}{flags}[0]);

    xlog $self, "EXPUNGE message from INBOX in other session";
    $res = $othertalk->expunge();

    # Should get EXPUNGE response for INBOX
    $res = $store->idle_response('EXPUNGE', 3);
    $self->assert($res, "received an unsolicited response");
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no more unsolicited responses");

    $self->assert_num_equals(1, $talk->get_response_code('expunge'));

    xlog $self, "Disable Notify";
    $res = $talk->_imap_cmd('NOTIFY', 0, "", "NONE");

    xlog $self, "Deliver a message";
    $msg = $self->{gen}->generate(subject => "Message 4");
    $self->{instance}->deliver($msg);

    # Should get no unsolicited responses
    $res = $store->idle_response({}, 1);
    $self->assert(!$res, "no unsolicited responses");

    # make sure that the connection is ended so that imapd reset happens
    $talk->logout();
    undef $talk;

    # we enabled NOTIFY, so we should see it in client behaviors
    my $pat = qr/session ended.*notify=<1>/;
    $self->assert_syslog_matches($self->{instance}, $pat);
}
