import { describe, it, expect } from 'vitest'; import { getEnvInfo, getContexts, getContextId, getPageUrl, getReferrer, sanitizeUrl, shouldIgnoreUrl, } from '../src/utils/env'; describe('env utils', () => { describe('getEnvInfo', () => { it('should return env info object', () => { const env = getEnvInfo(); expect(env).toBeDefined(); expect(env).toHaveProperty('browser'); expect(env).toHaveProperty('os'); expect(env).toHaveProperty('device'); }); it('should have valid browser info', () => { const env = getEnvInfo(); expect(env.browser.name).toBeDefined(); expect(env.browser.version).toBeDefined(); expect(env.browser.major).toBeDefined(); }); it('should have valid os info', () => { const env = getEnvInfo(); expect(env.os.name).toBeDefined(); expect(env.os.version).toBeDefined(); }); it('should have valid device info', () => { const env = getEnvInfo(); expect(['desktop', 'mobile', 'tablet']).toContain(env.device.type); }); }); describe('getContexts', () => { it('should return contexts with encoded values', () => { const contexts = getContexts(); expect(contexts).toBeDefined(); expect(contexts.browser).toBeDefined(); expect(contexts.os).toBeDefined(); expect(contexts.device).toBeDefined(); }); it('should use short codes for browser', () => { const contexts = getContexts(); expect(contexts.browser.name.length).toBeLessThanOrEqual(10); }); it('should use short codes for os', () => { const contexts = getContexts(); expect(contexts.os.name.length).toBeLessThanOrEqual(10); }); }); describe('getContextId', () => { it('should return a 16 character string', () => { const contextId = getContextId(); expect(contextId).toBeDefined(); expect(contextId.length).toBe(16); expect(contextId).toMatch(/^[a-f0-9]+$/i); }); it('should return consistent id for same environment', () => { const id1 = getContextId(); const id2 = getContextId(); expect(id1).toBe(id2); }); }); describe('getPageUrl', () => { it('should return current page URL or empty string', () => { const url = getPageUrl(); expect(typeof url).toBe('string'); }); }); describe('getReferrer', () => { it('should return referrer or empty string', () => { const referrer = getReferrer(); expect(typeof referrer).toBe('string'); }); }); describe('sanitizeUrl', () => { it('should filter sensitive parameters', () => { const url = 'https://example.com/api?user=123&password=secret&token=abc'; const sanitized = sanitizeUrl(url); expect(sanitized).toContain('password'); expect(sanitized).toContain('token'); // URL encoded [Filtered] = %5BFiltered%5D expect(sanitized).toContain('%5BFiltered%5D'); }); it('should handle URLs without sensitive params', () => { const url = 'https://example.com/api?page=1&limit=10'; const sanitized = sanitizeUrl(url); expect(sanitized).toBe(url); }); it('should handle invalid URLs', () => { const sanitized = sanitizeUrl('not-a-url'); expect(sanitized).toBe('not-a-url'); }); it('should filter api_key parameter', () => { const url = 'https://api.com/endpoint?api_key=secret123'; const sanitized = sanitizeUrl(url); expect(sanitized).toContain('api_key'); expect(sanitized).toContain('%5BFiltered%5D'); }); it('should filter authorization headers in URL', () => { const url = 'https://example.com/api?code=auth123'; const sanitized = sanitizeUrl(url); expect(sanitized).toContain('code'); expect(sanitized).toContain('%5BFiltered%5D'); }); }); describe('shouldIgnoreUrl', () => { it('should return false for empty patterns', () => { const result = shouldIgnoreUrl('https://example.com/api', []); expect(result).toBe(false); }); it('should match string pattern', () => { const result = shouldIgnoreUrl('https://example.com/api/health', ['/health']); expect(result).toBe(true); }); it('should not match non-matching pattern', () => { const result = shouldIgnoreUrl('https://example.com/api/users', ['/health']); expect(result).toBe(false); }); it('should match wildcard pattern', () => { const result = shouldIgnoreUrl('https://example.com/api/v1/users', ['/api/v1/*']); expect(result).toBe(true); }); it('should not match wildcard when no wildcard in URL', () => { const result = shouldIgnoreUrl('https://example.com/api/v2/users', ['/api/v1/*']); expect(result).toBe(false); }); it('should match regex pattern', () => { const result = shouldIgnoreUrl('https://example.com/api/users/123', [/api\/users\/\d+/]); expect(result).toBe(true); }); }); });