#!perl
use Cassandane::Tiny;

sub test_searchsnippet_get_text_rtf
    :min_version_3_4 :needs_component_jmap :JMAPExtensions
    :SearchAttachmentExtractor
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();
    my $instance = $self->{instance};

    my $uri = URI->new($instance->{config}->get('search_attachment_extractor_url'));

    # Start a dummy extractor server.
    my $handler = sub {
        my ($conn, $req) = @_;
        if ($req->method eq 'HEAD') {
            my $res = HTTP::Response->new(204);
            $res->content("");
            $conn->send_response($res);
        } else {
            my $res = HTTP::Response->new(200);
            $res->header("Keep-Alive" => "timeout=1");  # Force client timeout
            $res->content("This is an RTF attachment with formatting.");
            $conn->send_response($res);
        }
    };
    $instance->start_httpd($handler, $uri->port());

    my $using = [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:mail',
        'urn:ietf:params:jmap:submission',
        'https://cyrusimap.org/ns/jmap/mail',
        'https://cyrusimap.org/ns/jmap/debug',
        'https://cyrusimap.org/ns/jmap/performance',
    ];

    my $rawMessage = <<'EOF';
From: from@local
To: to@local
Subject: test
Date: Mon, 13 Apr 2020 15:34:03 +0200
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary=c4683f7a320d4d20902b000486fbdf9b

--c4683f7a320d4d20902b000486fbdf9b
Content-Type: text/plain

test

--c4683f7a320d4d20902b000486fbdf9b
Content-Disposition: attachment;filename="test.rtf"
Content-Type: text/rtf; name="test.rtf"
Content-Transfer-Encoding: BASE64

e1xydGYxXGFuc2lcZGVmZjNcYWRlZmxhbmcxMDI1CntcZm9udHRibHtcZjBcZnJvbWFuXGZw
cnEyXGZjaGFyc2V0MCBUaW1lcyBOZXcgUm9tYW47fXtcZjFcZnJvbWFuXGZwcnEyXGZjaGFy
c2V0MiBTeW1ib2w7fXtcZjJcZnN3aXNzXGZwcnEyXGZjaGFyc2V0MCBBcmlhbDt9e1xmM1xm
cm9tYW5cZnBycTJcZmNoYXJzZXQwIExpYmVyYXRpb24gU2VyaWZ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dW5sb2NrZWQxXHBnbmRlY1xwZ3dzeG4xMjI0MFxw
Z2hzeG4xNTg0MFxtYXJnbHN4bjExMzRcbWFyZ3JzeG4xMTM0XG1hcmd0c3huMTEzNFxtYXJn
YnN4bjExMzRcZnRuYmpcZnRuc3RhcnQxXGZ0bnJzdGNvbnRcZnRubmFyXGFlbmRkb2NcYWZ0
bnJzdGNvbnRcYWZ0bnN0YXJ0MVxhZnRubnJsYwp7XCpcZnRuc2VwXGNoZnRuc2VwfVxwZ25k
ZWNccGFyZFxwbGFpbiBcczBcd2lkY3RscGFyXGh5cGhwYXIwXGNmMFxrZXJuaW5nMVxkYmNo
XGFmOFxsYW5nZmUyMDUyXGRiY2hcYWY2XGFmczI0XGFsYW5nMTA4MVxsb2NoXGYzXGhpY2hc
YWYzXGZzMjRcbGFuZzEwMzN7XHJ0bGNoIFxsdHJjaFxsb2NoClRoaXMgaXMgYW4gfXtcaVxh
aVxydGxjaCBcbHRyY2hcbG9jaApSVEZ9e1xydGxjaCBcbHRyY2hcbG9jaAogfXtcYlxhYlxy
dGxjaCBcbHRyY2hcbG9jaAphdHRhY2htZW50fXtccnRsY2ggXGx0cmNoXGxvY2gKIHdpdGgg
fXtcdWxcdWxjMFxydGxjaCBcbHRyY2hcbG9jaApmb3JtYXR0aW5nfXtccnRsY2ggXGx0cmNo
XGxvY2gKLn0KXHBhciB9

--c4683f7a320d4d20902b000486fbdf9b--

test
EOF
    $rawMessage =~ s/\r?\n/\r\n/gs;
    $imap->append('INBOX', $rawMessage) || die $@;

    $self->{instance}->run_command({cyrus => 1}, 'squatter');

    my $res = $jmap->CallMethods([
        ['Email/query', {
            filter => {
                text => 'formatting',
            },
            findMatchingParts => JSON::true,
        }, 'R1'],
        ['SearchSnippet/get', {
            '#emailIds' => {
                resultOf => 'R1',
                name => 'Email/query',
                path => '/ids',
            },
            '#filter' => {
                resultOf => 'R1',
                name => 'Email/query',
                path => '/filter',
            },
            '#partIds' => {
                resultOf => 'R1',
                name => 'Email/query',
                path => '/partIds',
            },
        }, 'R2'],
    ], $using);

    $self->assert_str_equals('This is an RTF attachment with <mark>formatting</mark>.',
        $res->[1][1]{list}[0]{preview});
}
