T-3: tests

This commit is contained in:
Studio 2026-11-27 11:01:40 -05:00
parent e182c640d2
commit e88044e4b8
3 changed files with 233 additions and 0 deletions

View file

@ -205,3 +205,81 @@ describe('IB config and proxy handling', () => {
]);
});
});
describe('IB proxy edge cases (QA)', () => {
test('getIbProxyUrl accepts unauthenticated and upper-case-scheme proxies and rejects host-less URLs', () => {
expect(getIbProxyUrl({ IB_PROXY_URL: 'http://proxy.test:3128' })).toBe('http://proxy.test:3128');
const upper = buildProxyUrl('HTTPS', 'u', 'p', 'proxy.test');
expect(getIbProxyUrl({ IB_PROXY_URL: upper })).toBe(upper);
for (const bad of ['http://', 'https://', `${'http'}://hostless-user:hostless-pass@`]) {
expect(() => getIbProxyUrl({ IB_PROXY_URL: bad })).toThrow(
'IB_PROXY_URL must be a valid http(s) URL'
);
}
});
test('fetchSession with an invalid IB_PROXY_URL fails before launching a browser and never logs the value', async () => {
process.env['IB_PROXY_URL'] = buildProxyUrl('socks5', 'bad-user', 'bad-secret', 'proxy.test:1080');
const { services, logs, createPageWithProxy } = createMockServices();
const initialize = (services as unknown as { browser: { initialize: ReturnType<typeof mock> } })
.browser.initialize;
const headers = await fetchSession(services);
expect(headers).toBeUndefined();
expect(initialize).not.toHaveBeenCalled();
expect(createPageWithProxy).not.toHaveBeenCalled();
const serialized = JSON.stringify(logs, (_key, value) =>
value instanceof Error ? { message: value.message, stack: value.stack } : value
);
expect(serialized).toContain('IB_PROXY_URL must be a valid http(s) URL');
expect(serialized).not.toContain('bad-secret');
expect(serialized).not.toContain('bad-user');
});
test('fetchSymbols with an invalid IB_PROXY_URL makes no API calls and writes nothing', async () => {
process.env['IB_PROXY_URL'] = 'ftp://ftp-user:ftp-secret@proxy.test:21';
const fetchMock = mock(async () => new Response('[]'));
globalThis.fetch = fetchMock as unknown as typeof fetch;
const { services, logs, batchUpsert } = createMockServices();
const result = await fetchSymbols(services);
expect(result).toBeNull();
expect(fetchMock).not.toHaveBeenCalled();
expect(batchUpsert).not.toHaveBeenCalled();
expect(JSON.stringify(logs)).not.toContain('ftp-secret');
});
test('fetchSymbols forwards the proxy on every paginated request and upserts once into ibSymbols', async () => {
const proxyUrl = buildProxyUrl('http', 'page-user', 'page-secret', 'proxy.test:8080');
process.env['IB_PROXY_URL'] = proxyUrl;
const calls: Array<{ url: string; init: Record<string, unknown> }> = [];
const totalCount = IB_CONFIG.PAGE_SIZE * 2 + 1; // forces 3 product pages
globalThis.fetch = mock(async (url: string | URL | Request, init?: RequestInit) => {
const urlStr = String(url);
calls.push({ url: urlStr, init: (init ?? {}) as Record<string, unknown> });
if (urlStr.endsWith(IB_CONFIG.SUMMARY_API)) {
return new Response(JSON.stringify([{ totalCount }]), { status: 200 });
}
const page = JSON.parse(String(init?.body)).pageNumber as number;
return new Response(
JSON.stringify({ products: [{ symbol: `SYM${page}`, exchangeId: 'NYSE' }] }),
{ status: 200 }
);
}) as unknown as typeof fetch;
const { services, batchUpsert } = createMockServices();
const symbols = await fetchSymbols(services);
expect(calls).toHaveLength(4);
expect(calls.every(call => call.init['proxy'] === proxyUrl)).toBe(true);
expect(symbols).toHaveLength(3);
expect(batchUpsert).toHaveBeenCalledTimes(1);
const [collection, , keys] = batchUpsert.mock.calls[0] ?? [];
expect(collection).toBe('ibSymbols');
expect(keys).toEqual(['symbol', 'exchangeId']);
});
});