Compare commits

...

22 commits

Author SHA1 Message Date
29208d8b4d build(babel): install babel/core, babel/plugin-transform-modules-commonjs, and babel-jest 2024-11-20 15:21:11 +03:00
c1c6ae47de test: create jest.config,js file 2024-11-20 15:19:13 +03:00
174a845c78 fix(import): enable compatibility between ES modules and CommonJS for ical.jsRenamed ical.js to ical.cjs to explicitly mark it as a CommonJS module and used createRequire to import it into the ES module project. This resolves module system conflicts and ensures seamless integration without modifying ical.js content. 2024-11-20 13:58:35 +03:00
ad3ec6f7a6 build: update start script to correctly point to src/app.js 2024-11-20 13:25:05 +03:00
fab6753c5a fix: ensure accurate TZID inclusion in DTSTART/DTEND- Preserve TZID only when explicitly included in source calendar. - Added a helper function hasTZID to check for explicit TZID in raw properties.- Adjusted datetime processing logic in processDateTimeProperty to streamline handling of various cases. 2024-11-20 01:49:40 +03:00
379a79617b fix(lineendings): normalize calendar line endings to CRLF 2024-11-19 19:06:22 +03:00
d15d0be707 feat(calendar): extract and include X-WR-TIMEZONE if available 2024-11-19 19:03:06 +03:00
5e3e8eab5b feat(calendar): add METHOD property to calendar component if provided 2024-11-19 15:59:23 +03:00
2283b82ba7 test: add google-calendar.ics test calendar 2024-11-19 15:27:07 +03:00
2f3b791e5d test: reorder calendar components in nextcloud-minimal test calendar 2024-11-19 15:26:06 +03:00
ac26afe778 feat(calendars): reorder calendar components 2024-11-19 15:24:16 +03:00
e5e9dbb680 test: add test calendar for google-calendar-minimal 2024-11-19 15:22:43 +03:00
8881361a1f text: create and run test for google-calendar-mininal 2024-11-19 15:21:19 +03:00
8965eaa319 fix(line-endings): normalize line endings to LF for cross-platform consistency- Normalized line endings in generated calendar content to LF.- Updated test assertions to handle line-ending discrepancies.- Ensured compatibility across Windows and UNIX-like systems. 2024-11-19 14:17:42 +03:00
ca52065f66 feat(calendar): include CALSCALE and SEQUENCE details 2024-11-19 14:03:42 +03:00
8ab685dec0 test: run tests for nextcloud minimal alone first 2024-11-19 13:35:14 +03:00
585b83b03c test: run tests for nextcloud minimal alone first 2024-11-19 13:34:35 +03:00
73e46d7f99 feat(prodid): introduce custom PRODID for merged calendars 2024-11-19 13:32:55 +03:00
d2b0234b4b feat(location): conditionally handle VEVENT location based on override flag 2024-11-19 13:21:22 +03:00
2b7002303c fix(calendar): align VEVENT property order and include time zone details 2024-11-19 01:29:32 +03:00
6cf5b8420e feat(calendar): copy summary, timestamps, and recurrence details for VEVENTs 2024-11-18 23:49:50 +03:00
ddcfedbbdf feat(calendar): support extracting and writing VTIMEZONE subcomponents 2024-11-18 23:40:41 +03:00
10 changed files with 285 additions and 114 deletions

7
jest.config.js Normal file
View file

@ -0,0 +1,7 @@
export default {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
moduleFileExtensions: ['js', 'jsx'],
testEnvironment: 'node',
};

9
package-lock.json generated
View file

@ -17,8 +17,11 @@
"supertest": "^7.0.0"
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/register": "^7.25.9",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"rewire": "^7.0.0"
}
@ -3181,9 +3184,9 @@
}
},
"node_modules/cross-spawn": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
"dev": true,
"dependencies": {
"path-key": "^3.1.0",

View file

@ -2,7 +2,7 @@
"name": "calendar-merger",
"version": "1.1.0",
"scripts": {
"start": "node app.js",
"start": "node src/app.js",
"test": "jest ./test"
},
"dependencies": {
@ -15,12 +15,15 @@
},
"description": "calmerger",
"main": "server.js",
"type": "module",
"author": "Ryan",
"type": "module",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/register": "^7.25.9",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"rewire": "^7.0.0"
}

