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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
- import cliState from '../src/cliState';
- import {
- getEnvBool,
- getEnvFloat,
- getEnvInt,
- getEnvString,
- getMaxEvalTimeMs,
- isCI,
- } from '../src/envars';
- import type { EnvVarKey } from '../src/envars';
- describe('envars', () => {
- const originalEnv = process.env;
- const originalCliState = { ...cliState };
- beforeEach(() => {
- jest.resetModules();
- process.env = { ...originalEnv };
- // Reset cliState to empty for each test
- Object.keys(cliState).forEach((key) => {
- delete cliState[key as keyof typeof cliState];
- });
- });
- afterAll(() => {
- process.env = originalEnv;
- // Restore original cliState
- Object.keys(cliState).forEach((key) => {
- delete cliState[key as keyof typeof cliState];
- });
- Object.assign(cliState, originalCliState);
- });
- describe('getEnvar', () => {
- it('should return the value of an existing environment variable', () => {
- process.env.PROMPTFOO_AUTHOR = 'test value';
- expect(getEnvString('PROMPTFOO_AUTHOR')).toBe('test value');
- });
- it('should return undefined for a non-existing environment variable', () => {
- expect(getEnvString('PROMPTFOO_AUTHOR')).toBeUndefined();
- });
- it('should return the default value for a non-existing environment variable', () => {
- expect(getEnvString('PROMPTFOO_AUTHOR', 'default')).toBe('default');
- });
- it('should prioritize cliState.config.env over process.env', () => {
- process.env.OPENAI_API_KEY = 'process-env-key';
- cliState.config = {
- env: {
- OPENAI_API_KEY: 'config-env-key',
- },
- };
- expect(getEnvString('OPENAI_API_KEY')).toBe('config-env-key');
- });
- it('should convert non-string values from cliState.config.env to strings', () => {
- cliState.config = {
- env: {
- OPENAI_TEMPERATURE: 0.7 as any,
- PROMPTFOO_CACHE_ENABLED: true as any,
- },
- };
- expect(getEnvString('OPENAI_TEMPERATURE')).toBe('0.7');
- expect(getEnvString('PROMPTFOO_CACHE_ENABLED')).toBe('true');
- });
- it('should handle HTTP proxy environment variables', () => {
- process.env.HTTP_PROXY = 'http://proxy.example.com:8080';
- process.env.HTTPS_PROXY = 'https://proxy.example.com:8443';
- expect(getEnvString('HTTP_PROXY')).toBe('http://proxy.example.com:8080');
- expect(getEnvString('HTTPS_PROXY')).toBe('https://proxy.example.com:8443');
- });
- it('should handle provider-specific environment variables', () => {
- process.env.CDP_DOMAIN = 'custom.domain';
- process.env.PORTKEY_API_BASE_URL = 'https://api.portkey.example.com';
- expect(getEnvString('CDP_DOMAIN')).toBe('custom.domain');
- expect(getEnvString('PORTKEY_API_BASE_URL')).toBe('https://api.portkey.example.com');
- });
- it('should handle arbitrary string keys not defined in EnvVars type', () => {
- process.env.CUSTOM_ENV_VAR = 'custom value';
- expect(getEnvString('CUSTOM_ENV_VAR' as EnvVarKey)).toBe('custom value');
- });
- });
- describe('getEnvBool', () => {
- it('should return true for truthy string values', () => {
- ['1', 'true', 'yes', 'yup', 'yeppers'].forEach((value) => {
- process.env.PROMPTFOO_CACHE_ENABLED = value;
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(true);
- });
- });
- it('should return false for falsy string values', () => {
- ['0', 'false', 'no', 'nope'].forEach((value) => {
- process.env.PROMPTFOO_CACHE_ENABLED = value;
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(false);
- });
- });
- it('should return the default value for a non-existing environment variable', () => {
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED', true)).toBe(true);
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED', false)).toBe(false);
- });
- it('should return true for uppercase truthy string values', () => {
- ['TRUE', 'YES', 'YUP', 'YEPPERS'].forEach((value) => {
- process.env.PROMPTFOO_CACHE_ENABLED = value;
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(true);
- });
- });
- it('should return false for any other string values', () => {
- ['maybe', 'enabled', 'on'].forEach((value) => {
- process.env.PROMPTFOO_CACHE_ENABLED = value;
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(false);
- });
- });
- it('should return true when the environment variable is set to "1"', () => {
- process.env.PROMPTFOO_CACHE_ENABLED = '1';
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(true);
- });
- it('should return false when the environment variable is set to "0"', () => {
- process.env.PROMPTFOO_CACHE_ENABLED = '0';
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(false);
- });
- it('should return false when no default value is provided and the environment variable is not set', () => {
- delete process.env.PROMPTFOO_CACHE_ENABLED;
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(false);
- });
- it('should prioritize cliState.config.env over process.env for boolean values', () => {
- process.env.PROMPTFOO_CACHE_ENABLED = 'false';
- cliState.config = {
- env: {
- PROMPTFOO_CACHE_ENABLED: true as any,
- },
- };
- expect(getEnvBool('PROMPTFOO_CACHE_ENABLED')).toBe(true);
- });
- it('should handle arbitrary string keys for boolean values', () => {
- process.env.CUSTOM_BOOL_VAR = 'true';
- expect(getEnvBool('CUSTOM_BOOL_VAR' as EnvVarKey)).toBe(true);
- });
- it('should handle PROMPTFOO_DISABLE_OBJECT_STRINGIFY environment variable', () => {
- expect(getEnvBool('PROMPTFOO_DISABLE_OBJECT_STRINGIFY')).toBe(false);
- process.env.PROMPTFOO_DISABLE_OBJECT_STRINGIFY = 'true';
- expect(getEnvBool('PROMPTFOO_DISABLE_OBJECT_STRINGIFY')).toBe(true);
- process.env.PROMPTFOO_DISABLE_OBJECT_STRINGIFY = 'false';
- expect(getEnvBool('PROMPTFOO_DISABLE_OBJECT_STRINGIFY')).toBe(false);
- cliState.config = {
- env: {
- PROMPTFOO_DISABLE_OBJECT_STRINGIFY: true as any,
- },
- };
- expect(getEnvBool('PROMPTFOO_DISABLE_OBJECT_STRINGIFY')).toBe(true);
- });
- });
- describe('getEnvInt', () => {
- it('should return the integer value of an existing environment variable', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '42';
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBe(42);
- });
- it('should return undefined for a non-numeric environment variable', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = 'not a number';
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBeUndefined();
- });
- it('should return the default value for a non-existing environment variable', () => {
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT', 100)).toBe(100);
- });
- it('should floor a floating-point number in the environment variable', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '42.7';
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBe(42);
- });
- it('should handle negative numbers', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '-42';
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBe(-42);
- });
- it('should return undefined for empty string', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '';
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBeUndefined();
- });
- it('should return the default value when the environment variable is undefined', () => {
- delete process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT;
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT', 100)).toBe(100);
- });
- it('should return undefined when no default value is provided and the environment variable is not set', () => {
- delete process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT;
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBeUndefined();
- });
- it('should prioritize cliState.config.env over process.env for integer values', () => {
- process.env.PROMPTFOO_CACHE_MAX_FILE_COUNT = '100';
- cliState.config = {
- env: {
- PROMPTFOO_CACHE_MAX_FILE_COUNT: 42 as any,
- },
- };
- expect(getEnvInt('PROMPTFOO_CACHE_MAX_FILE_COUNT')).toBe(42);
- });
- it('should handle arbitrary string keys for integer values', () => {
- process.env.CUSTOM_INT_VAR = '123';
- expect(getEnvInt('CUSTOM_INT_VAR' as EnvVarKey)).toBe(123);
- });
- });
- describe('getEnvFloat', () => {
- it('should return the float value of an existing environment variable', () => {
- process.env.OPENAI_TEMPERATURE = '3.14';
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBe(3.14);
- });
- it('should return undefined for a non-numeric environment variable', () => {
- process.env.OPENAI_TEMPERATURE = 'not a number';
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBeUndefined();
- });
- it('should return the default value for a non-existing environment variable', () => {
- expect(getEnvFloat('OPENAI_TEMPERATURE', 2.718)).toBe(2.718);
- });
- it('should handle integer values', () => {
- process.env.OPENAI_TEMPERATURE = '42';
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBe(42);
- });
- it('should handle negative numbers', () => {
- process.env.OPENAI_TEMPERATURE = '-3.14';
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBe(-3.14);
- });
- it('should return undefined for empty string', () => {
- process.env.OPENAI_TEMPERATURE = '';
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBeUndefined();
- });
- it('should return the default value when the environment variable is undefined', () => {
- delete process.env.OPENAI_TEMPERATURE;
- expect(getEnvFloat('OPENAI_TEMPERATURE', 2.718)).toBe(2.718);
- });
- it('should return undefined when no default value is provided and the environment variable is not set', () => {
- delete process.env.OPENAI_TEMPERATURE;
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBeUndefined();
- });
- it('should prioritize cliState.config.env over process.env for float values', () => {
- process.env.OPENAI_TEMPERATURE = '1.0';
- cliState.config = {
- env: {
- OPENAI_TEMPERATURE: 0.7 as any,
- },
- };
- expect(getEnvFloat('OPENAI_TEMPERATURE')).toBe(0.7);
- });
- it('should handle arbitrary string keys for float values', () => {
- process.env.CUSTOM_FLOAT_VAR = '3.14159';
- expect(getEnvFloat('CUSTOM_FLOAT_VAR' as EnvVarKey)).toBe(3.14159);
- });
- });
- describe('isCI', () => {
- const ciEnvironments = [
- 'CI',
- 'GITHUB_ACTIONS',
- 'TRAVIS',
- 'CIRCLECI',
- 'JENKINS',
- 'GITLAB_CI',
- 'APPVEYOR',
- 'CODEBUILD_BUILD_ID',
- 'TF_BUILD',
- 'BITBUCKET_COMMIT',
- 'BUDDY',
- 'BUILDKITE',
- 'TEAMCITY_VERSION',
- ];
- beforeEach(() => {
- // Clear all CI-related environment variables before each test
- ciEnvironments.forEach((env) => delete process.env[env]);
- });
- it('should return false when no CI environment variables are set', () => {
- expect(isCI()).toBe(false);
- });
- ciEnvironments.forEach((env) => {
- it(`should return true when ${env} is set to 'true'`, () => {
- process.env[env] = 'true';
- expect(isCI()).toBe(true);
- });
- it(`should return false when ${env} is set to 'false'`, () => {
- process.env[env] = 'false';
- expect(isCI()).toBe(false);
- });
- });
- it('should return true if any CI environment variable is set to true', () => {
- process.env.GITHUB_ACTIONS = 'true';
- process.env.TRAVIS = 'false';
- expect(isCI()).toBe(true);
- });
- it('should prioritize cliState.config.env over process.env for CI detection', () => {
- process.env.CI = 'false';
- cliState.config = {
- env: {
- CI: 'true',
- },
- };
- expect(isCI()).toBe(true);
- });
- });
- describe('getMaxEvalTimeMs', () => {
- it('should return default value when environment variable is not set', () => {
- expect(getMaxEvalTimeMs()).toBe(0);
- expect(getMaxEvalTimeMs(5000)).toBe(5000);
- });
- it('should return parsed integer value from environment variable', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = '10000';
- expect(getMaxEvalTimeMs()).toBe(10000);
- });
- it('should handle invalid values', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = 'invalid';
- expect(getMaxEvalTimeMs(5000)).toBe(5000);
- });
- it('should prioritize cliState.config.env over process.env', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = '5000';
- cliState.config = {
- env: {
- PROMPTFOO_MAX_EVAL_TIME_MS: 10000 as any,
- },
- };
- expect(getMaxEvalTimeMs()).toBe(10000);
- });
- it('should floor floating point values', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = '1234.56';
- expect(getMaxEvalTimeMs()).toBe(1234);
- });
- it('should handle negative values', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = '-1000';
- expect(getMaxEvalTimeMs()).toBe(-1000);
- });
- it('should handle empty string', () => {
- process.env.PROMPTFOO_MAX_EVAL_TIME_MS = '';
- expect(getMaxEvalTimeMs(1000)).toBe(1000);
- });
- });
- });
|