44 ORCHESTRATION_V2_WS_METHODS ,
55 OrchestrationDispatchCommandError ,
66 ThreadId ,
7+ WS_METHODS ,
78} from "@t3tools/contracts" ;
89import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings" ;
910import type * as Context from "effect/Context" ;
@@ -28,6 +29,33 @@ import {
2829} from "../../test/fakeEnvironment" ;
2930import { render } from "../../test/render" ;
3031
32+ const PNG = Uint8Array . from (
33+ atob (
34+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" ,
35+ ) ,
36+ ( char ) => char . charCodeAt ( 0 ) ,
37+ ) ;
38+
39+ /** Accepts every attachment upload without sending its bytes, until disposed. */
40+ function acceptAttachmentUploads ( ) : Disposable {
41+ const mint = answerRpc ( WS_METHODS . attachmentsCreateUploadUrl , ( ) =>
42+ Effect . succeed ( { attachmentId : "upload-1" , relativeUrl : "/upload" , expiresAt : 0 } ) ,
43+ ) ;
44+ const status = vi . spyOn ( XMLHttpRequest . prototype , "status" , "get" ) . mockReturnValue ( 200 ) ;
45+ const send = vi . spyOn ( XMLHttpRequest . prototype , "send" ) . mockImplementation ( function (
46+ this : XMLHttpRequest ,
47+ ) {
48+ queueMicrotask ( ( ) => this . dispatchEvent ( new ProgressEvent ( "load" ) ) ) ;
49+ } ) ;
50+ return {
51+ [ Symbol . dispose ] : ( ) => {
52+ send . mockRestore ( ) ;
53+ status . mockRestore ( ) ;
54+ mint [ Symbol . dispose ] ( ) ;
55+ } ,
56+ } ;
57+ }
58+
3159// The socket is the only fake layer; see test/appScenarios.renders.tsx.
3260vi . mock ( "../../../../packages/client-runtime/src/connection/driver.ts" , async ( importOriginal ) => {
3361 const [ actual , { fakeDriver } , Layer ] = await Promise . all ( [
@@ -80,14 +108,22 @@ it.each([
80108 new OrchestrationDispatchCommandError ( { message : "Provider is unavailable." } ) ,
81109 ) ,
82110 banner : "Provider is unavailable." ,
111+ failedText : "first message that will fail" ,
83112 } ,
84113 // A dropped connection interrupts the request and shows no thread error.
85- { failure : "interrupted" , end : Effect . interrupt , banner : null } ,
114+ {
115+ failure : "interrupted" ,
116+ end : Effect . interrupt ,
117+ banner : null ,
118+ failedText : "first message that will fail" ,
119+ } ,
120+ // An image alone leaves no text to copy, but the failure still needs a toast.
121+ { failure : "interrupted image" , end : Effect . interrupt , banner : null , failedText : "" } ,
86122] ) (
87123 "a failed send ($failure) drops its message from the timeline and keeps the draft typed meanwhile" ,
88- async ( { failure, end, banner } ) => {
124+ async ( { failure, end, banner, failedText } ) => {
89125 // Drafts persist per thread, so each case gets its own.
90- const thread = threadWithHistory ( `thread-${ failure } -send` , 1 ) ;
126+ const thread = threadWithHistory ( `thread-${ failure . replace ( " " , "-" ) } -send` , 1 ) ;
91127 using _environment = installFakeEnvironment ( [ thread ] ) ;
92128 __setClientSettingsForTests ( {
93129 ...DEFAULT_CLIENT_SETTINGS ,
@@ -109,30 +145,45 @@ it.each([
109145 router : false ,
110146 } ) ;
111147 using write = vi . spyOn ( navigator . clipboard , "writeText" ) . mockResolvedValue ( undefined ) ;
112- // Only timeline rows, so the composer and sidebar cannot match the text.
148+ using _uploads = failedText ? null : acceptAttachmentUploads ( ) ;
149+ // Only timeline rows, not the composer or sidebar.
113150 const sentMessages = ( ) =>
114151 Array . from ( document . querySelectorAll ( '[data-message-role="user"]' ) , ( row ) => row . textContent ) ;
115- const failedText = "first message that will fail" ;
152+ const onlyHistory = [ expect . stringContaining ( ` ${ thread . id } : question 0` ) ] ;
116153 const editor = page . getByTestId ( "composer-editor" ) ;
117154 await expect . element ( editor ) . toBeVisible ( ) ;
155+ await vi . waitFor ( ( ) => expect ( sentMessages ( ) ) . toEqual ( onlyHistory ) ) ;
118156
119157 await userEvent . click ( editor ) ;
120- await userEvent . keyboard ( `${ failedText } {Enter}` ) ;
121- await vi . waitFor ( ( ) =>
122- expect ( sentMessages ( ) ) . toContainEqual ( expect . stringContaining ( failedText ) ) ,
123- ) ;
158+ if ( failedText ) {
159+ await userEvent . keyboard ( failedText ) ;
160+ } else {
161+ const clipboardData = new DataTransfer ( ) ;
162+ clipboardData . items . add ( new File ( [ PNG ] , "shot.png" , { type : "image/png" } ) ) ;
163+ editor
164+ . element ( )
165+ . dispatchEvent (
166+ new ClipboardEvent ( "paste" , { clipboardData, bubbles : true , cancelable : true } ) ,
167+ ) ;
168+ await expect . element ( page . getByRole ( "img" , { name : "shot.png" } ) ) . toBeVisible ( ) ;
169+ }
170+ await userEvent . keyboard ( "{Enter}" ) ;
171+ await vi . waitFor ( ( ) => expect ( sentMessages ( ) ) . toHaveLength ( 2 ) ) ;
124172 await userEvent . keyboard ( "next draft" ) ;
125173 await expect . element ( editor ) . toHaveTextContent ( "next draft" ) ;
126174 failDispatch ( ) ;
127175
128- await vi . waitFor ( ( ) =>
129- expect ( sentMessages ( ) ) . not . toContainEqual ( expect . stringContaining ( failedText ) ) ,
130- ) ;
176+ await vi . waitFor ( ( ) => expect ( sentMessages ( ) ) . toEqual ( onlyHistory ) ) ;
131177 await expect . element ( page . getByText ( "Message not sent" ) ) . toBeVisible ( ) ;
132178 if ( banner ) await expect . element ( page . getByText ( banner ) ) . toBeVisible ( ) ;
133179 await expect . element ( editor ) . toHaveTextContent ( "next draft" ) ;
134180 // By text, since each timeline row has an icon button labelled the same.
135- await page . getByText ( "Copy message" , { exact : true } ) . click ( ) ;
181+ const copyAction = page . getByText ( "Copy message" , { exact : true } ) ;
182+ if ( ! failedText ) {
183+ expect ( copyAction . query ( ) ) . toBeNull ( ) ;
184+ return ;
185+ }
186+ await copyAction . click ( ) ;
136187 expect ( write ) . toHaveBeenCalledWith ( failedText ) ;
137188 } ,
138189) ;
0 commit comments