#!perl
use Cassandane::Tiny;

# X-ME-Memo-For alone is enough to put a memo in its parent's thread:
# neither In-Reply-To nor a matching Subject is needed.
sub test_email_set_create_memo_for_only ($self)
{
  my $jmap = $self->{jmap};

  $self->make_message('Email A', messageid => "msg1\@example.com");

  my $res = $jmap->CallMethods([
    [ 'Email/query', {}, 'R1' ],
    [
      'Email/get',
      {
        '#ids' => {
          resultOf => 'R1',
          name     => 'Email/query',
          path     => '/ids'
        },
        properties => [ 'threadId', 'blobId' ],
      },
      'R2'
    ],
  ]);
  my $parent = $res->[1][1]{list}[0];
  $self->assert_not_null($parent->{threadId});

  xlog $self, "Create memo with only X-ME-Memo-For linking it to the parent";
  $res = $jmap->CallMethods([
    [
      'Email/set',
      {
        create => {
          memo1 => {
            mailboxIds             => { '$inbox' => JSON::true },
            from                   => [ { email => 'from@local' } ],
            subject                => 'unrelated subject',
            messageId              => ["memo1\@example.com"],
            'header:X-ME-Memo-For' => $parent->{blobId},
            bodyStructure          => {
              type   => 'text/plain',
              partId => 'part1',
            },
            bodyValues => { part1 => { value => "memo" } },
            keywords   => { '$memo' => JSON::true },
          },
        },
      },
      'R1'
    ],
  ]);

  $self->assert_str_equals(
    $parent->{threadId},
    $res->[0][1]{created}{memo1}{threadId}
  );
}
