fix slash commands bug + test

This commit is contained in:
Nate 2025-07-19 22:53:21 -07:00
parent 889d19e448
commit 387427273c
2 changed files with 102 additions and 3 deletions

View file

@ -101,7 +101,21 @@ const UserInput: React.FC<UserInputProps> = ({
const afterSlash = beforeCursor.slice(1);
if (!afterSlash.includes(" ") && !afterSlash.includes("\n")) {
// We're in a slash command context
// We're in a slash command context - check if it's a complete command
const allCommands = getSlashCommands();
const exactMatch = allCommands.find(cmd => cmd.name === afterSlash);
// Hide selector if we have an exact match and there's more content after the cursor
if (exactMatch) {
const restOfText = text.slice(cursor);
// If there's a space immediately after cursor, or we're at end of line/text, hide selector
if (restOfText.startsWith(" ") || restOfText === "" || restOfText.startsWith("\n")) {
setShowSlashCommands(false);
return;
}
}
// Show selector for partial matches
setShowSlashCommands(true);
setSlashCommandFilter(afterSlash);
setSelectedCommandIndex(0);
@ -507,4 +521,4 @@ const UserInput: React.FC<UserInputProps> = ({
);
};
export default UserInput;
export default UserInput;

View file

@ -11,7 +11,7 @@ describe("TUIChat - Slash Commands Tests", () => {
stdin.write("/log");
// Wait a bit for the UI to update
await new Promise((resolve) => setTimeout(resolve, 50));
await new Promise((resolve) => setTimeout(resolve, 100));
const frame = lastFrame();
@ -38,4 +38,89 @@ describe("TUIChat - Slash Commands Tests", () => {
// Should autocomplete to /login (first matching command)
expect(frameAfterTab).toContain("/login ");
});
it("hides slash command selector when complete command is typed", async () => {
const { lastFrame, stdin } = render(<TUIChat {...createProps()} />);
// Type /cle to show partial command dropdown
stdin.write("/cle");
await new Promise((resolve) => setTimeout(resolve, 50));
const framePartial = lastFrame();
// Should show the slash command dropdown for partial match
expect(framePartial).toContain("/clear");
expect(framePartial).toContain("Clear the chat history");
// Type the rest to complete the command /clear
stdin.write("ar");
await new Promise((resolve) => setTimeout(resolve, 50));
const frameComplete = lastFrame();
// Should NOT show the slash command dropdown anymore since we have a complete command
expect(frameComplete).not.toContain("Clear the chat history");
expect(frameComplete).not.toContain(
"Use ↑/↓ to navigate, Enter to select, Tab to complete"
);
// But should still show the input with /clear
expect(frameComplete).toContain("/clear");
});
it("shows slash command selector again if typing continues past complete command", async () => {
const { lastFrame, stdin } = render(<TUIChat {...createProps()} />);
// Type complete command /clear
stdin.write("/clear");
await new Promise((resolve) => setTimeout(resolve, 50));
const frameComplete = lastFrame();
// Should NOT show the dropdown for complete command
expect(frameComplete).not.toContain("Clear the chat history");
// Type more characters to make it not match any command
stdin.write("xyz");
await new Promise((resolve) => setTimeout(resolve, 50));
const frameInvalid = lastFrame();
// Should show "No matching commands found" since /clearxyz doesn't match anything
expect(frameInvalid).toContain("No matching commands found");
});
it("hides slash command selector when space is typed after complete command", async () => {
const { lastFrame, stdin } = render(<TUIChat {...createProps()} />);
// Type complete command /help
stdin.write("/help");
await new Promise((resolve) => setTimeout(resolve, 50));
const frameComplete = lastFrame();
// Should NOT show the dropdown for complete command
expect(frameComplete).not.toContain("Show help message");
// Type a space after the complete command
stdin.write(" ");
await new Promise((resolve) => setTimeout(resolve, 50));
const frameWithSpace = lastFrame();
// Should still NOT show the dropdown after space
expect(frameWithSpace).not.toContain("Show help message");
expect(frameWithSpace).not.toContain(
"Use ↑/↓ to navigate, Enter to select, Tab to complete"
);
// But should show the input with /help
expect(frameWithSpace).toContain("/help ");
});
});