Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

10.7 KB
Raw Blame
import ICAL from 'ical.js'
import { parseEvents } from '../src/main/calendar'
import { describe, expect, it, vi } from 'vitest'
import { icsEvents, icsFeed, icsName } from '../src/main/calendarLink/ics'
import { bigCalendar } from './helpers/bigCalendar'

// A made-up feed in the shape Google's "Secret address in iCal format" gives: its own time zone, attendees, a Meet link
const feed = (events: string) => `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Google Inc//Google Calendar 70.9054//EN
X-WR-CALNAME:[email protected]
BEGIN:VTIMEZONE
TZID:America/Los_Angeles
BEGIN:STANDARD
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
TZOFFSETFROM:-0700
TZOFFSETTO:-0800
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
TZOFFSETFROM:-0800
TZOFFSETTO:-0700
END:DAYLIGHT
END:VTIMEZONE
${events}
END:VCALENDAR
`
const sync = `BEGIN:VEVENT
UID:[email protected]
SUMMARY:Weekly sync
DTSTART;TZID=America/Los_Angeles:20261102T100000
DTEND;TZID=America/Los_Angeles:20261102T103000
RRULE:FREQ=WEEKLY;BYDAY=MO
EXDATE;TZID=America/Los_Angeles:20261109T100000
ORGANIZER;CN=Dana Smith:mailto:[email protected]
ATTENDEE;CN=Dana Smith;PARTSTAT=ACCEPTED:mailto:[email protected]
ATTENDEE;CN=Alex Morgan;PARTSTAT=ACCEPTED:mailto:[email protected]
ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:[email protected]
DESCRIPTION:Join: https://meet.google.com/abc-defg-hij
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
RECURRENCE-ID;TZID=America/Los_Angeles:20261116T100000
SUMMARY:Weekly sync (moved)
DTSTART;TZID=America/Los_Angeles:20261116T140000
DTEND;TZID=America/Los_Angeles:20261116T143000
ATTENDEE;CN=Dana Smith:mailto:[email protected]
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
SUMMARY:Lunch
DTSTART:20261117T200000Z
DTEND:20261117T210000Z
LOCATION:Cafe
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
SUMMARY:Day off
DTSTART;VALUE=DATE:20261118
DTEND;VALUE=DATE:20261119
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
SUMMARY:Cancelled review
STATUS:CANCELLED
DTSTART:20261117T180000Z
DTEND:20261117T183000Z
END:VEVENT`

const at = (iso: string) => Date.parse(iso)
const link = { id: 'link-1', name: 'Work' }

