Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/ui/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
const [showIdeRestartPrompt, setShowIdeRestartPrompt] = useState(false);

const { isFolderTrustDialogOpen, handleFolderTrustSelect, isRestarting } =
useFolderTrust(settings, config, setIsTrustedFolder, refreshStatic);
useFolderTrust(settings, setIsTrustedFolder, refreshStatic);
const { needsRestart: ideNeedsRestart } = useIdeTrustListener();
const isInitialMount = useRef(true);

Expand Down
31 changes: 14 additions & 17 deletions packages/cli/src/ui/hooks/useFolderTrust.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ vi.mock('process', () => ({

describe('useFolderTrust', () => {
let mockSettings: LoadedSettings;
let mockConfig: unknown;
let mockTrustedFolders: LoadedTrustedFolders;
let loadTrustedFoldersSpy: vi.SpyInstance;
let isWorkspaceTrustedSpy: vi.SpyInstance;
Expand All @@ -41,8 +40,6 @@ describe('useFolderTrust', () => {
setValue: vi.fn(),
} as unknown as LoadedSettings;

mockConfig = {} as unknown;

mockTrustedFolders = {
setValue: vi.fn(),
} as unknown as LoadedTrustedFolders;
Expand All @@ -63,7 +60,7 @@ describe('useFolderTrust', () => {
it('should not open dialog when folder is already trusted', () => {
isWorkspaceTrustedSpy.mockReturnValue(true);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);
expect(result.current.isFolderTrustDialogOpen).toBe(false);
expect(onTrustChange).toHaveBeenCalledWith(true);
Expand All @@ -72,7 +69,7 @@ describe('useFolderTrust', () => {
it('should not open dialog when folder is already untrusted', () => {
isWorkspaceTrustedSpy.mockReturnValue(false);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);
expect(result.current.isFolderTrustDialogOpen).toBe(false);
expect(onTrustChange).toHaveBeenCalledWith(false);
Expand All @@ -81,7 +78,7 @@ describe('useFolderTrust', () => {
it('should open dialog when folder trust is undefined', () => {
isWorkspaceTrustedSpy.mockReturnValue(undefined);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);
expect(result.current.isFolderTrustDialogOpen).toBe(true);
expect(onTrustChange).toHaveBeenCalledWith(undefined);
Expand All @@ -92,7 +89,7 @@ describe('useFolderTrust', () => {
.mockReturnValueOnce(undefined)
.mockReturnValueOnce(true);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

isWorkspaceTrustedSpy.mockReturnValue(true);
Expand All @@ -114,7 +111,7 @@ describe('useFolderTrust', () => {
.mockReturnValueOnce(undefined)
.mockReturnValueOnce(true);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

act(() => {
Expand All @@ -134,7 +131,7 @@ describe('useFolderTrust', () => {
.mockReturnValueOnce(undefined)
.mockReturnValueOnce(false);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

act(() => {
Expand All @@ -146,14 +143,14 @@ describe('useFolderTrust', () => {
TrustLevel.DO_NOT_TRUST,
);
expect(onTrustChange).toHaveBeenLastCalledWith(false);
expect(result.current.isRestarting).toBe(false);
expect(result.current.isFolderTrustDialogOpen).toBe(false);
expect(result.current.isRestarting).toBe(true);
expect(result.current.isFolderTrustDialogOpen).toBe(true);
});

it('should do nothing for default choice', () => {
isWorkspaceTrustedSpy.mockReturnValue(undefined);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

act(() => {
Expand All @@ -171,23 +168,23 @@ describe('useFolderTrust', () => {
it('should set isRestarting to true when trust status changes from false to true', () => {
isWorkspaceTrustedSpy.mockReturnValueOnce(false).mockReturnValueOnce(true); // Initially untrusted, then trusted
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

act(() => {
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
});

expect(result.current.isRestarting).toBe(false);
expect(result.current.isFolderTrustDialogOpen).toBe(false); // Dialog should close after selection
expect(result.current.isRestarting).toBe(true);
expect(result.current.isFolderTrustDialogOpen).toBe(true); // Dialog should stay open
});

it('should not set isRestarting to true when trust status does not change', () => {
isWorkspaceTrustedSpy
.mockReturnValueOnce(undefined)
.mockReturnValueOnce(true); // Initially undefined, then trust
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

act(() => {
Expand All @@ -201,7 +198,7 @@ describe('useFolderTrust', () => {
it('should call refreshStatic when dialog opens and closes', () => {
isWorkspaceTrustedSpy.mockReturnValue(undefined);
const { result } = renderHook(() =>
useFolderTrust(mockSettings, mockConfig, onTrustChange, refreshStatic),
useFolderTrust(mockSettings, onTrustChange, refreshStatic),
);

// The hook runs, isFolderTrustDialogOpen becomes true, useEffect triggers.
Expand Down
25 changes: 17 additions & 8 deletions packages/cli/src/ui/hooks/useFolderTrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { useState, useCallback, useEffect } from 'react';
import { type Config } from '@google/gemini-cli-core';
import type { LoadedSettings } from '../../config/settings.js';
import { FolderTrustChoice } from '../components/FolderTrustDialog.js';
import {
Expand All @@ -17,13 +16,12 @@ import * as process from 'node:process';

export const useFolderTrust = (
settings: LoadedSettings,
config: Config,
onTrustChange: (isTrusted: boolean | undefined) => void,
refreshStatic: () => void,
) => {
const [isTrusted, setIsTrusted] = useState<boolean | undefined>(undefined);
const [isFolderTrustDialogOpen, setIsFolderTrustDialogOpen] = useState(false);
const [isRestarting] = useState(false);
const [isRestarting, setIsRestarting] = useState(false);

const folderTrust = settings.merged.security?.folderTrust?.enabled;

Expand All @@ -46,6 +44,8 @@ export const useFolderTrust = (
const cwd = process.cwd();
let trustLevel: TrustLevel;

const wasTrusted = isTrusted ?? true;

switch (choice) {
case FolderTrustChoice.TRUST_FOLDER:
trustLevel = TrustLevel.TRUST_FOLDER;
Expand All @@ -61,12 +61,21 @@ export const useFolderTrust = (
}

trustedFolders.setValue(cwd, trustLevel);
const trusted = isWorkspaceTrusted(settings.merged);
setIsTrusted(trusted);
setIsFolderTrustDialogOpen(false);
onTrustChange(trusted);
const currentIsTrusted =
trustLevel === TrustLevel.TRUST_FOLDER ||
trustLevel === TrustLevel.TRUST_PARENT;
setIsTrusted(currentIsTrusted);
onTrustChange(currentIsTrusted);

const needsRestart = wasTrusted !== currentIsTrusted;
if (needsRestart) {
setIsRestarting(true);
setIsFolderTrustDialogOpen(true);
} else {
setIsFolderTrustDialogOpen(false);
}
},
[settings.merged, onTrustChange],
[onTrustChange, isTrusted],
);

return {
Expand Down
Loading