View file

@ -26,77 +26,128 @@ export async function fetchCalendarData(calendar) {
}
}
// Helper function to check if TZID exists in the raw property string
function hasTZID(rawProperty) {
return rawProperty.includes('TZID=');
}
// Function to process DTSTART/DTEND
function processDateTimeProperty(event, propertyName, newEvent) {
const rawProperty = event.getFirstProperty(propertyName)?.toICALString();
if (!rawProperty) return;
const dateTime = event.getFirstPropertyValue(propertyName);
const dateTimeString = dateTime.toString();
const property = new ICAL.Property(propertyName, newEvent);
property.setValue(dateTimeString);
if (hasTZID(rawProperty)) {
// If raw property includes TZID, add it
property.setParameter('TZID', dateTime.zone.tzid);
}
newEvent.addProperty(property);
}
// Create a top-level VCALENDAR component
export function createCalendarComponent(name) {
const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
calendarComponent.updatePropertyWithValue('name', name);
calendarComponent.updatePropertyWithValue('prodid', '-//CalMerge//Calendar Merger 1.0//EN');
calendarComponent.updatePropertyWithValue('version', '2.0');
calendarComponent.updatePropertyWithValue('calscale', 'GREGORIAN');
return calendarComponent;
}
// Add events to the calendar component
export function addEventsToCalendar(calendarComponent, results) {
export function addEventsToCalendar(calendarComponent, results, overrideFlag = false) {
let defaultTimeZone = null; // To store the first found X-WR-TIMEZONE
results.forEach((result) => {
try {
const parsed = ICAL.parse(result.data);
const component = new ICAL.Component(parsed);
// Extract METHOD from the parsed data (if available)
const method = component.getFirstPropertyValue('method');
if (method) {
console.log(`Extracted METHOD: ${method}`);
// Only add the METHOD property once
if (!calendarComponent.getFirstPropertyValue('method')) {
calendarComponent.updatePropertyWithValue('method', method.toUpperCase());
}
}
// Extract X-WR-TIMEZONE if available
const wrTimeZone = component.getFirstPropertyValue('x-wr-timezone');
if (wrTimeZone) {
console.log(`Extracted X-WR-TIMEZONE: ${wrTimeZone}`);
// Set it as the default if not already set
if (!defaultTimeZone) {
defaultTimeZone = wrTimeZone;
if (!calendarComponent.getFirstPropertyValue('x-wr-timezone')) {
calendarComponent.updatePropertyWithValue('x-wr-timezone', defaultTimeZone);
}
}
}
// Extract and add VTIMEZONE components
const timezones = component.getAllSubcomponents('vtimezone');
timezones.forEach((timezone) => {
const tzid = timezone.getFirstPropertyValue('tzid');
if (!calendarComponent.getFirstSubcomponent((comp) => comp.name === 'vtimezone' && comp.getFirstPropertyValue('tzid') === tzid)) {
calendarComponent.addSubcomponent(timezone);
}
});
// Process VEVENT components
component.getAllSubcomponents('vevent').forEach((event) => {
const vevent = new ICAL.Event(event);
const newEvent = new ICAL.Component('vevent');
// 1. Add DTSTART
processDateTimeProperty(event, 'dtstart', newEvent);
// Use ICAL.Time to handle dates correctly
const startDate = vevent.startDate;
const endDate = vevent.endDate;
// 2. Add DTEND
processDateTimeProperty(event, 'dtend', newEvent);
// Create new ICAL.Time objects for start and end dates
const startTime = new ICAL.Time({
year: startDate.year,
month: startDate.month,
day: startDate.day,
hour: startDate.isDate ? null : startDate.hour,
minute: startDate.isDate ? null : startDate.minute,
second: startDate.isDate ? null : startDate.second,
zone: startDate.zone
});
startTime.isDate = startDate.isDate;
// 3. Copy DTSTAMP
const dtstamp = event.getFirstPropertyValue('dtstamp');
if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp);
const endTime = new ICAL.Time({
year: endDate.year,
month: endDate.month,
day: endDate.day,
hour: endDate.isDate ? null : endDate.hour,
minute: endDate.isDate ? null : endDate.minute,
second: endDate.isDate ? null : endDate.second,
zone: endDate.zone
});
endTime.isDate = endDate.isDate;
// Retain the existing DTSTAMP from vevent
const dtstampProperty = event.getFirstProperty('dtstamp');
const dtstamp = dtstampProperty ? dtstampProperty.getFirstValue() : null;
// Add properties to the new event
// 4. Copy UID
newEvent.updatePropertyWithValue('uid', vevent.uid);
if (dtstamp) {
newEvent.updatePropertyWithValue('dtstamp', dtstamp); // Retain the existing DTSTAMP
// 5. Add LOCATION (conditionally included)
if (!overrideFlag && vevent.location) {
newEvent.updatePropertyWithValue('location', vevent.location);
} else if (overrideFlag && vevent.location) {
// Modify SUMMARY if override is set
const modifiedSummary = `${vevent.summary.trim()} (Location omitted)`;
newEvent.updatePropertyWithValue('summary', modifiedSummary);
} else {
console.warn('DTSTAMP not found in the original event.'); // Warn if DTSTAMP is missing
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
}
// 6. Copy Recurrence Rules (RRULE) and Recurrence ID
const rrule = event.getFirstPropertyValue('rrule');
if (rrule) newEvent.updatePropertyWithValue('rrule', rrule);
const recurrenceId = event.getFirstPropertyValue('recurrence-id');
if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId);
// Set the dtstart and dtend properties using ICAL.Time
newEvent.updatePropertyWithValue('dtstart', startTime);
newEvent.updatePropertyWithValue('dtend', endTime);
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
// 7. Copy SUMMARY
newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
// Add the new event to the calendar component
// 8. Add SEQUENCE (if available or default to 0)
const sequence = event.getFirstPropertyValue('sequence') || 0;
newEvent.updatePropertyWithValue('sequence', sequence);
// Add the VEVENT to the calendar
calendarComponent.addSubcomponent(newEvent);
});
// Log the added events for debugging
console.log('Added events:', calendarComponent.toString());
} catch (error) {
console.log(`Processed VEVENT components for calendar: ${result.name}`);
} catch (error) {
console.error('Error processing calendar data:', error.message);
}
});
@ -104,8 +155,9 @@ export function addEventsToCalendar(calendarComponent, results) {
// Save calendar data to file
export function saveCalendarFile(filename, content) {
const normalizedContent = content.replace(/\r?\n/g, '\r\n').trimEnd(); // Normalize to CRLF
const filePath = path.join(MERGED_CALENDARS_DIR, filename);
console.log(`Saving calendar data to file: ${filePath}`);
fs.writeFileSync(filePath, content);
// console.log(`Saving calendar data to file: ${filePath}`);
fs.writeFileSync(filePath, normalizedContent);
return filePath;
}

View file

@ -1,4 +1,7 @@
const ICAL = require('./ical.js');
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const ICAL = require('./ical.cjs');
(function() {
function register(tzdata) { ICAL.TimezoneService.register(ICAL.Component.fromString("BEGIN:VTIMEZONE\r\n" + tzdata + "\r\nEND:VTIMEZONE")) };
ICAL.TimezoneService.IANA_TZDB_VERSION = "2024b";

View file

@ -9,9 +9,11 @@ const EXPECTED_OUTPUTS_DIR = path.join(__dirname, 'expected_outputs');
let server;
process.chdir(__dirname)
console.log(process.cwd());
// console.log(process.cwd());
const app = require('../src/server').default;
const normalizeLineEndings = (str) => str.replace(/\r\n/g, '\r\n').trimEnd(); // Normalize to CRLF
describe('Calendar Merging API', () => {
beforeAll(async () => {
// Start the server
@ -51,20 +53,27 @@ describe('Calendar Merging API', () => {
expect(response.status).toBe(200);
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'nextcloud-minimal.ics');
console.log('Checking if file exists at:', filePath);
// console.log('Checking if file exists at:', filePath);
expect(fs.existsSync(filePath)).toBe(true);
// Load expected output and compare
// Load expected output
const expectedOutput = fs.readFileSync(input, 'utf8');
const actualOutput = fs.readFileSync(filePath, 'utf8');
expect(actualOutput).toBe(expectedOutput);
// Normalize line endings
const normalizedActual = normalizeLineEndings(actualOutput);
const normalizedExpected = normalizeLineEndings(expectedOutput);
//compare
expect(normalizedActual).toBe(normalizedExpected);
});
test('Preserve date-based calendar', async () => {
const input = getTestCalendarFilename('US_Holidays.ics');
test('Preserve google calendar', async () => {
const input = getTestCalendarFilename('google-calendar-minimal.ics');
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'US Holidays',
linkGroupName: 'google-calendar-minimal',
calendars: [
{
url: input,
@ -75,15 +84,48 @@ describe('Calendar Merging API', () => {
});
expect(response.status).toBe(200);
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'US_Holidays.ics');
console.log('Checking if file exists at:', filePath);
const filePath = path.join(CALENDARS_DIR, 'google-calendar-minimal.ics');
// console.log('Checking if file exists at:', filePath);
expect(fs.existsSync(filePath)).toBe(true);
// Load expected output and compare
// Load expected output
const expectedOutput = fs.readFileSync(input, 'utf8');
const actualOutput = fs.readFileSync(filePath, 'utf8');
expect(actualOutput).toBe(expectedOutput);
// Normalize line endings
const normalizedActual = normalizeLineEndings(actualOutput);
const normalizedExpected = normalizeLineEndings(expectedOutput);
//compare
expect(normalizedActual).toBe(normalizedExpected);
});
// test('Preserve date-based calendar', async () => {
// const input = getTestCalendarFilename('US_Holidays.ics');
// const response = await request(server)
// .post('/merge')
// .send({
// linkGroupName: 'US Holidays',
// calendars: [
// {
// url: input,
// prefix: '',
// override: false,
// },
// ],
// });
// expect(response.status).toBe(200);
// // Check if the file was created in the test directory
// const filePath = path.join(CALENDARS_DIR, 'US_Holidays.ics');
// console.log('Checking if file exists at:', filePath);
// expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = fs.readFileSync(input, 'utf8');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// expect(actualOutput).toBe(expectedOutput);
// });
// test('Merge date-based calendar', async () => {
// const response = await request(server)
// .post('/merge')
@ -116,61 +158,61 @@ describe('Calendar Merging API', () => {
// expect(actualOutput).toBe(expectedOutput);
// });
test('Merge time-based calendar', async () => {
const input = getTestCalendarFilename('work_task_calendar.ics');
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'Time Based Calendar',
calendars: [
{
url: input,
prefix: 'work_task',
override: false,
},
],
});
// test('Merge time-based calendar', async () => {
// const input = getTestCalendarFilename('work_task_calendar.ics');
// const response = await request(server)
// .post('/merge')
// .send({
// linkGroupName: 'Time Based Calendar',
// calendars: [
// {
// url: input,
// prefix: 'work_task',
// override: false,
// },
// ],
// });
expect(response.status).toBe(200);
expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/);
// expect(response.status).toBe(200);
// expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/);
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'Time_Based_Calendar.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Check if the file was created in the test directory
// const filePath = path.join(CALENDARS_DIR, 'Time_Based_Calendar.ics');
// expect(fs.existsSync(filePath)).toBe(true);
// Load expected output and compare
const expectedOutput = fs.readFileSync(input, 'utf8');
const actualOutput = fs.readFileSync(filePath, 'utf8');
expect(actualOutput).toBe(expectedOutput);
});
// // Load expected output and compare
// const expectedOutput = fs.readFileSync(input, 'utf8');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// expect(actualOutput).toBe(expectedOutput);
// });
test('EAT Event', async () => {
const input = getTestCalendarFilename('eat_time_zone_event.ics');
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'EAT Event',
calendars: [
{
url: input,
prefix: 'EAT Event',
override: false,
},
],
});
// test('EAT Event', async () => {
// const input = getTestCalendarFilename('eat_time_zone_event.ics');
// const response = await request(server)
// .post('/merge')
// .send({
// linkGroupName: 'EAT Event',
// calendars: [
// {
// url: input,
// prefix: 'EAT Event',
// override: false,
// },
// ],
// });
expect(response.status).toBe(200);
expect(response.body.url).toMatch(/calendar\/EAT_Event/);
// expect(response.status).toBe(200);
// expect(response.body.url).toMatch(/calendar\/EAT_Event/);
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'EAT_Event.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Check if the file was created in the test directory
// const filePath = path.join(CALENDARS_DIR, 'EAT_Event.ics');
// expect(fs.existsSync(filePath)).toBe(true);
// Load expected output and compare
const expectedOutput = fs.readFileSync(input, 'utf8');
const actualOutput = fs.readFileSync(filePath, 'utf8');
expect(actualOutput).toBe(expectedOutput);
});
// // Load expected output and compare
// const expectedOutput = fs.readFileSync(input, 'utf8');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// expect(actualOutput).toBe(expectedOutput);
// });
// test('Merge calendar without prefix', async () => {
// const response = await request(server)

View file

@ -0,0 +1,16 @@
BEGIN:VCALENDAR
NAME:google-calendar-minimal
PRODID:-//CalMerge//Calendar Merger 1.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:Africa/Nairobi
BEGIN:VEVENT
DTSTART:20241003T190000Z
DTEND:20241003T200000Z
DTSTAMP:20241119T115316Z
UID:6tbrvsitniuu72li7kk15gou2b@google.com
SUMMARY:progodessey
SEQUENCE:0
END:VEVENT
END:VCALENDAR

View file

@ -0,0 +1,44 @@
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:work
X-WR-TIMEZONE:Africa/Nairobi
BEGIN:VEVENT
DTSTART:20240930T113000Z
DTEND:20240930T123000Z
DTSTAMP:20241119T115316Z
UID:0d0p2hp0l26ebuk2r0kb1q9kuo@google.com
CREATED:20240930T111532Z
LAST-MODIFIED:20240930T111532Z
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:other work
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20241003T190000Z
DTEND:20241003T200000Z
DTSTAMP:20241119T115316Z
UID:6tbrvsitniuu72li7kk15gou2b@google.com
CREATED:20241001T194455Z
LAST-MODIFIED:20241001T194455Z
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:progodessey
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20241009T173000Z
DTEND:20241009T183000Z
DTSTAMP:20241119T115316Z
UID:7l7n9nltrudluv65gfgll2q930@google.com
CREATED:20241010T123337Z
LAST-MODIFIED:20241010T123337Z
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:do
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

View file

@ -1,7 +1,8 @@
BEGIN:VCALENDAR
NAME:nextcloud-minimal
PRODID:-//CalMerge//Calendar Merger 1.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//SabreDAV//SabreDAV//EN
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
@ -20,13 +21,13 @@ RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;TZID=Europe/Berlin:20241120T211500
DTEND;TZID=Europe/Berlin:20241120T215000
DTSTAMP:20241113T212909Z
DTSTART;TZID=Europe/Berlin:20241120T211500
UID:5f4ad965-16a8-48eb-8233-78bf93a8b35e
LOCATION:FaceTime
RRULE:FREQ=WEEKLY;BYDAY=WE
SEQUENCE:0
SUMMARY:JR Weekly Check-In
UID:5f4ad965-16a8-48eb-8233-78bf93a8b35e
SEQUENCE:0
END:VEVENT
END:VCALENDAR