Files
Docwell/Server/internal/calendar/ics_test.go
T
tajniak81andClaude Opus 5.5 3d1cf362d7 Add calendars, a web clipper, more importers and workspace snapshots
Phase F of the AFFiNE plan (ADR-16):

- Calendars: people subscribe to iCal feeds (webcal, Google's secret
  address, Outlook) or CalDAV accounts in account settings; their
  events show at the top of journal pages in the reader's time zone.
  Repeats, moved and cancelled occurrences, IANA and Windows zones
  are handled. Events are read on demand and kept in memory for 15
  minutes; passwords are sealed with DOCWELL_SECRET_KEY; sources
  follow the webhook address rules. New calendar_accounts collection.
- Importers: zips of Markdown (Obsidian vaults, Bear TextBundles and
  .bear2bk, Readwise's Markdown, Docwell's own space export) keep
  folders as pages, resolve [[wikilinks]] and embeds by name, and turn
  front matter and #tags into properties and tags. OneNote .mht
  exports, Readwise CSV, and any other CSV as a database page.
- Exports: PNG of a page drawn in the browser, print styles that print
  only the page in the light theme, and workspace snapshots (records,
  Yjs states, JSON and files in one zip) that restore as a new
  workspace. Old ids are swapped byte for byte inside the Yjs updates,
  so links point at the restored pages and comments stay anchored.
- Web-Clipper/: a Manifest V3 extension for Chrome, Edge and Firefox
  that saves the article or the selection as Markdown through the
  import API with a personal API token.
- OpenAPI, generated clients, translations and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:56:03 +01:00

100 lines
3.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package calendar
import (
"strings"
"testing"
"time"
)
const sampleICS = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//test//EN\r\n" +
"BEGIN:VTIMEZONE\r\nTZID:Custom Zone\r\nBEGIN:STANDARD\r\nDTSTART:19700101T000000\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n" +
// A one-off meeting in Warsaw time.
"BEGIN:VEVENT\r\nUID:one@test\r\nDTSTART;TZID=Europe/Warsaw:20261116T090000\r\nDTEND;TZID=Europe/Warsaw:20261116T100000\r\n" +
"SUMMARY:Planning\\, Q1\r\nLOCATION:Room 2\r\nEND:VEVENT\r\n" +
// An all-day event.
"BEGIN:VEVENT\r\nUID:allday@test\r\nDTSTART;VALUE=DATE:20261116\r\nDTEND;VALUE=DATE:20261117\r\nSUMMARY:Holiday\r\nEND:VEVENT\r\n" +
// Every weekday at 08:30 UTC, the 18th skipped, the 19th moved to 12:00.
"BEGIN:VEVENT\r\nUID:standup@test\r\nDTSTART:20261109T083000Z\r\nDURATION:PT15M\r\n" +
"RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR\r\nEXDATE:20261118T083000Z\r\nSUMMARY:Standup\r\nEND:VEVENT\r\n" +
"BEGIN:VEVENT\r\nUID:standup@test\r\nRECURRENCE-ID:20261119T083000Z\r\nDTSTART:20261119T120000Z\r\nDTEND:20261119T121500Z\r\nSUMMARY:Standup (moved)\r\nEND:VEVENT\r\n" +
// A cancelled event, an Outlook zone name and a calendar's own zone.
"BEGIN:VEVENT\r\nUID:gone@test\r\nDTSTART:20261116T150000Z\r\nSTATUS:CANCELLED\r\nSUMMARY:Gone\r\nEND:VEVENT\r\n" +
"BEGIN:VEVENT\r\nUID:win@test\r\nDTSTART;TZID=W. Europe Standard Time:20261117T140000\r\nDTEND;TZID=W. Europe Standard Time:20261117T143000\r\nSUMMARY:Outlook\r\nEND:VEVENT\r\n" +
"BEGIN:VEVENT\r\nUID:custom@test\r\nDTSTART;TZID=Custom Zone:20261117T180000\r\nSUMMARY:Custom\r\nEND:VEVENT\r\n" +
"END:VCALENDAR\r\n"
func TestFeedBetween(t *testing.T) {
warsaw, _ := time.LoadLocation("Europe/Warsaw")
f, err := parseFeed(strings.NewReader(sampleICS), warsaw)
if err != nil {
t.Fatal(err)
}
day := func(d string) (time.Time, time.Time) {
from, _ := time.ParseInLocation(time.DateOnly, d, warsaw)
return from, from.AddDate(0, 0, 1)
}
titles := func(occ []occurrence) []string {
var out []string
for _, o := range occ {
out = append(out, o.def.title+"@"+o.start.In(warsaw).Format("15:04"))
}
return out
}
cases := map[string]string{
"2026-11-16": "Holiday@00:00|Standup@09:30|Planning, Q1@09:00",
"2026-11-17": "Standup@09:30|Outlook@14:00|Custom@16:00",
"2026-11-18": "",
"2026-11-19": "Standup (moved)@13:00",
"2026-11-21": "", // Saturday
}
for d, want := range cases {
got := titles(f.between(day(d)))
// Order is by start time; compare as sets of entries.
var wantList []string
if want != "" {
wantList = strings.Split(want, "|")
}
if len(got) != len(wantList) {
t.Errorf("%s: got %v, want %v", d, got, wantList)
continue
}
for _, w := range wantList {
found := false
for _, g := range got {
found = found || g == w
}
if !found {
t.Errorf("%s: %q missing from %v", d, w, got)
}
}
}
occ := f.between(day("2026-11-16"))
if occ[0].def.title != "Holiday" || !occ[0].def.allDay {
t.Errorf("all-day event should come first: %+v", occ[0].def)
}
for _, o := range occ {
if o.def.title == "Planning, Q1" && (o.def.location != "Room 2" || o.end.Sub(o.start) != time.Hour) {
t.Errorf("planning = %+v", o)
}
}
}
func TestParseFeedRejectsJunk(t *testing.T) {
if _, err := parseFeed(strings.NewReader("<html>not a calendar</html>"), time.UTC); err == nil {
t.Fatal("HTML read as a calendar")
}
}
func TestParseOffset(t *testing.T) {
for in, want := range map[string]int{"+0100": 3600, "-0530": -19800, "+013015": 5415} {
if got, ok := parseOffset(in); !ok || got != want {
t.Errorf("%s = %d, %v", in, got, ok)
}
}
if _, ok := parseOffset("0100"); ok {
t.Error("offset without a sign accepted")
}
}