#!perl
use Cassandane::Tiny;

sub test_calendarevent_get_reset_iter
    :min_version_3_7 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    xlog "Create events in calendar A and B, both have IMAP uid 1";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            create => {
                calendarA => {
                    name => 'A',
                },
                calendarB => {
                    name => 'B',
                },
            },
        }, 'R1'],
        ['CalendarEvent/set', {
            create => {
                eventA => {
                    calendarIds => {
                        '#calendarA' => JSON::true,
                    },
                    '@type' => 'Event',
                    uid => 'eventA-uid',
                    title => 'eventA',
                    start => '2021-01-01T15:30:00',
                    timeZone => 'Europe/Berlin',
                    duration => 'PT1H',
                },
                eventB => {
                    calendarIds => {
                        '#calendarB' => JSON::true,
                    },
                    '@type' => 'Event',
                    uid => 'eventB-uid',
                    title => 'eventB',
                    start => '2022-01-01T15:30:00',
                    timeZone => 'Europe/Berlin',
                    duration => 'PT1H',
                },
            },
        }, 'R2'],
        ['CalendarEvent/get', {
                properties => ['calendarIds', 'uid', 'title', 'start'],
        }, 'R2'],
    ]);

    xlog "Assert CalendarEvent/get iterator state is reset properly";
    $self->assert_num_equals(2, scalar @{$res->[2][1]{list}});
    $self->assert_str_not_equals((keys %{$res->[2][1]{list}[0]{calendarIds}})[0],
        (keys %{$res->[2][1]{list}[1]{calendarIds}})[0]);
    $self->assert_str_not_equals($res->[2][1]{list}[0]{uid},
        $res->[2][1]{list}[1]{uid});
    $self->assert_str_not_equals($res->[2][1]{list}[0]{title},
        $res->[2][1]{list}[1]{title});
    $self->assert_str_not_equals($res->[2][1]{list}[0]{start},
        $res->[2][1]{list}[1]{start});
}
