File size: 7,993 Bytes
d810ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
  resolveEnvVarsInString,
  resolveEnvVarsInObject,
} from './envVarResolver.js';

describe('resolveEnvVarsInString', () => {
  let originalEnv: NodeJS.ProcessEnv;

  beforeEach(() => {
    originalEnv = { ...process.env };
  });

  afterEach(() => {
    process.env = originalEnv;
  });

  it('should resolve $VAR_NAME format', () => {
    process.env['TEST_VAR'] = 'test-value';

    const result = resolveEnvVarsInString('Value is $TEST_VAR');

    expect(result).toBe('Value is test-value');
  });

  it('should resolve ${VAR_NAME} format', () => {
    process.env['TEST_VAR'] = 'test-value';

    const result = resolveEnvVarsInString('Value is ${TEST_VAR}');

    expect(result).toBe('Value is test-value');
  });

  it('should resolve multiple variables in the same string', () => {
    process.env['HOST'] = 'localhost';
    process.env['PORT'] = '3000';

    const result = resolveEnvVarsInString('URL: http://$HOST:${PORT}/api');

    expect(result).toBe('URL: http://localhost:3000/api');
  });

  it('should leave undefined variables unchanged', () => {
    const result = resolveEnvVarsInString('Value is $UNDEFINED_VAR');

    expect(result).toBe('Value is $UNDEFINED_VAR');
  });

  it('should leave undefined variables with braces unchanged', () => {
    const result = resolveEnvVarsInString('Value is ${UNDEFINED_VAR}');

    expect(result).toBe('Value is ${UNDEFINED_VAR}');
  });

  it('should handle empty string', () => {
    const result = resolveEnvVarsInString('');

    expect(result).toBe('');
  });

  it('should handle string without variables', () => {
    const result = resolveEnvVarsInString('No variables here');

    expect(result).toBe('No variables here');
  });

  it('should handle mixed defined and undefined variables', () => {
    process.env['DEFINED'] = 'value';

    const result = resolveEnvVarsInString('$DEFINED and $UNDEFINED mixed');

    expect(result).toBe('value and $UNDEFINED mixed');
  });
});

