@@ -6,8 +6,10 @@ import {
66} from "@t3tools/contracts" ;
77import { useState , type ReactNode } from "react" ;
88import { describe , expect , it , vi } from "vite-plus/test" ;
9+ import { userEvent } from "vite-plus/test/browser" ;
910
1011import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection" ;
12+ import "~/index.css" ;
1113import { act , render } from "../../../test/render" ;
1214
1315const mocks = vi . hoisted ( ( ) => ( { observeSource : vi . fn ( ) , dispose : vi . fn ( ) } ) ) ;
@@ -125,3 +127,51 @@ describe("citation comment source disappearance", () => {
125127 expect ( textarea ( ) ) . toBeNull ( ) ;
126128 } ) ;
127129} ) ;
130+
131+ describe ( "citation comment popup position" , ( ) => {
132+ it ( "stays on screen when the cited text ends in a code block's hidden overflow" , async ( ) => {
133+ using _source = setup ( ) ;
134+ const message = document . createElement ( "div" ) ;
135+ message . className = "chat-markdown" ;
136+ message . style . width = "240px" ;
137+ const pre = document . createElement ( "pre" ) ;
138+ const code = document . createElement ( "code" ) ;
139+ // Highlighted code is split into token spans, so the range's last client rect
140+ // covers only the final token, wholly inside the hidden overflow.
141+ for ( let index = 0 ; index < 40 ; index += 1 ) {
142+ const token = document . createElement ( "span" ) ;
143+ token . textContent = `token${ index } ` ;
144+ code . append ( token ) ;
145+ }
146+ pre . append ( code ) ;
147+ message . append ( pre ) ;
148+ document . body . prepend ( message ) ;
149+ using _message = { [ Symbol . dispose ] : ( ) => message . remove ( ) } ;
150+ const range = document . createRange ( ) ;
151+ range . selectNodeContents ( code ) ;
152+ const rects = range . getClientRects ( ) ;
153+ expect ( rects . item ( rects . length - 1 ) ! . left ) . toBeGreaterThan ( window . innerWidth ) ;
154+ const sourceAnchor = {
155+ source : message ,
156+ range,
157+ viewport : document . body ,
158+ } as AssistantCitationSourceAnchor ;
159+
160+ await using _screen = await mount (
161+ sourceAnchor ,
162+ vi . fn ( ( ) => true ) ,
163+ ) ;
164+ const popup = ( ) => document . querySelector < HTMLElement > ( '[data-slot="popover-popup"]' ) ;
165+ await vi . waitFor ( ( ) => expect ( popup ( ) ?. getBoundingClientRect ( ) . width ) . toBeGreaterThan ( 0 ) ) ;
166+ // Typing scrolls ancestors to reveal the caret, which shifts the whole app
167+ // sideways when the box is off screen.
168+ await userEvent . keyboard ( "note" ) ;
169+
170+ expect ( textarea ( ) ?. value ) . toBe ( "note" ) ;
171+ expect ( document . scrollingElement ?. scrollLeft ) . toBe ( 0 ) ;
172+ expect ( document . body . scrollLeft ) . toBe ( 0 ) ;
173+ const rect = popup ( ) ! . getBoundingClientRect ( ) ;
174+ expect ( rect . left ) . toBeGreaterThanOrEqual ( 0 ) ;
175+ expect ( rect . right ) . toBeLessThanOrEqual ( window . innerWidth ) ;
176+ } ) ;
177+ } ) ;
0 commit comments