tests
This commit is contained in:
parent
3a7254708e
commit
b63e58784c
41 changed files with 5762 additions and 4477 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { SimpleBrowser } from '../src/simple-browser';
|
||||
import type { BrowserOptions } from '../src/types';
|
||||
|
||||
|
||||
describe('Browser', () => {
|
||||
let browser: SimpleBrowser;
|
||||
|
|
@ -13,27 +13,27 @@ describe('Browser', () => {
|
|||
beforeEach(() => {
|
||||
logger.info = mock(() => {});
|
||||
logger.error = mock(() => {});
|
||||
|
||||
|
||||
browser = new SimpleBrowser(logger);
|
||||
});
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should initialize browser on first call', async () => {
|
||||
await browser.initialize();
|
||||
|
||||
|
||||
expect(logger.info).toHaveBeenCalledWith('Initializing browser...');
|
||||
});
|
||||
|
||||
it('should not reinitialize if already initialized', async () => {
|
||||
await browser.initialize();
|
||||
await browser.initialize();
|
||||
|
||||
|
||||
expect(logger.info).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should merge options', async () => {
|
||||
await browser.initialize({ headless: false, timeout: 60000 });
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
|
@ -43,14 +43,14 @@ describe('Browser', () => {
|
|||
it('should create new context', async () => {
|
||||
await browser.initialize();
|
||||
const contextId = await browser.createContext('test');
|
||||
|
||||
|
||||
expect(contextId).toBe('test');
|
||||
});
|
||||
|
||||
it('should generate context ID if not provided', async () => {
|
||||
await browser.initialize();
|
||||
const contextId = await browser.createContext();
|
||||
|
||||
|
||||
expect(contextId).toBeDefined();
|
||||
expect(typeof contextId).toBe('string');
|
||||
});
|
||||
|
|
@ -59,7 +59,7 @@ describe('Browser', () => {
|
|||
await browser.initialize();
|
||||
const contextId = await browser.createContext('test');
|
||||
await browser.closeContext(contextId);
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
|
@ -75,7 +75,7 @@ describe('Browser', () => {
|
|||
await browser.initialize();
|
||||
const contextId = await browser.createContext();
|
||||
const page = await browser.newPage(contextId);
|
||||
|
||||
|
||||
expect(page).toBeDefined();
|
||||
});
|
||||
|
||||
|
|
@ -83,18 +83,18 @@ describe('Browser', () => {
|
|||
await browser.initialize();
|
||||
const contextId = await browser.createContext();
|
||||
const page = await browser.newPage(contextId);
|
||||
|
||||
|
||||
await browser.goto(page, 'https://example.com');
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
||||
it('should scrape page', async () => {
|
||||
await browser.initialize();
|
||||
|
||||
|
||||
const result = await browser.scrape('https://example.com');
|
||||
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.data.title).toBeDefined();
|
||||
expect(result.data.text).toBeDefined();
|
||||
|
|
@ -107,7 +107,7 @@ describe('Browser', () => {
|
|||
await browser.initialize({ blockResources: true });
|
||||
const contextId = await browser.createContext();
|
||||
const page = await browser.newPage(contextId);
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(page).toBeDefined();
|
||||
});
|
||||
|
|
@ -116,7 +116,7 @@ describe('Browser', () => {
|
|||
await browser.initialize({ blockResources: false });
|
||||
const contextId = await browser.createContext();
|
||||
const page = await browser.newPage(contextId);
|
||||
|
||||
|
||||
expect(page).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
|
@ -125,7 +125,7 @@ describe('Browser', () => {
|
|||
it('should close browser', async () => {
|
||||
await browser.initialize();
|
||||
await browser.close();
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
|
@ -138,9 +138,9 @@ describe('Browser', () => {
|
|||
await browser.initialize();
|
||||
await browser.createContext('test1');
|
||||
await browser.createContext('test2');
|
||||
|
||||
|
||||
await browser.close();
|
||||
|
||||
|
||||
// Just verify it doesn't throw
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
|
|
@ -156,18 +156,20 @@ describe('Browser', () => {
|
|||
|
||||
it('should handle page creation failure', async () => {
|
||||
await browser.initialize();
|
||||
|
||||
|
||||
// Should throw for non-existent context
|
||||
await expect(browser.newPage('non-existent')).rejects.toThrow('Context non-existent not found');
|
||||
await expect(browser.newPage('non-existent')).rejects.toThrow(
|
||||
'Context non-existent not found'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle scrape errors', async () => {
|
||||
// SimpleBrowser catches errors and returns success: false
|
||||
await browser.initialize();
|
||||
|
||||
|
||||
const result = await browser.scrape('https://example.com');
|
||||
|
||||
|
||||
expect(result.success).toBe(true); // SimpleBrowser always succeeds
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue