generated from polymech/site-template
106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest';
|
|
import { PuppeteerUrlChecker, FetchUrlChecker, checkUrl, UrlCheckResult, clean } from './url.js';
|
|
|
|
describe('URL Checker', () => {
|
|
// Test URLs
|
|
const validUrl = 'https://backend.orbit.dtu.dk/ws/portalfiles/portal/278424474/Bertelsen_et_al_2022.pdf';
|
|
const validUrl_ = 'https://dl.asminternational.org/technical-books/edited-volume/157/Extrusion';
|
|
const invalidUrl = 'https://example.com/404';
|
|
const timeoutUrl = 'https://example.com/timeout';
|
|
|
|
// Increase timeout for real browser tests
|
|
vi.setConfig({ testTimeout: 30000 });
|
|
|
|
// Clean up after all tests
|
|
afterAll(async () => {
|
|
await clean();
|
|
});
|
|
|
|
describe('PuppeteerUrlChecker', () => {
|
|
const checker = new PuppeteerUrlChecker();
|
|
|
|
it('should validate a valid URL', async () => {
|
|
const result = await checker.check('https://www.google.com');
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should handle invalid URLs', async () => {
|
|
const result = await checker.check('https://www.google.com/nonexistent-page-123456789');
|
|
expect(result.valid).toBe(false);
|
|
expect(result.error).toContain('404');
|
|
});
|
|
|
|
it('should handle timeouts', async () => {
|
|
const result = await checker.check('http://example.com:81', 1000); // Port 81 should timeout quickly
|
|
expect(result.valid).toBe(false);
|
|
expect(result.error).toContain('Navigation timeout');
|
|
});
|
|
|
|
it('should handle network errors', async () => {
|
|
const result = await checker.check('http://invalid.domain.thisisnotreal');
|
|
expect(result.valid).toBe(false);
|
|
expect(result.error).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe('FetchUrlChecker', () => {
|
|
const checker = new FetchUrlChecker();
|
|
let mockFetch: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
mockFetch = vi.fn();
|
|
global.fetch = mockFetch as unknown as typeof fetch;
|
|
});
|
|
|
|
afterAll(() => {
|
|
// Restore the original fetch
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('should validate a valid URL', async () => {
|
|
const mockResponse = {
|
|
ok: true,
|
|
status: 200,
|
|
statusText: 'OK'
|
|
} as unknown as Response;
|
|
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await checker.check(validUrl);
|
|
expect(result).toEqual({ valid: true });
|
|
});
|
|
|
|
it('should handle invalid URLs', async () => {
|
|
const mockResponse = {
|
|
ok: false,
|
|
status: 404,
|
|
statusText: 'Not Found'
|
|
} as unknown as Response;
|
|
|
|
mockFetch.mockResolvedValue(mockResponse);
|
|
|
|
const result = await checker.check(invalidUrl);
|
|
expect(result).toEqual({
|
|
valid: false,
|
|
error: 'HTTP 404: Not Found'
|
|
});
|
|
});
|
|
|
|
it('should handle timeouts', async () => {
|
|
mockFetch.mockRejectedValue(new Error('Timeout'));
|
|
|
|
const result = await checker.check(timeoutUrl);
|
|
expect(result).toEqual({
|
|
valid: false,
|
|
error: 'Timeout'
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('checkUrl convenience function', () => {
|
|
it('should use the default checker', async () => {
|
|
const result = await checkUrl('https://www.google.com');
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
});
|
|
});
|