diff --git a/packages/cli/src/ui/hooks/useSlashCompletion.test.ts b/packages/cli/src/ui/hooks/useSlashCompletion.test.ts index 0bcb3863ce..d9a2012de5 100644 --- a/packages/cli/src/ui/hooks/useSlashCompletion.test.ts +++ b/packages/cli/src/ui/hooks/useSlashCompletion.test.ts @@ -513,6 +513,70 @@ describe('useSlashCompletion', () => { unmountResume(); }); + it('should NOT suggest the auto-list command when typing a non-matching partial after /chat', async () => { + const slashCommands = [ + createTestCommand({ + name: 'chat', + description: 'Manage chat history', + subCommands: [ + createTestCommand({ name: 'list', description: 'List chats' }), + ], + }), + ]; + + const { result, unmount } = await renderHook(() => + useTestHarnessForSlashCompletion( + true, + '/chat x', // 'x' does not match 'list' + slashCommands, + mockCommandContext, + ), + ); + + await resolveMatch(); + + await waitFor(() => { + // It should NOT have the 'auto' section 'list' suggestion + const autoSuggestion = result.current.suggestions.find( + (s) => s.sectionTitle === 'auto', + ); + expect(autoSuggestion).toBeUndefined(); + }); + unmount(); + }); + + it('should STILL suggest the auto-list command when typing a matching partial after /chat', async () => { + const slashCommands = [ + createTestCommand({ + name: 'chat', + description: 'Manage chat history', + subCommands: [ + createTestCommand({ name: 'list', description: 'List chats' }), + ], + }), + ]; + + const { result, unmount } = await renderHook(() => + useTestHarnessForSlashCompletion( + true, + '/chat l', // 'l' matches 'list' + slashCommands, + mockCommandContext, + ), + ); + + await resolveMatch(); + + await waitFor(() => { + const autoSuggestion = result.current.suggestions.find( + (s) => s.sectionTitle === 'auto', + ); + expect(autoSuggestion).toBeDefined(); + expect(autoSuggestion?.label).toBe('list'); + }); + unmount(); + }); + it('should sort exact altName matches to the top', async () => { const slashCommands = [ createTestCommand({ diff --git a/packages/cli/src/ui/hooks/useSlashCompletion.ts b/packages/cli/src/ui/hooks/useSlashCompletion.ts index 7b06fdc1f4..3124a8b620 100644 --- a/packages/cli/src/ui/hooks/useSlashCompletion.ts +++ b/packages/cli/src/ui/hooks/useSlashCompletion.ts @@ -338,17 +338,23 @@ function useCommandSuggestions( if (isTopLevelChatOrResumeContext) { const canonicalParentName = leafCommand.name; - const autoSectionSuggestion: Suggestion = { - label: 'list', - value: 'list', - insertValue: canonicalParentName, - description: 'Browse auto-saved chats', - commandKind: CommandKind.BUILT_IN, - sectionTitle: 'auto', - submitValue: `/${canonicalParentName}`, - }; - setSuggestions([autoSectionSuggestion, ...finalSuggestions]); - return; + const autoLabel = 'list'; + if ( + partial === '' || + autoLabel.toLowerCase().startsWith(partial.toLowerCase()) + ) { + const autoSectionSuggestion: Suggestion = { + label: autoLabel, + value: autoLabel, + insertValue: canonicalParentName, + description: 'Browse auto-saved chats', + commandKind: CommandKind.BUILT_IN, + sectionTitle: 'auto', + submitValue: `/${canonicalParentName}`, + }; + setSuggestions([autoSectionSuggestion, ...finalSuggestions]); + return; + } } setSuggestions(finalSuggestions);