import { describe, expect, it } from 'vitest'; import { isSocksProxyUrl, redactProxyUrlForLog, resolveWhatsAppProxyUrl, shouldBypassWhatsAppProxy, } from '@electron/utils/whatsapp-proxy'; import type { ProxySettings } from '@electron/utils/proxy'; function settings(overrides: Partial): ProxySettings { return { proxyEnabled: false, proxyServer: '', proxyHttpServer: '', proxyHttpsServer: '', proxyAllServer: '', proxyBypassRules: ';localhost;127.0.0.1;::1', ...overrides, }; } describe('WhatsApp proxy helpers', () => { it('does not use a proxy when app proxy is disabled', () => { expect(resolveWhatsAppProxyUrl(settings({ proxyEnabled: false, proxyServer: 'http://127.0.0.1:7890', }))).toBe(''); }); it('uses the all-proxy value first for WhatsApp WebSocket traffic', () => { expect(resolveWhatsAppProxyUrl(settings({ proxyEnabled: true, proxyServer: 'http://127.0.0.1:7890', proxyHttpsServer: 'http://127.0.0.1:7892', proxyAllServer: 'socks5://127.0.0.1:7891', }))).toBe('socks5://127.0.0.1:7891'); }); it('falls back to HTTPS/base proxy when no all-proxy is configured', () => { expect(resolveWhatsAppProxyUrl(settings({ proxyEnabled: true, proxyServer: '127.0.0.1:7890', proxyHttpsServer: '', proxyAllServer: '', }))).toBe('http://127.0.0.1:7890'); }); it('honors bypass rules for web.whatsapp.com', () => { expect(shouldBypassWhatsAppProxy('*.whatsapp.com')).toBe(true); expect(resolveWhatsAppProxyUrl(settings({ proxyEnabled: true, proxyServer: 'http://127.0.0.1:7890', proxyBypassRules: '*.whatsapp.com', }))).toBe(''); }); it('detects SOCKS proxy URLs and redacts credentials for logs', () => { expect(isSocksProxyUrl('socks5://127.0.0.1:7891')).toBe(true); expect(isSocksProxyUrl('http://127.0.0.1:7890')).toBe(false); expect(redactProxyUrlForLog('http://user:secret@127.0.0.1:7890')) .toBe('http://redacted:redacted@127.0.0.1:7890/'); }); });