|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +describe('Tunnel Route Caching (Environment Variable)', () => { |
| 4 | + let originalNextPhase: string | undefined; |
| 5 | + let originalTunnelRoute: string | undefined; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Save and clear env vars |
| 9 | + originalNextPhase = process.env.NEXT_PHASE; |
| 10 | + originalTunnelRoute = process.env.__SENTRY_TUNNEL_ROUTE__; |
| 11 | + delete process.env.__SENTRY_TUNNEL_ROUTE__; |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + // Restore env vars |
| 16 | + if (originalNextPhase !== undefined) { |
| 17 | + process.env.NEXT_PHASE = originalNextPhase; |
| 18 | + } else { |
| 19 | + delete process.env.NEXT_PHASE; |
| 20 | + } |
| 21 | + |
| 22 | + if (originalTunnelRoute !== undefined) { |
| 23 | + process.env.__SENTRY_TUNNEL_ROUTE__ = originalTunnelRoute; |
| 24 | + } else { |
| 25 | + delete process.env.__SENTRY_TUNNEL_ROUTE__; |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it('caches tunnel route in environment variable during build phase', () => { |
| 30 | + process.env.NEXT_PHASE = 'phase-production-build'; |
| 31 | + process.env.__SENTRY_TUNNEL_ROUTE__ = '/cached-route-123'; |
| 32 | + |
| 33 | + // The env var should be accessible |
| 34 | + expect(process.env.__SENTRY_TUNNEL_ROUTE__).toBe('/cached-route-123'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('environment variable persists across different contexts', () => { |
| 38 | + process.env.NEXT_PHASE = 'phase-production-build'; |
| 39 | + process.env.__SENTRY_TUNNEL_ROUTE__ = '/test-route-456'; |
| 40 | + |
| 41 | + // Simulate accessing from different module |
| 42 | + const cachedRoute = process.env.__SENTRY_TUNNEL_ROUTE__; |
| 43 | + |
| 44 | + expect(cachedRoute).toBe('/test-route-456'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('verifies NEXT_PHASE detection for build time', () => { |
| 48 | + process.env.NEXT_PHASE = 'phase-production-build'; |
| 49 | + |
| 50 | + const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build'; |
| 51 | + |
| 52 | + expect(isBuildTime).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it('verifies NEXT_PHASE detection for non-build time', () => { |
| 56 | + process.env.NEXT_PHASE = 'phase-development-server'; |
| 57 | + |
| 58 | + const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build'; |
| 59 | + |
| 60 | + expect(isBuildTime).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('handles missing NEXT_PHASE', () => { |
| 64 | + delete process.env.NEXT_PHASE; |
| 65 | + |
| 66 | + const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build'; |
| 67 | + |
| 68 | + expect(isBuildTime).toBe(false); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('Random Tunnel Route Generation', () => { |
| 73 | + it('generates an 8-character alphanumeric string', () => { |
| 74 | + const randomString = Math.random().toString(36).substring(2, 10); |
| 75 | + const tunnelRoute = `/${randomString}`; |
| 76 | + |
| 77 | + // Should be a path with 8 alphanumeric chars |
| 78 | + expect(tunnelRoute).toMatch(/^\/[a-z0-9]{8}$/); |
| 79 | + }); |
| 80 | + |
| 81 | + it('generates different values on multiple calls', () => { |
| 82 | + const route1 = `/${Math.random().toString(36).substring(2, 10)}`; |
| 83 | + const route2 = `/${Math.random().toString(36).substring(2, 10)}`; |
| 84 | + |
| 85 | + // Very unlikely to be the same (but not impossible) |
| 86 | + // This is more of a sanity check |
| 87 | + expect(route1).toMatch(/^\/[a-z0-9]{8}$/); |
| 88 | + expect(route2).toMatch(/^\/[a-z0-9]{8}$/); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
0 commit comments