fix(cli): Exit CLI when trust save unsuccessful during launch (#11968)

This commit is contained in:
shrutip90
2025-11-14 11:56:39 -08:00
committed by GitHub
parent 6d83d3440c
commit d683e1c0db
7 changed files with 174 additions and 30 deletions
@@ -14,8 +14,10 @@ import { FolderTrustChoice } from '../components/FolderTrustDialog.js';
import type { LoadedTrustedFolders } from '../../config/trustedFolders.js';
import { TrustLevel } from '../../config/trustedFolders.js';
import * as trustedFolders from '../../config/trustedFolders.js';
import { coreEvents } from '@google/gemini-cli-core';
const mockedCwd = vi.hoisted(() => vi.fn());
const mockedExit = vi.hoisted(() => vi.fn());
vi.mock('node:process', async () => {
const actual =
@@ -23,6 +25,7 @@ vi.mock('node:process', async () => {
return {
...actual,
cwd: mockedCwd,
exit: mockedExit,
platform: 'linux',
};
});
@@ -35,6 +38,7 @@ describe('useFolderTrust', () => {
let addItem: Mock;
beforeEach(() => {
vi.useFakeTimers();
mockSettings = {
merged: {
security: {
@@ -60,6 +64,7 @@ describe('useFolderTrust', () => {
});
afterEach(() => {
vi.useRealTimers();
vi.clearAllMocks();
});
@@ -260,4 +265,30 @@ describe('useFolderTrust', () => {
expect(result.current.isRestarting).toBe(false);
expect(result.current.isFolderTrustDialogOpen).toBe(false); // Dialog should close
});
it('should emit feedback on failure to set value', () => {
isWorkspaceTrustedSpy.mockReturnValue({
isTrusted: undefined,
source: undefined,
});
(mockTrustedFolders.setValue as Mock).mockImplementation(() => {
throw new Error('test error');
});
const emitFeedbackSpy = vi.spyOn(coreEvents, 'emitFeedback');
const { result } = renderHook(() =>
useFolderTrust(mockSettings, onTrustChange, addItem),
);
act(() => {
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
});
vi.runAllTimers();
expect(emitFeedbackSpy).toHaveBeenCalledWith(
'error',
'Failed to save trust settings. Exiting Gemini CLI.',
);
expect(mockedExit).toHaveBeenCalledWith(1);
});
});