File size: 8,768 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  describe,
  it,
  expect,
  vi,
  beforeEach,
  afterEach,
  type Mock,
} from 'vitest';
import { configureCommand } from './configure.js';
import yargs from 'yargs';
import { debugLogger } from '@google/gemini-cli-core';
import {
  updateSetting,
  getScopedEnvContents,
  type ExtensionSetting,
} from '../../config/extensions/extensionSettings.js';
import { cleanupTmpDir } from '@google/gemini-cli-test-utils';
import prompts from 'prompts';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

const { mockExtensionManager, mockGetExtensionManager, mockLoadSettings } =
  vi.hoisted(() => {
    const extensionManager = {
      loadExtensionConfig: vi.fn(),
      getExtensions: vi.fn(),
      loadExtensions: vi.fn(),
      getSettings: vi.fn(),
    };
    return {
      mockExtensionManager: extensionManager,
      mockGetExtensionManager: vi.fn(),
      mockLoadSettings: vi.fn().mockReturnValue({ merged: {} }),
    };
  });

vi.mock('../../config/extension-manager.js', () => ({
  ExtensionManager: vi.fn().mockImplementation(() => mockExtensionManager),
}));

vi.mock('../../config/extensions/extensionSettings.js', () => ({
  updateSetting: vi.fn(),
  promptForSetting: vi.fn(),
  getScopedEnvContents: vi.fn(),
  ExtensionSettingScope: {
    USER: 'user',
    WORKSPACE: 'workspace',
  },
}));

vi.mock('../utils.js', () => ({
  exitCli: vi.fn(),
}));

vi.mock('./utils.js', async (importOriginal) => {
  const actual = await importOriginal<typeof import('./utils.js')>();
  return {
    ...actual,
    getExtensionManager: mockGetExtensionManager,
  };
});

vi.mock('prompts');

vi.mock('../../config/extensions/consent.js', () => ({
  requestConsentNonInteractive: vi.fn(),
}));

import { ExtensionManager } from '../../config/extension-manager.js';

vi.mock('../../config/settings.js', () => ({
  loadSettings: mockLoadSettings,
}));

