From 7868ef82299ae1da5a09334f67d57eb3b472563a Mon Sep 17 00:00:00 2001 From: Abhi <43648792+abhipatel12@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:01:02 -0400 Subject: feat: Introduce session context and add session duration stat for `/stats` command (#854) --- .../cli/src/ui/hooks/slashCommandProcessor.test.ts | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'packages/cli/src/ui/hooks/slashCommandProcessor.test.ts') diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts index 221893a2..3fcdff97 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts @@ -61,10 +61,15 @@ import { MCPServerStatus, getMCPServerStatus, } from '@gemini-cli/core'; +import { useSession } from '../contexts/SessionContext.js'; import * as ShowMemoryCommandModule from './useShowMemoryCommand.js'; import { GIT_COMMIT_INFO } from '../../generated/git-commit.js'; +vi.mock('../contexts/SessionContext.js', () => ({ + useSession: vi.fn(), +})); + vi.mock('./useShowMemoryCommand.js', () => ({ SHOW_MEMORY_COMMAND_NAME: '/memory show', createShowMemoryAction: vi.fn(() => vi.fn()), @@ -84,6 +89,7 @@ describe('useSlashCommandProcessor', () => { let mockPerformMemoryRefresh: ReturnType; let mockConfig: Config; let mockCorgiMode: ReturnType; + const mockUseSession = useSession as Mock; beforeEach(() => { mockAddItem = vi.fn(); @@ -99,6 +105,9 @@ describe('useSlashCommandProcessor', () => { getModel: vi.fn(() => 'test-model'), } as unknown as Config; mockCorgiMode = vi.fn(); + mockUseSession.mockReturnValue({ + startTime: new Date('2025-01-01T00:00:00.000Z'), + }); (open as Mock).mockClear(); mockProcessExit.mockClear(); @@ -230,6 +239,34 @@ describe('useSlashCommandProcessor', () => { }); }); + describe('/stats command', () => { + it('should show the session duration', async () => { + const { handleSlashCommand } = getProcessor(); + let commandResult: SlashCommandActionReturn | boolean = false; + + // Mock current time + const mockDate = new Date('2025-01-01T00:01:05.000Z'); + vi.setSystemTime(mockDate); + + await act(async () => { + commandResult = handleSlashCommand('/stats'); + }); + + expect(mockAddItem).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + type: MessageType.INFO, + text: 'Session duration: 1m 5s', + }), + expect.any(Number), + ); + expect(commandResult).toBe(true); + + // Restore system time + vi.useRealTimers(); + }); + }); + describe('Other commands', () => { it('/help should open help and return true', async () => { const { handleSlashCommand } = getProcessor(); -- cgit v1.2.3