11import { EnvironmentId , MessageId , ThreadId , type AssistantCitation } from "@t3tools/contracts" ;
22import {
3+ MAX_CITATION_HREF_LENGTH ,
34 formatAssistantCitationHref ,
45 parseAssistantCitationHref ,
56 serializeAssistantCitation ,
@@ -105,11 +106,25 @@ it("keeps an unsaved citation comment when a question takes the composer", async
105106 await expect . element ( page . getByLabelText ( "Edit citation comment" ) ) . toBeInTheDocument ( ) ;
106107} ) ;
107108
108- it . each ( [ "save" , "send" , "question" ] as const ) (
109- "rejects a comment exceeding the complete citation URL budget on %s" ,
110- async ( action ) => {
111- const sourceCitation = { ...citation , messageId : MessageId . make ( "界" . repeat ( 17_750 ) ) } ;
112- const oversizedComment = "界" . repeat ( 100 ) ;
109+ it . each ( [
110+ [ "save" , 100 ] ,
111+ [ "send" , 100 ] ,
112+ [ "question" , 100 ] ,
113+ [ "save" , 0 ] ,
114+ ] as const ) (
115+ "rejects a comment exceeding the citation URL budget on %s (%i characters available)" ,
116+ async ( action , availableSpace ) => {
117+ const source = { ...citation , messageId : MessageId . make ( "界" . repeat ( 17_750 ) ) } ;
118+ const sourceCitation = {
119+ ...source ,
120+ messageId : MessageId . make (
121+ source . messageId +
122+ "m" . repeat (
123+ MAX_CITATION_HREF_LENGTH - formatAssistantCitationHref ( source ) . length - availableSpace ,
124+ ) ,
125+ ) ,
126+ } ;
127+ const oversizedComment = availableSpace === 0 ? "x" : "界" . repeat ( 100 ) ;
113128 const prompt = `See ${ serializeAssistantCitation ( sourceCitation ) } ` ;
114129 expect ( parseAssistantCitationHref ( formatAssistantCitationHref ( sourceCitation ) ) ) . toEqual (
115130 sourceCitation ,
@@ -144,6 +159,12 @@ it.each(["save", "send", "question"] as const)(
144159 const comment = page . getByLabelText ( "Comment on selected text" ) ;
145160 await expect . element ( comment ) . toBeVisible ( ) ;
146161 await comment . fill ( oversizedComment ) ;
162+ await expect . element ( comment ) . toHaveAttribute ( "aria-invalid" , "true" ) ;
163+ const error = page . getByText (
164+ "The citation link is too long. Shorten the comment or quoted text." ,
165+ ) ;
166+ await expect . element ( error ) . toBeVisible ( ) ;
167+ await expect . element ( page . getByRole ( "button" , { name : "Save" , exact : true } ) ) . toBeDisabled ( ) ;
147168
148169 if ( action === "question" ) {
149170 await screen . rerender (
@@ -162,11 +183,15 @@ it.each(["save", "send", "question"] as const)(
162183 } ) ;
163184 if ( action !== "question" ) {
164185 await expect . element ( comment ) . toHaveValue ( oversizedComment ) ;
165- await comment . fill ( "ok" ) ;
186+ const acceptedComment = availableSpace === 0 ? "" : "ok" ;
187+ await comment . fill ( acceptedComment ) ;
188+ await expect . element ( comment ) . not . toHaveAttribute ( "aria-invalid" , "true" ) ;
189+ await expect . element ( error ) . not . toBeInTheDocument ( ) ;
190+ await expect . element ( page . getByRole ( "button" , { name : "Save" , exact : true } ) ) . toBeEnabled ( ) ;
166191 await userEvent . keyboard ( action === "send" ? "{Control>}{Enter}{/Control}" : "{Enter}" ) ;
167192 await vi . waitFor ( ( ) => {
168193 expect ( screen . container . querySelector ( "output" ) ?. dataset . prompt ) . toBe (
169- `See ${ serializeAssistantCitation ( withAssistantCitationComment ( sourceCitation , "ok" ) ) } ` ,
194+ `See ${ serializeAssistantCitation ( withAssistantCitationComment ( sourceCitation , acceptedComment ) ) } ` ,
170195 ) ;
171196 expect ( onSend ) . toHaveBeenCalledTimes ( action === "send" ? 1 : 0 ) ;
172197 } ) ;
0 commit comments