describe('resolveEnvVarsInObject', () => {
  let originalEnv: NodeJS.ProcessEnv;

  beforeEach(() => {
    originalEnv = { ...process.env };
  });

  afterEach(() => {
    process.env = originalEnv;
  });

  it('should resolve variables in nested objects', () => {
    process.env['API_KEY'] = 'secret-123';
    process.env['DB_URL'] = 'postgresql://localhost/test';

    const config = {
      server: {
        auth: {
          key: '$API_KEY',
        },
        database: '${DB_URL}',
      },
      port: 3000,
    };

    const result = resolveEnvVarsInObject(config);

    expect(result).toEqual({
      server: {
        auth: {
          key: 'secret-123',
        },
        database: 'postgresql://localhost/test',
      },
      port: 3000,
    });
  });

  it('should resolve variables in arrays', () => {
    process.env['ENV'] = 'production';
    process.env['VERSION'] = '1.0.0';

    const config = {
      tags: ['$ENV', 'app', '${VERSION}'],
      metadata: {
        env: '$ENV',
      },
    };

    const result = resolveEnvVarsInObject(config);

    expect(result).toEqual({
      tags: ['production', 'app', '1.0.0'],
      metadata: {
        env: 'production',
      },
    });
  });

  it('should preserve non-string types', () => {
    const config = {
      enabled: true,
      count: 42,
      value: null,
      data: undefined,
      tags: ['item1', 'item2'],
    };

    const result = resolveEnvVarsInObject(config);

    expect(result).toEqual(config);
  });

  it('should handle MCP server config structure', () => {
    process.env['API_TOKEN'] = 'token-123';
    process.env['SERVER_PORT'] = '8080';

    const extensionConfig = {
      name: 'test-extension',
      version: '1.0.0',
      mcpServers: {
        'test-server': {
          command: 'node',
          args: ['server.js', '--port', '${SERVER_PORT}'],
          env: {
            API_KEY: '$API_TOKEN',
            STATIC_VALUE: 'unchanged',
          },
          timeout: 5000,
        },
      },
    };

    const result = resolveEnvVarsInObject(extensionConfig);

    expect(result).toEqual({
      name: 'test-extension',
      version: '1.0.0',
      mcpServers: {
        'test-server': {
          command: 'node',
          args: ['server.js', '--port', '8080'],
          env: {
            API_KEY: 'token-123',
            STATIC_VALUE: 'unchanged',
          },
          timeout: 5000,
        },
      },
    });
  });

  it('should handle empty and null values', () => {
    const config = {
      empty: '',
      nullValue: null,
      undefinedValue: undefined,
      zero: 0,
      false: false,
    };

    const result = resolveEnvVarsInObject(config);

    expect(result).toEqual(config);
  });

  it('should handle circular references in objects without infinite recursion', () => {
    process.env['TEST_VAR'] = 'resolved-value';

    type ConfigWithCircularRef = {
      name: string;
      value: number;
      self?: ConfigWithCircularRef;
    };

    const config: ConfigWithCircularRef = {
      name: '$TEST_VAR',
      value: 42,
    };
    // Create circular reference
    config.self = config;

    const result = resolveEnvVarsInObject(config);

    expect(result.name).toBe('resolved-value');
    expect(result.value).toBe(42);
    expect(result.self).toBeDefined();
    expect(result.self?.name).toBe('$TEST_VAR'); // Circular reference should be shallow copied
    expect(result.self?.value).toBe(42);
    // Verify it doesn't create infinite recursion by checking it's not the same object
    expect(result.self).not.toBe(result);
  });

  it('should handle circular references in arrays without infinite recursion', () => {
    process.env['ARRAY_VAR'] = 'array-value';

    type ArrayWithCircularRef = Array<string | number | ArrayWithCircularRef>;
    const arr: ArrayWithCircularRef = ['$ARRAY_VAR', 123];
    // Create circular reference
    arr.push(arr);

    const result = resolveEnvVarsInObject(arr) as ArrayWithCircularRef;

    expect(result[0]).toBe('array-value');
    expect(result[1]).toBe(123);
    expect(Array.isArray(result[2])).toBe(true);
    const subArray = result[2] as ArrayWithCircularRef;
    expect(subArray[0]).toBe('$ARRAY_VAR'); // Circular reference should be shallow copied
    expect(subArray[1]).toBe(123);
    // Verify it doesn't create infinite recursion
    expect(result[2]).not.toBe(result);
  });

  it('should handle complex nested circular references', () => {
    process.env['NESTED_VAR'] = 'nested-resolved';

    type ObjWithRef = {
      name: string;
      id: number;
      ref?: ObjWithRef;
    };

    const obj1: ObjWithRef = { name: '$NESTED_VAR', id: 1 };
    const obj2: ObjWithRef = { name: 'static', id: 2 };

    // Create cross-references
    obj1.ref = obj2;
    obj2.ref = obj1;

    const config = {
      primary: obj1,
      secondary: obj2,
      value: '$NESTED_VAR',
    };

    const result = resolveEnvVarsInObject(config);

    expect(result.value).toBe('nested-resolved');
    expect(result.primary.name).toBe('nested-resolved');
    expect(result.primary.id).toBe(1);
    expect(result.secondary.name).toBe('static');
    expect(result.secondary.id).toBe(2);

    // Check that circular references are handled (shallow copied)
    expect(result.primary.ref).toBeDefined();
    expect(result.secondary.ref).toBeDefined();
    expect(result.primary.ref?.name).toBe('static'); // Should be shallow copy
    expect(result.secondary.ref?.name).toBe('nested-resolved'); // The shallow copy still gets processed

    // Most importantly: verify no infinite recursion by checking objects are different
    expect(result.primary.ref).not.toBe(result.secondary);
    expect(result.secondary.ref).not.toBe(result.primary);
    expect(result.primary).not.toBe(obj1); // New object created
    expect(result.secondary).not.toBe(obj2); // New object created
  });
});