#!perl
use Cassandane::Tiny;

sub test_email_query_header_sieve
    :min_version_3_5 :needs_component_sieve :needs_component_jmap
    :JMAPExtensions :AltNamespace
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();

    $imap->create("matches") or die;

    $self->{instance}->install_sieve_script(<<'EOF'
require ["x-cyrus-jmapquery", "x-cyrus-log", "variables", "fileinto"];
if
  allof( not string :is "${stop}" "Y",
    jmapquery text:
  {
    "header" : [ "subject", "zzz", "endsWith" ]
  }
.
  )
{
  fileinto "matches";
}
EOF
    );

    xlog "Deliver matching message";
    my $msg1 = $self->{gen}->generate(
        subject => 'xxxyyyzzz',
        body => "msg1"
    );
    $self->{instance}->deliver($msg1);

    xlog "Assert that message got moved into INBOX.matches";
    $self->{store}->set_folder('matches');
    $self->check_messages({ 1 => $msg1 }, check_guid => 0);

    xlog $self, "Deliver a non-matching message";
    my $msg2 = $self->{gen}->generate(
        subject => 'zzzyyyyxxx',
        body => "msg2"
    );
    $self->{instance}->deliver($msg2);
    $msg2->set_attribute(uid => 1);

    xlog "Assert that message got moved into INBOX";
    $self->{store}->set_folder('INBOX');
    $self->check_messages({ 1 => $msg2 }, check_guid => 0);
}
