feat: add recurrence grid expansion
This commit is contained in:
69
test/recur.test.js
Normal file
69
test/recur.test.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { expandOccurrences, intervalDays } from '../src/lib/recur.js'
|
||||
|
||||
describe('intervalDays', () => {
|
||||
it('returns 7 for WEEKLY interval=1', () => {
|
||||
const ev = { rrule: { freq: 'WEEKLY', interval: 1 } }
|
||||
expect(intervalDays(ev)).toBe(7)
|
||||
})
|
||||
|
||||
it('returns 14 for WEEKLY interval=2', () => {
|
||||
const ev = { rrule: { freq: 'WEEKLY', interval: 2 } }
|
||||
expect(intervalDays(ev)).toBe(14)
|
||||
})
|
||||
|
||||
it('returns interval for DAILY', () => {
|
||||
const ev = { rrule: { freq: 'DAILY', interval: 3 } }
|
||||
expect(intervalDays(ev)).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('expandOccurrences', () => {
|
||||
it('returns single occurrence for non-recurring event', () => {
|
||||
const ev = { dtstartDate: '2026-07-27', rrule: null, exdates: [] }
|
||||
const occs = expandOccurrences(ev)
|
||||
expect(occs).toHaveLength(1)
|
||||
expect(occs[0]).toEqual({ date: '2026-07-27', skipped: false })
|
||||
})
|
||||
|
||||
it('expands weekly event from Jul 27 to Oct 19', () => {
|
||||
const ev = {
|
||||
dtstartDate: '2026-07-27',
|
||||
rrule: { freq: 'WEEKLY', interval: 1, byday: 'MO', untilDate: '2026-10-19' },
|
||||
exdates: ['2026-09-21'],
|
||||
}
|
||||
const occs = expandOccurrences(ev)
|
||||
// Jul 27 + 12 weeks = Oct 19 -> 13 occurrences
|
||||
expect(occs).toHaveLength(13)
|
||||
expect(occs[0].date).toBe('2026-07-27')
|
||||
expect(occs[12].date).toBe('2026-10-19')
|
||||
// Sep 21 is skipped
|
||||
const sep21 = occs.find(o => o.date === '2026-09-21')
|
||||
expect(sep21.skipped).toBe(true)
|
||||
// Others not skipped
|
||||
expect(occs[0].skipped).toBe(false)
|
||||
})
|
||||
|
||||
it('respects exdates set', () => {
|
||||
const ev = {
|
||||
dtstartDate: '2026-07-27',
|
||||
rrule: { freq: 'WEEKLY', interval: 1, byday: 'MO', untilDate: '2026-08-24' },
|
||||
exdates: ['2026-08-03', '2026-08-17'],
|
||||
}
|
||||
const occs = expandOccurrences(ev)
|
||||
expect(occs).toHaveLength(5) // Jul27, Aug3, Aug10, Aug17, Aug24
|
||||
expect(occs[1].skipped).toBe(true) // Aug 3
|
||||
expect(occs[2].skipped).toBe(false) // Aug 10
|
||||
expect(occs[3].skipped).toBe(true) // Aug 17
|
||||
})
|
||||
|
||||
it('caps at 500 occurrences', () => {
|
||||
const ev = {
|
||||
dtstartDate: '2020-01-01',
|
||||
rrule: { freq: 'DAILY', interval: 1, untilDate: '2030-12-31' },
|
||||
exdates: [],
|
||||
}
|
||||
const occs = expandOccurrences(ev)
|
||||
expect(occs.length).toBeLessThanOrEqual(500)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user