File size: 7,503 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 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
import { renderWithProviders } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js';
import { BannedAccountDialog } from './BannedAccountDialog.js';
import { RadioButtonSelect } from '../components/shared/RadioButtonSelect.js';
import { useKeypress } from '../hooks/useKeypress.js';
import {
  openBrowserSecurely,
  shouldLaunchBrowser,
} from '@google/gemini-cli-core';
import { Text } from 'ink';
import { runExitCleanup } from '../../utils/cleanup.js';
import type { AccountSuspensionInfo } from '../contexts/UIStateContext.js';

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

vi.mock('../../utils/cleanup.js', () => ({
  runExitCleanup: vi.fn().mockResolvedValue(undefined),
}));

vi.mock('../hooks/useKeypress.js', () => ({
  useKeypress: vi.fn(),
}));

vi.mock('../components/shared/RadioButtonSelect.js', () => ({
  RadioButtonSelect: vi.fn(({ items }) => (
    <>
      {items.map((item: { value: string; label: string }) => (
        <Text key={item.value}>{item.label}</Text>
      ))}
    </>
  )),
}));

const mockedRadioButtonSelect = RadioButtonSelect as Mock;
const mockedUseKeypress = useKeypress as Mock;
const mockedOpenBrowser = openBrowserSecurely as Mock;
const mockedShouldLaunchBrowser = shouldLaunchBrowser as Mock;
const mockedRunExitCleanup = runExitCleanup as Mock;

const DEFAULT_SUSPENSION_INFO: AccountSuspensionInfo = {
  message:
    'This service has been disabled in this account for violation of Terms of Service. Please submit an appeal to continue using this product.',
  appealUrl: 'https://example.com/appeal',
  appealLinkText: 'Appeal Here',
};

describe('BannedAccountDialog', () => {
  let onExit: Mock;
  let onChangeAuth: Mock;

  beforeEach(() => {
    vi.resetAllMocks();
    mockedShouldLaunchBrowser.mockReturnValue(true);
    mockedOpenBrowser.mockResolvedValue(undefined);
    mockedRunExitCleanup.mockResolvedValue(undefined);
    onExit = vi.fn();
    onChangeAuth = vi.fn();
  });

  it('renders the suspension message from accountSuspensionInfo', async () => {
    const { lastFrame, unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const frame = lastFrame();
    expect(frame).toContain('Account Suspended');
    expect(frame).toContain('violation of Terms of Service');
    expect(frame).toContain('Escape to exit');
    unmount();
  });

  it('renders menu options with appeal link text from response', async () => {
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const items = mockedRadioButtonSelect.mock.calls[0][0].items;
    expect(items).toHaveLength(3);
    expect(items[0].label).toBe('Appeal Here');
    expect(items[1].label).toBe('Change authentication');
    expect(items[2].label).toBe('Exit');
    unmount();
  });

  it('hides form option when no appealUrl is provided', async () => {
    const infoWithoutUrl: AccountSuspensionInfo = {
      message: 'Account suspended.',
    };
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={infoWithoutUrl}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const items = mockedRadioButtonSelect.mock.calls[0][0].items;
    expect(items).toHaveLength(2);
    expect(items[0].label).toBe('Change authentication');
    expect(items[1].label).toBe('Exit');
    unmount();
  });

  it('uses default label when appealLinkText is not provided', async () => {
    const infoWithoutLinkText: AccountSuspensionInfo = {
      message: 'Account suspended.',
      appealUrl: 'https://example.com/appeal',
    };
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={infoWithoutLinkText}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const items = mockedRadioButtonSelect.mock.calls[0][0].items;
    expect(items[0].label).toBe('Open the Google Form');
    unmount();
  });

  it('opens browser when appeal option is selected', async () => {
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const { onSelect } = mockedRadioButtonSelect.mock.calls[0][0];
    await onSelect('open_form');
    expect(mockedOpenBrowser).toHaveBeenCalledWith(
      'https://example.com/appeal',
    );
    expect(onExit).not.toHaveBeenCalled();
    unmount();
  });

  it('shows URL when browser cannot be launched', async () => {
    mockedShouldLaunchBrowser.mockReturnValue(false);
    const { lastFrame, unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const { onSelect } = mockedRadioButtonSelect.mock.calls[0][0];
    onSelect('open_form');
    await waitFor(() => {
      expect(lastFrame()).toContain('Please open this URL in a browser');
    });
    expect(mockedOpenBrowser).not.toHaveBeenCalled();
    unmount();
  });

  it('calls onExit when "Exit" is selected', async () => {
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const { onSelect } = mockedRadioButtonSelect.mock.calls[0][0];
    await onSelect('exit');
    expect(mockedRunExitCleanup).toHaveBeenCalled();
    expect(onExit).toHaveBeenCalled();
    unmount();
  });

  it('calls onChangeAuth when "Change authentication" is selected', async () => {
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const { onSelect } = mockedRadioButtonSelect.mock.calls[0][0];
    onSelect('change_auth');
    expect(onChangeAuth).toHaveBeenCalled();
    expect(onExit).not.toHaveBeenCalled();
    unmount();
  });

  it('exits on escape key', async () => {
    const { unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    const keypressHandler = mockedUseKeypress.mock.calls[0][0];
    const result = keypressHandler({ name: 'escape' });
    expect(result).toBe(true);
    unmount();
  });

  it('renders snapshot correctly', async () => {
    const { lastFrame, unmount } = await renderWithProviders(
      <BannedAccountDialog
        accountSuspensionInfo={DEFAULT_SUSPENSION_INFO}
        onExit={onExit}
        onChangeAuth={onChangeAuth}
      />,
    );
    expect(lastFrame()).toMatchSnapshot();
    unmount();
  });
});