|
| 1 | +/** |
| 2 | + * Slash command autocomplete stories |
| 3 | + * |
| 4 | + * Demonstrates the command suggestions popup with: |
| 5 | + * - Built-in commands |
| 6 | + * - Custom commands (from .mux/commands/) |
| 7 | + * - Visual distinction between built-in and custom |
| 8 | + * - Docs link for adding custom commands |
| 9 | + */ |
| 10 | + |
| 11 | +import { appMeta, AppWithMocks, type AppStory } from "./meta.js"; |
| 12 | +import { setupSimpleChatStory, setWorkspaceInput } from "./storyHelpers"; |
| 13 | +import { userEvent, within, waitFor } from "@storybook/test"; |
| 14 | + |
| 15 | +export default { |
| 16 | + ...appMeta, |
| 17 | + title: "App/Slash Commands", |
| 18 | +}; |
| 19 | + |
| 20 | +const DEFAULT_WORKSPACE_ID = "ws-slash"; |
| 21 | + |
| 22 | +/** Shows built-in slash commands only */ |
| 23 | +export const BuiltInCommands: AppStory = { |
| 24 | + render: () => ( |
| 25 | + <AppWithMocks |
| 26 | + setup={() => { |
| 27 | + const client = setupSimpleChatStory({ |
| 28 | + workspaceId: DEFAULT_WORKSPACE_ID, |
| 29 | + messages: [], |
| 30 | + }); |
| 31 | + // Pre-fill input with "/" to trigger suggestions |
| 32 | + setWorkspaceInput(DEFAULT_WORKSPACE_ID, "/"); |
| 33 | + return client; |
| 34 | + }} |
| 35 | + /> |
| 36 | + ), |
| 37 | + play: async ({ canvasElement }) => { |
| 38 | + const canvas = within(canvasElement); |
| 39 | + |
| 40 | + // Wait for textarea to be available and focused |
| 41 | + const textarea = await canvas.findByLabelText("Message Claude", undefined, { |
| 42 | + timeout: 10_000, |
| 43 | + }); |
| 44 | + textarea.focus(); |
| 45 | + |
| 46 | + // Trigger suggestions by typing "/" (input is pre-filled, but we need to trigger the event) |
| 47 | + await userEvent.clear(textarea); |
| 48 | + await userEvent.type(textarea, "/"); |
| 49 | + |
| 50 | + // Wait for suggestions popup to appear |
| 51 | + await waitFor( |
| 52 | + () => { |
| 53 | + const suggestions = document.querySelector("[data-command-suggestions]"); |
| 54 | + if (!suggestions) throw new Error("Suggestions popup not found"); |
| 55 | + }, |
| 56 | + { timeout: 5_000 } |
| 57 | + ); |
| 58 | + |
| 59 | + // Double RAF for scroll stabilization |
| 60 | + await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); |
| 61 | + }, |
| 62 | +}; |
| 63 | + |
| 64 | +/** Shows custom commands alongside built-in commands */ |
| 65 | +export const WithCustomCommands: AppStory = { |
| 66 | + render: () => ( |
| 67 | + <AppWithMocks |
| 68 | + setup={() => { |
| 69 | + const client = setupSimpleChatStory({ |
| 70 | + workspaceId: DEFAULT_WORKSPACE_ID, |
| 71 | + messages: [], |
| 72 | + slashCommands: new Map([ |
| 73 | + [ |
| 74 | + DEFAULT_WORKSPACE_ID, |
| 75 | + [{ name: "dry" }, { name: "review" }, { name: "context" }, { name: "pr-summary" }], |
| 76 | + ], |
| 77 | + ]), |
| 78 | + }); |
| 79 | + setWorkspaceInput(DEFAULT_WORKSPACE_ID, "/"); |
| 80 | + return client; |
| 81 | + }} |
| 82 | + /> |
| 83 | + ), |
| 84 | + play: async ({ canvasElement }) => { |
| 85 | + const canvas = within(canvasElement); |
| 86 | + |
| 87 | + const textarea = await canvas.findByLabelText("Message Claude", undefined, { |
| 88 | + timeout: 10_000, |
| 89 | + }); |
| 90 | + textarea.focus(); |
| 91 | + |
| 92 | + await userEvent.clear(textarea); |
| 93 | + await userEvent.type(textarea, "/"); |
| 94 | + |
| 95 | + // Wait for suggestions popup with custom commands |
| 96 | + await waitFor( |
| 97 | + () => { |
| 98 | + const suggestions = document.querySelector("[data-command-suggestions]"); |
| 99 | + if (!suggestions) throw new Error("Suggestions popup not found"); |
| 100 | + // Verify custom badge appears |
| 101 | + if (!suggestions.textContent?.includes("custom")) { |
| 102 | + throw new Error("Custom command badge not found"); |
| 103 | + } |
| 104 | + }, |
| 105 | + { timeout: 5_000 } |
| 106 | + ); |
| 107 | + |
| 108 | + await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); |
| 109 | + }, |
| 110 | +}; |
| 111 | + |
| 112 | +/** Filtering commands by typing */ |
| 113 | +export const FilteredCommands: AppStory = { |
| 114 | + render: () => ( |
| 115 | + <AppWithMocks |
| 116 | + setup={() => { |
| 117 | + const client = setupSimpleChatStory({ |
| 118 | + workspaceId: DEFAULT_WORKSPACE_ID, |
| 119 | + messages: [], |
| 120 | + slashCommands: new Map([ |
| 121 | + [DEFAULT_WORKSPACE_ID, [{ name: "dry" }, { name: "review" }, { name: "context" }]], |
| 122 | + ]), |
| 123 | + }); |
| 124 | + setWorkspaceInput(DEFAULT_WORKSPACE_ID, "/co"); |
| 125 | + return client; |
| 126 | + }} |
| 127 | + /> |
| 128 | + ), |
| 129 | + play: async ({ canvasElement }) => { |
| 130 | + const canvas = within(canvasElement); |
| 131 | + |
| 132 | + const textarea = await canvas.findByLabelText("Message Claude", undefined, { |
| 133 | + timeout: 10_000, |
| 134 | + }); |
| 135 | + textarea.focus(); |
| 136 | + |
| 137 | + await userEvent.clear(textarea); |
| 138 | + await userEvent.type(textarea, "/co"); |
| 139 | + |
| 140 | + // Wait for filtered suggestions (should show /compact and /context) |
| 141 | + await waitFor( |
| 142 | + () => { |
| 143 | + const suggestions = document.querySelector("[data-command-suggestions]"); |
| 144 | + if (!suggestions) throw new Error("Suggestions popup not found"); |
| 145 | + // Should have filtered to commands starting with "co" |
| 146 | + if (!suggestions.textContent?.includes("compact")) { |
| 147 | + throw new Error("Expected /compact in filtered results"); |
| 148 | + } |
| 149 | + }, |
| 150 | + { timeout: 5_000 } |
| 151 | + ); |
| 152 | + |
| 153 | + await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); |
| 154 | + }, |
| 155 | +}; |
0 commit comments