#!perl
use Cassandane::Tiny;

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

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

--6c3338934661485f87537c19b5f9d933
Content-Type: text/plain

body

--6c3338934661485f87537c19b5f9d933
Content-Type: message/rfc822

From: <attachedfrom@local>
To: attachedto@local
Reply-To: attachedreplyto@local
Subject: attachedtest
Date: Mon, 13 Apr 2020 15:34:03 +0200
MIME-Version: 1.0
Content-Type: text/plain

attachedbody

--6c3338934661485f87537c19b5f9d933--
EOF
    $rawMessage =~ s/\r?\n/\r\n/gs;
    $imap->append('INBOX', $rawMessage) || die $@;
    my $res = $jmap->CallMethods([
        ['Email/query', {
        }, 'R1'],
        ['Email/get', {
            '#ids' => {
                resultOf => 'R1',
                name => 'Email/query',
                path => '/ids'
            },
            properties => ['bodyStructure'],
        }, 'R2'],
    ]);
    my $emailId = $res->[0][1]{ids}[0];
    $self->assert_not_null($emailId);

    my $blobId = $res->[1][1]{list}[0]{bodyStructure}{subParts}[1]{blobId};
    $self->assert_not_null($blobId);

    $res = $jmap->CallMethods([
        ['Email/parse', {
            blobIds => [$blobId],
            properties => ['from', 'replyTo'],
        }, 'R1'],
    ]);
    $self->assert_str_equals('attachedfrom@local',
        $res->[0][1]{parsed}{$blobId}{from}[0]{email});
    $self->assert_str_equals('attachedreplyto@local',
        $res->[0][1]{parsed}{$blobId}{replyTo}[0]{email});
}
