|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 8 | +import { upgradeCommand } from './upgradeCommand.js'; |
| 9 | +import { type CommandContext } from './types.js'; |
| 10 | +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; |
| 11 | +import { |
| 12 | + AuthType, |
| 13 | + openBrowserSecurely, |
| 14 | + UPGRADE_URL_PAGE, |
| 15 | +} from '@google/gemini-cli-core'; |
| 16 | + |
| 17 | +vi.mock('@google/gemini-cli-core', async (importOriginal) => { |
| 18 | + const actual = |
| 19 | + await importOriginal<typeof import('@google/gemini-cli-core')>(); |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + openBrowserSecurely: vi.fn(), |
| 23 | + UPGRADE_URL_PAGE: 'https://goo.gle/set-up-gemini-code-assist', |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +describe('upgradeCommand', () => { |
| 28 | + let mockContext: CommandContext; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + mockContext = createMockCommandContext({ |
| 33 | + services: { |
| 34 | + config: { |
| 35 | + getContentGeneratorConfig: vi.fn().mockReturnValue({ |
| 36 | + authType: AuthType.LOGIN_WITH_GOOGLE, |
| 37 | + }), |
| 38 | + }, |
| 39 | + }, |
| 40 | + } as unknown as CommandContext); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should have the correct name and description', () => { |
| 44 | + expect(upgradeCommand.name).toBe('upgrade'); |
| 45 | + expect(upgradeCommand.description).toBe( |
| 46 | + 'Upgrade your Gemini Code Assist tier for higher limits', |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should call openBrowserSecurely with UPGRADE_URL_PAGE when logged in with Google', async () => { |
| 51 | + if (!upgradeCommand.action) { |
| 52 | + throw new Error('The upgrade command must have an action.'); |
| 53 | + } |
| 54 | + |
| 55 | + await upgradeCommand.action(mockContext, ''); |
| 56 | + |
| 57 | + expect(openBrowserSecurely).toHaveBeenCalledWith(UPGRADE_URL_PAGE); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return an error message when NOT logged in with Google', async () => { |
| 61 | + vi.mocked( |
| 62 | + mockContext.services.config!.getContentGeneratorConfig, |
| 63 | + ).mockReturnValue({ |
| 64 | + authType: AuthType.USE_GEMINI, |
| 65 | + }); |
| 66 | + |
| 67 | + if (!upgradeCommand.action) { |
| 68 | + throw new Error('The upgrade command must have an action.'); |
| 69 | + } |
| 70 | + |
| 71 | + const result = await upgradeCommand.action(mockContext, ''); |
| 72 | + |
| 73 | + expect(result).toEqual({ |
| 74 | + type: 'message', |
| 75 | + messageType: 'error', |
| 76 | + content: |
| 77 | + 'The /upgrade command is only available when logged in with Google.', |
| 78 | + }); |
| 79 | + expect(openBrowserSecurely).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return an error message if openBrowserSecurely fails', async () => { |
| 83 | + vi.mocked(openBrowserSecurely).mockRejectedValue( |
| 84 | + new Error('Failed to open'), |
| 85 | + ); |
| 86 | + |
| 87 | + if (!upgradeCommand.action) { |
| 88 | + throw new Error('The upgrade command must have an action.'); |
| 89 | + } |
| 90 | + |
| 91 | + const result = await upgradeCommand.action(mockContext, ''); |
| 92 | + |
| 93 | + expect(result).toEqual({ |
| 94 | + type: 'message', |
| 95 | + messageType: 'error', |
| 96 | + content: 'Failed to open upgrade page: Failed to open', |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments