@@ -7,6 +7,7 @@ import { createRoot, type Root } from "react-dom/client";
77import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
88
99import { serializeEditorDoc } from "~/composer-rich-text-doc" ;
10+ import { collapseExpandedComposerCursor } from "~/composer-logic" ;
1011import { ComposerPromptEditor , type ComposerPromptEditorHandle } from "./ComposerPromptEditor" ;
1112
1213const draft =
@@ -28,11 +29,15 @@ afterEach(async () => {
2829 vi . unstubAllGlobals ( ) ;
2930} ) ;
3031
31- async function mountEditor ( value : string , richTextEnabled = true ) {
32+ async function mountEditor (
33+ value : string ,
34+ richTextEnabled = true ,
35+ editorRef = createRef < ComposerPromptEditorHandle > ( ) ,
36+ ) {
3237 await act ( async ( ) =>
3338 root . render (
3439 < ComposerPromptEditor
35- editorRef = { createRef < ComposerPromptEditorHandle > ( ) }
40+ editorRef = { editorRef }
3641 value = { value }
3742 cursor = { 0 }
3843 richTextEnabled = { richTextEnabled }
@@ -57,6 +62,32 @@ function press(editor: Editor, key: string, options: KeyboardEventInit = {}) {
5762 return handled ;
5863}
5964
65+ it . each ( [
66+ {
67+ value : "Ultrathink:\nAlpha beta gamma" ,
68+ expected : "Ultrathink:\nAlpha X gamma" ,
69+ } ,
70+ { value : "Alpha beta gamma" , expected : "Alpha X gamma" } ,
71+ ] ) (
72+ "restores the editing range after an effort prefix change to $value" ,
73+ async ( { value, expected } ) => {
74+ const start = value . indexOf ( "beta" ) ;
75+ const end = start + "beta" . length ;
76+ const editorRef = createRef < ComposerPromptEditorHandle > ( ) ;
77+ const editor = await mountEditor ( value , true , editorRef ) ;
78+ editor . view . setProps ( { handleScrollToSelection : ( ) => true } ) ;
79+ await act ( async ( ) => {
80+ editorRef . current ?. focusAt (
81+ collapseExpandedComposerCursor ( value , start ) ,
82+ collapseExpandedComposerCursor ( value , end ) ,
83+ ) ;
84+ expect ( editorRef . current ?. readSelectionRange ( ) ) . toEqual ( { start, end } ) ;
85+ editor . commands . insertContent ( "X" ) ;
86+ } ) ;
87+ expect ( serializeEditorDoc ( editor . state . doc ) . value ) . toBe ( expected ) ;
88+ } ,
89+ ) ;
90+
6091it ( "keeps Up before the opening bold marker in a quote, including while typing" , async ( ) => {
6192 const editor = await mountEditor ( draft ) ;
6293 const position = editor . state . selection . from ;
0 commit comments