Skip to content

Commit

Permalink
Test calendar feed extraction including recurring events
Browse files Browse the repository at this point in the history
  • Loading branch information
abrain committed Aug 8, 2024
1 parent 0d7f417 commit 102e741
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/src/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export default function (app: Application): void {
// Migrate and sync to the database
app.set('sequelizeSync', sequelize.authenticate({ retry: retryOptions }).then(() => {
logger.info('Connected to database');
// Tear down the existing tables for testing
if (process.env.NODE_ENV === 'test') {
logger.debug('Tearing down tables for testing ...');
return umzug.down();
}
}).then(() => {
return umzug.up();
}, (reason: Error) => {
logger.error('Database connection failed:', reason.message);
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/calendar-items/calendar-items.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum CalendarItemStatus {
cancelled = 'cancelled'
}

interface CalendarItemData {
export interface CalendarItemData {
uid: string
summary: string
startDate: Date
Expand Down
81 changes: 81 additions & 0 deletions server/test/feed.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//SabreDAV//SabreDAV//EN
REFRESH-INTERVAL;VALUE=DURATION:PT4H
X-PUBLISHED-TTL:PT4H
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20240808T201535Z
DTSTAMP:20240808T201636Z
LAST-MODIFIED:20240808T201636Z
SEQUENCE:2
UID:2748ddf3-886e-430a-8392-79d565b952aa
DTSTART;TZID=Europe/Berlin:20240815T160000
DTEND;TZID=Europe/Berlin:20240815T170000
STATUS:CONFIRMED
SUMMARY:Recurring event weekly only twice
RRULE:FREQ=WEEKLY;COUNT=2;BYDAY=TH
END:VEVENT
BEGIN:VEVENT
CREATED:20240808T201642Z
DTSTAMP:20240808T201649Z
LAST-MODIFIED:20240808T201649Z
SEQUENCE:2
UID:bc541fc4-fbdd-4186-b102-c04f387c8dc9
DTSTART;VALUE=DATE:20240816
DTEND;VALUE=DATE:20240817
STATUS:CONFIRMED
SUMMARY:Whole day
END:VEVENT
BEGIN:VEVENT
CREATED:20240808T204507Z
DTSTAMP:20240808T204518Z
LAST-MODIFIED:20240808T204518Z
SEQUENCE:2
UID:7388dbc8-03ed-4ecb-b43b-3349361d3942
DTSTART;TZID=Europe/Berlin:20240811T180000
DTEND;TZID=Europe/Berlin:20240811T190000
STATUS:CONFIRMED
SUMMARY:Future Event
END:VEVENT
BEGIN:VEVENT
CREATED:20240808T201404Z
DTSTAMP:20240808T201529Z
LAST-MODIFIED:20240808T201529Z
SEQUENCE:2
UID:6099e8c0-b233-4dc8-83da-7ad56191ddb0
DTSTART;TZID=Europe/Berlin:20240730T130000
DTEND;TZID=Europe/Berlin:20240730T140000
STATUS:CONFIRMED
SUMMARY:Recurring event weekly
RRULE:FREQ=WEEKLY;BYDAY=TU
END:VEVENT
BEGIN:VEVENT
CREATED:20240808T201232Z
DTSTAMP:20240808T204505Z
LAST-MODIFIED:20240808T204505Z
SEQUENCE:3
UID:881a3000-eae7-4ebf-8eb4-d1bef216957d
DTSTART;TZID=Europe/Berlin:20240808T100000
DTEND;TZID=Europe/Berlin:20240808T110000
STATUS:CONFIRMED
SUMMARY:Past Event
END:VEVENT
END:VCALENDAR
36 changes: 36 additions & 0 deletions server/test/services/calendar-items.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
import { expect, jest, it } from '@jest/globals';
import app from '../../src/app';
import axios from 'axios';
import { CalendarItemData } from '../../src/services/calendar-items/calendar-items.class';
import fs from 'fs/promises';
import path from 'path';

jest.mock('axios');

describe('\'Calendar Items\' service', () => {
beforeAll(done => {
app.setup();
(app.get('databaseReady') as Promise<void>).then(done);
});

it('registered the service', () => {
const service = app.service('calendar-items');
expect(service).toBeTruthy();
});

it('should fetch a calendar feed', async () => {
const testFeed = await fs.readFile(path.resolve(__dirname, '../feed.ics'), { encoding: 'utf-8' });
axios.get.mockResolvedValue({ data: testFeed });

jest.useFakeTimers();
jest.setSystemTime(1723149778415); // simulate Thu Aug 08 2024 20:42:58 GMT+0000

const calendarFeedsService = app.service('calendar-feeds');
await calendarFeedsService.create({ name: 'Test', url: 'https://example.org/testfeed.ics' });

const service = app.service('calendar-items');
const items = await service.find({ paginate: false }) as CalendarItemData[];

expect(axios.get).toHaveBeenCalledWith('https://example.org/testfeed.ics');
expect(items.length).toBe(9);
expect(items.filter(item => item.summary === 'Recurring event weekly').length).toBe(5);
expect(items.filter(item => item.summary === 'Recurring event weekly only twice').length).toBe(2);
expect(items.filter(item => item.summary === 'Future Event').length).toBe(1);
expect(items.filter(item => item.summary === 'Past Event').length).toBe(0);
expect(items.filter(item => item.summary === 'Whole day').length).toBe(1);

jest.useRealTimers();
});
});

0 comments on commit 102e741

Please # to comment.