updated `/quit` to use new slash command arch (#4259)
Co-authored-by: Abhi <abhipatel@google.com>
This commit is contained in:
parent
01e66bb123
commit
9ab44ea9d6
|
@ -25,6 +25,7 @@ import { compressCommand } from '../ui/commands/compressCommand.js';
|
||||||
import { mcpCommand } from '../ui/commands/mcpCommand.js';
|
import { mcpCommand } from '../ui/commands/mcpCommand.js';
|
||||||
import { editorCommand } from '../ui/commands/editorCommand.js';
|
import { editorCommand } from '../ui/commands/editorCommand.js';
|
||||||
import { bugCommand } from '../ui/commands/bugCommand.js';
|
import { bugCommand } from '../ui/commands/bugCommand.js';
|
||||||
|
import { quitCommand } from '../ui/commands/quitCommand.js';
|
||||||
|
|
||||||
// Mock the command modules to isolate the service from the command implementations.
|
// Mock the command modules to isolate the service from the command implementations.
|
||||||
vi.mock('../ui/commands/memoryCommand.js', () => ({
|
vi.mock('../ui/commands/memoryCommand.js', () => ({
|
||||||
|
@ -75,9 +76,12 @@ vi.mock('../ui/commands/editorCommand.js', () => ({
|
||||||
vi.mock('../ui/commands/bugCommand.js', () => ({
|
vi.mock('../ui/commands/bugCommand.js', () => ({
|
||||||
bugCommand: { name: 'bug', description: 'Mock Bug' },
|
bugCommand: { name: 'bug', description: 'Mock Bug' },
|
||||||
}));
|
}));
|
||||||
|
vi.mock('../ui/commands/quitCommand.js', () => ({
|
||||||
|
quitCommand: { name: 'quit', description: 'Mock Quit' },
|
||||||
|
}));
|
||||||
|
|
||||||
describe('CommandService', () => {
|
describe('CommandService', () => {
|
||||||
const subCommandLen = 16;
|
const subCommandLen = 17;
|
||||||
let mockConfig: Mocked<Config>;
|
let mockConfig: Mocked<Config>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
@ -144,6 +148,7 @@ describe('CommandService', () => {
|
||||||
const commandNames = tree.map((cmd) => cmd.name);
|
const commandNames = tree.map((cmd) => cmd.name);
|
||||||
expect(commandNames).toContain('ide');
|
expect(commandNames).toContain('ide');
|
||||||
expect(commandNames).toContain('editor');
|
expect(commandNames).toContain('editor');
|
||||||
|
expect(commandNames).toContain('quit');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should overwrite any existing commands when called again', async () => {
|
it('should overwrite any existing commands when called again', async () => {
|
||||||
|
@ -183,6 +188,7 @@ describe('CommandService', () => {
|
||||||
mcpCommand,
|
mcpCommand,
|
||||||
memoryCommand,
|
memoryCommand,
|
||||||
privacyCommand,
|
privacyCommand,
|
||||||
|
quitCommand,
|
||||||
statsCommand,
|
statsCommand,
|
||||||
themeCommand,
|
themeCommand,
|
||||||
toolsCommand,
|
toolsCommand,
|
||||||
|
|
|
@ -23,6 +23,7 @@ import { toolsCommand } from '../ui/commands/toolsCommand.js';
|
||||||
import { compressCommand } from '../ui/commands/compressCommand.js';
|
import { compressCommand } from '../ui/commands/compressCommand.js';
|
||||||
import { ideCommand } from '../ui/commands/ideCommand.js';
|
import { ideCommand } from '../ui/commands/ideCommand.js';
|
||||||
import { bugCommand } from '../ui/commands/bugCommand.js';
|
import { bugCommand } from '../ui/commands/bugCommand.js';
|
||||||
|
import { quitCommand } from '../ui/commands/quitCommand.js';
|
||||||
|
|
||||||
const loadBuiltInCommands = async (
|
const loadBuiltInCommands = async (
|
||||||
config: Config | null,
|
config: Config | null,
|
||||||
|
@ -42,6 +43,7 @@ const loadBuiltInCommands = async (
|
||||||
mcpCommand,
|
mcpCommand,
|
||||||
memoryCommand,
|
memoryCommand,
|
||||||
privacyCommand,
|
privacyCommand,
|
||||||
|
quitCommand,
|
||||||
statsCommand,
|
statsCommand,
|
||||||
themeCommand,
|
themeCommand,
|
||||||
toolsCommand,
|
toolsCommand,
|
||||||
|
|
|
@ -76,15 +76,13 @@ export const createMockCommandContext = (
|
||||||
const targetValue = output[key];
|
const targetValue = output[key];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
sourceValue &&
|
// We only want to recursivlty merge plain objects
|
||||||
typeof sourceValue === 'object' &&
|
Object.prototype.toString.call(sourceValue) === '[object Object]' &&
|
||||||
!Array.isArray(sourceValue) &&
|
Object.prototype.toString.call(targetValue) === '[object Object]'
|
||||||
targetValue &&
|
|
||||||
typeof targetValue === 'object' &&
|
|
||||||
!Array.isArray(targetValue)
|
|
||||||
) {
|
) {
|
||||||
output[key] = merge(targetValue, sourceValue);
|
output[key] = merge(targetValue, sourceValue);
|
||||||
} else {
|
} else {
|
||||||
|
// If not, we do a direct assignment. This preserves Date objects and others.
|
||||||
output[key] = sourceValue;
|
output[key] = sourceValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
|
@ -9,6 +9,7 @@ import { HistoryItemWithoutId } from '../types.js';
|
||||||
import { Config, GitService, Logger } from '@google/gemini-cli-core';
|
import { Config, GitService, Logger } from '@google/gemini-cli-core';
|
||||||
import { LoadedSettings } from '../../config/settings.js';
|
import { LoadedSettings } from '../../config/settings.js';
|
||||||
import { UseHistoryManagerReturn } from '../hooks/useHistoryManager.js';
|
import { UseHistoryManagerReturn } from '../hooks/useHistoryManager.js';
|
||||||
|
import type { HistoryItem } from '../types.js';
|
||||||
import { SessionStatsState } from '../contexts/SessionContext.js';
|
import { SessionStatsState } from '../contexts/SessionContext.js';
|
||||||
|
|
||||||
// Grouped dependencies for clarity and easier mocking
|
// Grouped dependencies for clarity and easier mocking
|
||||||
|
@ -56,6 +57,12 @@ export interface ToolActionReturn {
|
||||||
toolArgs: Record<string, unknown>;
|
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
|
* The return type for a command action that results in a simple message
|
||||||
* being displayed to the user.
|
* being displayed to the user.
|
||||||
|
@ -87,6 +94,7 @@ export interface LoadHistoryActionReturn {
|
||||||
export type SlashCommandActionReturn =
|
export type SlashCommandActionReturn =
|
||||||
| ToolActionReturn
|
| ToolActionReturn
|
||||||
| MessageActionReturn
|
| MessageActionReturn
|
||||||
|
| QuitActionReturn
|
||||||
| OpenDialogActionReturn
|
| OpenDialogActionReturn
|
||||||
| LoadHistoryActionReturn;
|
| LoadHistoryActionReturn;
|
||||||
// The standardized contract for any command in the system.
|
// The standardized contract for any command in the system.
|
||||||
|
|
|
@ -54,16 +54,7 @@ vi.mock('../../utils/version.js', () => ({
|
||||||
}));
|
}));
|
||||||
|
|
||||||
import { act, renderHook } from '@testing-library/react';
|
import { act, renderHook } from '@testing-library/react';
|
||||||
import {
|
import { vi, describe, it, expect, beforeEach, beforeAll, Mock } from 'vitest';
|
||||||
vi,
|
|
||||||
describe,
|
|
||||||
it,
|
|
||||||
expect,
|
|
||||||
beforeEach,
|
|
||||||
afterEach,
|
|
||||||
beforeAll,
|
|
||||||
Mock,
|
|
||||||
} from 'vitest';
|
|
||||||
import open from 'open';
|
import open from 'open';
|
||||||
import { useSlashCommandProcessor } from './slashCommandProcessor.js';
|
import { useSlashCommandProcessor } from './slashCommandProcessor.js';
|
||||||
import { SlashCommandProcessorResult } from '../types.js';
|
import { SlashCommandProcessorResult } from '../types.js';
|
||||||
|
@ -203,8 +194,6 @@ describe('useSlashCommandProcessor', () => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getProcessor = () => getProcessorHook().result.current;
|
|
||||||
|
|
||||||
describe('New command registry', () => {
|
describe('New command registry', () => {
|
||||||
let ActualCommandService: typeof CommandService;
|
let ActualCommandService: typeof CommandService;
|
||||||
|
|
||||||
|
@ -451,47 +440,4 @@ describe('useSlashCommandProcessor', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('/quit and /exit commands', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.useFakeTimers();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
vi.useRealTimers();
|
|
||||||
});
|
|
||||||
|
|
||||||
it.each([['/quit'], ['/exit']])(
|
|
||||||
'should handle %s, set quitting messages, and exit the process',
|
|
||||||
async (command) => {
|
|
||||||
const { handleSlashCommand } = getProcessor();
|
|
||||||
const mockDate = new Date('2025-01-01T01:02:03.000Z');
|
|
||||||
vi.setSystemTime(mockDate);
|
|
||||||
|
|
||||||
await act(async () => {
|
|
||||||
handleSlashCommand(command);
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(mockAddItem).not.toHaveBeenCalled();
|
|
||||||
expect(mockSetQuittingMessages).toHaveBeenCalledWith([
|
|
||||||
{
|
|
||||||
type: 'user',
|
|
||||||
text: command,
|
|
||||||
id: expect.any(Number),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'quit',
|
|
||||||
duration: '1h 2m 3s',
|
|
||||||
id: expect.any(Number),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Fast-forward timers to trigger process.exit
|
|
||||||
await act(async () => {
|
|
||||||
vi.advanceTimersByTime(100);
|
|
||||||
});
|
|
||||||
expect(mockProcessExit).toHaveBeenCalledWith(0);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,6 @@ import {
|
||||||
} from '../types.js';
|
} from '../types.js';
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { formatDuration } from '../utils/formatters.js';
|
|
||||||
import { LoadedSettings } from '../../config/settings.js';
|
import { LoadedSettings } from '../../config/settings.js';
|
||||||
import {
|
import {
|
||||||
type CommandContext,
|
type CommandContext,
|
||||||
|
@ -202,33 +201,6 @@ export const useSlashCommandProcessor = (
|
||||||
toggleCorgiMode();
|
toggleCorgiMode();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'quit',
|
|
||||||
altName: 'exit',
|
|
||||||
description: 'exit the cli',
|
|
||||||
action: async (mainCommand, _subCommand, _args) => {
|
|
||||||
const now = new Date();
|
|
||||||
const { sessionStartTime } = session.stats;
|
|
||||||
const wallDuration = now.getTime() - sessionStartTime.getTime();
|
|
||||||
|
|
||||||
setQuittingMessages([
|
|
||||||
{
|
|
||||||
type: 'user',
|
|
||||||
text: `/${mainCommand}`,
|
|
||||||
id: now.getTime() - 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'quit',
|
|
||||||
duration: formatDuration(wallDuration),
|
|
||||||
id: now.getTime(),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
process.exit(0);
|
|
||||||
}, 100);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
|
|
||||||
if (config?.getCheckpointingEnabled()) {
|
if (config?.getCheckpointingEnabled()) {
|
||||||
|
@ -352,15 +324,7 @@ export const useSlashCommandProcessor = (
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return commands;
|
return commands;
|
||||||
}, [
|
}, [addMessage, toggleCorgiMode, config, gitService, loadHistory]);
|
||||||
addMessage,
|
|
||||||
toggleCorgiMode,
|
|
||||||
config,
|
|
||||||
session,
|
|
||||||
gitService,
|
|
||||||
loadHistory,
|
|
||||||
setQuittingMessages,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const handleSlashCommand = useCallback(
|
const handleSlashCommand = useCallback(
|
||||||
async (
|
async (
|
||||||
|
@ -470,6 +434,12 @@ export const useSlashCommandProcessor = (
|
||||||
});
|
});
|
||||||
return { type: 'handled' };
|
return { type: 'handled' };
|
||||||
}
|
}
|
||||||
|
case 'quit':
|
||||||
|
setQuittingMessages(result.messages);
|
||||||
|
setTimeout(() => {
|
||||||
|
process.exit(0);
|
||||||
|
}, 100);
|
||||||
|
return { type: 'handled' };
|
||||||
default: {
|
default: {
|
||||||
const unhandled: never = result;
|
const unhandled: never = result;
|
||||||
throw new Error(`Unhandled slash command result: ${unhandled}`);
|
throw new Error(`Unhandled slash command result: ${unhandled}`);
|
||||||
|
@ -549,6 +519,7 @@ export const useSlashCommandProcessor = (
|
||||||
openThemeDialog,
|
openThemeDialog,
|
||||||
openPrivacyNotice,
|
openPrivacyNotice,
|
||||||
openEditorDialog,
|
openEditorDialog,
|
||||||
|
setQuittingMessages,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue