|
| 1 | +// @vitest-environment jsdom |
| 2 | +import type { Editor } from "@tiptap/core"; |
| 3 | +import { closeHistory } from "@tiptap/pm/history"; |
| 4 | +import { act, createRef, useImperativeHandle, useState } from "react"; |
| 5 | +import { createRoot, type Root } from "react-dom/client"; |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 7 | + |
| 8 | +import { editableOwnsUndo } from "../lib/editableFocus"; |
| 9 | +import { formatInlineContextReference } from "../lib/composerContextReferences"; |
| 10 | +import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor"; |
| 11 | + |
| 12 | +let root: Root; |
| 13 | +let container: HTMLDivElement; |
| 14 | +const controls = createRef<{ setDraft: (value: string, thread?: string) => void }>(); |
| 15 | +function setDraft(value: string, thread?: string) { |
| 16 | + if (!controls.current) throw new Error("Controlled composer missing"); |
| 17 | + controls.current.setDraft(value, thread); |
| 18 | +} |
| 19 | +const editorRef = createRef<ComposerPromptEditorHandle>(); |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 23 | + container = document.createElement("div"); |
| 24 | + document.body.append(container); |
| 25 | + root = createRoot(container); |
| 26 | +}); |
| 27 | + |
| 28 | +afterEach(async () => { |
| 29 | + await act(async () => root.unmount()); |
| 30 | + container.remove(); |
| 31 | + vi.unstubAllGlobals(); |
| 32 | +}); |
| 33 | + |
| 34 | +function ControlledComposer() { |
| 35 | + const [draft, updateDraft] = useState({ value: "", thread: "a" }); |
| 36 | + useImperativeHandle( |
| 37 | + controls, |
| 38 | + () => ({ |
| 39 | + setDraft: (value, thread) => |
| 40 | + updateDraft((current) => ({ value, thread: thread ?? current.thread })), |
| 41 | + }), |
| 42 | + [], |
| 43 | + ); |
| 44 | + return ( |
| 45 | + <ComposerPromptEditor |
| 46 | + historyScopeKey={draft.thread} |
| 47 | + value={draft.value} |
| 48 | + cursor={draft.value.length} |
| 49 | + contextRecords={new Map()} |
| 50 | + skills={[]} |
| 51 | + disabled={false} |
| 52 | + placeholder="Prompt" |
| 53 | + onChange={(value) => updateDraft((current) => ({ ...current, value }))} |
| 54 | + onPaste={() => {}} |
| 55 | + editorRef={editorRef} |
| 56 | + /> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +function editor() { |
| 61 | + const element = container.querySelector<HTMLElement>('[data-testid="composer-editor"]'); |
| 62 | + if (!element) throw new Error("Composer editor missing"); |
| 63 | + // Tiptap attaches the live instance to its editable DOM element. |
| 64 | + return (element as HTMLElement & { editor: Editor }).editor; |
| 65 | +} |
| 66 | + |
| 67 | +async function mount() { |
| 68 | + await act(async () => root.render(<ControlledComposer />)); |
| 69 | + // jsdom has no layout; keep focus/selection real without measuring its caret. |
| 70 | + editor().setOptions({ |
| 71 | + editorProps: { ...editor().options.editorProps, handleScrollToSelection: () => true }, |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +async function edit(run: (editor: Editor) => void) { |
| 76 | + await act(async () => run(editor())); |
| 77 | +} |
| 78 | + |
| 79 | +function ownsUndo() { |
| 80 | + return editableOwnsUndo(editor().view.dom); |
| 81 | +} |
| 82 | + |
| 83 | +describe("controlled composer undo ownership", () => { |
| 84 | + it("lets the empty composer yield after sending, without restoring sent text", async () => { |
| 85 | + await mount(); |
| 86 | + await edit((current) => current.commands.insertContent("sent prompt")); |
| 87 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 88 | + await act(async () => setDraft("")); |
| 89 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 90 | + expect(ownsUndo()).toBe(false); |
| 91 | + expect(editor().commands.undo()).toBe(false); |
| 92 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 93 | + }); |
| 94 | + |
| 95 | + it("resets an already empty editor when an attachment-only send clears the draft", async () => { |
| 96 | + await mount(); |
| 97 | + await edit((current) => current.commands.insertContent("deleted draft")); |
| 98 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 99 | + await edit((current) => current.commands.selectAll()); |
| 100 | + await edit((current) => current.commands.deleteSelection()); |
| 101 | + expect(ownsUndo()).toBe(true); |
| 102 | + // ChatComposer.resetCursorState uses this when send clears an already |
| 103 | + // empty prompt, which causes no controlled value change. |
| 104 | + await act(async () => { |
| 105 | + setDraft(""); |
| 106 | + editorRef.current?.resetUndoHistory(); |
| 107 | + }); |
| 108 | + expect(ownsUndo()).toBe(false); |
| 109 | + expect(editor().commands.undo()).toBe(false); |
| 110 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 111 | + }); |
| 112 | + |
| 113 | + it("preserves user deletion and yields after undo history is exhausted", async () => { |
| 114 | + await mount(); |
| 115 | + await edit((current) => current.commands.insertContent("draft")); |
| 116 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 117 | + await edit((current) => current.commands.selectAll()); |
| 118 | + await edit((current) => current.commands.deleteSelection()); |
| 119 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 120 | + expect(ownsUndo()).toBe(true); |
| 121 | + await edit((current) => current.commands.undo()); |
| 122 | + expect(editorRef.current?.readSnapshot().value).toBe("draft"); |
| 123 | + await edit((current) => current.commands.undo()); |
| 124 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 125 | + expect(ownsUndo()).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it("keeps a new thread's draft separate from the old thread's history", async () => { |
| 129 | + await mount(); |
| 130 | + await edit((current) => current.commands.insertContent("thread a draft")); |
| 131 | + await act(async () => setDraft("thread b draft", "b")); |
| 132 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 133 | + await edit((current) => current.commands.selectAll()); |
| 134 | + await edit((current) => current.commands.deleteSelection()); |
| 135 | + expect(ownsUndo()).toBe(true); |
| 136 | + await edit((current) => current.commands.undo()); |
| 137 | + expect(editorRef.current?.readSnapshot().value).toBe("thread b draft"); |
| 138 | + expect(editor().commands.undo()).toBe(false); |
| 139 | + await act(async () => setDraft("", "a")); |
| 140 | + expect(ownsUndo()).toBe(false); |
| 141 | + expect(editor().commands.undo()).toBe(false); |
| 142 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 143 | + }); |
| 144 | + |
| 145 | + it("restores a chip-only draft after its editor deletion", async () => { |
| 146 | + await mount(); |
| 147 | + const chip = formatInlineContextReference({ |
| 148 | + kind: "file", |
| 149 | + contextId: "file_one", |
| 150 | + label: "one.ts", |
| 151 | + }); |
| 152 | + await act(async () => setDraft(chip)); |
| 153 | + expect(editorRef.current?.readSnapshot().contextIds).toEqual(["file_one"]); |
| 154 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 155 | + await edit((current) => current.commands.selectAll()); |
| 156 | + await edit((current) => current.commands.deleteSelection()); |
| 157 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 158 | + expect(ownsUndo()).toBe(true); |
| 159 | + await edit((current) => current.commands.undo()); |
| 160 | + expect(editorRef.current?.readSnapshot().contextIds).toEqual(["file_one"]); |
| 161 | + }); |
| 162 | + |
| 163 | + it.each(["same draft", ""])( |
| 164 | + "keeps composer focus while isolating equal-text thread scopes (%j)", |
| 165 | + async (value) => { |
| 166 | + await mount(); |
| 167 | + await edit((current) => current.commands.insertContent("same draft")); |
| 168 | + if (value === "") { |
| 169 | + await edit((current) => current.commands.selectAll()); |
| 170 | + await edit((current) => current.commands.deleteSelection()); |
| 171 | + } |
| 172 | + const focusedEditor = editor().view.dom; |
| 173 | + await act(async () => focusedEditor.focus()); |
| 174 | + expect(document.activeElement).toBe(focusedEditor); |
| 175 | + await act(async () => setDraft(value, "b")); |
| 176 | + expect(document.activeElement).toBe(focusedEditor); |
| 177 | + expect(editorRef.current?.readSnapshot().value).toBe(value); |
| 178 | + expect(editor().commands.undo()).toBe(false); |
| 179 | + }, |
| 180 | + ); |
| 181 | + |
| 182 | + it("keeps new user history undoable after a send-clear", async () => { |
| 183 | + await mount(); |
| 184 | + await edit((current) => current.commands.insertContent("sent prompt")); |
| 185 | + await act(async () => setDraft("")); |
| 186 | + await edit((current) => current.commands.insertContent("new draft")); |
| 187 | + await edit((current) => current.view.dispatch(closeHistory(current.state.tr))); |
| 188 | + await edit((current) => current.commands.selectAll()); |
| 189 | + await edit((current) => current.commands.deleteSelection()); |
| 190 | + expect(ownsUndo()).toBe(true); |
| 191 | + await edit((current) => current.commands.undo()); |
| 192 | + expect(editorRef.current?.readSnapshot().value).toBe("new draft"); |
| 193 | + await edit((current) => current.commands.undo()); |
| 194 | + expect(editorRef.current?.readSnapshot().value).toBe(""); |
| 195 | + expect(ownsUndo()).toBe(false); |
| 196 | + expect(editor().commands.undo()).toBe(false); |
| 197 | + }); |
| 198 | +}); |
0 commit comments