#!perl
use Cassandane::Tiny;

sub test_email_query_sieve_8bit_header
    :min_version_3_9 :needs_component_jmap :needs_component_sieve :NoMunge8Bit :RFC2047_UTF8
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();

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

    xlog "Assert that message got moved into INBOX.matches";
    $imap->select('matches');
    $self->assert_num_equals(0, $imap->get_response_code('exists'));
    $imap->unselect();

    # "subject" : "płatność"

use utf8;
    $self->{instance}->install_sieve_script(<<'EOF'
require ["x-cyrus-jmapquery", "x-cyrus-log", "variables", "fileinto"];
if
  allof( not string :is "${stop}" "Y",
    jmapquery text:
  {
    "subject" : "płatność"
  }
.
  )
{
  fileinto "matches";
}
EOF
    );

    my $mime = <<'EOF';
From: from@local
To: to@local
Subject: test płatność
Date: Mon, 13 Apr 2020 15:34:03 +0200
MIME-Version: 1.0
Content-Type: text/plain;charset=us-ascii
Content-Transfer-Encoding: 7bit

hello

EOF
    $mime =~ s/\r?\n/\r\n/gs;
no utf8;

    my $msg = Cassandane::Message->new();
    $msg->set_lines(split /\n/, $mime);
    $self->{instance}->deliver($msg);

    xlog "Assert that message got moved into INBOX.matches";
    $imap->select('matches');
    $self->assert_num_equals(1, $imap->get_response_code('exists'));
    $imap->unselect();
}
