#!perl
use Cassandane::Tiny;

sub test_calendarevent_guesstz_nonstandard_utcoffset
    :needs_dependency_guesstz
    ($self)
{
    my $jmap = $self->default_user->jmap;

    # This iCalendar object contains two non-standard timezones for which no
    # equivalent IANA timezone can be found in the 'guesstz' database. Their
    # offsets are not whole hours, so there is no Etc/GMT zone to fall back to
    # either, and by decision we report no timeZone at all rather than name a
    # zone that is half an hour out. (Compare
    # calendarevent_get_guesstz_custom_tzid_guess_gmt, where a whole-hour
    # offset does resolve to Etc/GMT+10.)
    my $ical = to_crlf(<<'EOF');
BEGIN:VCALENDAR
PRODID:-//Foo//Bar//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Offset0430
BEGIN:STANDARD
DTSTART:16010101T000000
TZOFFSETFROM:-0430
TZOFFSETTO:-0430
END:STANDARD
END:VTIMEZONE
BEGIN:VTIMEZONE
TZID:Offset0730
BEGIN:STANDARD
DTSTART:16010101T000000
TZOFFSETFROM:-0730
TZOFFSETTO:-0730
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
UID:C14C9D9D-D9E0-4008-B995-A1ADEB47E3F9
DTSTART;TZID=Offset0730:20250130T183000
DTSTAMP:20250131T231705Z
DURATION:PT1H
SEQUENCE:0
SUMMARY:offset0730
END:VEVENT
BEGIN:VEVENT
UID:1F95E16C-A0EE-4041-8717-F93E175EDBF7
DTSTART;TZID=Offset0430:20240824T153000
DTSTAMP:20250131T231705Z
DURATION:PT1H
SEQUENCE:0
SUMMARY:offset0430
END:VEVENT
END:VCALENDAR
EOF

    my $res = $jmap->CallMethods(
        [
            ['Blob/upload', {
                create => {
                    ical => {
                        data => [ {
                            'data:asText' => $ical,
                        } ],
                    },
                },
            }, 'R0'],
            ['CalendarEvent/parse', {
                blobIds    => ["#ical"],
                properties =>
                [ 'start', 'timeZone', 'timeZones', 'title' ],
            },
            'R1' ]
        ],
        [
            'urn:ietf:params:jmap:core',
            'urn:ietf:params:jmap:calendars',
            'https://cyrusimap.org/ns/jmap/jscalendarbis',
            'https://cyrusimap.org/ns/jmap/calendars',
            'urn:ietf:params:jmap:blob',
        ]
    );

    my %eventByTitle = map { $_->{title} => $_ }
      @{ $res->[1][1]{parsed}{'#ical'}{entries} };

    my %wantStart = (
        offset0730 => '2025-01-30T18:30:00',
        offset0430 => '2024-08-24T15:30:00',
    );

    for my $title (sort keys %wantStart) {
        my $event = $eventByTitle{$title};
        $self->assert_not_null($event);

        # The local time is preserved, but we do not claim to know the zone.
        $self->assert_str_equals($wantStart{$title}, $event->{start});
        $self->assert_null($event->{timeZone});
    }
}
