#!perl
use Cassandane::Tiny;

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

    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/set', {
            create => {
                email => {
                    mailboxIds => {
                        '$inbox' => JSON::true,
                    },
                    subject => 'email',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part1',
                    },
                    bodyValues => {
                        part1 => {
                            value => 'email',
                        }
                    },
                },
            },
        }, 'R1'],
        ['Mailbox/query', {
        }, 'R2'],
    ], $using);
    my $emailId = $res->[0][1]{created}{email}{id};
    $self->assert_not_null($emailId);
    my $inboxId = $res->[1][1]{ids}[0];
    $self->assert_not_null($inboxId);

    $res = $jmap->CallMethods([
        ['Email/query', {
            filter => {
                operator => 'OR',
                conditions => [{
                    inMailbox => $inboxId,
                }],
            },
        }, 'R1'],
    ], $using);

    $self->assert_deep_equals([$emailId], $res->[0][1]{ids});
    $self->assert_equals(JSON::true, $res->[0][1]{canCalculateChanges});
    my $queryState = $res->[0][1]{queryState};

    $res = $jmap->CallMethods([
        ['Email/queryChanges', {
            filter => {
                operator => 'OR',
                conditions => [{
                    inMailbox => $inboxId,
                }],
            },
            sinceQueryState => $queryState,
        }, 'R1'],
    ], $using);
    $self->assert_deep_equals([], $res->[0][1]{added});
    $self->assert_deep_equals([], $res->[0][1]{removed});
}
