|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { resolveBaseUrl } from '../../packages/action/src/resolve-base-url.js'; |
| 3 | +import type { GitGlimpseConfig } from '../../packages/core/src/config/schema.js'; |
| 4 | + |
| 5 | +function makeConfig(app: GitGlimpseConfig['app']): GitGlimpseConfig { |
| 6 | + return { |
| 7 | + app, |
| 8 | + llm: { provider: 'anthropic' }, |
| 9 | + recording: { format: 'gif', maxDuration: 30, viewport: { width: 1280, height: 720 } }, |
| 10 | + } as unknown as GitGlimpseConfig; |
| 11 | +} |
| 12 | + |
| 13 | +describe('resolveBaseUrl', () => { |
| 14 | + const originalEnv = process.env; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + process.env = { ...originalEnv }; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + process.env = originalEnv; |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns URL directly when previewUrl is a literal http URL', () => { |
| 25 | + const result = resolveBaseUrl(makeConfig({ previewUrl: 'https://my-preview.vercel.app' })); |
| 26 | + expect(result.url).toBe('https://my-preview.vercel.app'); |
| 27 | + expect(result.error).toBeUndefined(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('resolves previewUrl as env var name when the env var is set', () => { |
| 31 | + process.env['VERCEL_PREVIEW_URL'] = 'https://my-preview.vercel.app'; |
| 32 | + const result = resolveBaseUrl(makeConfig({ previewUrl: 'VERCEL_PREVIEW_URL' })); |
| 33 | + expect(result.url).toBe('https://my-preview.vercel.app'); |
| 34 | + expect(result.error).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns a descriptive error when env var name is set but env var is missing', () => { |
| 38 | + delete process.env['VERCEL_PREVIEW_URL']; |
| 39 | + const result = resolveBaseUrl(makeConfig({ previewUrl: 'VERCEL_PREVIEW_URL' })); |
| 40 | + expect(result.url).toBeUndefined(); |
| 41 | + expect(result.error).toMatch(/VERCEL_PREVIEW_URL/); |
| 42 | + expect(result.error).toMatch(/env var/); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns a descriptive error when env var is set but value is not a URL', () => { |
| 46 | + process.env['PREVIEW_URL'] = 'not-a-url'; |
| 47 | + const result = resolveBaseUrl(makeConfig({ previewUrl: 'PREVIEW_URL' })); |
| 48 | + expect(result.url).toBeUndefined(); |
| 49 | + expect(result.error).toMatch(/PREVIEW_URL/); |
| 50 | + expect(result.error).toMatch(/not a valid URL/); |
| 51 | + }); |
| 52 | + |
| 53 | + it('falls back to localhost when no previewUrl and no readyWhen', () => { |
| 54 | + const result = resolveBaseUrl(makeConfig({ startCommand: 'npm run dev' })); |
| 55 | + expect(result.url).toBe('http://localhost:3000'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('uses readyWhen.url origin as base when set', () => { |
| 59 | + const result = resolveBaseUrl( |
| 60 | + makeConfig({ startCommand: 'npm run dev', readyWhen: { url: 'http://localhost:4000/health' } }) |
| 61 | + ); |
| 62 | + expect(result.url).toBe('http://localhost:4000'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('previewUrlOverride takes precedence over config', () => { |
| 66 | + process.env['OVERRIDE_URL'] = 'https://override.example.com'; |
| 67 | + const result = resolveBaseUrl( |
| 68 | + makeConfig({ previewUrl: 'SOME_OTHER_VAR' }), |
| 69 | + 'OVERRIDE_URL' |
| 70 | + ); |
| 71 | + expect(result.url).toBe('https://override.example.com'); |
| 72 | + }); |
| 73 | +}); |
0 commit comments