2025-06-13 17:44:14 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, vi } from 'vitest';
|
|
|
|
|
import { ToolConfirmationMessage } from './ToolConfirmationMessage.js';
|
2025-08-25 17:30:04 -07:00
|
|
|
import type {
|
|
|
|
|
ToolCallConfirmationDetails,
|
|
|
|
|
Config,
|
|
|
|
|
} from '@google/gemini-cli-core';
|
2026-02-03 17:08:10 -08:00
|
|
|
import { renderWithProviders } from '../../../test-utils/render.js';
|
|
|
|
|
import { createMockSettings } from '../../../test-utils/settings.js';
|
2026-01-21 16:16:30 -05:00
|
|
|
import { useToolActions } from '../../contexts/ToolActionsContext.js';
|
|
|
|
|
|
|
|
|
|
vi.mock('../../contexts/ToolActionsContext.js', async (importOriginal) => {
|
|
|
|
|
const actual =
|
|
|
|
|
await importOriginal<
|
|
|
|
|
typeof import('../../contexts/ToolActionsContext.js')
|
|
|
|
|
>();
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
useToolActions: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-06-13 17:44:14 -07:00
|
|
|
|
|
|
|
|
describe('ToolConfirmationMessage', () => {
|
2026-01-21 16:16:30 -05:00
|
|
|
const mockConfirm = vi.fn();
|
|
|
|
|
vi.mocked(useToolActions).mockReturnValue({
|
|
|
|
|
confirm: mockConfirm,
|
|
|
|
|
cancel: vi.fn(),
|
2026-01-26 21:24:25 -05:00
|
|
|
isDiffingEnabled: false,
|
2026-01-21 16:16:30 -05:00
|
|
|
});
|
|
|
|
|
|
2025-08-26 10:02:22 -07:00
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
2025-06-13 17:44:14 -07:00
|
|
|
it('should not display urls if prompt and url are the same', () => {
|
|
|
|
|
const confirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'info',
|
|
|
|
|
title: 'Confirm Web Fetch',
|
|
|
|
|
prompt: 'https://example.com',
|
|
|
|
|
urls: ['https://example.com'],
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-15 10:54:00 -07:00
|
|
|
const { lastFrame } = renderWithProviders(
|
2025-06-19 20:17:23 +00:00
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-06-19 20:17:23 +00:00
|
|
|
confirmationDetails={confirmationDetails}
|
2025-08-26 10:02:22 -07:00
|
|
|
config={mockConfig}
|
2025-06-19 20:17:23 +00:00
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
2025-06-13 17:44:14 -07:00
|
|
|
);
|
|
|
|
|
|
2025-11-22 08:17:29 +05:30
|
|
|
expect(lastFrame()).toMatchSnapshot();
|
2025-06-13 17:44:14 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should display urls if prompt and url are different', () => {
|
|
|
|
|
const confirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'info',
|
|
|
|
|
title: 'Confirm Web Fetch',
|
|
|
|
|
prompt:
|
|
|
|
|
'fetch https://github.com/google/gemini-react/blob/main/README.md',
|
|
|
|
|
urls: [
|
|
|
|
|
'https://raw.githubusercontent.com/google/gemini-react/main/README.md',
|
|
|
|
|
],
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-15 10:54:00 -07:00
|
|
|
const { lastFrame } = renderWithProviders(
|
2025-06-19 20:17:23 +00:00
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-06-19 20:17:23 +00:00
|
|
|
confirmationDetails={confirmationDetails}
|
2025-08-26 10:02:22 -07:00
|
|
|
config={mockConfig}
|
2025-06-19 20:17:23 +00:00
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
2025-06-13 17:44:14 -07:00
|
|
|
);
|
|
|
|
|
|
2025-11-22 08:17:29 +05:30
|
|
|
expect(lastFrame()).toMatchSnapshot();
|
2025-06-13 17:44:14 -07:00
|
|
|
});
|
2025-08-25 17:30:04 -07:00
|
|
|
|
2026-01-16 15:06:52 -08:00
|
|
|
it('should display multiple commands for exec type when provided', () => {
|
|
|
|
|
const confirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'exec',
|
|
|
|
|
title: 'Confirm Multiple Commands',
|
|
|
|
|
command: 'echo "hello"', // Primary command
|
|
|
|
|
rootCommand: 'echo',
|
|
|
|
|
rootCommands: ['echo'],
|
|
|
|
|
commands: ['echo "hello"', 'ls -la', 'whoami'], // Multi-command list
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2026-01-16 15:06:52 -08:00
|
|
|
confirmationDetails={confirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const output = lastFrame();
|
|
|
|
|
expect(output).toContain('echo "hello"');
|
|
|
|
|
expect(output).toContain('ls -la');
|
|
|
|
|
expect(output).toContain('whoami');
|
|
|
|
|
expect(output).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-25 17:30:04 -07:00
|
|
|
describe('with folder trust', () => {
|
|
|
|
|
const editConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'edit',
|
|
|
|
|
title: 'Confirm Edit',
|
|
|
|
|
fileName: 'test.txt',
|
|
|
|
|
filePath: '/test.txt',
|
|
|
|
|
fileDiff: '...diff...',
|
|
|
|
|
originalContent: 'a',
|
|
|
|
|
newContent: 'b',
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const execConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'exec',
|
|
|
|
|
title: 'Confirm Execution',
|
|
|
|
|
command: 'echo "hello"',
|
|
|
|
|
rootCommand: 'echo',
|
2026-01-14 13:50:28 -05:00
|
|
|
rootCommands: ['echo'],
|
2025-08-25 17:30:04 -07:00
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const infoConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'info',
|
|
|
|
|
title: 'Confirm Web Fetch',
|
|
|
|
|
prompt: 'https://example.com',
|
|
|
|
|
urls: ['https://example.com'],
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mcpConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'mcp',
|
|
|
|
|
title: 'Confirm MCP Tool',
|
|
|
|
|
serverName: 'test-server',
|
|
|
|
|
toolName: 'test-tool',
|
|
|
|
|
toolDisplayName: 'Test Tool',
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
|
{
|
|
|
|
|
description: 'for edit confirmations',
|
|
|
|
|
details: editConfirmationDetails,
|
2025-12-18 16:38:53 -08:00
|
|
|
alwaysAllowText: 'Allow for this session',
|
2025-08-25 17:30:04 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
description: 'for exec confirmations',
|
|
|
|
|
details: execConfirmationDetails,
|
2025-12-18 16:38:53 -08:00
|
|
|
alwaysAllowText: 'Allow for this session',
|
2025-08-25 17:30:04 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
description: 'for info confirmations',
|
|
|
|
|
details: infoConfirmationDetails,
|
2025-12-18 16:38:53 -08:00
|
|
|
alwaysAllowText: 'Allow for this session',
|
2025-08-25 17:30:04 -07:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
description: 'for mcp confirmations',
|
|
|
|
|
details: mcpConfirmationDetails,
|
|
|
|
|
alwaysAllowText: 'always allow',
|
|
|
|
|
},
|
2025-11-22 08:17:29 +05:30
|
|
|
])('$description', ({ details }) => {
|
2025-08-25 17:30:04 -07:00
|
|
|
it('should show "allow always" when folder is trusted', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-08-25 17:30:04 -07:00
|
|
|
confirmationDetails={details}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-22 08:17:29 +05:30
|
|
|
expect(lastFrame()).toMatchSnapshot();
|
2025-08-25 17:30:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should NOT show "allow always" when folder is untrusted', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => false,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-08-25 17:30:04 -07:00
|
|
|
confirmationDetails={details}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-22 08:17:29 +05:30
|
|
|
expect(lastFrame()).toMatchSnapshot();
|
2025-08-25 17:30:04 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-12-19 09:25:23 -08:00
|
|
|
|
|
|
|
|
describe('enablePermanentToolApproval setting', () => {
|
|
|
|
|
const editConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'edit',
|
|
|
|
|
title: 'Confirm Edit',
|
|
|
|
|
fileName: 'test.txt',
|
|
|
|
|
filePath: '/test.txt',
|
|
|
|
|
fileDiff: '...diff...',
|
|
|
|
|
originalContent: 'a',
|
|
|
|
|
newContent: 'b',
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('should NOT show "Allow for all future sessions" when setting is false (default)', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-12-19 09:25:23 -08:00
|
|
|
confirmationDetails={editConfirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
{
|
|
|
|
|
settings: createMockSettings({
|
|
|
|
|
security: { enablePermanentToolApproval: false },
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(lastFrame()).not.toContain('Allow for all future sessions');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should show "Allow for all future sessions" when setting is true', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
2026-01-21 16:16:30 -05:00
|
|
|
callId="test-call-id"
|
2025-12-19 09:25:23 -08:00
|
|
|
confirmationDetails={editConfirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
{
|
|
|
|
|
settings: createMockSettings({
|
|
|
|
|
security: { enablePermanentToolApproval: true },
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(lastFrame()).toContain('Allow for all future sessions');
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-01-26 21:24:25 -05:00
|
|
|
|
|
|
|
|
describe('Modify with external editor option', () => {
|
|
|
|
|
const editConfirmationDetails: ToolCallConfirmationDetails = {
|
|
|
|
|
type: 'edit',
|
|
|
|
|
title: 'Confirm Edit',
|
|
|
|
|
fileName: 'test.txt',
|
|
|
|
|
filePath: '/test.txt',
|
|
|
|
|
fileDiff: '...diff...',
|
|
|
|
|
originalContent: 'a',
|
|
|
|
|
newContent: 'b',
|
|
|
|
|
onConfirm: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it('should show "Modify with external editor" when NOT in IDE mode', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => false,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
vi.mocked(useToolActions).mockReturnValue({
|
|
|
|
|
confirm: vi.fn(),
|
|
|
|
|
cancel: vi.fn(),
|
|
|
|
|
isDiffingEnabled: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
|
|
|
|
callId="test-call-id"
|
|
|
|
|
confirmationDetails={editConfirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(lastFrame()).toContain('Modify with external editor');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should show "Modify with external editor" when in IDE mode but diffing is NOT enabled', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => true,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
vi.mocked(useToolActions).mockReturnValue({
|
|
|
|
|
confirm: vi.fn(),
|
|
|
|
|
cancel: vi.fn(),
|
|
|
|
|
isDiffingEnabled: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
|
|
|
|
callId="test-call-id"
|
|
|
|
|
confirmationDetails={editConfirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(lastFrame()).toContain('Modify with external editor');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should NOT show "Modify with external editor" when in IDE mode AND diffing is enabled', () => {
|
|
|
|
|
const mockConfig = {
|
|
|
|
|
isTrustedFolder: () => true,
|
|
|
|
|
getIdeMode: () => true,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
vi.mocked(useToolActions).mockReturnValue({
|
|
|
|
|
confirm: vi.fn(),
|
|
|
|
|
cancel: vi.fn(),
|
|
|
|
|
isDiffingEnabled: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { lastFrame } = renderWithProviders(
|
|
|
|
|
<ToolConfirmationMessage
|
|
|
|
|
callId="test-call-id"
|
|
|
|
|
confirmationDetails={editConfirmationDetails}
|
|
|
|
|
config={mockConfig}
|
|
|
|
|
availableTerminalHeight={30}
|
|
|
|
|
terminalWidth={80}
|
|
|
|
|
/>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(lastFrame()).not.toContain('Modify with external editor');
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-06-13 17:44:14 -07:00
|
|
|
});
|