|
| 1 | +import { DraftId } from "../../composerDraftStore"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { composerCommandKeyHandler } from "./composerCommandKey"; |
| 5 | + |
| 6 | +function setup() { |
| 7 | + const dismiss = vi.fn(); |
| 8 | + const submit = vi.fn(); |
| 9 | + const saveDraft = vi.fn(); |
| 10 | + const promptRef = { current: "" }; |
| 11 | + const handler = composerCommandKeyHandler({ |
| 12 | + keybindings: [], |
| 13 | + sendShortcut: "enter", |
| 14 | + routeKind: "draft", |
| 15 | + phase: "ready", |
| 16 | + isCompacting: false, |
| 17 | + followUpBehavior: "queue", |
| 18 | + isSoftwareKeyboardOpen: false, |
| 19 | + planModeUiEnabled: false, |
| 20 | + isLiteralPendingAnswer: false, |
| 21 | + pendingUserInputs: [], |
| 22 | + promptHistoryMessages: [], |
| 23 | + promptHistoryPositionRef: { current: null }, |
| 24 | + composerDraftTarget: DraftId.make("draft"), |
| 25 | + promptRef, |
| 26 | + composerImagesRef: { current: [] }, |
| 27 | + composerFilesRef: { current: [] }, |
| 28 | + composerTerminalContextsRef: { current: [] }, |
| 29 | + composerPreviewAnnotations: [], |
| 30 | + composerReviewComments: [], |
| 31 | + promptSuggestion: { text: "Run tests", dismiss }, |
| 32 | + composerEditorRef: { current: null }, |
| 33 | + composerMenuOpenRef: { current: false }, |
| 34 | + composerMenuItemsRef: { current: [] }, |
| 35 | + activeComposerMenuItemRef: { current: null }, |
| 36 | + setComposerDraftPrompt: saveDraft, |
| 37 | + setComposerCursor: vi.fn(), |
| 38 | + setComposerTrigger: vi.fn(), |
| 39 | + setComposerHighlightedItemId: vi.fn(), |
| 40 | + dismissComposerTrigger: vi.fn(), |
| 41 | + toggleInteractionMode: vi.fn(), |
| 42 | + nudgeComposerMenuHighlight: vi.fn(), |
| 43 | + onSelectComposerItem: vi.fn(), |
| 44 | + resolveActiveComposerTrigger: () => ({ trigger: null }), |
| 45 | + readComposerSnapshot: () => ({ value: "" }), |
| 46 | + applyPromptReplacement: () => false, |
| 47 | + submitComposer: submit, |
| 48 | + }); |
| 49 | + return { handler, dismiss, submit, saveDraft, promptRef }; |
| 50 | +} |
| 51 | + |
| 52 | +describe("next prompt keyboard acceptance", () => { |
| 53 | + it.each(["Tab", "ArrowRight"])("accepts and consumes on %s without sending", (key) => { |
| 54 | + const { handler, dismiss, submit, saveDraft, promptRef } = setup(); |
| 55 | + expect( |
| 56 | + handler(key, { |
| 57 | + key, |
| 58 | + shiftKey: false, |
| 59 | + altKey: false, |
| 60 | + metaKey: false, |
| 61 | + ctrlKey: false, |
| 62 | + isComposing: false, |
| 63 | + } as KeyboardEvent), |
| 64 | + ).toBe(true); |
| 65 | + expect(promptRef.current).toBe("Run tests"); |
| 66 | + expect(saveDraft).toHaveBeenCalledWith(DraftId.make("draft"), "Run tests"); |
| 67 | + expect(dismiss).toHaveBeenCalledOnce(); |
| 68 | + expect(submit).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("dismisses on Escape without changing or sending the draft", () => { |
| 72 | + const { handler, dismiss, submit, saveDraft } = setup(); |
| 73 | + expect( |
| 74 | + handler("Escape", { |
| 75 | + key: "Escape", |
| 76 | + shiftKey: false, |
| 77 | + altKey: false, |
| 78 | + metaKey: false, |
| 79 | + ctrlKey: false, |
| 80 | + isComposing: false, |
| 81 | + } as KeyboardEvent), |
| 82 | + ).toBe(true); |
| 83 | + expect(dismiss).toHaveBeenCalledOnce(); |
| 84 | + expect(saveDraft).not.toHaveBeenCalled(); |
| 85 | + expect(submit).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments