From 21163a1636f7d7fbf9fca196ae68b34ed5b9a060 Mon Sep 17 00:00:00 2001 From: Sandy Tao Date: Fri, 17 Oct 2025 14:20:49 -0700 Subject: [PATCH] fix(cli): enable typechecking for ui/commands tests (#11413) --- .../cli/src/ui/commands/chatCommand.test.ts | 12 +++++---- .../src/ui/commands/extensionsCommand.test.ts | 3 +++ .../cli/src/ui/commands/initCommand.test.ts | 7 ++++-- .../cli/src/ui/commands/memoryCommand.test.ts | 2 +- .../ui/commands/terminalSetupCommand.test.ts | 8 +++--- .../cli/src/ui/commands/toolsCommand.test.ts | 14 +++++++---- packages/cli/tsconfig.json | 25 ------------------- 7 files changed, 29 insertions(+), 42 deletions(-) diff --git a/packages/cli/src/ui/commands/chatCommand.test.ts b/packages/cli/src/ui/commands/chatCommand.test.ts index cee807e5b5..0eceb42e42 100644 --- a/packages/cli/src/ui/commands/chatCommand.test.ts +++ b/packages/cli/src/ui/commands/chatCommand.test.ts @@ -99,8 +99,10 @@ describe('chatCommand', () => { const date1 = new Date(); const date2 = new Date(date1.getTime() + 1000); - mockFs.readdir.mockResolvedValue(fakeFiles); - mockFs.stat.mockImplementation(async (path: string): Promise => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + mockFs.readdir.mockResolvedValue(fakeFiles as any); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + mockFs.stat.mockImplementation(async (path: any): Promise => { if (path.endsWith('test1.json')) { return { mtime: date1 } as Stats; } @@ -500,7 +502,7 @@ Hi there!`; const expectedPath = path.join(process.cwd(), 'my-chat.json'); const [actualPath, actualContent] = mockFs.writeFile.mock.calls[0]; expect(actualPath).toEqual(expectedPath); - const parsedContent = JSON.parse(actualContent); + const parsedContent = JSON.parse(actualContent as string); expect(Array.isArray(parsedContent)).toBe(true); parsedContent.forEach((item: Content) => { expect(item).toHaveProperty('role'); @@ -515,9 +517,9 @@ Hi there!`; const expectedPath = path.join(process.cwd(), 'my-chat.md'); const [actualPath, actualContent] = mockFs.writeFile.mock.calls[0]; expect(actualPath).toEqual(expectedPath); - const entries = actualContent.split('\n\n---\n\n'); + const entries = (actualContent as string).split('\n\n---\n\n'); expect(entries.length).toBe(mockHistory.length); - entries.forEach((entry, index) => { + entries.forEach((entry: string, index: number) => { const { role, parts } = mockHistory[index]; const text = parts.map((p) => p.text).join(''); const roleIcon = role === 'user' ? '🧑‍💻' : '✨'; diff --git a/packages/cli/src/ui/commands/extensionsCommand.test.ts b/packages/cli/src/ui/commands/extensionsCommand.test.ts index ee41668a74..a15cc70c60 100644 --- a/packages/cli/src/ui/commands/extensionsCommand.test.ts +++ b/packages/cli/src/ui/commands/extensionsCommand.test.ts @@ -226,6 +226,7 @@ describe('extensionsCommand', () => { version: '1.0.0', isActive: true, path: '/test/dir/ext-one', + contextFiles: [], installMetadata: { type: 'git', autoUpdate: false, @@ -237,6 +238,7 @@ describe('extensionsCommand', () => { version: '1.0.0', isActive: true, path: '/test/dir/another-ext', + contextFiles: [], installMetadata: { type: 'git', autoUpdate: false, @@ -248,6 +250,7 @@ describe('extensionsCommand', () => { version: '1.0.0', isActive: true, path: '/test/dir/all-ext', + contextFiles: [], installMetadata: { type: 'git', autoUpdate: false, diff --git a/packages/cli/src/ui/commands/initCommand.test.ts b/packages/cli/src/ui/commands/initCommand.test.ts index 7e0254a677..c38fd4196f 100644 --- a/packages/cli/src/ui/commands/initCommand.test.ts +++ b/packages/cli/src/ui/commands/initCommand.test.ts @@ -9,7 +9,7 @@ import * as fs from 'node:fs'; import * as path from 'node:path'; import { initCommand } from './initCommand.js'; import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; -import { type CommandContext } from './types.js'; +import type { SubmitPromptActionReturn, CommandContext } from './types.js'; // Mock the 'fs' module vi.mock('fs', () => ({ @@ -61,7 +61,10 @@ describe('initCommand', () => { vi.mocked(fs.existsSync).mockReturnValue(false); // Act: Run the command's action - const result = await initCommand.action!(mockContext, ''); + const result = (await initCommand.action!( + mockContext, + '', + )) as SubmitPromptActionReturn; // Assert: Check that writeFileSync was called correctly expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8'); diff --git a/packages/cli/src/ui/commands/memoryCommand.test.ts b/packages/cli/src/ui/commands/memoryCommand.test.ts index 3cb8987357..2a4ea30364 100644 --- a/packages/cli/src/ui/commands/memoryCommand.test.ts +++ b/packages/cli/src/ui/commands/memoryCommand.test.ts @@ -198,7 +198,7 @@ describe('memoryCommand', () => { importFormat: 'tree', }, }, - } as LoadedSettings, + } as unknown as LoadedSettings, }, ui: { setGeminiMdFileCount: vi.fn(), diff --git a/packages/cli/src/ui/commands/terminalSetupCommand.test.ts b/packages/cli/src/ui/commands/terminalSetupCommand.test.ts index d0d3d6c6c5..3bee0d591c 100644 --- a/packages/cli/src/ui/commands/terminalSetupCommand.test.ts +++ b/packages/cli/src/ui/commands/terminalSetupCommand.test.ts @@ -28,7 +28,7 @@ describe('terminalSetupCommand', () => { message: 'Terminal configured successfully', }); - const result = await terminalSetupCommand.action({} as CommandContext, ''); + const result = await terminalSetupCommand.action!({} as CommandContext, ''); expect(result).toEqual({ type: 'message', @@ -44,7 +44,7 @@ describe('terminalSetupCommand', () => { requiresRestart: true, }); - const result = await terminalSetupCommand.action({} as CommandContext, ''); + const result = await terminalSetupCommand.action!({} as CommandContext, ''); expect(result).toEqual({ type: 'message', @@ -60,7 +60,7 @@ describe('terminalSetupCommand', () => { message: 'Failed to detect terminal', }); - const result = await terminalSetupCommand.action({} as CommandContext, ''); + const result = await terminalSetupCommand.action!({} as CommandContext, ''); expect(result).toEqual({ type: 'message', @@ -74,7 +74,7 @@ describe('terminalSetupCommand', () => { new Error('Unexpected error'), ); - const result = await terminalSetupCommand.action({} as CommandContext, ''); + const result = await terminalSetupCommand.action!({} as CommandContext, ''); expect(result).toEqual({ type: 'message', diff --git a/packages/cli/src/ui/commands/toolsCommand.test.ts b/packages/cli/src/ui/commands/toolsCommand.test.ts index 5f2ef4efbc..d44be3f973 100644 --- a/packages/cli/src/ui/commands/toolsCommand.test.ts +++ b/packages/cli/src/ui/commands/toolsCommand.test.ts @@ -9,7 +9,7 @@ import { describe, it, expect } from 'vitest'; import { toolsCommand } from './toolsCommand.js'; import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; import { MessageType } from '../types.js'; -import type { Tool } from '@google/gemini-cli-core'; +import type { ToolBuilder, ToolResult } from '@google/gemini-cli-core'; // Mock tools for testing const mockTools = [ @@ -25,7 +25,7 @@ const mockTools = [ description: 'Edits code files.', schema: {}, }, -] as Tool[]; +] as unknown as Array>; describe('toolsCommand', () => { it('should display an error if the tool registry is unavailable', async () => { @@ -53,7 +53,9 @@ describe('toolsCommand', () => { const mockContext = createMockCommandContext({ services: { config: { - getToolRegistry: () => ({ getAllTools: () => [] as Tool[] }), + getToolRegistry: () => ({ + getAllTools: () => [] as Array>, + }), }, }, }); @@ -83,7 +85,8 @@ describe('toolsCommand', () => { if (!toolsCommand.action) throw new Error('Action not defined'); await toolsCommand.action(mockContext, ''); - const [message] = (mockContext.ui.addItem as vi.Mock).mock.calls[0]; + const [message] = (mockContext.ui.addItem as ReturnType).mock + .calls[0]; expect(message.type).toBe(MessageType.TOOLS_LIST); expect(message.showDescriptions).toBe(false); expect(message.tools).toHaveLength(2); @@ -103,7 +106,8 @@ describe('toolsCommand', () => { if (!toolsCommand.action) throw new Error('Action not defined'); await toolsCommand.action(mockContext, 'desc'); - const [message] = (mockContext.ui.addItem as vi.Mock).mock.calls[0]; + const [message] = (mockContext.ui.addItem as ReturnType).mock + .calls[0]; expect(message.type).toBe(MessageType.TOOLS_LIST); expect(message.showDescriptions).toBe(true); expect(message.tools).toHaveLength(2); diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index e73b51ab8b..ebff48d3fa 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -28,31 +28,6 @@ "src/utils/handleAutoUpdate.test.ts", "src/utils/startupWarnings.test.ts", "src/ui/App.test.tsx", - "src/ui/commands/aboutCommand.test.ts", - "src/ui/commands/authCommand.test.ts", - "src/ui/commands/bugCommand.test.ts", - "src/ui/commands/clearCommand.test.ts", - "src/ui/commands/compressCommand.test.ts", - "src/ui/commands/copyCommand.test.ts", - "src/ui/commands/corgiCommand.test.ts", - "src/ui/commands/docsCommand.test.ts", - "src/ui/commands/editorCommand.test.ts", - "src/ui/commands/extensionsCommand.test.ts", - "src/ui/commands/helpCommand.test.ts", - "src/ui/commands/restoreCommand.test.ts", - "src/ui/commands/settingsCommand.test.ts", - "src/ui/commands/themeCommand.test.ts", - "src/ui/commands/chatCommand.test.ts", - "src/ui/commands/directoryCommand.test.tsx", - "src/ui/commands/ideCommand.test.ts", - "src/ui/commands/initCommand.test.ts", - "src/ui/commands/privacyCommand.test.ts", - "src/ui/commands/quitCommand.test.ts", - "src/ui/commands/mcpCommand.test.ts", - "src/ui/commands/memoryCommand.test.ts", - "src/ui/commands/statsCommand.test.ts", - "src/ui/commands/terminalSetupCommand.test.ts", - "src/ui/commands/toolsCommand.test.ts", "src/ui/components/ContextSummaryDisplay.test.tsx", "src/ui/components/Footer.test.tsx", "src/ui/components/InputPrompt.test.tsx",