#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_preserve_class
    :min_version_3_7 :needs_component_jmap
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $caldav = $self->{caldav};

    my $ical = <<EOF;
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.9.5//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART;TZID=Europe/Vienna:20160928T160000
DTEND;TZID=Europe/Vienna:20160928T170000
UID:40d6fe3c-6a51-489e-823e-3ea22f427a3e
DTSTAMP:20150928T132434Z
CREATED:20150928T125212Z
DESCRIPTION:
SUMMARY:test
CLASS:PRIVATE
LAST-MODIFIED:20150928T132434Z
END:VEVENT
END:VCALENDAR
EOF

    $caldav->Request('PUT', '/dav/calendars/user/cassandane/Default/test.ics',
        $ical, 'Content-Type' => 'text/calendar');

    $res = $caldav->Request('GET', '/dav/calendars/user/cassandane/Default/test.ics');
    $self->assert_matches(qr/CLASS:PRIVATE/, $res->{content});

    my $res = $jmap->CallMethods([
        ['CalendarEvent/query', {
        }, 'R1'],
    ]);
    my $eventId = $res->[0][1]{ids}[0];
    $self->assert_not_null($eventId);


    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    title => 'update'
                },
            },
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventId});

    $res = $caldav->Request('GET', '/dav/calendars/user/cassandane/Default/test.ics');
    $self->assert_matches(qr/CLASS:PRIVATE/, $res->{content});
}
