diff options
Diffstat (limited to 'packages/cli/src/ui/commands')
| -rw-r--r-- | packages/cli/src/ui/commands/bugCommand.test.ts | 98 | ||||
| -rw-r--r-- | packages/cli/src/ui/commands/bugCommand.ts | 78 |
2 files changed, 176 insertions, 0 deletions
diff --git a/packages/cli/src/ui/commands/bugCommand.test.ts b/packages/cli/src/ui/commands/bugCommand.test.ts new file mode 100644 index 00000000..1a618fd1 --- /dev/null +++ b/packages/cli/src/ui/commands/bugCommand.test.ts @@ -0,0 +1,98 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import open from 'open'; +import { bugCommand } from './bugCommand.js'; +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; +import { getCliVersion } from '../../utils/version.js'; +import { GIT_COMMIT_INFO } from '../../generated/git-commit.js'; +import { formatMemoryUsage } from '../utils/formatters.js'; + +// Mock dependencies +vi.mock('open'); +vi.mock('../../utils/version.js'); +vi.mock('../utils/formatters.js'); +vi.mock('node:process', () => ({ + default: { + platform: 'test-platform', + version: 'v20.0.0', + // Keep other necessary process properties if needed by other parts of the code + env: process.env, + memoryUsage: () => ({ rss: 0 }), + }, +})); + +describe('bugCommand', () => { + beforeEach(() => { + vi.mocked(getCliVersion).mockResolvedValue('0.1.0'); + vi.mocked(formatMemoryUsage).mockReturnValue('100 MB'); + vi.stubEnv('SANDBOX', 'gemini-test'); + }); + + afterEach(() => { + vi.unstubAllEnvs(); + vi.clearAllMocks(); + }); + + it('should generate the default GitHub issue URL', async () => { + const mockContext = createMockCommandContext({ + services: { + config: { + getModel: () => 'gemini-pro', + getBugCommand: () => undefined, + }, + }, + }); + + if (!bugCommand.action) throw new Error('Action is not defined'); + await bugCommand.action(mockContext, 'A test bug'); + + const expectedInfo = ` +* **CLI Version:** 0.1.0 +* **Git Commit:** ${GIT_COMMIT_INFO} +* **Operating System:** test-platform v20.0.0 +* **Sandbox Environment:** test +* **Model Version:** gemini-pro +* **Memory Usage:** 100 MB +`; + const expectedUrl = + 'https://github.com/google-gemini/gemini-cli/issues/new?template=bug_report.yml&title=A%20test%20bug&info=' + + encodeURIComponent(expectedInfo); + + expect(open).toHaveBeenCalledWith(expectedUrl); + }); + + it('should use a custom URL template from config if provided', async () => { + const customTemplate = + 'https://internal.bug-tracker.com/new?desc={title}&details={info}'; + const mockContext = createMockCommandContext({ + services: { + config: { + getModel: () => 'gemini-pro', + getBugCommand: () => ({ urlTemplate: customTemplate }), + }, + }, + }); + + if (!bugCommand.action) throw new Error('Action is not defined'); + await bugCommand.action(mockContext, 'A custom bug'); + + const expectedInfo = ` +* **CLI Version:** 0.1.0 +* **Git Commit:** ${GIT_COMMIT_INFO} +* **Operating System:** test-platform v20.0.0 +* **Sandbox Environment:** test +* **Model Version:** gemini-pro +* **Memory Usage:** 100 MB +`; + const expectedUrl = customTemplate + .replace('{title}', encodeURIComponent('A custom bug')) + .replace('{info}', encodeURIComponent(expectedInfo)); + + expect(open).toHaveBeenCalledWith(expectedUrl); + }); +}); diff --git a/packages/cli/src/ui/commands/bugCommand.ts b/packages/cli/src/ui/commands/bugCommand.ts new file mode 100644 index 00000000..c1b99db9 --- /dev/null +++ b/packages/cli/src/ui/commands/bugCommand.ts @@ -0,0 +1,78 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import open from 'open'; +import process from 'node:process'; +import { type CommandContext, type SlashCommand } from './types.js'; +import { MessageType } from '../types.js'; +import { GIT_COMMIT_INFO } from '../../generated/git-commit.js'; +import { formatMemoryUsage } from '../utils/formatters.js'; +import { getCliVersion } from '../../utils/version.js'; + +export const bugCommand: SlashCommand = { + name: 'bug', + description: 'submit a bug report', + action: async (context: CommandContext, args?: string): Promise<void> => { + const bugDescription = (args || '').trim(); + const { config } = context.services; + + const osVersion = `${process.platform} ${process.version}`; + let sandboxEnv = 'no sandbox'; + if (process.env.SANDBOX && process.env.SANDBOX !== 'sandbox-exec') { + sandboxEnv = process.env.SANDBOX.replace(/^gemini-(?:code-)?/, ''); + } else if (process.env.SANDBOX === 'sandbox-exec') { + sandboxEnv = `sandbox-exec (${ + process.env.SEATBELT_PROFILE || 'unknown' + })`; + } + const modelVersion = config?.getModel() || 'Unknown'; + const cliVersion = await getCliVersion(); + const memoryUsage = formatMemoryUsage(process.memoryUsage().rss); + + const info = ` +* **CLI Version:** ${cliVersion} +* **Git Commit:** ${GIT_COMMIT_INFO} +* **Operating System:** ${osVersion} +* **Sandbox Environment:** ${sandboxEnv} +* **Model Version:** ${modelVersion} +* **Memory Usage:** ${memoryUsage} +`; + + let bugReportUrl = + 'https://github.com/google-gemini/gemini-cli/issues/new?template=bug_report.yml&title={title}&info={info}'; + + const bugCommandSettings = config?.getBugCommand(); + if (bugCommandSettings?.urlTemplate) { + bugReportUrl = bugCommandSettings.urlTemplate; + } + + bugReportUrl = bugReportUrl + .replace('{title}', encodeURIComponent(bugDescription)) + .replace('{info}', encodeURIComponent(info)); + + context.ui.addItem( + { + type: MessageType.INFO, + text: `To submit your bug report, please open the following URL in your browser:\n${bugReportUrl}`, + }, + Date.now(), + ); + + try { + await open(bugReportUrl); + } catch (error) { + const errorMessage = + error instanceof Error ? error.message : String(error); + context.ui.addItem( + { + type: MessageType.ERROR, + text: `Could not open URL in browser: ${errorMessage}`, + }, + Date.now(), + ); + } + }, +}; |
