CalMerger/test/calendar.test.js

219 lines
8.2 KiB
JavaScript
Raw Normal View History

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:27:29 +00:00
const CALENDARS_DIR = path.join(__dirname, 'calendar')
const TEST_CALENDARS_DIR = path.join(__dirname, 'test_calendars');
const EXPECTED_OUTPUTS_DIR = path.join(__dirname, 'expected_outputs');
let server;
process.chdir(__dirname)
console.log(process.cwd());
const app = require('../server').default;
describe('Calendar Merging API', () => {
2024-10-30 11:00:02 +00:00
beforeAll(async () => {
// Start the server
server = app.listen(0);
});
2024-10-30 11:03:00 +00:00
2024-11-04 15:24:10 +00:00
afterAll(async () => {
// Ensure the server is closed before cleanup
await new Promise(resolve => server.close(resolve));
// Optional: Add a delay to ensure all handles are released
await new Promise(resolve => setTimeout(resolve, 100));
});
2024-11-04 10:12:44 +00:00
const loadCalendarFile = (filename) => {
return path.join(TEST_CALENDARS_DIR, filename);
2024-11-04 10:12:44 +00:00
};
const loadExpectedOutput = (filename) => {
return fs.readFileSync(path.join(EXPECTED_OUTPUTS_DIR, filename), 'utf8');
};
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: loadCalendarFile('holiday_calendar_2023.ics'),
prefix: 'holiday_calendar_2023',
2024-10-29 18:34:45 +00:00
override: false,
},
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('US_Holidays.ics'),
prefix: 'US_holidays',
override: false,
},
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(new RegExp(`calendar/Date_Based_Calendar`));
2024-10-29 18:40:57 +00:00
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'Date_Based_Calendar.ics');
console.log('Checking if file exists at:', filePath);
2024-10-29 18:40:57 +00:00
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('Date_Based_Calendar.ics');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
2024-10-29 18:40:57 +00:00
});
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-11-04 15:24:10 +00:00
.post('/merge')
2024-10-29 18:43:11 +00:00
.send({
linkGroupName: 'Time Based Calendar',
calendars: [
{
url: loadCalendarFile('team_meeting_calendar.ics'),
prefix: 'team_meeting_calendar',
2024-10-29 18:43:11 +00:00
override: false,
},
{
url: loadCalendarFile('work_task_calendar.ics'),
prefix: 'work_task',
override: false,
},
2024-10-29 18:43:11 +00:00
],
});
expect(response.status).toBe(200);
expect(response.body.url).toMatch(/calendar\/Time_Based_Calendar/);
2024-10-29 18:43:11 +00:00
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'Time_Based_Calendar.ics');
2024-10-29 18:43:11 +00:00
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('Time_Based_Calendar.ics');
// const actualOutput = fs.readFileSync (filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
2024-10-29 18:43:11 +00:00
});
2024-10-30 11:23:44 +00:00
test('Merge calendar without prefix', async () => {
2024-10-30 12:00:20 +00:00
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'No Prefix Calendar',
calendars: [
{
url: loadCalendarFile('sf_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 in the test directory
const filePath = path.join(CALENDARS_DIR, 'No_Prefix_Calendar.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('No_Prefix_Calendar.ics');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
});
2024-10-30 11:23:44 +00:00
test('Merge calendar with override', async () => {
2024-10-30 12:00:20 +00:00
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'Override Calendar',
calendars: [
{
url: loadCalendarFile('sf_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 in the test directory
const filePath = path.join(CALENDARS_DIR, 'Override_Calendar.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('Override_Calendar.ics');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
});
2024-10-29 18:33:03 +00:00
test('Merge UTC and EAT time zone calendar', async () => {
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'UTCEAT Time Zone Calendar',
calendars: [
{
url: loadCalendarFile('utc_time_zone_event.ics'),
prefix: 'UTC_Event',
override: false,
},
{
url: loadCalendarFile('eat_time_zone_event.ics'),
prefix: 'EAT_Event',
override: false,
},
],
});
expect(response.status).toBe(200);
expect(response.body.url).toMatch(new RegExp(`calendar/UTCEAT_Time_Zone_Calendar`));
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'UTCEAT_Time_Zone_Calendar.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('UTC_Time_Zone_Calendar.ics');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
});
test('Merge date-based and time-based calendars', async () => {
const response = await request(server)
.post('/merge')
.send({
linkGroupName: 'Merged Date and Time Based Calendar',
calendars: [
{
url: loadCalendarFile('holiday_calendar_2023.ics'), // Date-based calendar
prefix: 'Holiday_2023',
override: false,
},
{
url: loadCalendarFile('work_task_calendar.ics'), // Time-based calendar
prefix: 'Work_Task',
override: false,
},
],
});
expect(response.status).toBe(200);
expect(response.body.url).toMatch(new RegExp('calendar/Merged_Date_and_Time_Based_Calendar'));
// Check if the file was created in the test directory
const filePath = path.join(CALENDARS_DIR, 'Merged_Date_and_Time_Based_Calendar.ics');
expect(fs.existsSync(filePath)).toBe(true);
// // Load expected output and compare
// const expectedOutput = loadExpectedOutput('Merged_Date_and_Time_Based_Calendar.ics');
// const actualOutput = fs.readFileSync(filePath, 'utf8');
// // expect(actualOutput).toBe(expectedOutput);
});
2024-11-04 15:24:10 +00:00
});