import { afterEach, describe, expect, it } from 'vitest' import { resolveBrowserAppUrl, resolveServerAppUrl } from './appUrls' function installWindow(hostname: string, protocol = 'https:') { Object.defineProperty(globalThis, 'window', { configurable: true, value: { location: { hostname, protocol } }, }) } afterEach(() => { Reflect.deleteProperty(globalThis, 'window') }) describe('carplace app URL resolution', () => { it('leaves server-side browser fallback unchanged when window is unavailable', () => { expect(resolveBrowserAppUrl('http://localhost:3000')).toBe('http://localhost:3000') }) it('preserves localhost fallbacks but removes trailing slash', () => { installWindow('localhost') expect(resolveBrowserAppUrl('http://localhost:3000/')).toBe('http://localhost:3000') }) it('rewrites production browser fallbacks to the active host and protocol', () => { installWindow('www.rentaldrivego.ma', 'https:') expect(resolveBrowserAppUrl('http://localhost:3000')).toBe('https://www.rentaldrivego.ma') }) it('keeps path prefixes while rewriting the browser origin', () => { installWindow('rental.example.com', 'https:') expect(resolveBrowserAppUrl('http://localhost:3000/carplace')).toBe('https://rental.example.com/carplace') }) it('resolves server URLs from forwarded host/proto and handles invalid fallbacks safely', () => { expect(resolveServerAppUrl('http://localhost:3000', 'market.example.com', 'https')).toBe('https://market.example.com:3000') expect(resolveServerAppUrl('http://localhost:3000', null)).toBe('http://localhost:3000') expect(resolveServerAppUrl('/relative', 'market.example.com')).toBe('/relative') }) })