diff --git a/packages/cli/src/ui/commands/initCommand.test.ts b/packages/cli/src/ui/commands/initCommand.test.ts index 1130c5b3..3ed32d8e 100644 --- a/packages/cli/src/ui/commands/initCommand.test.ts +++ b/packages/cli/src/ui/commands/initCommand.test.ts @@ -53,7 +53,7 @@ describe('initCommand', () => { vi.clearAllMocks(); }); - it(`should inform the user if ${DEFAULT_CONTEXT_FILENAME} already exists and is non-empty`, async () => { + it(`should ask for confirmation if ${DEFAULT_CONTEXT_FILENAME} already exists and is non-empty`, async () => { // Arrange: Simulate that the file exists vi.mocked(fs.existsSync).mockReturnValue(true); vi.spyOn(fs, 'readFileSync').mockReturnValue('# Existing content'); @@ -61,13 +61,15 @@ describe('initCommand', () => { // Act: Run the command's action const result = await initCommand.action!(mockContext, ''); - // Assert: Check for the correct informational message - expect(result).toEqual({ - type: 'message', - messageType: 'info', - content: `A ${DEFAULT_CONTEXT_FILENAME} file already exists in this directory. No changes were made.`, - }); - // Assert: Ensure no file was written + // Assert: Check for the correct confirmation request + expect(result).toEqual( + expect.objectContaining({ + type: 'confirm_action', + prompt: expect.anything(), // React element, not a string + originalInvocation: expect.anything(), + }), + ); + // Assert: Ensure no file was written yet expect(fs.writeFileSync).not.toHaveBeenCalled(); }); @@ -91,9 +93,13 @@ describe('initCommand', () => { ); // Assert: Check that the correct prompt is submitted - expect(result.type).toBe('submit_prompt'); - expect(result.content).toContain( - 'You are Qwen Code, an interactive CLI agent', + expect(result).toEqual( + expect.objectContaining({ + type: 'submit_prompt', + content: expect.stringContaining( + 'You are Qwen Code, an interactive CLI agent', + ), + }), ); }); @@ -104,7 +110,43 @@ describe('initCommand', () => { const result = await initCommand.action!(mockContext, ''); expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8'); - expect(result.type).toBe('submit_prompt'); + expect(result).toEqual( + expect.objectContaining({ + type: 'submit_prompt', + }), + ); + }); + + it(`should regenerate ${DEFAULT_CONTEXT_FILENAME} when overwrite is confirmed`, async () => { + // Arrange: Simulate that the file exists and overwrite is confirmed + vi.mocked(fs.existsSync).mockReturnValue(true); + vi.spyOn(fs, 'readFileSync').mockReturnValue('# Existing content'); + mockContext.overwriteConfirmed = true; + + // Act: Run the command's action + const result = await initCommand.action!(mockContext, ''); + + // Assert: Check that writeFileSync was called correctly + expect(fs.writeFileSync).toHaveBeenCalledWith(geminiMdPath, '', 'utf8'); + + // Assert: Check that an informational message was added to the UI + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: 'info', + text: `Empty ${DEFAULT_CONTEXT_FILENAME} created. Now analyzing the project to populate it.`, + }, + expect.any(Number), + ); + + // Assert: Check that the correct prompt is submitted + expect(result).toEqual( + expect.objectContaining({ + type: 'submit_prompt', + content: expect.stringContaining( + 'You are Qwen Code, an interactive CLI agent', + ), + }), + ); }); it('should return an error if config is not available', async () => { diff --git a/packages/cli/src/ui/commands/initCommand.ts b/packages/cli/src/ui/commands/initCommand.ts index 3d90db3c..efec93f1 100644 --- a/packages/cli/src/ui/commands/initCommand.ts +++ b/packages/cli/src/ui/commands/initCommand.ts @@ -1,6 +1,6 @@ /** * @license - * Copyright 2025 Google LLC + * Copyright 2025 Qwen * SPDX-License-Identifier: Apache-2.0 */ @@ -13,6 +13,8 @@ import { CommandKind, } from './types.js'; import { getCurrentGeminiMdFilename } from '@qwen-code/qwen-code-core'; +import { Text } from 'ink'; +import React from 'react'; export const initCommand: SlashCommand = { name: 'init', @@ -35,15 +37,27 @@ export const initCommand: SlashCommand = { try { if (fs.existsSync(contextFilePath)) { - // If file exists but is empty (or whitespace), continue to initialize; otherwise, bail out + // If file exists but is empty (or whitespace), continue to initialize try { const existing = fs.readFileSync(contextFilePath, 'utf8'); if (existing && existing.trim().length > 0) { - return { - type: 'message', - messageType: 'info', - content: `A ${contextFileName} file already exists in this directory. No changes were made.`, - }; + // File exists and has content - ask for confirmation to overwrite + if (!context.overwriteConfirmed) { + return { + type: 'confirm_action', + // TODO: Move to .tsx file to use JSX syntax instead of React.createElement + // For now, using React.createElement to maintain .ts compatibility for PR review + prompt: React.createElement( + Text, + null, + `A ${contextFileName} file already exists in this directory. Do you want to regenerate it?`, + ), + originalInvocation: { + raw: context.invocation?.raw || '/init', + }, + }; + } + // User confirmed overwrite, continue with regeneration } } catch { // If we fail to read, conservatively proceed to (re)create the file