Files
RSSHub/lib/utils/puppeteer.mock.test.ts
T
DIYgod 81ff849337 feat(tests): add comprehensive unit tests for various utility functions and views (#20779)
* feat(tests): add comprehensive unit tests for various utility functions and views

- Implement tests for cache utility to ensure proper behavior with no cache and TTL keys.
- Add tests for common utilities including string manipulation and path handling.
- Introduce tests for directory import functionality to validate file imports and pattern matching.
- Create tests for git hash retrieval to handle fallback scenarios.
- Develop tests for deprecated got utility to verify response handling and retry logic.
- Enhance got utility tests to include request hooks and search parameter handling.
- Mock header generator tests to validate user agent handling.
- Expand helpers tests to cover current path retrieval and duration parsing.
- Implement tests for ofetch utility to ensure proper proxy handling and logging.
- Add OpenTelemetry metric tests to validate metric serialization.
- Create proxy tests to verify multi-proxy selection and failure handling.
- Introduce request rewriter tests to validate fetch and get wrapper functionality.
- Add timezone utility tests to handle various input types.
- Implement view tests for Atom and RSS rendering to ensure correct output.
- Create index view tests to validate debug information display based on configuration.

* refactor: remove deprecated got implementation and associated tests

* test: expand coverage for api, middleware, and utils
2025-12-29 17:34:48 +08:00

109 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
const connect = vi.fn();
const launch = vi.fn();
const page = {
goto: vi.fn(),
authenticate: vi.fn(),
};
const browser = {
newPage: vi.fn(() => Promise.resolve(page)),
close: vi.fn(),
};
const proxyMock = {
proxyObj: { url_regex: '.*' },
proxyUrlHandler: new URL('http://proxy.local'),
multiProxy: undefined as any,
getCurrentProxy: vi.fn(),
markProxyFailed: vi.fn(),
getDispatcherForProxy: vi.fn(),
};
vi.mock('rebrowser-puppeteer', () => ({
default: {
connect,
launch,
},
}));
vi.mock('@/utils/proxy', () => ({
default: proxyMock,
}));
vi.mock('@/utils/logger', () => ({
default: {
warn: vi.fn(),
debug: vi.fn(),
},
}));
const loadPuppeteer = async () => {
vi.resetModules();
const mod = await import('@/utils/puppeteer');
return mod.getPuppeteerPage;
};
describe('getPuppeteerPage (mocked)', () => {
it('connects via ws endpoint and runs onBeforeLoad', async () => {
connect.mockResolvedValue(browser);
launch.mockResolvedValue(browser);
page.goto.mockResolvedValue(undefined);
page.authenticate.mockResolvedValue(undefined);
browser.close.mockResolvedValue(undefined);
process.env.PUPPETEER_WS_ENDPOINT = 'ws://localhost:3000/?token=abc';
proxyMock.getCurrentProxy.mockReturnValue(null);
const getPuppeteerPage = await loadPuppeteer();
const onBeforeLoad = vi.fn();
const result = await getPuppeteerPage('https://example.com', {
noGoto: true,
onBeforeLoad,
});
const endpoint = connect.mock.calls[0][0].browserWSEndpoint as string;
expect(endpoint).toContain('launch=');
expect(endpoint).toContain('stealth=true');
expect(onBeforeLoad).toHaveBeenCalled();
await result.destory();
expect(browser.close).toHaveBeenCalled();
delete process.env.PUPPETEER_WS_ENDPOINT;
});
it('marks proxy failed when navigation throws with multi-proxy', async () => {
connect.mockResolvedValue(browser);
launch.mockResolvedValue(browser);
page.goto.mockRejectedValueOnce(new Error('fail'));
page.authenticate.mockResolvedValue(undefined);
const currentProxy = {
uri: 'http://user:pass@proxy.local:8080',
urlHandler: new URL('http://user:pass@proxy.local:8080'),
};
proxyMock.multiProxy = {};
proxyMock.getCurrentProxy.mockReturnValue(currentProxy);
const getPuppeteerPage = await loadPuppeteer();
await expect(getPuppeteerPage('https://example.com')).rejects.toThrow('fail');
expect(proxyMock.markProxyFailed).toHaveBeenCalledWith(currentProxy.uri);
});
it('rethrows navigation errors without multi-proxy', async () => {
connect.mockResolvedValue(browser);
launch.mockResolvedValue(browser);
page.goto.mockRejectedValueOnce(new Error('fail'));
proxyMock.multiProxy = undefined;
proxyMock.getCurrentProxy.mockReturnValue(null);
const getPuppeteerPage = await loadPuppeteer();
await expect(getPuppeteerPage('https://example.com')).rejects.toThrow('fail');
});
});