|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/git-calendar/core/pkg/core" |
| 16 | + "github.com/git-calendar/core/pkg/filesystem" |
| 17 | +) |
| 18 | + |
| 19 | +func TestImportICalFilePersistsEvents(t *testing.T) { |
| 20 | + const calendar = "test-ical-file" |
| 21 | + c := core.NewCore() |
| 22 | + if err := c.CreateCalendar(calendar, ""); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + t.Cleanup(func() { _ = c.RemoveCalendar(calendar) }) |
| 26 | + |
| 27 | + if err := c.ImportICalFile(calendar, strings.NewReader(icalFeed("Imported event"))); err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + events := importedEvents(c, calendar) |
| 32 | + if len(events) != 1 { |
| 33 | + t.Fatalf("got %d imported events, want 1", len(events)) |
| 34 | + } |
| 35 | + if events[0].Id.Version() != 4 { |
| 36 | + t.Errorf("event ID version = %d, want 4", events[0].Id.Version()) |
| 37 | + } |
| 38 | + id := events[0].Id |
| 39 | + |
| 40 | + home, err := os.UserHomeDir() |
| 41 | + if err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + eventPath := filepath.Join(home, filesystem.DirName, calendar, core.EventsDirName, id.String()+".json") |
| 45 | + if _, err := os.Stat(eventPath); err != nil { |
| 46 | + t.Fatalf("imported event file was not saved: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + if err := c.LoadCalendars(); err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + events = importedEvents(c, calendar) |
| 53 | + if len(events) != 1 || events[0].Id != id { |
| 54 | + t.Fatalf("imported event did not survive reload: %+v", events) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestImportICalURLRefetchesOnLoad(t *testing.T) { |
| 59 | + const name = "test-ical-url" |
| 60 | + |
| 61 | + var feed atomic.Value |
| 62 | + feed.Store(icalFeed("First title")) |
| 63 | + var requests atomic.Int32 |
| 64 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 65 | + requests.Add(1) |
| 66 | + _, _ = w.Write([]byte(feed.Load().(string))) |
| 67 | + })) |
| 68 | + defer server.Close() |
| 69 | + |
| 70 | + sourceURL, err := url.Parse(server.URL) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + c := core.NewCore() |
| 76 | + if err := c.ImportICalURL(name, sourceURL); err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + t.Cleanup(func() { _ = c.RemoveCalendar(name) }) |
| 80 | + |
| 81 | + events := importedEvents(c, name) |
| 82 | + if len(events) != 1 || events[0].Title != "First title" { |
| 83 | + t.Fatalf("first URL import = %+v", events) |
| 84 | + } |
| 85 | + if events[0].Id.Version() != 8 { |
| 86 | + t.Errorf("event ID version = %d, want 8", events[0].Id.Version()) |
| 87 | + } |
| 88 | + id := events[0].Id |
| 89 | + |
| 90 | + calendars, err := c.ListCalendars() |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + for _, calendar := range calendars { |
| 95 | + if calendar.Name == name && !calendar.Readonly { |
| 96 | + t.Fatal("URL calendar is not read-only") |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + home, err := os.UserHomeDir() |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + data, err := os.ReadFile(filepath.Join(home, filesystem.DirName, name)) |
| 105 | + if err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + if string(data) != server.URL { |
| 109 | + t.Errorf("URL file = %q, want %q", data, server.URL) |
| 110 | + } |
| 111 | + |
| 112 | + feed.Store(icalFeed("Second title")) |
| 113 | + if err := c.LoadCalendars(); err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + events = importedEvents(c, name) |
| 118 | + if len(events) != 1 || events[0].Title != "Second title" { |
| 119 | + t.Fatalf("refetched URL import = %+v", events) |
| 120 | + } |
| 121 | + if events[0].Id != id { |
| 122 | + t.Errorf("event ID changed after refetch: got %s, want %s", events[0].Id, id) |
| 123 | + } |
| 124 | + if requests.Load() != 2 { |
| 125 | + t.Errorf("URL was fetched %d times, want 2", requests.Load()) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func importedEvents(c *core.Core, calendar string) []core.Event { |
| 130 | + from := time.Date(2026, 7, 1, 0, 0, 0, 0, time.UTC) |
| 131 | + to := time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC) |
| 132 | + return c.GetEvents(from, to, core.GetEventsFilter{calendar: nil}) |
| 133 | +} |
| 134 | + |
| 135 | +func icalFeed(title string) string { |
| 136 | + return fmt.Sprintf(`BEGIN:VCALENDAR |
| 137 | +VERSION:2.0 |
| 138 | +PRODID:-//git-calendar//test//EN |
| 139 | +BEGIN:VEVENT |
| 140 | +UID:stable@example.com |
| 141 | +DTSTART:20260714T100000Z |
| 142 | +DTEND:20260714T110000Z |
| 143 | +SUMMARY:%s |
| 144 | +END:VEVENT |
| 145 | +END:VCALENDAR`, title) |
| 146 | +} |
0 commit comments