File size: 6,113 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  renderWithProviders,
  persistentStateMock,
} from '../../test-utils/render.js';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { AlternateBufferQuittingDisplay } from './AlternateBufferQuittingDisplay.js';
import type { HistoryItem, HistoryItemWithoutId } from '../types.js';
import { Text } from 'ink';
import { CoreToolCallStatus } from '@google/gemini-cli-core';

vi.mock('../utils/terminalSetup.js', () => ({
  getTerminalProgram: () => null,
}));

vi.mock('../contexts/AppContext.js', async (importOriginal) => {
  const actual =
    await importOriginal<typeof import('../contexts/AppContext.js')>();
  return {
    ...actual,
    useAppContext: () => ({
      version: '0.10.0',
    }),
  };
});

vi.mock('@google/gemini-cli-core', async (importOriginal) => {
  const actual =
    await importOriginal<typeof import('@google/gemini-cli-core')>();
  return {
    ...actual,
    getMCPServerStatus: vi.fn(),
  };
});

vi.mock('../GeminiRespondingSpinner.js', () => ({
  GeminiRespondingSpinner: () => <Text>Spinner</Text>,
}));

const mockHistory: HistoryItem[] = [
  {
    id: 1,
    type: 'tool_group',
    tools: [
      {
        callId: 'call1',
        name: 'tool1',
        description: 'Description for tool 1',
        status: CoreToolCallStatus.Success,
        resultDisplay: undefined,
        confirmationDetails: undefined,
      },
    ],
  },
  {
    id: 2,
    type: 'tool_group',
    tools: [
      {
        callId: 'call2',
        name: 'tool2',
        description: 'Description for tool 2',
        status: CoreToolCallStatus.Success,
        resultDisplay: undefined,
        confirmationDetails: undefined,
      },
    ],
  },
];

const mockPendingHistoryItems: HistoryItemWithoutId[] = [
  {
    type: 'tool_group',
    tools: [
      {
        callId: 'call3',
        name: 'tool3',
        description: 'Description for tool 3',
        status: CoreToolCallStatus.Scheduled,
        resultDisplay: undefined,
        confirmationDetails: undefined,
      },
    ],
  },
];

describe('AlternateBufferQuittingDisplay', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });
  const baseUIState = {
    terminalWidth: 80,
    mainAreaWidth: 80,
    slashCommands: [],
    activePtyId: undefined,
    embeddedShellFocused: false,
    renderMarkdown: false,
    bannerData: {
      defaultText: '',
      warningText: '',
    },
  };

  it('renders with active and pending tool messages', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history: mockHistory,
          pendingHistoryItems: mockPendingHistoryItems,
        },
      },
    );
    expect(lastFrame()).toMatchSnapshot('with_history_and_pending');
    unmount();
  });

  it('renders with empty history and no pending items', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history: [],
          pendingHistoryItems: [],
        },
      },
    );
    expect(lastFrame()).toMatchSnapshot('empty');
    unmount();
  });

  it('renders with history but no pending items', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history: mockHistory,
          pendingHistoryItems: [],
        },
      },
    );
    expect(lastFrame()).toMatchSnapshot('with_history_no_pending');
    unmount();
  });

  it('renders with pending items but no history', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history: [],
          pendingHistoryItems: mockPendingHistoryItems,
        },
      },
    );
    expect(lastFrame()).toMatchSnapshot('with_pending_no_history');
    unmount();
  });

  it('renders with a tool awaiting confirmation', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const pendingHistoryItems: HistoryItemWithoutId[] = [
      {
        type: 'tool_group',
        tools: [
          {
            callId: 'call4',
            name: 'confirming_tool',
            description: 'Confirming tool description',
            status: CoreToolCallStatus.AwaitingApproval,
            resultDisplay: undefined,
            confirmationDetails: {
              type: 'info',
              title: 'Confirm Tool',
              prompt: 'Confirm this action?',
            },
          },
        ],
      },
    ];
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history: [],
          pendingHistoryItems,
        },
      },
    );
    const output = lastFrame();
    expect(output).toContain('Action Required (was prompted):');
    expect(output).toContain('confirming_tool');
    expect(output).toContain('Confirming tool description');
    expect(output).toMatchSnapshot('with_confirming_tool');
    unmount();
  });

  it('renders with user and gemini messages', async () => {
    persistentStateMock.setData({ tipsShown: 0 });
    const history: HistoryItem[] = [
      { id: 1, type: 'user', text: 'Hello Gemini' },
      { id: 2, type: 'gemini', text: 'Hello User!' },
    ];
    const { lastFrame, unmount } = await renderWithProviders(
      <AlternateBufferQuittingDisplay />,
      {
        uiState: {
          ...baseUIState,
          history,
          pendingHistoryItems: [],
        },
      },
    );
    expect(lastFrame()).toMatchSnapshot('with_user_gemini_messages');
    unmount();
  });
});