-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.ts
More file actions
270 lines (231 loc) · 9.42 KB
/
config.test.ts
File metadata and controls
270 lines (231 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals'
import { validateConfiguration, getEnvironmentConfig } from '../validator'
import { Config } from '../index'
// Mock environment variables for testing
const mockEnv = {
NODE_ENV: 'development',
APP_ENV: 'development',
NEXT_PUBLIC_APP_URL: 'http://localhost:3000',
NEXT_PUBLIC_APP_NAME: 'StreamVault',
GCP_PROJECT_ID: 'test-project',
GCS_BUCKET_NAME: 'test-bucket',
GOOGLE_APPLICATION_CREDENTIALS: '/path/to/credentials.json',
GCS_SERVICE_ACCOUNT_EMAIL: 'test@test-project.iam.gserviceaccount.com',
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: 'pk_test_123',
CLERK_SECRET_KEY: 'sk_test_123',
FIREBASE_PROJECT_ID: 'test-firebase',
FIREBASE_PRIVATE_KEY:
'-----BEGIN PRIVATE KEY-----\ntest\n-----END PRIVATE KEY-----\n',
FIREBASE_CLIENT_EMAIL: 'firebase@test-firebase.iam.gserviceaccount.com',
NEXT_PUBLIC_FIREBASE_API_KEY: 'test-api-key',
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: 'test-firebase.firebaseapp.com',
NEXT_PUBLIC_FIREBASE_PROJECT_ID: 'test-firebase',
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: 'test-firebase.appspot.com',
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: '123456789',
NEXT_PUBLIC_FIREBASE_APP_ID: '1:123456789:web:test',
STRIPE_SECRET_KEY: 'sk_test_123',
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: 'pk_test_123',
STRIPE_WEBHOOK_SECRET: 'whsec_test_123',
STRIPE_BASIC_PRICE_ID: 'price_basic_123',
STRIPE_PREMIUM_PRICE_ID: 'price_premium_123',
STRIPE_PRO_PRICE_ID: 'price_pro_123',
STREAMING_DOMAIN: 'stream.test.com',
RTMP_INGEST_URL: 'rtmp://ingest.test.com/live',
HLS_DELIVERY_URL: 'https://cdn.test.com/hls',
CDN_BASE_URL: 'https://cdn.test.com',
JWT_SECRET: 'this-is-a-very-long-jwt-secret-for-testing-purposes-123456789',
ENCRYPTION_KEY: '12345678901234567890123456789012', // 32 characters
WEBHOOK_SECRET: 'webhook-secret-123456',
}
describe('Configuration System', () => {
let originalEnv: NodeJS.ProcessEnv
beforeEach(() => {
originalEnv = process.env
process.env = { ...mockEnv }
})
afterEach(() => {
process.env = originalEnv
})
describe('Environment Validation', () => {
it('should validate correct environment configuration', () => {
const result = validateConfiguration()
expect(result.success).toBe(true)
expect(result.errors).toHaveLength(0)
expect(result.config).toBeDefined()
})
it('should fail validation with missing required variables', () => {
delete process.env.NEXT_PUBLIC_APP_URL
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(result.errors.length).toBeGreaterThan(0)
})
it('should fail validation with invalid URL format', () => {
process.env.NEXT_PUBLIC_APP_URL = 'not-a-valid-url'
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(
result.errors.some(error => error.includes('not a valid URL'))
).toBe(true)
})
it('should fail validation with short JWT secret', () => {
process.env.JWT_SECRET = 'short'
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(result.errors.some(error => error.includes('JWT_SECRET'))).toBe(
true
)
})
it('should fail validation with wrong encryption key length', () => {
process.env.ENCRYPTION_KEY = 'wrong-length'
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(
result.errors.some(error => error.includes('ENCRYPTION_KEY'))
).toBe(true)
})
it('should warn about placeholder values', () => {
process.env.STRIPE_SECRET_KEY = 'sk_test_...'
const result = validateConfiguration()
expect(
result.warnings.some(warning => warning.includes('placeholder'))
).toBe(true)
})
it('should fail validation with GITHUB_ prefixed environment variables', () => {
// Add a GITHUB_ prefixed environment variable
process.env.GITHUB_SECRET = 'test-secret'
process.env.GITHUB_TOKEN = 'test-token'
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(
result.errors.some(error =>
error.includes("Environment variable 'GITHUB_SECRET'") &&
error.includes("starts with 'GITHUB_' prefix")
)
).toBe(true)
expect(
result.errors.some(error =>
error.includes("Environment variable 'GITHUB_TOKEN'") &&
error.includes("starts with 'GITHUB_' prefix")
)
).toBe(true)
// Clean up
delete process.env.GITHUB_SECRET
delete process.env.GITHUB_TOKEN
})
it('should allow non-GITHUB_ prefixed environment variables', () => {
// Add some custom environment variables that should be allowed
process.env.CUSTOM_SECRET = 'test-secret'
process.env.MY_TOKEN = 'test-token'
const result = validateConfiguration()
// Should not fail due to these custom variables
expect(
result.errors.some(error => error.includes("CUSTOM_SECRET"))
).toBe(false)
expect(
result.errors.some(error => error.includes("MY_TOKEN"))
).toBe(false)
// Clean up
delete process.env.CUSTOM_SECRET
delete process.env.MY_TOKEN
})
})
describe('Environment-Specific Configuration', () => {
it('should return development config for development environment', () => {
process.env.APP_ENV = 'development'
const config = getEnvironmentConfig()
expect(config.features.enableDebugMode).toBe(true)
expect(config.features.enableMockPayments).toBe(true)
})
it('should return production config for production environment', () => {
process.env.APP_ENV = 'production'
const config = getEnvironmentConfig()
expect(config.features.enableDebugMode).toBe(false)
expect(config.features.enableMockPayments).toBe(false)
})
it('should return staging config for staging environment', () => {
process.env.APP_ENV = 'staging'
const config = getEnvironmentConfig()
expect(config.features.enableMockPayments).toBe(true) // Still use test payments
expect(config.monitoring.enableDetailedLogging).toBe(true)
})
})
describe('Configuration Class', () => {
it('should be a singleton', () => {
const config1 = Config.getInstance()
const config2 = Config.getInstance()
expect(config1).toBe(config2)
})
it('should provide server configuration', () => {
const config = Config.getInstance()
const serverConfig = config.getServerConfig()
expect(serverConfig.NEXT_PUBLIC_APP_URL).toBe('http://localhost:3000')
expect(serverConfig.GCP_PROJECT_ID).toBe('test-project')
})
it('should provide client configuration', () => {
const config = Config.getInstance()
const clientConfig = config.getClientConfig()
expect(clientConfig.APP_URL).toBe('http://localhost:3000')
expect(clientConfig.CLERK.PUBLISHABLE_KEY).toBe('pk_test_123')
})
it('should detect environment correctly', () => {
const config = Config.getInstance()
expect(config.isDevelopment()).toBe(true)
expect(config.isProduction()).toBe(false)
expect(config.isStaging()).toBe(false)
})
it('should provide service-specific configurations', () => {
const config = Config.getInstance()
const storageConfig = config.getStorageConfig()
expect(storageConfig.gcp.projectId).toBe('test-project')
expect(storageConfig.gcp.bucketName).toBe('test-bucket')
const authConfig = config.getAuthConfig()
expect(authConfig.clerk.secretKey).toBe('sk_test_123')
expect(authConfig.jwt.secret).toBeDefined()
const paymentConfig = config.getPaymentConfig()
expect(paymentConfig.stripe.secretKey).toBe('sk_test_123')
expect(paymentConfig.stripe.prices.basic).toBe('price_basic_123')
})
})
describe('Feature Flags', () => {
it('should handle feature flags correctly', () => {
process.env.ENABLE_AI_FEATURES = 'true'
process.env.ENABLE_WHITE_LABEL = 'false'
const config = Config.getInstance()
expect(config.isFeatureEnabled('ENABLE_AI_FEATURES')).toBe(true)
expect(config.isFeatureEnabled('ENABLE_WHITE_LABEL')).toBe(false)
})
})
describe('Production Validation', () => {
beforeEach(() => {
process.env.NODE_ENV = 'production'
process.env.APP_ENV = 'production'
// Add required production variables
process.env.DATABASE_URL = 'postgresql://test:test@localhost:5432/test'
process.env.REDIS_URL = 'redis://localhost:6379'
process.env.SENTRY_DSN = 'https://test@sentry.io/123'
process.env.CLOUDFLARE_API_TOKEN = 'test-token'
process.env.CLOUDFLARE_ZONE_ID = 'test-zone'
process.env.CLOUDFLARE_ACCOUNT_ID = 'test-account'
process.env.ANALYTICS_ID = 'G-TEST123'
})
it('should require additional services in production', () => {
delete process.env.DATABASE_URL
const result = validateConfiguration()
expect(result.success).toBe(false)
expect(
result.errors.some(error =>
error.includes('DATABASE_URL is required in production')
)
).toBe(true)
})
it('should warn about debug mode in production', () => {
process.env.NEXT_PUBLIC_DEBUG_MODE = 'true'
const result = validateConfiguration()
expect(
result.errors.some(error =>
error.includes('Debug mode should be disabled in production')
)
).toBe(true)
})
})
})