@@ -1416,13 +1416,15 @@ async function waitForButtonByText(text: string): Promise<HTMLButtonElement> {
14161416 return waitForElement ( ( ) => findButtonByText ( text ) , `Unable to find "${ text } " button.` ) ;
14171417}
14181418
1419- function findButtonContainingText ( text : string ) : HTMLButtonElement | null {
1420- return ( Array . from ( document . querySelectorAll ( "button" ) ) . find ( ( button ) =>
1421- button . textContent ?. includes ( text ) ,
1422- ) ?? null ) as HTMLButtonElement | null ;
1419+ function findButtonContainingText ( text : string ) : HTMLElement | null {
1420+ return (
1421+ Array . from ( document . querySelectorAll < HTMLElement > ( 'button, [role="button"]' ) ) . find ( ( button ) =>
1422+ button . textContent ?. includes ( text ) ,
1423+ ) ?? null
1424+ ) ;
14231425}
14241426
1425- async function waitForButtonContainingText ( text : string ) : Promise < HTMLButtonElement > {
1427+ async function waitForButtonContainingText ( text : string ) : Promise < HTMLElement > {
14261428 return waitForElement (
14271429 ( ) => findButtonContainingText ( text ) ,
14281430 `Unable to find button containing "${ text } ".` ,
@@ -3219,6 +3221,89 @@ describe("ChatView timeline estimator parity (full app)", () => {
32193221 }
32203222 } ) ;
32213223
3224+ it ( "focuses the composer and inserts printable text typed from the page background" , async ( ) => {
3225+ const mounted = await mountChatView ( {
3226+ viewport : DEFAULT_VIEWPORT ,
3227+ snapshot : createSnapshotForTargetUser ( {
3228+ targetMessageId : "msg-user-target-type-to-focus" as MessageId ,
3229+ targetText : "type-to-focus target" ,
3230+ } ) ,
3231+ } ) ;
3232+
3233+ const backgroundTarget = document . createElement ( "div" ) ;
3234+ backgroundTarget . tabIndex = - 1 ;
3235+ document . body . append ( backgroundTarget ) ;
3236+
3237+ try {
3238+ const composerEditor = await waitForComposerEditor ( ) ;
3239+ backgroundTarget . focus ( ) ;
3240+ expect ( document . activeElement ) . not . toBe ( composerEditor ) ;
3241+
3242+ const event = new KeyboardEvent ( "keydown" , {
3243+ key : "h" ,
3244+ bubbles : true ,
3245+ cancelable : true ,
3246+ } ) ;
3247+ backgroundTarget . dispatchEvent ( event ) ;
3248+
3249+ await waitForComposerText ( "h" ) ;
3250+ expect ( event . defaultPrevented ) . toBe ( true ) ;
3251+ expect ( document . activeElement ) . toBe ( composerEditor ) ;
3252+
3253+ window . dispatchEvent (
3254+ new KeyboardEvent ( "keydown" , {
3255+ key : "i" ,
3256+ bubbles : true ,
3257+ cancelable : true ,
3258+ } ) ,
3259+ ) ;
3260+
3261+ await waitForComposerText ( "hi" ) ;
3262+ } finally {
3263+ backgroundTarget . remove ( ) ;
3264+ await mounted . cleanup ( ) ;
3265+ }
3266+ } ) ;
3267+
3268+ it ( "does not steal printable keys from editable targets or shortcut modifiers" , async ( ) => {
3269+ const mounted = await mountChatView ( {
3270+ viewport : DEFAULT_VIEWPORT ,
3271+ snapshot : createSnapshotForTargetUser ( {
3272+ targetMessageId : "msg-user-target-type-to-focus-guards" as MessageId ,
3273+ targetText : "type-to-focus guards target" ,
3274+ } ) ,
3275+ } ) ;
3276+ const input = document . createElement ( "input" ) ;
3277+ document . body . append ( input ) ;
3278+
3279+ try {
3280+ input . focus ( ) ;
3281+ input . dispatchEvent (
3282+ new KeyboardEvent ( "keydown" , {
3283+ key : "x" ,
3284+ bubbles : true ,
3285+ cancelable : true ,
3286+ } ) ,
3287+ ) ;
3288+ await waitForLayout ( ) ;
3289+ expect ( useComposerDraftStore . getState ( ) . draftsByThreadKey [ THREAD_KEY ] ?. prompt ?? "" ) . toBe ( "" ) ;
3290+
3291+ window . dispatchEvent (
3292+ new KeyboardEvent ( "keydown" , {
3293+ key : "k" ,
3294+ metaKey : true ,
3295+ bubbles : true ,
3296+ cancelable : true ,
3297+ } ) ,
3298+ ) ;
3299+ await waitForLayout ( ) ;
3300+ expect ( useComposerDraftStore . getState ( ) . draftsByThreadKey [ THREAD_KEY ] ?. prompt ?? "" ) . toBe ( "" ) ;
3301+ } finally {
3302+ input . remove ( ) ;
3303+ await mounted . cleanup ( ) ;
3304+ }
3305+ } ) ;
3306+
32223307 it ( "uses the active draft route session when changing the base branch" , async ( ) => {
32233308 const staleDraftId = draftIdFromPath ( "/draft/draft-stale-branch-session" ) ;
32243309 const activeDraftId = draftIdFromPath ( "/draft/draft-active-branch-session" ) ;
0 commit comments