summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/commands/initCommand.test.ts
diff options
context:
space:
mode:
authorshamso-goog <[email protected]>2025-07-29 12:49:01 -0400
committerGitHub <[email protected]>2025-07-29 16:49:01 +0000
commit80079cd2a5741d7024c8500853fe7a3af5e6ba0a (patch)
tree8955e78140bd0c5b8e93223fbcd5bd1978152169 /packages/cli/src/ui/commands/initCommand.test.ts
parent7356764a489b47bc43dae9e9653380cbe9bce294 (diff)
feat(cli): introduce /init command for GEMINI.md creation (#4852)
Co-authored-by: matt korwel <[email protected]>
Diffstat (limited to 'packages/cli/src/ui/commands/initCommand.test.ts')
-rw-r--r--packages/cli/src/ui/commands/initCommand.test.ts102
1 files changed, 102 insertions, 0 deletions
diff --git a/packages/cli/src/ui/commands/initCommand.test.ts b/packages/cli/src/ui/commands/initCommand.test.ts
new file mode 100644
index 00000000..83cea944
--- /dev/null
+++ b/packages/cli/src/ui/commands/initCommand.test.ts
@@ -0,0 +1,102 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
+import * as fs from 'fs';
+import * as path from 'path';
+import { initCommand } from './initCommand.js';
+import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
+import { type CommandContext } from './types.js';
+
+// Mock the 'fs' module
+vi.mock('fs', () => ({
+ existsSync: vi.fn(),
+ writeFileSync: vi.fn(),
+}));
+
+describe('initCommand', () => {
+ let mockContext: CommandContext;
+ const targetDir = '/test/dir';
+ const geminiMdPath = path.join(targetDir, 'GEMINI.md');
+
+ beforeEach(() => {
+ // Create a fresh mock context for each test
+ mockContext = createMockCommandContext({
+ services: {
+ config: {
+ getTargetDir: () => targetDir,
+ },
+ },
+ });
+ });
+
+ afterEach(() => {
+ // Clear all mocks after each test
+ vi.clearAllMocks();
+ });
+
+ it('should inform the user if GEMINI.md already exists', async () => {
+ // Arrange: Simulate that the file exists
+ vi.mocked(fs.existsSync).mockReturnValue(true);
+
+ // 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 GEMINI.md file already exists in this directory. No changes were made.',
+ });
+ // Assert: Ensure no file was written
+ expect(fs.writeFileSync).not.toHaveBeenCalled();
+ });
+
+ it('should create GEMINI.md and submit a prompt if it does not exist', async () => {
+ // Arrange: Simulate that the file does not exist
+ vi.mocked(fs.existsSync).mockReturnValue(false);
+
+ // 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 GEMINI.md created. Now analyzing the project to populate it.',
+ },
+ expect.any(Number),
+ );
+
+ // Assert: Check that the correct prompt is submitted
+ expect(result.type).toBe('submit_prompt');
+ expect(result.content).toContain(
+ 'You are an AI agent that brings the power of Gemini',
+ );
+ });
+
+ it('should return an error if config is not available', async () => {
+ // Arrange: Create a context without config
+ const noConfigContext = createMockCommandContext();
+ if (noConfigContext.services) {
+ noConfigContext.services.config = null;
+ }
+
+ // Act: Run the command's action
+ const result = await initCommand.action!(noConfigContext, '');
+
+ // Assert: Check for the correct error message
+ expect(result).toEqual({
+ type: 'message',
+ messageType: 'error',
+ content: 'Configuration not available.',
+ });
+ });
+});