stock-bot/libs/services/browser/test/browser.test.ts
2026-11-27 11:01:40 -05:00

123 lines
4.7 KiB
TypeScript

import { describe, expect, mock, test } from 'bun:test';
import type { Browser as PlaywrightBrowser } from 'playwright';
import { Browser } from '../src/browser';
function createBrowserWithMockPlaywright() {
const capturedContextOptions: Array<Record<string, unknown>> = [];
const gotoMock = mock(async () => {});
const page = {
setDefaultTimeout: mock(() => {}),
setDefaultNavigationTimeout: mock(() => {}),
goto: gotoMock,
on: mock(() => {}),
};
const context = {
route: mock(async () => {}),
newPage: mock(async () => page),
close: mock(async () => {}),
};
const newContext = mock(async (options: Record<string, unknown>) => {
capturedContextOptions.push(options);
return context;
});
const browser = new Browser({
info: () => {},
warn: () => {},
error: () => {},
debug: () => {},
});
(browser as unknown as { browser: PlaywrightBrowser }).browser = {
newContext,
close: mock(async () => {}),
} as unknown as PlaywrightBrowser;
return { browser, capturedContextOptions, newContext, gotoMock };
}
describe('Browser.createPageWithProxy', () => {
test('parses default-port HTTPS proxy URLs into origin and separate credentials without appending :undefined', async () => {
const { browser, capturedContextOptions } = createBrowserWithMockPlaywright();
const proxyUrl = `${'https'}://${'ib-user'}:${'ib-secret'}@proxy.test`;
await browser.createPageWithProxy('https://example.test/page', proxyUrl);
expect(capturedContextOptions[0]?.['proxy']).toEqual({
server: 'https://proxy.test',
username: 'ib-user',
password: 'ib-secret',
});
});
test('parses explicit-port and percent-encoded credentials, unauthenticated proxies, and omitted proxies', async () => {
const { browser, capturedContextOptions } = createBrowserWithMockPlaywright();
const encodedProxy = `${'http'}://${encodeURIComponent('user@org')}:${encodeURIComponent('p:ass%1')}@proxy.test:8080`;
await browser.createPageWithProxy('https://example.test/1', encodedProxy);
expect(capturedContextOptions[0]?.['proxy']).toEqual({
server: 'http://proxy.test:8080',
username: 'user@org',
password: 'p:ass%1',
});
await browser.createPageWithProxy('https://example.test/2', 'http://proxy.test');
expect(capturedContextOptions[1]?.['proxy']).toEqual({
server: 'http://proxy.test',
});
await browser.createPageWithProxy('https://example.test/3', undefined);
expect('proxy' in (capturedContextOptions[2] ?? {})).toBe(false);
});
test('rejects malformed proxy URLs without echoing credentials', async () => {
const { browser } = createBrowserWithMockPlaywright();
const badProxy = 'not-a-valid-proxy-with-secret-token-xyz';
let caught: Error | undefined;
try {
await browser.createPageWithProxy('https://example.test/page', badProxy);
} catch (error) {
caught = error as Error;
}
expect(caught).toBeInstanceOf(Error);
expect(caught?.message).toContain('Invalid proxy format');
expect(caught?.message).not.toContain('secret-token-xyz');
});
});
describe('Browser.createPageWithProxy edge cases (QA)', () => {
test('keeps non-special schemes, IPv6 hosts, and drops proxy paths/queries from the server', async () => {
const { browser, capturedContextOptions } = createBrowserWithMockPlaywright();
await browser.createPageWithProxy('https://example.test/a', `${'socks5'}://s-user:s-pass@proxy.test:1080`);
expect(capturedContextOptions[0]?.['proxy']).toEqual({
server: 'socks5://proxy.test:1080',
username: 's-user',
password: 's-pass',
});
await browser.createPageWithProxy('https://example.test/b', 'http://[::1]:3128');
expect(capturedContextOptions[1]?.['proxy']).toEqual({ server: 'http://[::1]:3128' });
await browser.createPageWithProxy('https://example.test/c', 'HTTP://PROXY.TEST:8080/some/path?x=1#frag');
expect(capturedContextOptions[2]?.['proxy']).toEqual({ server: 'http://proxy.test:8080' });
});
test('host-less proxy URLs carrying credentials are rejected before a context is created, without echoing them', async () => {
const { browser, newContext } = createBrowserWithMockPlaywright();
for (const bad of [`${'http'}://leak-user:leak-pass@`, `${'http'}://leak-user:leak-pass@bad host:80`]) {
let caught: Error | undefined;
try {
await browser.createPageWithProxy('https://example.test/page', bad);
} catch (error) {
caught = error as Error;
}
expect(caught?.message).toContain('Invalid proxy format');
expect(caught?.message).not.toContain('leak-pass');
expect(caught?.message).not.toContain('leak-user');
}
expect(newContext).not.toHaveBeenCalled();
});
});