Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(archiveUrlsFromText): Improve logging for archived URLs with error handling #358

Merged
merged 4 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions src/util/__tests__/archiveUrlsFromText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@ describe('archiveUrlsFromText', () => {
beforeAll(() => {
// Spy on and mock the global fetch function
mockedFetch = jest.spyOn(global, 'fetch');
mockedFetch.mockImplementation(async (url, reqInit) => {
// Make Tyepscript happy
if (typeof url !== 'string')
throw new Error(
'Fetch with non-string URL is not implemented in unit test'
);

// Extract URL to archive from fetched URL
const urlToArchive = (reqInit?.body as FormData).get('url');

return {
json: async () => ({ job_id: '123', url: urlToArchive }),
} as Response;
});

realEnvs = {
INTERNET_ARCHIVE_S3_ACCESS_KEY:
Expand All @@ -54,6 +40,21 @@ describe('archiveUrlsFromText', () => {
});

it('expect URL in text are archived', async () => {
mockedFetch.mockImplementation(async (url, reqInit) => {
// Make Tyepscript happy
if (typeof url !== 'string')
throw new Error(
'Fetch with non-string URL is not implemented in unit test'
);

// Extract URL to archive from fetched URL
const urlToArchive = (reqInit?.body as FormData).get('url');

return {
json: async () => ({ job_id: '123', url: urlToArchive }),
} as Response;
});

const text =
'Please check https://example.com and https://example2.com?foo=bar&fbclid=123';
const results = await archiveUrlsFromText(text);
Expand Down Expand Up @@ -141,6 +142,34 @@ describe('archiveUrlsFromText', () => {
`);
});

it('expect errors can be handled', async () => {
mockedFetch.mockImplementation(
async () =>
({
json: async () => ({
message: 'You need to be logged in to use Save Page Now.',
}),
} as Response)
);

const text =
'Please check https://example.com and https://example2.com?foo=bar&fbclid=123';
const results = await archiveUrlsFromText(text);

// Check if result is passed
//
expect(results).toMatchInlineSnapshot(`
Array [
Object {
"message": "You need to be logged in to use Save Page Now.",
},
Object {
"message": "You need to be logged in to use Save Page Now.",
},
]
`);
});

it('do nothing if no URL in text', async () => {
const text = 'No URL here';
const results = await archiveUrlsFromText(text);
Expand Down
14 changes: 11 additions & 3 deletions src/util/archiveUrlsFromText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ export default async function archiveUrlsFromText(text: string) {
);

console.info(`[archiveUrlsFromText] Archiving ${results.length} URLs`);
results.forEach((result) =>
console.info(`[archiveUrlsFromText] [${result.job_id}]: ${result.url}`)
);
results.forEach((result, i) => {
if (result.job_id) {
console.info(`[archiveUrlsFromText] [ ${result.url} ]: ${result.job_id}`);
} else {
console.error(
`[archiveUrlsFromText] [ ${normalizedUrls[i]} ]: ${JSON.stringify(
result
)}`
);
}
});
return results;
}
Loading