|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockGetEnv } = vi.hoisted(() => ({ |
| 7 | + mockGetEnv: vi.fn<(key: string) => string | undefined>(), |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('@/lib/core/config/env', () => ({ |
| 11 | + env: {}, |
| 12 | + getEnv: mockGetEnv, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 16 | + isProd: false, |
| 17 | +})) |
| 18 | + |
| 19 | +import { |
| 20 | + getBrowserOrigin, |
| 21 | + getSocketUrl, |
| 22 | + isLocalhostUrl, |
| 23 | + parseOriginList, |
| 24 | +} from '@/lib/core/utils/urls' |
| 25 | + |
| 26 | +function setLocation(url: string) { |
| 27 | + Object.defineProperty(window, 'location', { |
| 28 | + value: new URL(url), |
| 29 | + writable: true, |
| 30 | + configurable: true, |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +describe('getBrowserOrigin', () => { |
| 35 | + it('returns the page origin in the browser', () => { |
| 36 | + setLocation('https://example.com/some/path') |
| 37 | + expect(getBrowserOrigin()).toBe('https://example.com') |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('getSocketUrl', () => { |
| 42 | + beforeEach(() => { |
| 43 | + mockGetEnv.mockReset() |
| 44 | + mockGetEnv.mockReturnValue(undefined) |
| 45 | + }) |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + vi.restoreAllMocks() |
| 49 | + }) |
| 50 | + |
| 51 | + it('uses NEXT_PUBLIC_SOCKET_URL when explicitly set', () => { |
| 52 | + mockGetEnv.mockImplementation((key) => |
| 53 | + key === 'NEXT_PUBLIC_SOCKET_URL' ? 'https://socket.example.com' : undefined |
| 54 | + ) |
| 55 | + setLocation('https://app.example.com/') |
| 56 | + expect(getSocketUrl()).toBe('https://socket.example.com') |
| 57 | + }) |
| 58 | + |
| 59 | + it('returns the page origin when served from a non-localhost host', () => { |
| 60 | + setLocation('https://10.0.3.36/signup') |
| 61 | + expect(getSocketUrl()).toBe('https://10.0.3.36') |
| 62 | + }) |
| 63 | + |
| 64 | + it('falls back to localhost:3002 when served from localhost', () => { |
| 65 | + setLocation('http://localhost:3000/') |
| 66 | + expect(getSocketUrl()).toBe('http://localhost:3002') |
| 67 | + }) |
| 68 | + |
| 69 | + it('falls back to localhost:3002 when served from 127.0.0.1', () => { |
| 70 | + setLocation('http://127.0.0.1:3000/') |
| 71 | + expect(getSocketUrl()).toBe('http://localhost:3002') |
| 72 | + }) |
| 73 | + |
| 74 | + it('explicit env var wins over the localhost fallback', () => { |
| 75 | + mockGetEnv.mockImplementation((key) => |
| 76 | + key === 'NEXT_PUBLIC_SOCKET_URL' ? 'http://realtime.local:3002' : undefined |
| 77 | + ) |
| 78 | + setLocation('http://localhost:3000/') |
| 79 | + expect(getSocketUrl()).toBe('http://realtime.local:3002') |
| 80 | + }) |
| 81 | + |
| 82 | + it('treats whitespace-only env var as unset', () => { |
| 83 | + mockGetEnv.mockImplementation((key) => (key === 'NEXT_PUBLIC_SOCKET_URL' ? ' ' : undefined)) |
| 84 | + setLocation('https://app.example.com/') |
| 85 | + expect(getSocketUrl()).toBe('https://app.example.com') |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe('parseOriginList', () => { |
| 90 | + it('returns an empty array for undefined, null, or empty input', () => { |
| 91 | + expect(parseOriginList(undefined)).toEqual([]) |
| 92 | + expect(parseOriginList(null)).toEqual([]) |
| 93 | + expect(parseOriginList('')).toEqual([]) |
| 94 | + expect(parseOriginList(' ')).toEqual([]) |
| 95 | + }) |
| 96 | + |
| 97 | + it('parses comma-separated origins and normalizes them', () => { |
| 98 | + expect(parseOriginList('https://a.example.com, https://b.example.com/path')).toEqual([ |
| 99 | + 'https://a.example.com', |
| 100 | + 'https://b.example.com', |
| 101 | + ]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('dedupes equal origins after normalization', () => { |
| 105 | + expect( |
| 106 | + parseOriginList('https://a.example.com,https://a.example.com/foo,https://a.example.com') |
| 107 | + ).toEqual(['https://a.example.com']) |
| 108 | + }) |
| 109 | + |
| 110 | + it('drops invalid entries and reports them via the callback', () => { |
| 111 | + const invalid: string[] = [] |
| 112 | + const result = parseOriginList('https://ok.example.com, not-a-url, ', (v) => invalid.push(v)) |
| 113 | + expect(result).toEqual(['https://ok.example.com']) |
| 114 | + expect(invalid).toEqual(['not-a-url']) |
| 115 | + }) |
| 116 | + |
| 117 | + it('preserves non-default ports in the origin', () => { |
| 118 | + expect(parseOriginList('http://10.0.3.36:8080')).toEqual(['http://10.0.3.36:8080']) |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +describe('isLocalhostUrl', () => { |
| 123 | + it('matches localhost variants', () => { |
| 124 | + expect(isLocalhostUrl('http://localhost:3000')).toBe(true) |
| 125 | + expect(isLocalhostUrl('http://127.0.0.1')).toBe(true) |
| 126 | + expect(isLocalhostUrl('https://localhost')).toBe(true) |
| 127 | + }) |
| 128 | + |
| 129 | + it('does not match public hostnames or invalid URLs', () => { |
| 130 | + expect(isLocalhostUrl('https://10.0.3.36')).toBe(false) |
| 131 | + expect(isLocalhostUrl('https://app.example.com')).toBe(false) |
| 132 | + expect(isLocalhostUrl('not-a-url')).toBe(false) |
| 133 | + expect(isLocalhostUrl('')).toBe(false) |
| 134 | + }) |
| 135 | +}) |
0 commit comments