#!perl
use Cassandane::Tiny;

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

    xlog "Append emails with list-id";
    $self->make_message("msg1", # RFC 2919
        extra_headers => [['list-id', "Foo <xxx.y\tyy. ZZZ>"]],
        body => "msg1"
    ) || die;
    $self->make_message("msg2", # as seen at Yahoo, Google, et al
        extra_headers => [['list-id', 'list aaa@bbb.ccc; contact aaa-contact@bbb.ccc']],
        body => "msg2"
    ) || die;
    $self->make_message("msg3", # as seen from sentry, just plain text
        extra_headers => [['list-id', 'sub3.sub2.sub1.top']],
        body => "msg3"
    ) || die;
    $self->make_message("msg4", # as seen in the wild
        extra_headers => [['list-id', '"<b>foo</b>" <xxx.yyy.zzz']],
        body => "msg4"
    ) || die;
    $self->make_message("msg5", # as seen in the wild
        extra_headers => [['list-id', '1234567890 list <xxx.yyy.zzz']],
        body => "msg5"
    ) || die;

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

    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 $res = $jmap->CallMethods([
        ['Email/query', {
            sort => [{
                property => 'subject',
            }],
        }, 'R1'],
    ], $using);
    my @ids = @{$res->[0][1]{ids}};
    $self->assert_num_equals(5, scalar @ids);

    my @testCases = ({
        desc => 'simple list-id',
        listId => 'xxx.yyy.zzz',
        wantIds => [$ids[0], $ids[3], $ids[4]],
    }, {
        desc => 'no substring search for list-id',
        listId => 'yyy',
        wantIds => [],
    }, {
        desc => 'no wildcard search for list-id',
        listId => 'xxx.yyy.*',
        wantIds => [],
    }, {
        desc => 'no substring search for list-id #2',
        listId => 'foo',
        wantIds => [],
    }, {
        desc => 'ignore whitespace',
        listId => 'xxx . yyy . zzz',
        wantIds => [$ids[0], $ids[3], $ids[4]],
    }, {
        desc => 'Groups-style list-id',
        listId => 'aaa@bbb.ccc',
        wantIds => [$ids[1]],
    }, {
        desc => 'Ignore contact in groups-style list-id',
        listId => 'aaa-contact@bbb.ccc',
        wantIds => [],
    }, {
        desc => 'Groups-style list-id with whitespace',
        listId => 'aaa @ bbb . ccc',
        wantIds => [$ids[1]],
    }, {
        desc => 'Also no substring search in groups-style list-id',
        listId => 'aaa',
        wantIds => [],
    }, {
        desc => 'unbracketed list-id',
        listId => 'sub3.sub2.sub1.top',
        wantIds => [$ids[2]],
    });

    foreach (@testCases) {
        $res = $jmap->CallMethods([
            ['Email/query', {
                filter => {
                    listId => $_->{listId},
                },
                sort => [{ property => 'subject' }],
            }, 'R1'],
        ], $using);
        $self->assert_deep_equals($_->{wantIds}, $res->[0][1]{ids});
    }
}
