#!perl
use Cassandane::Tiny;

sub test_calendareventnotification_changes_shared
    :min_version_3_3 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $admin = $self->{adminstore}->get_client();

    $admin->create("user.manifold");
    my $http = $self->{instance}->get_service("http");
    my $mantalk = Net::CalDAVTalk->new(
        user => "manifold",
        password => 'pass',
        host => $http->host(),
        port => $http->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );
    my $manjmap = Mail::JMAPTalk->new(
        user => 'manifold',
        password => 'pass',
        host => $http->host(),
        port => $http->port(),
        scheme => 'http',
        url => '/jmap/',
    );
    $manjmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:calendars',
        'urn:ietf:params:jmap:principals',
        'https://cyrusimap.org/ns/jmap/calendars',
    ]);

    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    shareWith => {
                        manifold => {
                            mayReadFreeBusy => JSON::true,
                            mayReadItems => JSON::true,
                            mayUpdatePrivate => JSON::true,
                            mayWriteOwn => JSON::true,
                            mayAdmin => JSON::false
                        },
                    },
                },
            },
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    $res = $manjmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
        }, 'R1']
    ]);
    my $state = $res->[0][1]{state};
    $self->assert_not_null($state);

    # This should work, but it currently doesn't.
    # At least we can check for the correct error.

    $res = $jmap->CallMethods([
        ['CalendarEventNotification/queryChanges', {
            accountId => 'cassandane',
            sinceQueryState => $state,
        }, 'R1']
    ]);
    $self->assert_str_equals('cannotCalculateChanges', $res->[0][1]{type});
}
