diff options
| author | Harold Mciver <[email protected]> | 2025-07-16 11:56:05 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-16 15:56:05 +0000 |
| commit | ddcac4201d13bfa948c900f10f8c6cd227099c7b (patch) | |
| tree | d76faf860c45e5685cca281713407c210a7d0ec1 /packages/cli/src/ui/commands/docsCommand.test.ts | |
| parent | f4cd0055fd7fcb86bc7dddfd72aa8c6ed78a923e (diff) | |
update `/docs` to new slash command arch (#4133)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Diffstat (limited to 'packages/cli/src/ui/commands/docsCommand.test.ts')
| -rw-r--r-- | packages/cli/src/ui/commands/docsCommand.test.ts | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/packages/cli/src/ui/commands/docsCommand.test.ts b/packages/cli/src/ui/commands/docsCommand.test.ts new file mode 100644 index 00000000..73b7396a --- /dev/null +++ b/packages/cli/src/ui/commands/docsCommand.test.ts @@ -0,0 +1,99 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; +import open from 'open'; +import { docsCommand } from './docsCommand.js'; +import { type CommandContext } from './types.js'; +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; +import { MessageType } from '../types.js'; + +// Mock the 'open' library +vi.mock('open', () => ({ + default: vi.fn(), +})); + +describe('docsCommand', () => { + let mockContext: CommandContext; + beforeEach(() => { + // Create a fresh mock context before each test + mockContext = createMockCommandContext(); + // Reset the `open` mock + vi.mocked(open).mockClear(); + }); + + afterEach(() => { + // Restore any stubbed environment variables + vi.unstubAllEnvs(); + }); + + it("should add an info message and call 'open' in a non-sandbox environment", async () => { + if (!docsCommand.action) { + throw new Error('docsCommand must have an action.'); + } + + const docsUrl = 'https://goo.gle/gemini-cli-docs'; + + await docsCommand.action(mockContext, ''); + + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: MessageType.INFO, + text: `Opening documentation in your browser: ${docsUrl}`, + }, + expect.any(Number), + ); + + expect(open).toHaveBeenCalledWith(docsUrl); + }); + + it('should only add an info message in a sandbox environment', async () => { + if (!docsCommand.action) { + throw new Error('docsCommand must have an action.'); + } + + // Simulate a sandbox environment + process.env.SANDBOX = 'gemini-sandbox'; + const docsUrl = 'https://goo.gle/gemini-cli-docs'; + + await docsCommand.action(mockContext, ''); + + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: MessageType.INFO, + text: `Please open the following URL in your browser to view the documentation:\n${docsUrl}`, + }, + expect.any(Number), + ); + + // Ensure 'open' was not called in the sandbox + expect(open).not.toHaveBeenCalled(); + }); + + it("should not open browser for 'sandbox-exec'", async () => { + if (!docsCommand.action) { + throw new Error('docsCommand must have an action.'); + } + + // Simulate the specific 'sandbox-exec' environment + process.env.SANDBOX = 'sandbox-exec'; + const docsUrl = 'https://goo.gle/gemini-cli-docs'; + + await docsCommand.action(mockContext, ''); + + // The logic should fall through to the 'else' block + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: MessageType.INFO, + text: `Opening documentation in your browser: ${docsUrl}`, + }, + expect.any(Number), + ); + + // 'open' should be called in this specific sandbox case + expect(open).toHaveBeenCalledWith(docsUrl); + }); +}); |