describe('a calendar link: events from an iCal feed, in the shape the Mac helper gives', () => {
  it('names the calendar after the feed, and knows the address it belongs to', () => {
    expect(icsName(feed(sync))).toEqual({ name: '[email protected]', me: '[email protected]' })
  })

  it('expands a weekly meeting in its own time zone, skips an excluded week, and uses a moved one', () => {
    const got = icsEvents(feed(sync), at('2026-11-02T00:00:00Z'), at('2026-11-24T00:00:00Z'), link)
    const syncs = got.filter((e) => e.externalId === '[email protected]')
    // Nov 2 and 23 at 10:00 PST (18:00Z); Nov 9 excluded; Nov 16 moved to 14:00 PST (22:00Z) with its own title
    expect(syncs.map((e) => [e.title, e.start])).toEqual([
      ['Weekly sync', '2026-11-02T18:00:00.000Z'],
      ['Weekly sync (moved)', '2026-11-16T22:00:00.000Z'],
      ['Weekly sync', '2026-11-23T18:00:00.000Z'],
    ])
    // every occurrence has its own id, so the agenda and meeting links can tell them apart
    expect(new Set(syncs.map((e) => e.id)).size).toBe(3)
  })

  it('gives attendees by name and email (an address alone stands for the name), the call link, and the calendar', () => {
    const [first] = icsEvents(feed(sync), at('2026-11-02T00:00:00Z'), at('2026-11-03T00:00:00Z'), link)
    expect(first).toMatchObject({
      title: 'Weekly sync', end: '2026-11-02T18:30:00.000Z', calendarId: 'link-1', calendar: 'Work', calendarSource: 'Calendar link',
      notes: 'Join: https://meet.google.com/abc-defg-hij', me: '[email protected]',
    })
    // the person the feed belongs to is left out, as the Mac helper does
    expect(first.attendees).toEqual([{ name: 'Dana Smith', email: '[email protected]' }, { name: '[email protected]', email: '[email protected]' }])
  })

  it('leaves out all-day events and cancelled ones, and keeps one-off events in range', () => {
    const got = icsEvents(feed(sync), at('2026-11-17T00:00:00Z'), at('2026-11-19T00:00:00Z'), link)
    expect(got.map((e) => e.title)).toEqual(['Lunch'])
    expect(got[0]).toMatchObject({ start: '2026-11-17T20:00:00.000Z', location: 'Cafe' })
  })

  it('a feed that is not a calendar is an error, not an empty week', () => {
    expect(() => icsEvents('<html>Sign in</html>', 0, 1, link)).toThrow()
  })

  // #79: a work calendar with years of recurring meetings froze the app, every read walking each series from its start
  const years = `BEGIN:VEVENT
UID:[email protected]
SUMMARY:Standup
DTSTART:20190107T170000Z
DTEND:20190107T171500Z
RRULE:FREQ=DAILY
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
RECURRENCE-ID:20261116T170000Z
SUMMARY:Standup (moved)
DTSTART:20261117T190000Z
DTEND:20261117T191500Z
END:VEVENT`
  it('a series that has run for years: occurrences long before the window are not worked out one by one, and one moved into it still shows', () => {
    const details = vi.spyOn(ICAL.Event.prototype, 'getOccurrenceDetails')
    const got = icsEvents(feed(years), at('2026-11-17T00:00:00Z'), at('2026-11-18T00:00:00Z'), link)
    expect(got.map((e) => [e.title, e.start])).toEqual([['Standup', '2026-11-17T17:00:00.000Z'], ['Standup (moved)', '2026-11-17T19:00:00.000Z']])
    // the window and a week either side (kept for the next reads): about 15 of a daily series, not all ~2,900 since 2019
    expect(details.mock.calls.length).toBeLessThan(20)
    details.mockRestore()
  })
  it('an occurrence moved into the window from a date long after it still shows', () => {
    const weekly = `BEGIN:VEVENT
UID:[email protected]
SUMMARY:Sync
DTSTART:20261102T150000Z
DTEND:20261102T153000Z
RRULE:FREQ=WEEKLY
END:VEVENT
BEGIN:VEVENT
UID:[email protected]
RECURRENCE-ID:20261214T150000Z
SUMMARY:Moved up
DTSTART:20261120T180000Z
DTEND:20261120T183000Z
END:VEVENT`
    const got = icsEvents(feed(weekly), at('2026-11-20T00:00:00Z'), at('2026-11-21T00:00:00Z'), link)
    expect(got.map((e) => e.title)).toEqual(['Moved up'])
  })
  it('building a feed of many events does not look through the whole feed once per event (thousands of events took seconds)', () => {
    const many = Array.from({ length: 50 }, (_, i) => `BEGIN:VEVENT\nUID:one-${i}@example.com\nSUMMARY:One ${i}\nDTSTART:20190101T1000${String(i).padStart(2, '0')}Z\nDTEND:20190101T1100${String(i).padStart(2, '0')}Z\nEND:VEVENT`).join('\n')
    const scans = vi.spyOn(ICAL.Component.prototype, 'getAllSubcomponents')
    icsFeed(feed(`${years}\n${many}`), link)
    expect(scans.mock.calls.length).toBeLessThan(10)
    scans.mockRestore()
  })
  it('a feed is parsed once, however many windows are read from it', () => {
    const parse = vi.spyOn(ICAL, 'parse')
    const f = icsFeed(feed(years), link)
    for (let d = 15; d <= 19; d++) f.events(at(`2026-11-${d}T00:00:00Z`), at(`2026-11-${d + 1}T00:00:00Z`))
    expect(f.events(at('2026-11-17T00:00:00Z'), at('2026-11-18T00:00:00Z')).map((e) => e.title)).toEqual(['Standup', 'Standup (moved)'])
    expect(parse).toHaveBeenCalledTimes(1)
    parse.mockRestore()
  })

  // #146: a work calendar with years of history: thousands of meetings, hundreds of series, long invites
  describe('a feed with years of history', () => {
    const text = bigCalendar()
    const day = [at('2026-11-23T00:00:00Z'), at('2026-11-24T00:00:00Z')] as const
    /** every occurrence of every series walked from its start: slow, but plainly right */
    function reference(text: string, fromMs: number, toMs: number) {
      const root = new ICAL.Component(ICAL.parse(text))
      for (const tz of root.getAllSubcomponents('vtimezone')) ICAL.TimezoneService.register(tz)
      const vevents = root.getAllSubcomponents('vevent')
      const moved = new Map<unknown, ICAL.Component[]>()
      for (const v of vevents) if (v.hasProperty('recurrence-id')) moved.set(v.getFirstPropertyValue('uid'), [...moved.get(v.getFirstPropertyValue('uid')) ?? [], v])
      const out: string[] = []
      for (const v of vevents) {
        if (v.hasProperty('recurrence-id')) continue
        const ev = new ICAL.Event(v, { exceptions: moved.get(v.getFirstPropertyValue('uid')) ?? [] })
        const it = ev.iterator()
        for (let next = it.next(); next; next = it.next()) {
          const d = ev.getOccurrenceDetails(next)
          const s = d.startDate.toJSDate().getTime(), e = d.endDate.toJSDate().getTime()
          if (s >= toMs + 30 * 86_400_000) break
          if (s < toMs && e > fromMs) out.push(`${ev.uid}|${new Date(s).toISOString()}`)
        }
      }
      return out.sort()
    }

    it('is tens of megabytes, as the reporter\'s was', () => {
      expect(text.length).toBeGreaterThan(15 * 2 ** 20)
    })
    it('gives the same meetings as walking every series from its start, today and years back', () => {
      // fewer of them, so walking every series the slow way stays quick
      const small = bigCalendar({ oneOffs: 600, series: 200 })
      const f = icsFeed(small, link)
      for (const [from, to] of [[at('2026-11-16T00:00:00Z'), at('2026-11-30T00:00:00Z')], [at('2021-03-01T00:00:00Z'), at('2021-03-15T00:00:00Z')]] as const) {
        const got = f.events(from, to).map((e) => e.id).sort()
        expect(got.length).toBeGreaterThan(5)
        expect(got).toEqual(reference(small, from, to))
      }
    }, 30_000)
    it('series running for years are not walked occurrence by occurrence to reach today', () => {
      const steps = vi.spyOn(ICAL.RecurExpansion.prototype, 'next')
      icsFeed(text, link).events(...day)
      // ~600 series and a few weeks of each; walking each from its start is hundreds of thousands
      expect(steps.mock.calls.length).toBeLessThan(20_000)
      steps.mockRestore()
    })
  })

  it('a daily meeting that has run for more than ten years still shows today', () => {
    const old = `BEGIN:VEVENT
UID:[email protected]
SUMMARY:Daily check-in
DTSTART:20120102T090000Z
DTEND:20120102T091500Z
RRULE:FREQ=DAILY
END:VEVENT`
    expect(icsEvents(feed(old), at('2026-11-23T00:00:00Z'), at('2026-11-24T00:00:00Z'), link).map((e) => [e.title, e.start]))
      .toEqual([['Daily check-in', '2026-11-23T09:00:00.000Z']])
  })
})

it('marks a decline only when the feed identifies the attendee as its owner', () => {
  const text = (owner: string, respondent: string) => `BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:${owner}
BEGIN:VEVENT
UID:meeting
DTSTART:20261118T100000Z
DTEND:20261118T103000Z
SUMMARY:Weekly sync
ATTENDEE;PARTSTAT=DECLINED:mailto:${respondent}
END:VEVENT
END:VCALENDAR`
  for (const [owner, attendee, declined] of [
    ['[email protected]', '[email protected]', true],
    ['[email protected]', '[email protected]', false],
    ['Shared calendar', '[email protected]', false],
  ] as const) {
    const raw = icsEvents(text(owner, attendee), at('2026-11-18T00:00:00Z'), at('2026-11-19T00:00:00Z'), link)
    expect(parseEvents(raw)[0].declined === true).toBe(declined)
  }
})