diff options
| author | Harold Mciver <[email protected]> | 2025-07-16 22:40:56 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-17 02:40:56 +0000 |
| commit | 9ab44ea9d675cd9d560e22fba50d057f1764f25c (patch) | |
| tree | 492c12bd893fe0249545a2b4b16a86b11ca86fd1 /packages/cli/src/ui/commands | |
| parent | 01e66bb12392c3e8cd0222dc495c8dc61ebe4fba (diff) | |
updated `/quit` to use new slash command arch (#4259)
Co-authored-by: Abhi <[email protected]>
Diffstat (limited to 'packages/cli/src/ui/commands')
| -rw-r--r-- | packages/cli/src/ui/commands/quitCommand.test.ts | 55 | ||||
| -rw-r--r-- | packages/cli/src/ui/commands/quitCommand.ts | 35 | ||||
| -rw-r--r-- | packages/cli/src/ui/commands/types.ts | 8 |
3 files changed, 98 insertions, 0 deletions
diff --git a/packages/cli/src/ui/commands/quitCommand.test.ts b/packages/cli/src/ui/commands/quitCommand.test.ts new file mode 100644 index 00000000..e67723fd --- /dev/null +++ b/packages/cli/src/ui/commands/quitCommand.test.ts @@ -0,0 +1,55 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { quitCommand } from './quitCommand.js'; +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; +import { formatDuration } from '../utils/formatters.js'; + +vi.mock('../utils/formatters.js'); + +describe('quitCommand', () => { + beforeEach(() => { + vi.useFakeTimers(); + vi.setSystemTime(new Date('2025-01-01T01:00:00Z')); + vi.mocked(formatDuration).mockReturnValue('1h 0m 0s'); + }); + + afterEach(() => { + vi.useRealTimers(); + vi.clearAllMocks(); + }); + + it('returns a QuitActionReturn object with the correct messages', () => { + const mockContext = createMockCommandContext({ + session: { + stats: { + sessionStartTime: new Date('2025-01-01T00:00:00Z'), + }, + }, + }); + + if (!quitCommand.action) throw new Error('Action is not defined'); + const result = quitCommand.action(mockContext, 'quit'); + + expect(formatDuration).toHaveBeenCalledWith(3600000); // 1 hour in ms + expect(result).toEqual({ + type: 'quit', + messages: [ + { + type: 'user', + text: '/quit', + id: expect.any(Number), + }, + { + type: 'quit', + duration: '1h 0m 0s', + id: expect.any(Number), + }, + ], + }); + }); +}); diff --git a/packages/cli/src/ui/commands/quitCommand.ts b/packages/cli/src/ui/commands/quitCommand.ts new file mode 100644 index 00000000..48daf8c2 --- /dev/null +++ b/packages/cli/src/ui/commands/quitCommand.ts @@ -0,0 +1,35 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { formatDuration } from '../utils/formatters.js'; +import { type SlashCommand } from './types.js'; + +export const quitCommand: SlashCommand = { + name: 'quit', + altName: 'exit', + description: 'exit the cli', + action: (context) => { + const now = Date.now(); + const { sessionStartTime } = context.session.stats; + const wallDuration = now - sessionStartTime.getTime(); + + return { + type: 'quit', + messages: [ + { + type: 'user', + text: `/quit`, // Keep it consistent, even if /exit was used + id: now - 1, + }, + { + type: 'quit', + duration: formatDuration(wallDuration), + id: now, + }, + ], + }; + }, +}; diff --git a/packages/cli/src/ui/commands/types.ts b/packages/cli/src/ui/commands/types.ts index a61a29f2..d3d5ee8a 100644 --- a/packages/cli/src/ui/commands/types.ts +++ b/packages/cli/src/ui/commands/types.ts @@ -9,6 +9,7 @@ import { HistoryItemWithoutId } from '../types.js'; import { Config, GitService, Logger } from '@google/gemini-cli-core'; import { LoadedSettings } from '../../config/settings.js'; import { UseHistoryManagerReturn } from '../hooks/useHistoryManager.js'; +import type { HistoryItem } from '../types.js'; import { SessionStatsState } from '../contexts/SessionContext.js'; // Grouped dependencies for clarity and easier mocking @@ -56,6 +57,12 @@ export interface ToolActionReturn { toolArgs: Record<string, unknown>; } +/** The return type for a command action that results in the app quitting. */ +export interface QuitActionReturn { + type: 'quit'; + messages: HistoryItem[]; +} + /** * The return type for a command action that results in a simple message * being displayed to the user. @@ -87,6 +94,7 @@ export interface LoadHistoryActionReturn { export type SlashCommandActionReturn = | ToolActionReturn | MessageActionReturn + | QuitActionReturn | OpenDialogActionReturn | LoadHistoryActionReturn; // The standardized contract for any command in the system. |
