Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
273 changes: 271 additions & 2 deletions packages/cli/src/ui/hooks/useCommandCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ describe('useCommandCompletion', () => {
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};
const fileCommand: SlashCommand = {
name: 'store-notes',
description: 'Store notes',
kind: CommandKind.FILE,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [
Expand All @@ -529,7 +535,7 @@ describe('useCommandCompletion', () => {
useCommandCompletion(
useTextBufferForTest('please /store'),
testRootDir,
[skillCommand, builtInCommand],
[skillCommand, builtInCommand, fileCommand],
mockCommandContext,
false,
mockConfig,
Expand All @@ -544,11 +550,274 @@ describe('useCommandCompletion', () => {
expect.objectContaining({
enabled: true,
query: '/store',
slashCommands: [skillCommand],
slashCommands: [skillCommand, fileCommand],
}),
);
});

it.each([
{
input: '/review /sto',
expected: '/review /front-end-store-rules ',
},
{
input: '/review\n/sto',
expected: '/review\n/front-end-store-rules ',
},
])(
'should complete a repeated skill in $input',
async ({ input, expected }) => {
const firstSkill: SlashCommand = {
name: 'review',
description: 'Review changes',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const secondSkill: SlashCommand = {
name: 'front-end-store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const userOnlySkill: SlashCommand = {
name: 'store-locally',
description: 'Store locally',
kind: CommandKind.SKILL,
modelInvocable: false,
};
const fileCommand: SlashCommand = {
name: 'store-notes',
description: 'Store notes',
kind: CommandKind.FILE,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [
{ label: 'front-end-store-rules', value: 'front-end-store-rules' },
],
slashCompletionRange: { completionStart: 1, completionEnd: 4 },
});

const { result } = renderHook(() => {
const textBuffer = useTextBufferForTest(input);
const completion = useCommandCompletion(
textBuffer,
testRootDir,
[firstSkill, secondSkill, userOnlySkill, fileCommand],
mockCommandContext,
false,
mockConfig,
);
return { ...completion, textBuffer };
});

await waitFor(() => {
expect(result.current.showSuggestions).toBe(true);
});

expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: '/sto',
slashCommands: [firstSkill, secondSkill, userOnlySkill],
}),
);

act(() => {
result.current.handleAutocomplete(0);
});

expect(result.current.textBuffer.text).toBe(expected);
},
);

it('should exclude hidden and non-user-invocable skills from stacked skill completion candidates', async () => {
const firstSkill: SlashCommand = {
name: 'review',
description: 'Review changes',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const visibleSkill: SlashCommand = {
name: 'store-locally',
description: 'Store locally',
kind: CommandKind.SKILL,
modelInvocable: false,
};
const hiddenSkill: SlashCommand = {
name: 'store-secret',
description: 'Hidden store skill',
kind: CommandKind.SKILL,
modelInvocable: false,
hidden: true,
};
const nonUserInvocableSkill: SlashCommand = {
name: 'store-internal',
description: 'Internal store skill',
kind: CommandKind.SKILL,
modelInvocable: false,
userInvocable: false,
};
const fileCommand: SlashCommand = {
name: 'store-notes',
description: 'Store notes',
kind: CommandKind.FILE,
modelInvocable: true,
};

renderHook(() =>
useCommandCompletion(
useTextBufferForTest('/review /sto'),
testRootDir,
[
firstSkill,
visibleSkill,
hiddenSkill,
nonUserInvocableSkill,
fileCommand,
],
mockCommandContext,
false,
mockConfig,
),
);

await waitFor(() => {
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: '/sto',
slashCommands: [firstSkill, visibleSkill],
}),
);
});
});

it.each(['/stats /sto', '/unknown /sto', '/unknown\n/sto'])(
'should not treat an invalid stacked prefix as mid-input completion: %s',
async (input) => {
const skillCommand: SlashCommand = {
name: 'store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const builtInCommand: SlashCommand = {
name: 'stats',
description: 'Show stats',
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};

const { result } = renderHook(() =>
useCommandCompletion(
useTextBufferForTest(input),
testRootDir,
[skillCommand, builtInCommand],
mockCommandContext,
false,
mockConfig,
),
);

expect(result.current.midInputGhostText).toBeNull();

await waitFor(() => {
if (input.includes('\n')) {
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({ enabled: false }),
);
} else {
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: input,
slashCommands: [skillCommand, builtInCommand],
}),
);
}
});
},
);

it.each(['/tmp/foo.txt please /sto', '// note /sto'])(
'should treat non-command slash-led prefixes as regular mid-input completion: %s',
async (input) => {
const skillCommand: SlashCommand = {
name: 'store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};

setupMocks({
slashSuggestions: [{ label: 'store-rules', value: 'store-rules' }],
});

const { result } = renderHook(() =>
useCommandCompletion(
useTextBufferForTest(input),
testRootDir,
[skillCommand],
mockCommandContext,
false,
mockConfig,
),
);

await waitFor(() => {
expect(result.current.showSuggestions).toBe(true);
});

expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: '/sto',
slashCommands: [skillCommand],
}),
);
},
);

it('should keep an indented first command in line-start completion', async () => {
const skillCommand: SlashCommand = {
name: 'front-end-store-rules',
description: 'Store rules',
kind: CommandKind.SKILL,
modelInvocable: true,
};
const builtInCommand: SlashCommand = {
name: 'stats',
description: 'Show stats',
kind: CommandKind.BUILT_IN,
modelInvocable: false,
};

renderHook(() =>
useCommandCompletion(
useTextBufferForTest(' /sto'),
testRootDir,
[skillCommand, builtInCommand],
mockCommandContext,
false,
mockConfig,
),
);

await waitFor(() => {
// This pins routing to the existing line-start path. The real slash
// completion may still decide whether an indented query has candidates.
expect(useSlashCompletion).toHaveBeenLastCalledWith(
expect.objectContaining({
enabled: true,
query: ' /sto',
slashCommands: [skillCommand, builtInCommand],
}),
);
});
});

it('should complete a file path when @ appears after a slash command', async () => {
setupMocks({
atSuggestions: [{ label: 'src/index.ts', value: 'src/index.ts' }],
Expand Down
Loading
Loading