|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { browserless } from '../browserless'; |
| 4 | + |
| 5 | +describe('browserless', () => { |
| 6 | + it('should throw BrowserlessInitError when env vars not set', async () => { |
| 7 | + const originalEnv = { ...process.env }; |
| 8 | + process.env = { ...originalEnv }; |
| 9 | + delete process.env.BROWSERLESS_URL; |
| 10 | + delete process.env.BROWSERLESS_TOKEN; |
| 11 | + |
| 12 | + await expect(browserless('https://example.com', { filterOptions: {} })).rejects.toThrow( |
| 13 | + '`BROWSERLESS_URL` or `BROWSERLESS_TOKEN` are required', |
| 14 | + ); |
| 15 | + |
| 16 | + process.env = originalEnv; |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return undefined on fetch error', async () => { |
| 20 | + process.env.BROWSERLESS_TOKEN = 'test-token'; |
| 21 | + global.fetch = vi.fn().mockRejectedValue(new Error('Fetch error')); |
| 22 | + |
| 23 | + const result = await browserless('https://example.com', { filterOptions: {} }); |
| 24 | + expect(result).toBeUndefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return undefined when content is empty', async () => { |
| 28 | + process.env.BROWSERLESS_TOKEN = 'test-token'; |
| 29 | + global.fetch = vi.fn().mockResolvedValue({ |
| 30 | + text: vi.fn().mockResolvedValue('<html></html>'), |
| 31 | + } as any); |
| 32 | + |
| 33 | + const result = await browserless('https://example.com', { filterOptions: {} }); |
| 34 | + expect(result).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return undefined when title is "Just a moment..."', async () => { |
| 38 | + process.env.BROWSERLESS_TOKEN = 'test-token'; |
| 39 | + global.fetch = vi.fn().mockResolvedValue({ |
| 40 | + text: vi.fn().mockResolvedValue('<html><title>Just a moment...</title></html>'), |
| 41 | + } as any); |
| 42 | + |
| 43 | + const result = await browserless('https://example.com', { filterOptions: {} }); |
| 44 | + expect(result).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return crawl result on successful fetch', async () => { |
| 48 | + process.env.BROWSERLESS_TOKEN = 'test-token'; |
| 49 | + global.fetch = vi.fn().mockResolvedValue({ |
| 50 | + text: vi.fn().mockResolvedValue(` |
| 51 | + <html> |
| 52 | + <head> |
| 53 | + <title>Test Title</title> |
| 54 | + <meta name="description" content="Test Description"> |
| 55 | + </head> |
| 56 | + <body> |
| 57 | + <h1>Test Content</h1> |
| 58 | + </body> |
| 59 | + </html> |
| 60 | + `), |
| 61 | + } as any); |
| 62 | + |
| 63 | + const result = await browserless('https://example.com', { filterOptions: {} }); |
| 64 | + |
| 65 | + expect(result).toEqual({ |
| 66 | + content: expect.any(String), |
| 67 | + contentType: 'text', |
| 68 | + description: expect.any(String), |
| 69 | + length: expect.any(Number), |
| 70 | + siteName: null, |
| 71 | + title: 'Test Title', |
| 72 | + url: 'https://example.com', |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should use correct URL when BROWSERLESS_URL is provided', async () => { |
| 77 | + const customUrl = 'https://custom.browserless.io'; |
| 78 | + const originalEnv = { ...process.env }; |
| 79 | + process.env.BROWSERLESS_TOKEN = 'test-token'; |
| 80 | + process.env.BROWSERLESS_URL = customUrl; |
| 81 | + global.fetch = vi.fn().mockImplementation((url) => { |
| 82 | + expect(url).toContain(customUrl); |
| 83 | + return Promise.resolve({ |
| 84 | + text: () => Promise.resolve('<html><title>Test</title></html>'), |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + await browserless('https://example.com', { filterOptions: {} }); |
| 89 | + |
| 90 | + expect(global.fetch).toHaveBeenCalled(); |
| 91 | + |
| 92 | + process.env = originalEnv; |
| 93 | + }); |
| 94 | +}); |
0 commit comments