feat: implement parseICS with ical.js

This commit is contained in:
timeTable2 dev
2026-07-13 18:26:14 +08:00
parent 76e08d67d8
commit 138b89e3bf
3 changed files with 745 additions and 0 deletions

50
test/ical-io.test.js Normal file
View File

@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { parseICS } from '../src/lib/ical-io.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const GOLDEN = readFileSync(join(__dirname, 'golden-org.ics'), 'utf-8')
describe('parseICS', () => {
const { meta, events } = parseICS(GOLDEN)
it('parses calendar meta', () => {
expect(meta.prodid).toBe('-//Allocate//iCal4j 1.0//EN')
expect(meta.version).toBe('2.0')
expect(meta.calscale).toBe('GREGORIAN')
expect(meta.vtimezones.length).toBe(1)
})
it('parses all 61 events', () => {
expect(events).toHaveLength(61)
})
it('parses first event fields correctly', () => {
const e = events[0]
expect(e.summary).toBe('ACC INFO SYS, Tutorial')
expect(e.location).toBe('CA_B_B471')
expect(e.dtstartDate).toBe('2026-07-27')
expect(e.dtstartTime).toBe('18:00')
expect(e.dtendTime).toBe('20:00')
expect(e.tzid).toBe('Australia/Melbourne')
expect(e.rrule).toBeNull()
expect(e.exdates).toEqual([])
})
it('preserves description with escaped commas', () => {
const e = events[0]
expect(e.description).toContain('ACF2400_CA_S2_ON-CAMPUS')
expect(e.description).toContain('ACC INFO SYS')
})
it('captures dtstamp in _raw', () => {
expect(events[0]._raw.dtstamp).toBe('20260711T085008Z')
})
it('events have UIDs', () => {
expect(events[0].uid).toBe('uid0')
expect(events[60].uid).toBe('uid60')
})
})