|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest' |
| 2 | + |
| 3 | +const originalDevServerPort = process.env['DEV_SERVER_PORT'] |
| 4 | + |
| 5 | +// Reload the module with a fresh environment snapshot and mocked dependencies. |
| 6 | +const importSiteUrlServer = async () => { |
| 7 | + vi.resetModules() |
| 8 | + const isVercelMock = vi.fn(() => false) |
| 9 | + vi.doMock('../environmentServer', () => ({ |
| 10 | + isVercel: isVercelMock, |
| 11 | + })) |
| 12 | + vi.doMock('../../../../package.json', () => ({ |
| 13 | + domain: 'webstackbuilders.com', |
| 14 | + default: { domain: 'webstackbuilders.com' }, |
| 15 | + })) |
| 16 | + const module = await import('../siteUrlServer') |
| 17 | + return { getSiteUrl: module.getSiteUrl, isVercelMock } |
| 18 | +} |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + if (originalDevServerPort === undefined) { |
| 22 | + delete process.env['DEV_SERVER_PORT'] |
| 23 | + } else { |
| 24 | + process.env['DEV_SERVER_PORT'] = originalDevServerPort |
| 25 | + } |
| 26 | + vi.restoreAllMocks() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('getSiteUrl', () => { |
| 30 | + it('returns the production domain when Vercel runtime is detected', async () => { |
| 31 | + const { getSiteUrl, isVercelMock } = await importSiteUrlServer() |
| 32 | + isVercelMock.mockReturnValue(true) |
| 33 | + expect(getSiteUrl()).toBe('https://webstackbuilders.com') |
| 34 | + }) |
| 35 | + |
| 36 | + it('uses the provided DEV_SERVER_PORT when not running on Vercel', async () => { |
| 37 | + process.env['DEV_SERVER_PORT'] = '8888' |
| 38 | + const { getSiteUrl, isVercelMock } = await importSiteUrlServer() |
| 39 | + isVercelMock.mockReturnValue(false) |
| 40 | + expect(getSiteUrl()).toBe('http://localhost:8888') |
| 41 | + }) |
| 42 | + |
| 43 | + it('falls back to the default localhost:4321 when DEV_SERVER_PORT is unset', async () => { |
| 44 | + delete process.env['DEV_SERVER_PORT'] |
| 45 | + const { getSiteUrl, isVercelMock } = await importSiteUrlServer() |
| 46 | + isVercelMock.mockReturnValue(false) |
| 47 | + expect(getSiteUrl()).toBe('http://localhost:4321') |
| 48 | + }) |
| 49 | + |
| 50 | + it('falls back to the default when DEV_SERVER_PORT is whitespace', async () => { |
| 51 | + process.env['DEV_SERVER_PORT'] = ' ' |
| 52 | + const { getSiteUrl, isVercelMock } = await importSiteUrlServer() |
| 53 | + isVercelMock.mockReturnValue(false) |
| 54 | + expect(getSiteUrl()).toBe('http://localhost:4321') |
| 55 | + }) |
| 56 | +}) |
0 commit comments