1
0
Fork 0
CalMerger/calendar.test.js

164 lines
6.1 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:51:24 +00:00
import app from './server';
2024-10-29 18:27:29 +00:00
2024-11-05 11:32:53 +00:00
// Set environment variable for the test directory
process.env.TEST_MERGED_CALENDARS_DIR = path.join(__dirname, 'temp_test_calendar');
const TEST_MERGED_CALENDARS_DIR = process.env.TEST_MERGED_CALENDARS_DIR;
const TEST_CALENDARS_DIR = 'test_calendars';
const EXPECTED_OUTPUTS_DIR = 'expected_outputs';
2024-11-05 11:32:53 +00:00
console.log(`Test Merged Calendars Directory: ${TEST_MERGED_CALENDARS_DIR}`);
let server;
describe('Calendar Merging API', () => {
2024-10-30 11:00:02 +00:00
beforeAll(async () => {
// Change the working directory to the test-specific directory
process.chdir(path.join(__dirname, 'temp_test_calendar'));
2024-10-30 11:00:02 +00:00
// Start the server
2024-10-30 11:54:19 +00:00
server = app.listen(0);
// Ensure the test merged calendars directory exists
if (!fs.existsSync(TEST_MERGED_CALENDARS_DIR)) {
fs.mkdirSync(TEST_MERGED_CALENDARS_DIR, { recursive: true });
}
});
2024-10-30 11:03:00 +00:00
2024-11-04 15:24:10 +00:00
afterAll(async () => {
2024-11-05 11:32:53 +00:00
// // Clean up the merged calendars directory after tests
// if (fs.existsSync(TEST_MERGED_CALENDARS_DIR)) {
// fs.rmSync(TEST_MERGED_CALENDARS_DIR, { recursive: true, force: true });
// }
// Close the server
2024-10-30 11:56:38 +00:00
await new Promise(resolve => server.close(resolve));
});
2024-11-04 10:12:44 +00:00
const loadCalendarFile = (filename) => {
return path.join(__dirname, TEST_CALENDARS_DIR, filename);
};
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: [
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('ferien_bayern_2023.ics'),
prefix: 'Ferien_Bayern_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(/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(TEST_MERGED_CALENDARS_DIR, 'Date_Based_Calendar.ics');
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: [
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('other_work.ics'),
prefix: 'other_work',
2024-10-29 18:43:11 +00:00
override: false,
},
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('work.ics'),
prefix: 'work',
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(TEST_MERGED_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: [
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('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 in the test directory
2024-11-05 11:32:53 +00:00
const filePath = path.join(TEST_MERGED_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: [
{
2024-11-04 15:24:10 +00:00
url: loadCalendarFile('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 in the test directory
const filePath = path.join(TEST_MERGED_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
2024-11-04 15:24:10 +00:00
});