update /theme to new slash command arch (#3791)

Co-authored-by: matt korwel <matt.korwel@gmail.com>
Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com>
This commit is contained in:
haroldmciver-go 2025-07-11 16:01:28 -04:00 committed by GitHub
parent 2826c7a1c6
commit 4197f30278
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 102 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import { type SlashCommand } from '../ui/commands/types.js';
import { memoryCommand } from '../ui/commands/memoryCommand.js';
import { helpCommand } from '../ui/commands/helpCommand.js';
import { clearCommand } from '../ui/commands/clearCommand.js';
import { themeCommand } from '../ui/commands/themeCommand.js';
// Mock the command modules to isolate the service from the command implementations.
vi.mock('../ui/commands/memoryCommand.js', () => ({
@ -21,6 +22,9 @@ vi.mock('../ui/commands/helpCommand.js', () => ({
vi.mock('../ui/commands/clearCommand.js', () => ({
clearCommand: { name: 'clear', description: 'Mock Clear' },
}));
vi.mock('../ui/commands/themeCommand.js', () => ({
themeCommand: { name: 'theme', description: 'Mock Theme' },
}));
describe('CommandService', () => {
describe('when using default production loader', () => {
@ -46,25 +50,26 @@ describe('CommandService', () => {
const tree = commandService.getCommands();
// Post-condition assertions
expect(tree.length).toBe(3);
expect(tree.length).toBe(4);
const commandNames = tree.map((cmd) => cmd.name);
expect(commandNames).toContain('memory');
expect(commandNames).toContain('help');
expect(commandNames).toContain('clear');
expect(commandNames).toContain('theme');
});
it('should overwrite any existing commands when called again', async () => {
// Load once
await commandService.loadCommands();
expect(commandService.getCommands().length).toBe(3);
expect(commandService.getCommands().length).toBe(4);
// Load again
await commandService.loadCommands();
const tree = commandService.getCommands();
// Should not append, but overwrite
expect(tree.length).toBe(3);
expect(tree.length).toBe(4);
});
});
@ -76,8 +81,13 @@ describe('CommandService', () => {
await commandService.loadCommands();
const loadedTree = commandService.getCommands();
expect(loadedTree.length).toBe(3);
expect(loadedTree).toEqual([clearCommand, helpCommand, memoryCommand]);
expect(loadedTree.length).toBe(4);
expect(loadedTree).toEqual([
clearCommand,
helpCommand,
memoryCommand,
themeCommand,
]);
});
});
});

View File

@ -8,11 +8,13 @@ import { SlashCommand } from '../ui/commands/types.js';
import { memoryCommand } from '../ui/commands/memoryCommand.js';
import { helpCommand } from '../ui/commands/helpCommand.js';
import { clearCommand } from '../ui/commands/clearCommand.js';
import { themeCommand } from '../ui/commands/themeCommand.js';
const loadBuiltInCommands = async (): Promise<SlashCommand[]> => [
clearCommand,
helpCommand,
memoryCommand,
themeCommand,
];
export class CommandService {

View File

@ -0,0 +1,38 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { themeCommand } from './themeCommand.js';
import { type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
describe('themeCommand', () => {
let mockContext: CommandContext;
beforeEach(() => {
mockContext = createMockCommandContext();
});
it('should return a dialog action to open the theme dialog', () => {
// Ensure the command has an action to test.
if (!themeCommand.action) {
throw new Error('The theme command must have an action.');
}
const result = themeCommand.action(mockContext, '');
// Assert that the action returns the correct object to trigger the theme dialog.
expect(result).toEqual({
type: 'dialog',
dialog: 'theme',
});
});
it('should have the correct name and description', () => {
expect(themeCommand.name).toBe('theme');
expect(themeCommand.description).toBe('change the theme');
});
});

View File

@ -0,0 +1,16 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { OpenDialogActionReturn, SlashCommand } from './types.js';
export const themeCommand: SlashCommand = {
name: 'theme',
description: 'change the theme',
action: (_context, _args): OpenDialogActionReturn => ({
type: 'dialog',
dialog: 'theme',
}),
};

View File

@ -66,7 +66,7 @@ export interface MessageActionReturn {
export interface OpenDialogActionReturn {
type: 'dialog';
// TODO: Add 'theme' | 'auth' | 'editor' | 'privacy' as migration happens.
dialog: 'help';
dialog: 'help' | 'theme';
}
export type SlashCommandActionReturn =

View File

@ -507,6 +507,32 @@ describe('useSlashCommandProcessor', () => {
expect(commandResult).toEqual({ type: 'handled' });
});
it('should open the theme dialog when a new command returns a theme dialog action', async () => {
const mockAction = vi.fn().mockResolvedValue({
type: 'dialog',
dialog: 'theme',
});
const newCommand: SlashCommand = { name: 'test', action: mockAction };
const mockLoader = async () => [newCommand];
const commandServiceInstance = new ActualCommandService(mockLoader);
vi.mocked(CommandService).mockImplementation(
() => commandServiceInstance,
);
const { result } = getProcessorHook();
await vi.waitFor(() => {
expect(
result.current.slashCommands.some((c) => c.name === 'test'),
).toBe(true);
});
const commandResult = await result.current.handleSlashCommand('/test');
expect(mockAction).toHaveBeenCalledTimes(1);
expect(mockOpenThemeDialog).toHaveBeenCalledWith();
expect(commandResult).toEqual({ type: 'handled' });
});
it('should show help for a parent command with no action', async () => {
const parentCommand: SlashCommand = {
name: 'parent',

View File

@ -242,13 +242,6 @@ export const useSlashCommandProcessor = (
}
},
},
{
name: 'theme',
description: 'change the theme',
action: (_mainCommand, _subCommand, _args) => {
openThemeDialog();
},
},
{
name: 'auth',
description: 'change the auth method',
@ -1034,7 +1027,6 @@ export const useSlashCommandProcessor = (
return commands;
}, [
addMessage,
openThemeDialog,
openAuthDialog,
openEditorDialog,
openPrivacyNotice,
@ -1133,6 +1125,9 @@ export const useSlashCommandProcessor = (
case 'help':
setShowHelp(true);
return { type: 'handled' };
case 'theme':
openThemeDialog();
return { type: 'handled' };
default: {
const unhandled: never = result.dialog;
throw new Error(
@ -1214,6 +1209,7 @@ export const useSlashCommandProcessor = (
legacyCommands,
commandContext,
addMessage,
openThemeDialog,
],
);