#!perl
use Cassandane::Tiny;

sub test_implicit_keep_target_none
    :min_version_3_9 :needs_component_sieve :NoAltNameSpace
{
    my ($self) = @_;

    my $folder = "INBOX.foo";

    xlog $self, "Create folder";
    my $imaptalk = $self->{store}->get_client();
    $imaptalk->create($folder)
        or die "Cannot create $folder: $@";

    xlog $self, "Allow plus address delivery";
    $imaptalk->setacl($folder, 'anyone' => 'p');
    my $imaptalk = $self->{store}->get_client();

    xlog $self, "Install a script";
    $self->{instance}->install_sieve_script(<<EOF
require ["vnd.cyrus.implicit_keep_target", "imap4flags"];
if header :comparator "i;ascii-casemap" :is "Subject" "explicit" {
    addflag "\\\\Flagged";
    keep;
}
EOF
    );

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

    xlog $self, "Check that the message made it to INBOX";
    $self->{store}->set_folder('INBOX');
    $self->{store}->set_fetch_attributes(qw(uid flags));
    $msg->set_attribute(uid => 1);
    $msg->set_attribute(flags => [ '\\Recent' ]);
    $self->check_messages({ 1 => $msg }, check_guid => 0);

    xlog $self, "Deliver a message to plus address";
    $msg = $self->{gen}->generate(subject => "Explicit");
    $self->{instance}->deliver($msg, user => "cassandane+foo");

    xlog $self, "Check that the message made it to $folder";
    $self->{store}->set_folder($folder);
    $msg->set_attribute(uid => 1);
    $msg->set_attribute(flags => [ '\\Recent', '\\Flagged']);
    $self->check_messages({ 1 => $msg }, check_guid => 0);
}

