summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/useAutoAcceptIndicator.test.ts
blob: 8c611cccf0980a42d5cb334d96dea1fbf749bfcb (plain)
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
233
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  describe,
  it,
  expect,
  vi,
  beforeEach,
  type MockedFunction,
  type Mock,
} from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useAutoAcceptIndicator } from './useAutoAcceptIndicator.js';

import type { Config as ActualConfigType } from '@gemini-code/core';
import { useInput, type Key as InkKey } from 'ink';

vi.mock('ink');

vi.mock('@gemini-code/core', async () => {
  const actualServerModule = (await vi.importActual(
    '@gemini-code/core',
  )) as Record<string, unknown>;
  return {
    ...actualServerModule,
    Config: vi.fn(),
  };
});

import { Config } from '@gemini-code/core';

interface MockConfigInstanceShape {
  getAlwaysSkipModificationConfirmation: Mock<() => boolean>;
  setAlwaysSkipModificationConfirmation: Mock<(value: boolean) => void>;
  getCoreTools: Mock<() => string[]>;
  getToolDiscoveryCommand: Mock<() => string | undefined>;
  getTargetDir: Mock<() => string>;
  getApiKey: Mock<() => string>;
  getModel: Mock<() => string>;
  getSandbox: Mock<() => boolean | string>;
  getDebugMode: Mock<() => boolean>;
  getQuestion: Mock<() => string | undefined>;
  getFullContext: Mock<() => boolean>;
  getUserAgent: Mock<() => string>;
  getUserMemory: Mock<() => string>;
  getGeminiMdFileCount: Mock<() => number>;
  getToolRegistry: Mock<() => { discoverTools: Mock<() => void> }>;
}

type UseInputKey = InkKey;
type UseInputHandler = (input: string, key: UseInputKey) => void;

