|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 8 | +import prompts from 'prompts'; |
| 9 | +import { coreEvents, ExitCodes } from '@google/gemini-cli-core'; |
| 10 | +import { relaunchApp } from '../utils/processUtils.js'; |
| 11 | +import { |
| 12 | + isWorkspaceTrusted, |
| 13 | + loadTrustedFolders, |
| 14 | + TrustLevel, |
| 15 | +} from './trustedFolders.js'; |
| 16 | +import { maybePromptForFolderTrust } from './folderTrustPrompt.js'; |
| 17 | +import type { MergedSettings } from './settings.js'; |
| 18 | + |
| 19 | +vi.mock('prompts', () => ({ |
| 20 | + default: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('../utils/processUtils.js', () => ({ |
| 24 | + relaunchApp: vi.fn().mockResolvedValue(undefined), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('@google/gemini-cli-core', async (importOriginal) => { |
| 28 | + const actual = |
| 29 | + await importOriginal<typeof import('@google/gemini-cli-core')>(); |
| 30 | + const mockCoreEvents = Object.create( |
| 31 | + actual.coreEvents, |
| 32 | + ) as typeof actual.coreEvents; |
| 33 | + mockCoreEvents.emitFeedback = vi.fn(); |
| 34 | + return { |
| 35 | + ...actual, |
| 36 | + coreEvents: mockCoreEvents, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock('./trustedFolders.js', async (importOriginal) => { |
| 41 | + const actual = await importOriginal<typeof import('./trustedFolders.js')>(); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + isWorkspaceTrusted: vi.fn(), |
| 45 | + loadTrustedFolders: vi.fn(), |
| 46 | + }; |
| 47 | +}); |
| 48 | + |
| 49 | +describe('maybePromptForFolderTrust', () => { |
| 50 | + const settings = { |
| 51 | + security: { |
| 52 | + folderTrust: { |
| 53 | + enabled: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } as MergedSettings; |
| 57 | + const argv = { |
| 58 | + prompt: undefined, |
| 59 | + query: undefined, |
| 60 | + isCommand: undefined, |
| 61 | + }; |
| 62 | + const setValue = vi.fn().mockResolvedValue(undefined); |
| 63 | + const stdinIsTTY = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY'); |
| 64 | + const stdoutIsTTY = Object.getOwnPropertyDescriptor(process.stdout, 'isTTY'); |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + vi.clearAllMocks(); |
| 68 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 69 | + configurable: true, |
| 70 | + value: true, |
| 71 | + }); |
| 72 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 73 | + configurable: true, |
| 74 | + value: true, |
| 75 | + }); |
| 76 | + vi.mocked(isWorkspaceTrusted).mockReturnValue({ |
| 77 | + isTrusted: undefined, |
| 78 | + source: undefined, |
| 79 | + }); |
| 80 | + vi.mocked(loadTrustedFolders).mockReturnValue({ |
| 81 | + setValue, |
| 82 | + } as unknown as ReturnType<typeof loadTrustedFolders>); |
| 83 | + vi.mocked(prompts).mockResolvedValue({ |
| 84 | + trustLevel: TrustLevel.DO_NOT_TRUST, |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 90 | + configurable: true, |
| 91 | + value: stdinIsTTY?.value, |
| 92 | + }); |
| 93 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 94 | + configurable: true, |
| 95 | + value: stdoutIsTTY?.value, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('does nothing when the workspace trust state is already known', async () => { |
| 100 | + vi.mocked(isWorkspaceTrusted).mockReturnValue({ |
| 101 | + isTrusted: false, |
| 102 | + source: 'file', |
| 103 | + }); |
| 104 | + |
| 105 | + await maybePromptForFolderTrust(settings, argv, '/repo'); |
| 106 | + |
| 107 | + expect(prompts).not.toHaveBeenCalled(); |
| 108 | + expect(setValue).not.toHaveBeenCalled(); |
| 109 | + expect(relaunchApp).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('saves an explicit do-not-trust decision and continues without relaunching', async () => { |
| 113 | + await maybePromptForFolderTrust(settings, argv, '/repo'); |
| 114 | + |
| 115 | + expect(prompts).toHaveBeenCalledExactlyOnceWith( |
| 116 | + expect.objectContaining({ |
| 117 | + stdin: process.stdin, |
| 118 | + stdout: expect.anything(), |
| 119 | + }), |
| 120 | + ); |
| 121 | + expect(setValue).toHaveBeenCalledWith('/repo', TrustLevel.DO_NOT_TRUST); |
| 122 | + expect(relaunchApp).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('saves a trust decision and relaunches before authentication continues', async () => { |
| 126 | + vi.mocked(prompts).mockResolvedValue({ |
| 127 | + trustLevel: TrustLevel.TRUST_FOLDER, |
| 128 | + }); |
| 129 | + |
| 130 | + await maybePromptForFolderTrust(settings, argv, '/repo'); |
| 131 | + |
| 132 | + expect(setValue).toHaveBeenCalledWith('/repo', TrustLevel.TRUST_FOLDER); |
| 133 | + expect(relaunchApp).toHaveBeenCalledOnce(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('exits with a config error if the trust decision cannot be saved', async () => { |
| 137 | + const processExitSpy = vi |
| 138 | + .spyOn(process, 'exit') |
| 139 | + .mockImplementation((code?: string | number | null) => { |
| 140 | + throw new Error(`process.exit:${code}`); |
| 141 | + }); |
| 142 | + setValue.mockRejectedValueOnce(new Error('disk full')); |
| 143 | + |
| 144 | + await expect( |
| 145 | + maybePromptForFolderTrust(settings, argv, '/repo'), |
| 146 | + ).rejects.toThrow(`process.exit:${ExitCodes.FATAL_CONFIG_ERROR}`); |
| 147 | + |
| 148 | + expect(coreEvents.emitFeedback).toHaveBeenCalledWith( |
| 149 | + 'error', |
| 150 | + 'Failed to save trust settings. Exiting Gemini CLI.', |
| 151 | + ); |
| 152 | + processExitSpy.mockRestore(); |
| 153 | + }); |
| 154 | +}); |
0 commit comments