#!perl
use Cassandane::Tiny;
use utf8;

sub test_note_get
    :JMAPExtensions :NoAltNameSpace
    ($self)
{
    my $jmap = $self->{jmap};

    my $id1 = 'bfaf3ef9-6569-4aa1-be58-a53e1c701373';
    my $note1 = to_crlf(<<"EOF");
MIME-Version: 1.0 (Cyrus-JMAP/3.11.0-alpha0-2705-g73ba330e6)
X-Uniform-Type-Identifier: com.apple.mail-note
X-Universally-Unique-Identifier: $id1
X-Mail-Created-Date: Tue, 11 Nov 2025 09:24:17 -0500
Date: Tue, 11 Nov 2025 09:24:17 -0500
From: <cassandane\@local>
Subject: snowman
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

=E2=98=83=0D
EOF

    my $id2 = 'c07117fe-1643-420d-a964-c11860f97e19';
    my $note2 = to_crlf(<<"EOF");
X-Uniform-Type-Identifier: com.apple.mail-note
X-Universally-Unique-Identifier: $id2
X-Mail-Created-Date: Tue, 11 Nov 2025 09:24:17 -0500
Date: Tue, 11 Nov 2025 09:24:17 -0500
From: <cassandane\@local>
Subject: bad charset
Content-Type: text/plain; charset=foo

foo bar
EOF

    my $id3 = '16ebdca7-ff85-4f37-bf5c-2694d652255e';
    my $note3 = to_crlf(<<"EOF");
X-Uniform-Type-Identifier: com.apple.mail-note
X-Universally-Unique-Identifier: $id3
X-Mail-Created-Date: Tue, 11 Nov 2025 09:24:17 -0500
Date: Tue, 11 Nov 2025 09:24:17 -0500
From: <cassandane\@local>
Subject: bad encoding
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: foo

foo bar
EOF

    my $imap = $self->{store}->get_client();
    $imap->create('INBOX.Notes') || die $@;
    $imap->append('INBOX.Notes', $note1) || die $@;
    $imap->append('INBOX.Notes', $note2) || die $@;
    $imap->append('INBOX.Notes', $note3) || die $@;

    my $res = $jmap->CallMethods([['Note/get', { }, "R1"]]);
    $self->assert_cmp_deeply([
        superhashof({
            id     => $id1,
            title  => 'snowman',
            body   => "☃\n\n",
            isHTML => JSON::false
        }),
        superhashof({
            id     => $id2,
            title  => 'bad charset',
            body   => "foo bar\n",
            isHTML => JSON::false
        }),
        superhashof({
            id     => $id3,
            title  => 'bad encoding',
            body   => "foo bar\n",
            isHTML => JSON::false
        }),
    ], $res->[0][1]{list});

    my $id4 = $id2 =~ tr/0-9/5-90-4/r;
    my $id5 = $id3 =~ tr/0-9/5-90-4/r;
    $res = $jmap->CallMethods([['Note/get',
                                { ids => [$id4, $id1, $id5] },
                                "R2"]]);
    $self->assert_cmp_deeply(
        bag($id4, $id5),
        $res->[0][1]{notFound},
    );
    $self->assert_cmp_deeply([
        superhashof({
            id     => $id1,
            title  => 'snowman',
            body   => "☃\n\n",
            isHTML => JSON::false
        })],
        $res->[0][1]{list},
    );

    $res = $jmap->CallMethods([['Note/get',
                                { ids => [] },
                                "R2"]]);
    $self->assert_cmp_deeply([],
        $res->[0][1]{list},
    );
}
