#!perl
use Cassandane::Tiny;

sub test_email_query_sort_category_unsupported
    :JMAPExtensions
    ($self)
{
    my $user = $self->default_user;
    my $jmap = $user->jmap;
    $jmap->AddUsing('https://cyrusimap.org/ns/jmap/mail');

    my $unread = {
        operator   => 'NOT',
        conditions => [ { allInThreadHaveKeyword => '$seen' } ],
    };

    my @testcases = (
        {
            desc => 'no_group_by',
            sort => [ { property => 'category' } ],
        },
        {
            desc => 'empty_group_by',
            sort => [ { property => 'category', groupBy => [] } ],
        },
        {
            desc => 'group_by_not_an_array',
            sort => [ { property => 'category', groupBy => $unread } ],
        },
        {
            # A category sort takes at most EMAILQUERY_MAX_CATEGORIES filters
            desc => 'too_many_group_by',
            sort => [ {
                property => 'category',
                groupBy  => [ map { $unread } 0 .. 32 ],
            } ],
        },
        {
            desc => 'invalid_condition',
            sort => [ {
                property => 'category',
                groupBy  => [ { noSuchCondition => JSON::true } ],
            } ],
        },
        # A body condition is indexed, but we only support sorting by
        # header content
        {
            desc => 'condition_matches_body_part',
            sort => [ {
                property => 'category',
                groupBy  => [ { body => 'hello' } ],
            } ],
        },
        {
            desc => 'condition_matches_any_part',
            sort => [ {
                property => 'category',
                groupBy  => [ { text => 'hello' } ],
            } ],
        },
        {
            desc => 'condition_needs_index_record',
            sort => [ {
                property => 'category',
                groupBy  => [ { minSize => 42 } ],
            } ],
        },
        {
            # A user keyword is not in the G records of a guid
            desc => 'condition_needs_user_keyword',
            sort => [ {
                property => 'category',
                groupBy  => [ { hasKeyword => 'pinned' } ],
            } ],
        },
        {
            # One unsupported condition rejects the whole filter, wherever
            # it sits in the boolean tree
            desc => 'condition_nested_in_operator',
            sort => [ {
                property => 'category',
                groupBy  => [ {
                    operator   => 'OR',
                    conditions => [ { hasKeyword => '$flagged' },
                                    { body => 'hello' } ],
                } ],
            } ],
        },
        {
            # The second filter is what is unsupported here
            desc => 'second_filter_unsupported',
            sort => [ {
                property => 'category',
                groupBy  => [ $unread, { body => 'hello' } ],
            } ],
        },
        {
            desc => 'not_the_primary_sort',
            pos  => 1,
            sort => [
                { property => 'receivedAt' },
                { property => 'category', groupBy => [ $unread ] },
            ],
        },
        {
            desc => 'two_category_comparators',
            pos  => 1,
            sort => [
                { property => 'category', groupBy => [ $unread ] },
                { property => 'category', groupBy => [ $unread ] },
            ],
        },
    );

    # Get the queryState for a supported sort. We need to pass this to
    # queryChanges to assert unsupported sorts, as otherwise the request
    # would already be rejected due to the invalid query state.
    my $query_state = $jmap->request([
        [ 'Email/query', { sort => [ { property => 'receivedAt' } ] }, 'R1' ],
    ])->single_sentence('Email/query')->arguments->{queryState};

    for my $case (@testcases) {
        my $res = $jmap->request([
            [ 'Email/query', { sort => $case->{sort} }, 'query' ],
            [ 'Email/queryChanges', { sort            => $case->{sort},
                                      sinceQueryState => $query_state },
              'querychanges' ],
        ]);

        for my $method (qw( query querychanges )) {
            my $args = $res->paragraph_by_client_id($method)
                           ->single('error')->arguments;

            $self->assert_str_equals('unsupportedSort', $args->{type},
                "error type for $method $case->{desc}");

            # The error names the comparator that is not supported
            $self->assert_cmp_deeply([ sprintf('sort[%d]', $case->{pos} // 0) ],
                $args->{sort}, "unsupported sort for $method $case->{desc}");
        }
    }
}
