2024-10-29 18:24:44 +00:00
|
|
|
import request from 'supertest';
|
2024-10-29 18:26:17 +00:00
|
|
|
import express from 'express';
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2024-10-29 18:51:24 +00:00
|
|
|
import app from './server';
|
2024-10-29 18:27:29 +00:00
|
|
|
|
2024-10-31 12:46:44 +00:00
|
|
|
const MERGED_CALENDARS_DIR = 'calendar';
|
2024-11-04 10:10:43 +00:00
|
|
|
const TEST_CALENDARS_DIR = 'test_calendars';
|
2024-10-30 10:58:05 +00:00
|
|
|
let server;
|
2024-10-29 18:29:24 +00:00
|
|
|
|
|
|
|
describe('Calendar Merging API', () => {
|
2024-10-30 11:00:02 +00:00
|
|
|
beforeAll(async () => {
|
|
|
|
// Start the server
|
2024-10-30 11:54:19 +00:00
|
|
|
server = app.listen(0);
|
2024-10-30 11:00:02 +00:00
|
|
|
console.log(`Server started on port 3000`);
|
2024-10-30 12:00:20 +00:00
|
|
|
|
2024-10-29 18:29:24 +00:00
|
|
|
// Ensure the merged calendars directory exists
|
|
|
|
if (!fs.existsSync(MERGED_CALENDARS_DIR)) {
|
|
|
|
fs.mkdirSync(MERGED_CALENDARS_DIR);
|
|
|
|
}
|
|
|
|
});
|
2024-10-30 11:03:00 +00:00
|
|
|
|
|
|
|
afterAll( async () => {
|
2024-10-29 18:32:00 +00:00
|
|
|
// Clean up the merged calendars directory after tests
|
2024-10-30 11:55:41 +00:00
|
|
|
if (fs.existsSync(MERGED_CALENDARS_DIR)) {
|
2024-10-30 12:46:20 +00:00
|
|
|
fs.rmSync(MERGED_CALENDARS_DIR, { recursive: true, force: true });
|
2024-10-30 11:55:41 +00:00
|
|
|
}
|
2024-10-30 11:56:38 +00:00
|
|
|
// Close the server
|
|
|
|
await new Promise(resolve => server.close(resolve));
|
2024-10-29 18:32:00 +00:00
|
|
|
});
|
2024-10-29 18:29:24 +00:00
|
|
|
|
2024-11-04 10:12:44 +00:00
|
|
|
const loadCalendarFile = (filename) => {
|
|
|
|
return path.join(__dirname, TEST_CALENDARS_DIR, filename);
|
|
|
|
};
|
|
|
|
|
2024-10-29 18:33:03 +00:00
|
|
|
test('Merge date-based calendar', async () => {
|
2024-10-30 12:00:20 +00:00
|
|
|
const response = await request(server)
|
2024-10-29 18:34:45 +00:00
|
|
|
.post('/merge')
|
|
|
|
.send({
|
|
|
|
linkGroupName: 'Date Based Calendar',
|
|
|
|
calendars: [
|
|
|
|
{
|
|
|
|
url: 'https://www.schulferien.org/media/ical/deutschland/ferien_bayern_2023.ics?k=PsL0S2B9rScFMn5PAxtf4OVQjMkWZsqqkK13zEJ0FCW5Q-2xQejfLJYaTN4EdYUsQHLDDbGVnVl93ms7en5vMUISjZ3H9Esu88Vp2ndnL5Q',
|
|
|
|
prefix: 'Date Event',
|
|
|
|
override: false,
|
|
|
|
},
|
2024-10-31 12:41:08 +00:00
|
|
|
{
|
|
|
|
"prefix": "holidays",
|
|
|
|
"override": false,
|
|
|
|
"url": "https://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics"
|
2024-10-31 12:43:13 +00:00
|
|
|
},
|
2024-10-29 18:34:45 +00:00
|
|
|
],
|
|
|
|
});
|
2024-10-29 18:35:41 +00:00
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(response.body.url).toMatch(/calendar\/Date_Based_Calendar/);
|
2024-10-29 18:40:57 +00:00
|
|
|
|
|
|
|
// Check if the file was created
|
|
|
|
const filePath = path.join(MERGED_CALENDARS_DIR, 'Date_Based_Calendar.ics');
|
|
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
|
|
});
|
2024-10-30 11:23:44 +00:00
|
|
|
|
2024-10-29 18:43:11 +00:00
|
|
|
test('Merge time-based calendar', async () => {
|
2024-10-30 12:00:20 +00:00
|
|
|
const response = await request(server)
|
2024-10-31 12:24:02 +00:00
|
|
|
.post('/merge')
|
2024-10-29 18:43:11 +00:00
|
|
|
.send({
|
|
|
|
linkGroupName: 'Time Based Calendar',
|
|
|
|
calendars: [
|
|
|
|
{
|
|
|
|
url: 'https://www.calendarlabs.com/ical-calendar/ics/65/San_Francisco_Public_Holidays.ics',
|
|
|
|
prefix: 'Time Event',
|
|
|
|
override: false,
|
|
|
|
},
|
2024-10-31 12:43:13 +00:00
|
|
|
{
|
|
|
|
"prefix": "work",
|
|
|
|
"override": false,
|
|
|
|
"url": "https://calendar.google.com/calendar/ical/b4c66eb4bb2cc15257d071bab3f935385778b042112ea1aaedada47f3f1a6e3a%40group.calendar.google.com/public/basic.ics"
|
|
|
|
},
|
2024-10-29 18:43:11 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/);
|
|
|
|
|
|
|
|
// Check if the file was created
|
|
|
|
const filePath = path.join(MERGED_CALENDARS_DIR, 'Time_Based_Calendar.ics');
|
|
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
|
|
});
|
2024-10-30 11:23:44 +00:00
|
|
|
|
2024-10-29 18:45:09 +00:00
|
|
|
test('Merge calendar without prefix', async () => {
|
2024-10-30 12:00:20 +00:00
|
|
|
const response = await request(server)
|
2024-10-29 18:45:09 +00:00
|
|
|
.post('/merge')
|
|
|
|
.send({
|
|
|
|
linkGroupName: 'No Prefix Calendar',
|
|
|
|
calendars: [
|
|
|
|
{
|
|
|
|
url: 'https://www.calendarlabs.com/ical-calendar/ics/65/San_Francisco_Public_Holidays.ics',
|
|
|
|
prefix: '',
|
|
|
|
override: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(response.body.url).toMatch(/calendar\/No_Prefix_Calendar/);
|
|
|
|
|
|
|
|
// Check if the file was created
|
|
|
|
const filePath = path.join(MERGED_CALENDARS_DIR, 'No_Prefix_Calendar.ics');
|
|
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
|
|
});
|
2024-10-30 11:23:44 +00:00
|
|
|
|
2024-10-29 18:46:40 +00:00
|
|
|
test('Merge calendar with override', async () => {
|
2024-10-30 12:00:20 +00:00
|
|
|
const response = await request(server)
|
2024-10-29 18:46:40 +00:00
|
|
|
.post('/merge')
|
|
|
|
.send({
|
|
|
|
linkGroupName: 'Override Calendar',
|
|
|
|
calendars: [
|
|
|
|
{
|
|
|
|
url: 'https://www.calendarlabs.com/ical-calendar/ics/65/San_Francisco_Public_Holidays.ics',
|
|
|
|
prefix: 'Override Event',
|
|
|
|
override: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(response.body.url).toMatch(/calendar\/Override_Calendar/);
|
|
|
|
|
|
|
|
// Check if the file was created
|
|
|
|
const filePath = path.join(MERGED_CALENDARS_DIR, 'Override_Calendar.ics');
|
|
|
|
expect(fs.existsSync(filePath)).toBe(true);
|
|
|
|
});
|
2024-10-29 18:33:03 +00:00
|
|
|
|
2024-10-29 18:29:24 +00:00
|
|
|
});
|