mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-18 01:51:20 -07:00
235 lines
7.2 KiB
TypeScript
235 lines
7.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import React from 'react';
|
|
import type { ToolMessageProps } from './ToolMessage.js';
|
|
import { ToolMessage } from './ToolMessage.js';
|
|
import { StreamingState, ToolCallStatus } from '../../types.js';
|
|
import { Text } from 'ink';
|
|
import { StreamingContext } from '../../contexts/StreamingContext.js';
|
|
import type { AnsiOutput } from '@google/gemini-cli-core';
|
|
import { renderWithProviders } from '../../../test-utils/render.js';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('../TerminalOutput.js', () => ({
|
|
TerminalOutput: function MockTerminalOutput({
|
|
cursor,
|
|
}: {
|
|
cursor: { x: number; y: number } | null;
|
|
}) {
|
|
return (
|
|
<Text>
|
|
MockCursor:({cursor?.x},{cursor?.y})
|
|
</Text>
|
|
);
|
|
},
|
|
}));
|
|
|
|
vi.mock('../AnsiOutput.js', () => ({
|
|
AnsiOutputText: function MockAnsiOutputText({ data }: { data: AnsiOutput }) {
|
|
// Simple serialization for snapshot stability
|
|
const serialized = data
|
|
.map((line) => line.map((token) => token.text || '').join(''))
|
|
.join('\n');
|
|
return <Text>MockAnsiOutput:{serialized}</Text>;
|
|
},
|
|
}));
|
|
|
|
// Mock child components or utilities if they are complex or have side effects
|
|
vi.mock('../GeminiRespondingSpinner.js', () => ({
|
|
GeminiRespondingSpinner: ({
|
|
nonRespondingDisplay,
|
|
}: {
|
|
nonRespondingDisplay?: string;
|
|
}) => {
|
|
const streamingState = React.useContext(StreamingContext)!;
|
|
if (streamingState === StreamingState.Responding) {
|
|
return <Text>MockRespondingSpinner</Text>;
|
|
}
|
|
return nonRespondingDisplay ? <Text>{nonRespondingDisplay}</Text> : null;
|
|
},
|
|
}));
|
|
vi.mock('./DiffRenderer.js', () => ({
|
|
DiffRenderer: function MockDiffRenderer({
|
|
diffContent,
|
|
}: {
|
|
diffContent: string;
|
|
}) {
|
|
return <Text>MockDiff:{diffContent}</Text>;
|
|
},
|
|
}));
|
|
vi.mock('../../utils/MarkdownDisplay.js', () => ({
|
|
MarkdownDisplay: function MockMarkdownDisplay({ text }: { text: string }) {
|
|
return <Text>MockMarkdown:{text}</Text>;
|
|
},
|
|
}));
|
|
|
|
describe('<ToolMessage />', () => {
|
|
const baseProps: ToolMessageProps = {
|
|
callId: 'tool-123',
|
|
name: 'test-tool',
|
|
description: 'A tool for testing',
|
|
resultDisplay: 'Test result',
|
|
status: ToolCallStatus.Success,
|
|
terminalWidth: 80,
|
|
confirmationDetails: undefined,
|
|
emphasis: 'medium',
|
|
isFirst: true,
|
|
borderColor: 'green',
|
|
borderDimColor: false,
|
|
};
|
|
|
|
const mockSetEmbeddedShellFocused = vi.fn();
|
|
const uiActions = {
|
|
setEmbeddedShellFocused: mockSetEmbeddedShellFocused,
|
|
};
|
|
|
|
// Helper to render with context
|
|
const renderWithContext = (
|
|
ui: React.ReactElement,
|
|
streamingState: StreamingState,
|
|
) =>
|
|
renderWithProviders(ui, {
|
|
uiActions,
|
|
uiState: { streamingState },
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('renders basic tool information', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} />,
|
|
StreamingState.Idle,
|
|
);
|
|
const output = lastFrame();
|
|
expect(output).toMatchSnapshot();
|
|
});
|
|
|
|
describe('ToolStatusIndicator rendering', () => {
|
|
it('shows ✓ for Success status', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Success} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows o for Pending status', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Pending} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows ? for Confirming status', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Confirming} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows - for Canceled status', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Canceled} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows x for Error status', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Error} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows paused spinner for Executing status when streamingState is Idle', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Executing} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows paused spinner for Executing status when streamingState is WaitingForConfirmation', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Executing} />,
|
|
StreamingState.WaitingForConfirmation,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('shows MockRespondingSpinner for Executing status when streamingState is Responding', () => {
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} status={ToolCallStatus.Executing} />,
|
|
StreamingState.Responding, // Simulate app still responding
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
});
|
|
|
|
it('renders DiffRenderer for diff results', () => {
|
|
const diffResult = {
|
|
fileDiff: '--- a/file.txt\n+++ b/file.txt\n@@ -1 +1 @@\n-old\n+new',
|
|
fileName: 'file.txt',
|
|
originalContent: 'old',
|
|
newContent: 'new',
|
|
filePath: 'file.txt',
|
|
};
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} resultDisplay={diffResult} />,
|
|
StreamingState.Idle,
|
|
);
|
|
// Check that the output contains the MockDiff content as part of the whole message
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('renders emphasis correctly', () => {
|
|
const { lastFrame: highEmphasisFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} emphasis="high" />,
|
|
StreamingState.Idle,
|
|
);
|
|
// Check for trailing indicator or specific color if applicable (Colors are not easily testable here)
|
|
expect(highEmphasisFrame()).toMatchSnapshot();
|
|
|
|
const { lastFrame: lowEmphasisFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} emphasis="low" />,
|
|
StreamingState.Idle,
|
|
);
|
|
// For low emphasis, the name and description might be dimmed (check for dimColor if possible)
|
|
// This is harder to assert directly in text output without color checks.
|
|
// We can at least ensure it doesn't have the high emphasis indicator.
|
|
expect(lowEmphasisFrame()).toMatchSnapshot();
|
|
});
|
|
|
|
it('renders AnsiOutputText for AnsiOutput results', () => {
|
|
const ansiResult: AnsiOutput = [
|
|
[
|
|
{
|
|
text: 'hello',
|
|
fg: '#ffffff',
|
|
bg: '#000000',
|
|
bold: false,
|
|
italic: false,
|
|
underline: false,
|
|
dim: false,
|
|
inverse: false,
|
|
},
|
|
],
|
|
];
|
|
const { lastFrame } = renderWithContext(
|
|
<ToolMessage {...baseProps} resultDisplay={ansiResult} />,
|
|
StreamingState.Idle,
|
|
);
|
|
expect(lastFrame()).toMatchSnapshot();
|
|
});
|
|
});
|