|
| 1 | +package ical_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/git-calendar/core/pkg/core" |
| 9 | + "github.com/git-calendar/core/pkg/ical" |
| 10 | +) |
| 11 | + |
| 12 | +func TestImport(t *testing.T) { |
| 13 | + input := `BEGIN:VCALENDAR |
| 14 | +VERSION:2.0 |
| 15 | +PRODID:-//git-calendar//test//EN |
| 16 | +BEGIN:VEVENT |
| 17 | +UID:one@example.com |
| 18 | +DTSTART:20260714T100000Z |
| 19 | +DTEND:20260714T113000Z |
| 20 | +SUMMARY:Planning\, review |
| 21 | +LOCATION:https://meeting.abc/123 |
| 22 | +DESCRIPTION:Line one\nLine two |
| 23 | +END:VEVENT |
| 24 | +BEGIN:VEVENT |
| 25 | +UID:two@example.com |
| 26 | +DTSTART:20260715T090000Z |
| 27 | +DTEND:20260715T100000Z |
| 28 | +SUMMARY:Fortnightly sync |
| 29 | +RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4 |
| 30 | +END:VEVENT |
| 31 | +END:VCALENDAR` |
| 32 | + |
| 33 | + events, err := ical.Import(strings.NewReader(input), "Work") |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + if len(events) != 2 { |
| 38 | + t.Fatalf("got %d events, want 2", len(events)) |
| 39 | + } |
| 40 | + |
| 41 | + first := events[0] |
| 42 | + if first.Id.Version() != 4 { |
| 43 | + t.Errorf("event ID version = %d, want 4", first.Id.Version()) |
| 44 | + } |
| 45 | + if first.Title != "Planning, review" { |
| 46 | + t.Errorf("Title = %q, want %q", first.Title, "Planning, review") |
| 47 | + } |
| 48 | + if first.Location != "https://meeting.abc/123" { |
| 49 | + t.Errorf("Location = %q, want %q", first.Location, "https://meeting.abc/123") |
| 50 | + } |
| 51 | + if first.Description != "Line one\nLine two" { |
| 52 | + t.Errorf("Description = %q, want %q", first.Description, "Line one\nLine two") |
| 53 | + } |
| 54 | + if first.Calendar != "Work" { |
| 55 | + t.Errorf("Calendar = %q, want %q", first.Calendar, "Work") |
| 56 | + } |
| 57 | + assertTime(t, first.From, time.Date(2026, 7, 14, 10, 0, 0, 0, time.UTC)) |
| 58 | + assertTime(t, first.To, time.Date(2026, 7, 14, 11, 30, 0, 0, time.UTC)) |
| 59 | + |
| 60 | + second := events[1] |
| 61 | + if second.Repeat == nil { |
| 62 | + t.Fatal("Repeat is nil") |
| 63 | + } |
| 64 | + if second.Repeat.Frequency != core.Week { |
| 65 | + t.Errorf("Frequency = %v, want %v", second.Repeat.Frequency, core.Week) |
| 66 | + } |
| 67 | + if second.Repeat.Interval != 2 { |
| 68 | + t.Errorf("Interval = %d, want 2", second.Repeat.Interval) |
| 69 | + } |
| 70 | + if second.Repeat.Count != 4 { |
| 71 | + t.Errorf("Count = %d, want 4", second.Repeat.Count) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestImportRejectsUnsupportedRecurrence(t *testing.T) { |
| 76 | + input := `BEGIN:VCALENDAR |
| 77 | +VERSION:2.0 |
| 78 | +PRODID:-//git-calendar//test//EN |
| 79 | +BEGIN:VEVENT |
| 80 | +UID:one@example.com |
| 81 | +DTSTART:20260714T100000Z |
| 82 | +DTEND:20260714T110000Z |
| 83 | +SUMMARY:Several days |
| 84 | +RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=MO,WE |
| 85 | +END:VEVENT |
| 86 | +END:VCALENDAR` |
| 87 | + |
| 88 | + _, err := ical.Import(strings.NewReader(input), "Work") |
| 89 | + if err == nil || !strings.Contains(err.Error(), "recurrence modifiers are not supported") { |
| 90 | + t.Fatalf("error = %v, want unsupported recurrence modifier error", err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func assertTime(t *testing.T, got, want time.Time) { |
| 95 | + t.Helper() |
| 96 | + if !got.Equal(want) { |
| 97 | + t.Errorf("time = %v, want %v", got, want) |
| 98 | + } |
| 99 | +} |
0 commit comments