describe('useAutoAcceptIndicator', () => {
  let mockConfigInstance: MockConfigInstanceShape;
  let capturedUseInputHandler: UseInputHandler;
  let mockedInkUseInput: MockedFunction<typeof useInput>;

  beforeEach(() => {
    vi.resetAllMocks();

    (
      Config as unknown as MockedFunction<() => MockConfigInstanceShape>
    ).mockImplementation(() => {
      const instanceGetAlwaysSkipMock = vi.fn();
      const instanceSetAlwaysSkipMock = vi.fn();

      const instance: MockConfigInstanceShape = {
        getAlwaysSkipModificationConfirmation:
          instanceGetAlwaysSkipMock as Mock<() => boolean>,
        setAlwaysSkipModificationConfirmation:
          instanceSetAlwaysSkipMock as Mock<(value: boolean) => void>,
        getCoreTools: vi.fn().mockReturnValue([]) as Mock<() => string[]>,
        getToolDiscoveryCommand: vi.fn().mockReturnValue(undefined) as Mock<
          () => string | undefined
        >,
        getTargetDir: vi.fn().mockReturnValue('.') as Mock<() => string>,
        getApiKey: vi.fn().mockReturnValue('test-api-key') as Mock<
          () => string
        >,
        getModel: vi.fn().mockReturnValue('test-model') as Mock<() => string>,
        getSandbox: vi.fn().mockReturnValue(false) as Mock<
          () => boolean | string
        >,
        getDebugMode: vi.fn().mockReturnValue(false) as Mock<() => boolean>,
        getQuestion: vi.fn().mockReturnValue(undefined) as Mock<
          () => string | undefined
        >,
        getFullContext: vi.fn().mockReturnValue(false) as Mock<() => boolean>,
        getUserAgent: vi.fn().mockReturnValue('test-user-agent') as Mock<
          () => string
        >,
        getUserMemory: vi.fn().mockReturnValue('') as Mock<() => string>,
        getGeminiMdFileCount: vi.fn().mockReturnValue(0) as Mock<() => number>,
        getToolRegistry: vi
          .fn()
          .mockReturnValue({ discoverTools: vi.fn() }) as Mock<
          () => { discoverTools: Mock<() => void> }
        >,
      };
      instanceSetAlwaysSkipMock.mockImplementation((value: boolean) => {
        instanceGetAlwaysSkipMock.mockReturnValue(value);
      });
      return instance;
    });

    mockedInkUseInput = useInput as MockedFunction<typeof useInput>;
    mockedInkUseInput.mockImplementation((handler: UseInputHandler) => {
      capturedUseInputHandler = handler;
    });

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    mockConfigInstance = new (Config as any)() as MockConfigInstanceShape;
  });

  it('should initialize with true if config.getAlwaysSkipModificationConfirmation returns true', () => {
    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      true,
    );
    const { result } = renderHook(() =>
      useAutoAcceptIndicator({
        config: mockConfigInstance as unknown as ActualConfigType,
      }),
    );
    expect(result.current).toBe(true);
    expect(
      mockConfigInstance.getAlwaysSkipModificationConfirmation,
    ).toHaveBeenCalledTimes(1);
  });

  it('should initialize with false if config.getAlwaysSkipModificationConfirmation returns false', () => {
    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      false,
    );
    const { result } = renderHook(() =>
      useAutoAcceptIndicator({
        config: mockConfigInstance as unknown as ActualConfigType,
      }),
    );
    expect(result.current).toBe(false);
    expect(
      mockConfigInstance.getAlwaysSkipModificationConfirmation,
    ).toHaveBeenCalledTimes(1);
  });

  it('should toggle the indicator and update config when Shift+Tab is pressed', () => {
    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      false,
    );
    const { result } = renderHook(() =>
      useAutoAcceptIndicator({
        config: mockConfigInstance as unknown as ActualConfigType,
      }),
    );
    expect(result.current).toBe(false);

    act(() => {
      capturedUseInputHandler('', { tab: true, shift: true } as InkKey);
    });
    expect(
      mockConfigInstance.setAlwaysSkipModificationConfirmation,
    ).toHaveBeenCalledWith(true);
    expect(result.current).toBe(true);

    act(() => {
      capturedUseInputHandler('', { tab: true, shift: true } as InkKey);
    });
    expect(
      mockConfigInstance.setAlwaysSkipModificationConfirmation,
    ).toHaveBeenCalledWith(false);
    expect(result.current).toBe(false);
  });

  it('should not toggle if only Tab, only Shift, or other keys are pressed', () => {
    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      false,
    );
    renderHook(() =>
      useAutoAcceptIndicator({
        config: mockConfigInstance as unknown as ActualConfigType,
      }),
    );

    act(() => {
      capturedUseInputHandler('', { tab: true, shift: false } as InkKey);
    });
    expect(
      mockConfigInstance.setAlwaysSkipModificationConfirmation,
    ).not.toHaveBeenCalled();

    act(() => {
      capturedUseInputHandler('', { tab: false, shift: true } as InkKey);
    });
    expect(
      mockConfigInstance.setAlwaysSkipModificationConfirmation,
    ).not.toHaveBeenCalled();

    act(() => {
      capturedUseInputHandler('a', { tab: false, shift: false } as InkKey);
    });
    expect(
      mockConfigInstance.setAlwaysSkipModificationConfirmation,
    ).not.toHaveBeenCalled();
  });

  it('should update indicator when config value changes externally (useEffect dependency)', () => {
    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      false,
    );
    const { result, rerender } = renderHook(
      (props: { config: ActualConfigType }) => useAutoAcceptIndicator(props),
      {
        initialProps: {
          config: mockConfigInstance as unknown as ActualConfigType,
        },
      },
    );
    expect(result.current).toBe(false);

    mockConfigInstance.getAlwaysSkipModificationConfirmation.mockReturnValue(
      true,
    );

    rerender({ config: mockConfigInstance as unknown as ActualConfigType });
    expect(result.current).toBe(true);
    expect(
      mockConfigInstance.getAlwaysSkipModificationConfirmation,
    ).toHaveBeenCalledTimes(3);
  });
});