File size: 9,894 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { handleCreditsFlow } from './creditsFlowHandler.js';
import type { UseHistoryManagerReturn } from './useHistoryManager.js';
import {
  type Config,
  type GeminiUserTier,
  makeFakeConfig,
  getG1CreditBalance,
  shouldAutoUseCredits,
  shouldShowOverageMenu,
  shouldShowEmptyWalletMenu,
  shouldLaunchBrowser,
  logBillingEvent,
  G1_CREDIT_TYPE,
  UserTierId,
} from '@google/gemini-cli-core';
import { MessageType } from '../types.js';

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

describe('handleCreditsFlow', () => {
  let mockConfig: Config;
  let mockHistoryManager: UseHistoryManagerReturn;
  let isDialogPending: React.MutableRefObject<boolean>;
  let mockSetOverageMenuRequest: ReturnType<typeof vi.fn>;
  let mockSetEmptyWalletRequest: ReturnType<typeof vi.fn>;
  let mockSetModelSwitchedFromQuotaError: ReturnType<typeof vi.fn>;
  const mockPaidTier: GeminiUserTier = {
    id: UserTierId.STANDARD,
    availableCredits: [{ creditType: G1_CREDIT_TYPE, creditAmount: '100' }],
  };

  beforeEach(() => {
    mockConfig = makeFakeConfig();
    mockHistoryManager = {
      addItem: vi.fn(),
      history: [],
      updateItem: vi.fn(),
      clearItems: vi.fn(),
      loadHistory: vi.fn(),
    };
    isDialogPending = { current: false };
    mockSetOverageMenuRequest = vi.fn();
    mockSetEmptyWalletRequest = vi.fn();
    mockSetModelSwitchedFromQuotaError = vi.fn();

    vi.spyOn(mockConfig, 'setQuotaErrorOccurred');
    vi.spyOn(mockConfig, 'setOverageStrategy');

    vi.mocked(getG1CreditBalance).mockReturnValue(100);
    vi.mocked(shouldAutoUseCredits).mockReturnValue(false);
    vi.mocked(shouldShowOverageMenu).mockReturnValue(false);
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(false);
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  function makeArgs(
    overrides?: Partial<Parameters<typeof handleCreditsFlow>[0]>,
  ) {
    return {
      config: mockConfig,
      paidTier: mockPaidTier,
      overageStrategy: 'ask' as const,
      failedModel: 'gemini-3-pro-preview',
      fallbackModel: 'gemini-3-flash-preview',
      usageLimitReachedModel: 'all Pro models',
      resetTime: '3:45 PM',
      historyManager: mockHistoryManager,
      setModelSwitchedFromQuotaError: mockSetModelSwitchedFromQuotaError,
      isDialogPending,
      setOverageMenuRequest: mockSetOverageMenuRequest,
      setEmptyWalletRequest: mockSetEmptyWalletRequest,
      ...overrides,
    };
  }

  it('should return null if credit balance is null (non-G1 user)', async () => {
    vi.mocked(getG1CreditBalance).mockReturnValue(null);
    const result = await handleCreditsFlow(makeArgs());
    expect(result).toBeNull();
  });

  it('should return null if credits are already auto-used (strategy=always)', async () => {
    vi.mocked(shouldAutoUseCredits).mockReturnValue(true);
    const result = await handleCreditsFlow(makeArgs());
    expect(result).toBeNull();
  });

  it('should show overage menu and return retry_with_credits when use_credits selected', async () => {
    vi.mocked(shouldShowOverageMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());

    // Extract the resolve callback from the setOverageMenuRequest call
    expect(mockSetOverageMenuRequest).toHaveBeenCalledOnce();
    const request = mockSetOverageMenuRequest.mock.calls[0][0];
    expect(request.failedModel).toBe('all Pro models');
    expect(request.creditBalance).toBe(100);

    // Simulate user choosing 'use_credits'
    request.resolve('use_credits');
    const result = await flowPromise;

    expect(result).toBe('retry_with_credits');
    expect(mockConfig.setOverageStrategy).toHaveBeenCalledWith('always');
    expect(logBillingEvent).toHaveBeenCalled();
  });

  it('should show overage menu and return retry_always when use_fallback selected', async () => {
    vi.mocked(shouldShowOverageMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());
    const request = mockSetOverageMenuRequest.mock.calls[0][0];
    request.resolve('use_fallback');
    const result = await flowPromise;

    expect(result).toBe('retry_always');
  });

  it('should show overage menu and return stop when stop selected', async () => {
    vi.mocked(shouldShowOverageMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());
    const request = mockSetOverageMenuRequest.mock.calls[0][0];
    request.resolve('stop');
    const result = await flowPromise;

    expect(result).toBe('stop');
  });

  it('should return stop immediately if dialog is already pending (overage)', async () => {
    vi.mocked(shouldShowOverageMenu).mockReturnValue(true);
    isDialogPending.current = true;

    const result = await handleCreditsFlow(makeArgs());
    expect(result).toBe('stop');
    expect(mockSetOverageMenuRequest).not.toHaveBeenCalled();
  });

  it('should show empty wallet menu and return stop when get_credits selected', async () => {
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());

    expect(mockSetEmptyWalletRequest).toHaveBeenCalledOnce();
    const request = mockSetEmptyWalletRequest.mock.calls[0][0];
    expect(request.failedModel).toBe('all Pro models');

    request.resolve('get_credits');
    const result = await flowPromise;

    expect(result).toBe('stop');
    expect(mockHistoryManager.addItem).toHaveBeenCalledWith(
      expect.objectContaining({
        type: MessageType.INFO,
        text: expect.stringContaining('few minutes'),
      }),
      expect.any(Number),
    );
  });

  it('should show empty wallet menu and return retry_always when use_fallback selected', async () => {
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());
    const request = mockSetEmptyWalletRequest.mock.calls[0][0];
    request.resolve('use_fallback');
    const result = await flowPromise;

    expect(result).toBe('retry_always');
  });

  it('should return stop immediately if dialog is already pending (empty wallet)', async () => {
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(true);
    isDialogPending.current = true;

    const result = await handleCreditsFlow(makeArgs());
    expect(result).toBe('stop');
    expect(mockSetEmptyWalletRequest).not.toHaveBeenCalled();
  });

  it('should return null if no flow conditions are met', async () => {
    vi.mocked(getG1CreditBalance).mockReturnValue(100);
    vi.mocked(shouldAutoUseCredits).mockReturnValue(false);
    vi.mocked(shouldShowOverageMenu).mockReturnValue(false);
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(false);

    const result = await handleCreditsFlow(makeArgs());
    expect(result).toBeNull();
  });

  it('should clear dialog state after overage menu resolves', async () => {
    vi.mocked(shouldShowOverageMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());
    expect(isDialogPending.current).toBe(true);

    const request = mockSetOverageMenuRequest.mock.calls[0][0];
    request.resolve('stop');
    await flowPromise;

    expect(isDialogPending.current).toBe(false);
    // Verify null was set to clear the request
    expect(mockSetOverageMenuRequest).toHaveBeenCalledWith(null);
  });

  it('should clear dialog state after empty wallet menu resolves', async () => {
    vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(true);

    const flowPromise = handleCreditsFlow(makeArgs());
    expect(isDialogPending.current).toBe(true);

    const request = mockSetEmptyWalletRequest.mock.calls[0][0];
    request.resolve('stop');
    await flowPromise;

    expect(isDialogPending.current).toBe(false);
    expect(mockSetEmptyWalletRequest).toHaveBeenCalledWith(null);
  });

  describe('headless mode (shouldLaunchBrowser=false)', () => {
    beforeEach(() => {
      vi.mocked(shouldLaunchBrowser).mockReturnValue(false);
    });

    it('should show manage URL in history when manage selected in headless mode', async () => {
      vi.mocked(shouldShowOverageMenu).mockReturnValue(true);

      const flowPromise = handleCreditsFlow(makeArgs());
      const request = mockSetOverageMenuRequest.mock.calls[0][0];
      request.resolve('manage');
      const result = await flowPromise;

      expect(result).toBe('stop');
      expect(mockHistoryManager.addItem).toHaveBeenCalledWith(
        expect.objectContaining({
          type: MessageType.INFO,
          text: expect.stringContaining('Please open this URL in a browser:'),
        }),
        expect.any(Number),
      );
    });

    it('should show credits URL in history when get_credits selected in headless mode', async () => {
      vi.mocked(shouldShowEmptyWalletMenu).mockReturnValue(true);

      const flowPromise = handleCreditsFlow(makeArgs());
      const request = mockSetEmptyWalletRequest.mock.calls[0][0];

      // Trigger onGetCredits callback and wait for it
      await request.onGetCredits();

      expect(mockHistoryManager.addItem).toHaveBeenCalledWith(
        expect.objectContaining({
          type: MessageType.INFO,
          text: expect.stringContaining('Please open this URL in a browser:'),
        }),
        expect.any(Number),
      );

      request.resolve('get_credits');
      await flowPromise;
    });
  });
});