describe('extensions configure command', () => {
  let tempWorkspaceDir: string;

  beforeEach(() => {
    vi.spyOn(debugLogger, 'log');
    vi.spyOn(debugLogger, 'error');
    vi.clearAllMocks();

    tempWorkspaceDir = fs.mkdtempSync(
      path.join(os.tmpdir(), 'gemini-cli-test-workspace-'),
    );
    vi.spyOn(process, 'cwd').mockReturnValue(tempWorkspaceDir);
    // Default behaviors
    mockLoadSettings.mockReturnValue({ merged: {} });
    mockGetExtensionManager.mockResolvedValue(mockExtensionManager);
    (ExtensionManager as unknown as Mock).mockImplementation(
      () => mockExtensionManager,
    );
  });

  afterEach(async () => {
    await cleanupTmpDir(tempWorkspaceDir);
    vi.restoreAllMocks();
  });

  const runCommand = async (command: string) => {
    const parser = yargs().command(configureCommand).help(false).version(false);
    await parser.parse(command);
  };

  const setupExtension = (
    name: string,
    settings: Array<Partial<ExtensionSetting>> = [],
    id = 'test-id',
    path = '/test/path',
  ) => {
    const extension = { name, path, id };

    mockExtensionManager.getExtensions.mockReturnValue([extension]);
    mockExtensionManager.loadExtensionConfig.mockResolvedValue({
      name,
      settings,
    });
    return extension;
  };

  describe('Specific setting configuration', () => {
    it('should configure a specific setting', async () => {
      setupExtension('test-ext', [
        { name: 'Test Setting', envVar: 'TEST_VAR' },
      ]);
      (updateSetting as Mock).mockResolvedValue(undefined);

      await runCommand('config test-ext TEST_VAR');

      expect(updateSetting).toHaveBeenCalledWith(
        expect.objectContaining({ name: 'test-ext' }),
        'test-id',
        'TEST_VAR',
        expect.any(Function),
        'user',
        tempWorkspaceDir,
      );
    });

    it('should handle missing extension', async () => {
      mockExtensionManager.getExtensions.mockReturnValue([]);

      await runCommand('config missing-ext TEST_VAR');

      expect(updateSetting).not.toHaveBeenCalled();
    });

    it('should reject invalid extension names', async () => {
      await runCommand('config ../invalid TEST_VAR');
      expect(debugLogger.error).toHaveBeenCalledWith(
        expect.stringContaining('Invalid extension name'),
      );

      await runCommand('config ext/with/slash TEST_VAR');
      expect(debugLogger.error).toHaveBeenCalledWith(
        expect.stringContaining('Invalid extension name'),
      );
    });
  });

  describe('Extension configuration (all settings)', () => {
    it('should configure all settings for an extension', async () => {
      const settings = [{ name: 'Setting 1', envVar: 'VAR_1' }];
      setupExtension('test-ext', settings);
      (getScopedEnvContents as Mock).mockResolvedValue({});
      (updateSetting as Mock).mockResolvedValue(undefined);

      await runCommand('config test-ext');

      expect(debugLogger.log).toHaveBeenCalledWith(
        'Configuring settings for "test-ext"...',
      );
      expect(updateSetting).toHaveBeenCalledWith(
        expect.objectContaining({ name: 'test-ext' }),
        'test-id',
        'VAR_1',
        expect.any(Function),
        'user',
        tempWorkspaceDir,
      );
    });

    it('should verify overwrite if setting is already set', async () => {
      const settings = [{ name: 'Setting 1', envVar: 'VAR_1' }];
      setupExtension('test-ext', settings);
      (getScopedEnvContents as Mock).mockImplementation(
        async (_config, _id, scope) => {
          if (scope === 'user') return { VAR_1: 'existing' };
          return {};
        },
      );
      (prompts as unknown as Mock).mockResolvedValue({ confirm: true });
      (updateSetting as Mock).mockResolvedValue(undefined);

      await runCommand('config test-ext');

      expect(prompts).toHaveBeenCalledWith(
        expect.objectContaining({
          type: 'confirm',
          message: expect.stringContaining('is already set. Overwrite?'),
        }),
      );
      expect(updateSetting).toHaveBeenCalled();
    });

    it('should note if setting is configured in workspace', async () => {
      const settings = [{ name: 'Setting 1', envVar: 'VAR_1' }];
      setupExtension('test-ext', settings);
      (getScopedEnvContents as Mock).mockImplementation(
        async (_config, _id, scope) => {
          if (scope === 'workspace') return { VAR_1: 'workspace_value' };
          return {};
        },
      );
      (updateSetting as Mock).mockResolvedValue(undefined);

      await runCommand('config test-ext');

      expect(debugLogger.log).toHaveBeenCalledWith(
        expect.stringContaining('is already configured in the workspace scope'),
      );
    });

    it('should skip update if user denies overwrite', async () => {
      const settings = [{ name: 'Setting 1', envVar: 'VAR_1' }];
      setupExtension('test-ext', settings);
      (getScopedEnvContents as Mock).mockResolvedValue({ VAR_1: 'existing' });
      (prompts as unknown as Mock).mockResolvedValue({ confirm: false });

      await runCommand('config test-ext');

      expect(prompts).toHaveBeenCalled();
      expect(updateSetting).not.toHaveBeenCalled();
    });
  });

  describe('Configure all extensions', () => {
    it('should configure settings for all installed extensions', async () => {
      const ext1 = {
        name: 'ext1',
        path: '/p1',
        id: 'id1',
        settings: [{ envVar: 'V1' }],
      };
      const ext2 = {
        name: 'ext2',
        path: '/p2',
        id: 'id2',
        settings: [{ envVar: 'V2' }],
      };
      mockExtensionManager.getExtensions.mockReturnValue([ext1, ext2]);

      mockExtensionManager.loadExtensionConfig.mockImplementation(
        async (path) => {
          if (path === '/p1')
            return { name: 'ext1', settings: [{ name: 'S1', envVar: 'V1' }] };
          if (path === '/p2')
            return { name: 'ext2', settings: [{ name: 'S2', envVar: 'V2' }] };
          return null;
        },
      );

      (getScopedEnvContents as Mock).mockResolvedValue({});
      (updateSetting as Mock).mockResolvedValue(undefined);

      await runCommand('config');

      expect(debugLogger.log).toHaveBeenCalledWith(
        expect.stringContaining('Configuring settings for "ext1"'),
      );
      expect(debugLogger.log).toHaveBeenCalledWith(
        expect.stringContaining('Configuring settings for "ext2"'),
      );
      expect(updateSetting).toHaveBeenCalledTimes(2);
    });

    it('should log if no extensions installed', async () => {
      mockExtensionManager.getExtensions.mockReturnValue([]);
      await runCommand('config');
      expect(debugLogger.log).toHaveBeenCalledWith('No extensions installed.');
    });
  });
});