feat(smart url): remove retry logic for alternate URLs along with the corresponding tests
This commit is contained in:
parent
55826b8dde
commit
eb0f2f145c
2 changed files with 13 additions and 86 deletions
|
@ -15,33 +15,18 @@ export const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g,
|
||||||
// Fetch calendar data from URL or file
|
// Fetch calendar data from URL or file
|
||||||
export async function fetchCalendarData(calendar) {
|
export async function fetchCalendarData(calendar) {
|
||||||
const isFilePath = !calendar.url.startsWith('http');
|
const isFilePath = !calendar.url.startsWith('http');
|
||||||
|
try {
|
||||||
if (isFilePath) {
|
if (isFilePath) {
|
||||||
// logger.debug(`Reading calendar from file: ${calendar.url}`);
|
// logger.debug(`Reading calendar from file: ${calendar.url}`);
|
||||||
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar };
|
return { data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'), ...calendar };
|
||||||
|
} else {
|
||||||
|
// logger.debug(`Fetching calendar from URL: ${calendar.url}`);
|
||||||
|
const response = await axios.get(calendar.url);
|
||||||
|
return { data: response.data, ...calendar };
|
||||||
}
|
}
|
||||||
try {
|
} catch (error) {
|
||||||
// First try the original URL
|
logger.error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
|
||||||
const initialResponse = await axios.get(calendar.url);
|
throw new Error(`Error retrieving calendar from ${calendar.url}: ${error.message}`);
|
||||||
return { data: initialResponse.data, ...calendar };
|
|
||||||
} catch (initialError) {
|
|
||||||
logger.debug(`Initial fetch failed, trying extension adjustment for: ${calendar.url}`);
|
|
||||||
|
|
||||||
// Determine alternate URL version
|
|
||||||
const altUrl = calendar.url.endsWith('.ics')
|
|
||||||
? calendar.url.slice(0, -4) // Remove .ics
|
|
||||||
: calendar.url + '.ics'; // Add .ics
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Try the alternate version
|
|
||||||
const altResponse = await axios.get(altUrl);
|
|
||||||
logger.debug(`Success with adjusted URL: ${altUrl}`);
|
|
||||||
return { data: altResponse.data, ...calendar };
|
|
||||||
} catch (altError) {
|
|
||||||
logger.error(`Both URL versions failed:
|
|
||||||
Original: ${calendar.url}
|
|
||||||
Adjusted: ${altUrl}`);
|
|
||||||
throw new Error(`Calendar fetch failed for both URL versions`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,12 @@ import request from 'supertest';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import axios from 'axios';
|
|
||||||
import MockAdapter from 'axios-mock-adapter';
|
|
||||||
import { fetchCalendarData } from '../src/calendarUtil.js';
|
|
||||||
|
|
||||||
// ESM equivalent of __dirname
|
// ESM equivalent of __dirname
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
const CALENDARS_DIR = path.join(process.cwd(), 'calendar');
|
const CALENDARS_DIR = path.join(__dirname, 'calendar');
|
||||||
const TEST_CALENDARS_DIR = path.join(__dirname, 'test_calendars');
|
const TEST_CALENDARS_DIR = path.join(__dirname, 'test_calendars');
|
||||||
const EXPECTED_OUTPUTS_DIR = path.join(__dirname, 'expected_outputs');
|
const EXPECTED_OUTPUTS_DIR = path.join(__dirname, 'expected_outputs');
|
||||||
|
|
||||||
|
@ -29,8 +26,9 @@ describe('Calendar Merging API', () => {
|
||||||
await new Promise(resolve => server.close(resolve));
|
await new Promise(resolve => server.close(resolve));
|
||||||
|
|
||||||
// Clean up the merged calendars directory after tests
|
// Clean up the merged calendars directory after tests
|
||||||
|
fs.rmdirSync(CALENDARS_DIR, { recursive: true });
|
||||||
if (fs.existsSync(CALENDARS_DIR)) {
|
if (fs.existsSync(CALENDARS_DIR)) {
|
||||||
fs.rmSync(CALENDARS_DIR, { recursive: true, force: true });
|
fs.rmdirSync(CALENDARS_DIR, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional: Add a delay to ensure all handles are released
|
// Optional: Add a delay to ensure all handles are released
|
||||||
|
@ -282,60 +280,4 @@ describe('Calendar Merging API', () => {
|
||||||
expect(actualOutput).toBe(expectedOutput);
|
expect(actualOutput).toBe(expectedOutput);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Smart URL Handling', () => {
|
|
||||||
let mockAxios;
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
mockAxios = new MockAdapter(axios);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
mockAxios.reset();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
mockAxios.restore();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should use original URL when valid without .ics', async () => {
|
|
||||||
const validUrl = 'https://cals.ftt.gmbh/calendar/germanholithunder';
|
|
||||||
mockAxios.onGet(validUrl).reply(200, 'VALID_CALENDAR');
|
|
||||||
|
|
||||||
const result = await fetchCalendarData({ url: validUrl });
|
|
||||||
expect(result.data).toBe('VALID_CALENDAR');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should try .ics version when original fails', async () => {
|
|
||||||
const invalidUrl = 'https://cals.ftt.gmbh/calendar/germanholithunder.ics';
|
|
||||||
const validUrl = invalidUrl.slice(0, -4);
|
|
||||||
|
|
||||||
mockAxios
|
|
||||||
.onGet(invalidUrl).reply(404)
|
|
||||||
.onGet(validUrl).reply(200, 'VALID_CALENDAR');
|
|
||||||
|
|
||||||
const result = await fetchCalendarData({ url: invalidUrl });
|
|
||||||
expect(result.data).toBe('VALID_CALENDAR');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should preserve valid .ics URLs', async () => {
|
|
||||||
const googleUrl = 'https://calendar.google.com/.../basic.ics';
|
|
||||||
mockAxios.onGet(googleUrl).reply(200, 'GOOGLE_CALENDAR');
|
|
||||||
|
|
||||||
const result = await fetchCalendarData({ url: googleUrl });
|
|
||||||
expect(result.data).toBe('GOOGLE_CALENDAR');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should try both versions for ambiguous URLs', async () => {
|
|
||||||
const baseUrl = 'https://example.com/calendar';
|
|
||||||
const icsUrl = baseUrl + '.ics';
|
|
||||||
|
|
||||||
mockAxios
|
|
||||||
.onGet(baseUrl).reply(404)
|
|
||||||
.onGet(icsUrl).reply(200, 'ICS_CALENDAR');
|
|
||||||
|
|
||||||
const result = await fetchCalendarData({ url: baseUrl });
|
|
||||||
expect(result.data).toBe('ICS_CALENDAR');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue