adding unit tests and bugfixing
This commit is contained in:
parent
d23a7ae591
commit
b375ce3e39
97
apps/api/src/scraper/WebScraper/__tests__/index.test.ts
Normal file
97
apps/api/src/scraper/WebScraper/__tests__/index.test.ts
Normal file
@ -0,0 +1,97 @@
|
||||
import { WebScraperDataProvider } from '../index';
|
||||
|
||||
describe('WebScraperDataProvider', () => {
|
||||
describe('replaceImgPathsWithAbsolutePaths', () => {
|
||||
it('should replace image paths with absolute paths', () => {
|
||||
const webScraperDataProvider = new WebScraperDataProvider();
|
||||
const documents = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/data-image' },
|
||||
content: '',
|
||||
}
|
||||
];
|
||||
|
||||
const expectedDocuments = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/data-image' },
|
||||
content: '',
|
||||
}
|
||||
];
|
||||
|
||||
const result = webScraperDataProvider.replaceImgPathsWithAbsolutePaths(documents);
|
||||
expect(result).toEqual(expectedDocuments);
|
||||
});
|
||||
|
||||
it('should handle absolute URLs without modification', () => {
|
||||
const webScraperDataProvider = new WebScraperDataProvider();
|
||||
const documents = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: '',
|
||||
}
|
||||
];
|
||||
|
||||
const expectedDocuments = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: '',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: '',
|
||||
}
|
||||
];
|
||||
|
||||
const result = webScraperDataProvider.replaceImgPathsWithAbsolutePaths(documents);
|
||||
expect(result).toEqual(expectedDocuments);
|
||||
});
|
||||
|
||||
it('should not replace non-image content within the documents', () => {
|
||||
const webScraperDataProvider = new WebScraperDataProvider();
|
||||
const documents = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: 'This is a test.  Here is a link: [Example](https://example.com).',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: 'Another test.  Here is some **bold text**.',
|
||||
}
|
||||
];
|
||||
|
||||
const expectedDocuments = [
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/page' },
|
||||
content: 'This is a test.  Here is a link: [Example](https://example.com).',
|
||||
},
|
||||
{
|
||||
metadata: { sourceURL: 'https://example.com/another-page' },
|
||||
content: 'Another test.  Here is some **bold text**.',
|
||||
}
|
||||
];
|
||||
|
||||
const result = webScraperDataProvider.replaceImgPathsWithAbsolutePaths(documents);
|
||||
expect(result).toEqual(expectedDocuments);
|
||||
});
|
||||
});
|
||||
});
|
@ -325,19 +325,24 @@ export class WebScraperDataProvider {
|
||||
documents.forEach(document => {
|
||||
const baseUrl = new URL(document.metadata.sourceURL).origin;
|
||||
const images = document.content.match(/!\[.*?\]\(((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\)/g) || [];
|
||||
|
||||
|
||||
images.forEach(image => {
|
||||
let imageUrl = image.match(/\(([^)]+)\)/)[1];
|
||||
let altText = image.match(/\[(.*?)\]/)[1];
|
||||
|
||||
|
||||
if (!imageUrl.startsWith("data:image")) {
|
||||
imageUrl = baseUrl + imageUrl;
|
||||
if (!imageUrl.startsWith("http")) {
|
||||
if (imageUrl.startsWith("/")) {
|
||||
imageUrl = imageUrl.substring(1);
|
||||
}
|
||||
imageUrl = new URL(imageUrl, baseUrl).toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
document.content = document.content.replace(image, ``);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
return documents;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user