From 387427273cb719320e2b8b3140e83ecbfb6f98d5 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 19 Jul 2025 22:53:21 -0700 Subject: [PATCH] fix slash commands bug + test --- src/ui/UserInput.tsx | 18 +++- .../__tests__/TUIChat.slashCommands.test.tsx | 87 ++++++++++++++++++- 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/ui/UserInput.tsx b/src/ui/UserInput.tsx index 8f6b39622..7204ce830 100644 --- a/src/ui/UserInput.tsx +++ b/src/ui/UserInput.tsx @@ -101,7 +101,21 @@ const UserInput: React.FC = ({ 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 = ({ ); }; -export default UserInput; +export default UserInput; \ No newline at end of file diff --git a/src/ui/__tests__/TUIChat.slashCommands.test.tsx b/src/ui/__tests__/TUIChat.slashCommands.test.tsx index d01ea6247..f00cb9aa1 100644 --- a/src/ui/__tests__/TUIChat.slashCommands.test.tsx +++ b/src/ui/__tests__/TUIChat.slashCommands.test.tsx @@ -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(); + + // 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(); + + // 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(); + + // 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 "); + }); });