Skip to content
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
53 changes: 53 additions & 0 deletions plugins/core/tests/extract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { extractLinks } from '../src/crawler/extract.js';
import { test, expect, describe, vi, afterEach } from 'vitest';
import * as cheerio from 'cheerio';

vi.mock('cheerio', async (importOriginal) => {
const mod = await importOriginal<any>();
return {
...mod,
load: vi.fn((...args: any[]) => mod.load(...args))
};
});

describe('extractLinks', () => {
afterEach(() => {
vi.restoreAllMocks();
});

test('should extract links correctly', () => {
const html = `
<html>
<body>
<a href="/foo">Foo</a>
<a href="bar">Bar</a>
<a href="https://other.com/baz">Baz</a>
<a href="#top">Top</a>
</body>
</html>
`;
const links = extractLinks(html, 'https://example.com/page/');
expect(links).toContain('https://example.com/foo');
expect(links).toContain('https://example.com/page/bar');
expect(links).toContain('https://other.com/baz');
expect(links).not.toContain('https://example.com/page/#top');
expect(links).toContain('https://example.com/page/');
});

test('should handle cheerio errors gracefully', () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
const error = new Error('Cheerio error');

vi.mocked(cheerio.load).mockImplementationOnce(() => {
throw error;
});

const links = extractLinks('<html></html>', 'https://example.com');

expect(links).toEqual([]);
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Error extracting links from https://example.com'),
error
);
});
});
19 changes: 0 additions & 19 deletions plugins/core/tests/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { normalizeUrl } from '../src/crawler/normalize.js';
import { extractLinks } from '../src/crawler/extract.js';
import { test, expect } from 'vitest';

test('normalizeUrl', () => {
Expand All @@ -11,24 +10,6 @@ test('normalizeUrl', () => {
expect(normalizeUrl('https://example.com/', '')).toBe('https://example.com/');
});

test('extractLinks', () => {
const html = `
<html>
<body>
<a href="/foo">Foo</a>
<a href="bar">Bar</a>
<a href="https://other.com/baz">Baz</a>
<a href="#top">Top</a>
</body>
</html>
`;
const links = extractLinks(html, 'https://example.com/page/');
expect(links).toContain('https://example.com/foo');
expect(links).toContain('https://example.com/page/bar');
expect(links).toContain('https://other.com/baz');
expect(links).not.toContain('https://example.com/page/#top');
expect(links).toContain('https://example.com/page/'); // #top resolves to base url without fragment
});
test('normalizeUrl: absolute resolution', () => {
expect(normalizeUrl('/foo', 'https://example.com')).toBe('https://example.com/foo');
expect(normalizeUrl('bar', 'https://example.com/baz/')).toBe('https://example.com/baz/bar');
Expand Down
Loading