| ... | ... | @@ -0,0 +1,126 @@ |
| 1 | // @vitest-environment jsdom |
| 2 | |
| 3 | import type { Editor } from "@tiptap/core"; |
| 4 | import { TextSelection } from "@tiptap/pm/state"; |
| 5 | import { act, createRef } from "react"; |
| 6 | import { createRoot, type Root } from "react-dom/client"; |
| 7 | import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 8 | |
| 9 | import { serializeEditorDoc } from "~/composer-rich-text-doc"; |
| 10 | import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor"; |
| 11 | |
| 12 | const draft = |
| 13 | "> **Assistant and reasoning text still save intermediate full snapshots**, using the existing 50 ms batching. Compaction still preserves their `turn-item.updated` events. [Code](/Users/yashsingh/p/projects/t3code/apps/server/src/orchestration-v2/ProjectionMaintenance.ts:235)"; |
| 14 | |
| 15 | let root: Root; |
| 16 | let container: HTMLDivElement; |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 20 | container = document.createElement("div"); |
| 21 | document.body.append(container); |
| 22 | root = createRoot(container); |
| 23 | }); |
| 24 | |
| 25 | afterEach(async () => { |
| 26 | await act(async () => root.unmount()); |
| 27 | container.remove(); |
| 28 | vi.unstubAllGlobals(); |
| 29 | }); |
| 30 | |
| 31 | async function mountEditor(value: string, richTextEnabled = true) { |
| 32 | await act(async () => |
| 33 | root.render( |
| 34 | <ComposerPromptEditor |
| 35 | editorRef={createRef<ComposerPromptEditorHandle>()} |
| 36 | value={value} |
| 37 | cursor={0} |
| 38 | richTextEnabled={richTextEnabled} |
| 39 | contextRecords={new Map()} |
| 40 | skills={[]} |
| 41 | disabled={false} |
| 42 | placeholder="" |
| 43 | onChange={() => {}} |
| 44 | onPaste={() => {}} |
| 45 | />, |
| 46 | ), |
| 47 | ); |
| 48 | const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap"); |
| 49 | if (!element) throw new Error("Composer did not mount"); |
| 50 | return element.editor; |
| 51 | } |
| 52 | |
| 53 | function press(editor: Editor, key: string, options: KeyboardEventInit = {}) { |
| 54 | const event = new KeyboardEvent("keydown", { key, cancelable: true, ...options }); |
| 55 | const handled = editor.view.props.handleKeyDown?.(editor.view, event); |
| 56 | expect(event.defaultPrevented).toBe(Boolean(handled)); |
| 57 | return handled; |
| 58 | } |
| 59 | |
| 60 | it("keeps Up before the opening bold marker in a quote, including while typing", async () => { |
| 61 | const editor = await mountEditor(draft); |
| 62 | const position = editor.state.selection.from; |
| 63 | await act(async () => { |
| 64 | editor.view.focus(); |
| 65 | expect(press(editor, "ArrowUp")).toBe(true); |
| 66 | expect(editor.state.selection.from).toBe(position); |
| 67 | editor.view.updateState(editor.state); |
| 68 | const marker = editor.view.dom.querySelector(".composer-rich-marker"); |
| 69 | const caret = window.getSelection()?.getRangeAt(0); |
| 70 | if (!marker || !caret) throw new Error("Caret or bold marker is missing"); |
| 71 | const markerRange = document.createRange(); |
| 72 | markerRange.selectNode(marker); |
| 73 | expect(caret.compareBoundaryPoints(Range.START_TO_START, markerRange)).toBeLessThanOrEqual(0); |
| 74 | // A repeated Up stays at the outside stop, ready for native navigation. |
| 75 | expect(press(editor, "ArrowUp")).toBe(false); |
| 76 | editor.commands.insertContent("x"); |
| 77 | }); |
| 78 | expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> x**")); |
| 79 | }); |
| 80 | |
| 81 | it("keeps Down after a closing marker, including while typing", async () => { |
| 82 | const editor = await mountEditor("> **bold**"); |
| 83 | await act(async () => { |
| 84 | editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc))); |
| 85 | expect(press(editor, "ArrowDown")).toBe(true); |
| 86 | expect(press(editor, "ArrowDown")).toBe(false); |
| 87 | editor.commands.insertContent("x"); |
| 88 | }); |
| 89 | expect(serializeEditorDoc(editor.state.doc).value).toBe("> **bold**x"); |
| 90 | }); |
| 91 | |
| 92 | it("leaves vertical navigation between paragraphs to the browser", async () => { |
| 93 | const editor = await mountEditor("**first**\n**second**"); |
| 94 | await act(async () => { |
| 95 | const first = editor.state.doc.child(0); |
| 96 | editor.commands.setTextSelection(first.nodeSize + 1); |
| 97 | expect(press(editor, "ArrowUp")).toBe(false); |
| 98 | editor.commands.setTextSelection(first.nodeSize - 1); |
| 99 | expect(press(editor, "ArrowDown")).toBe(false); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | it.each([ |
| 104 | { shiftKey: true }, |
| 105 | { altKey: true }, |
| 106 | { metaKey: true }, |
| 107 | { ctrlKey: true }, |
| 108 | { isComposing: true }, |
| 109 | ])("preserves modified Up behavior for %j", async (options) => { |
| 110 | const editor = await mountEditor(draft); |
| 111 | await act(async () => { |
| 112 | expect(press(editor, "ArrowUp", options)).toBe(false); |
| 113 | editor.commands.insertContent("x"); |
| 114 | }); |
| 115 | expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x")); |
| 116 | }); |
| 117 | |
| 118 | it("leaves literal Markdown navigation to the browser in plain mode", async () => { |
| 119 | const editor = await mountEditor(draft, false); |
| 120 | await act(async () => { |
| 121 | editor.commands.setTextSelection(5); |
| 122 | expect(press(editor, "ArrowUp")).toBe(false); |
| 123 | editor.commands.insertContent("x"); |
| 124 | }); |
| 125 | expect(serializeEditorDoc(editor.state.doc).value).toBe(draft.replace("> **", "> **x")); |
| 126 | }); |