@@ -16,6 +16,7 @@ import {
1616import {
1717 PromptInput ,
1818 PromptInputActionAddAttachments ,
19+ PromptInputActionAddScreenshot ,
1920 PromptInputActionMenu ,
2021 PromptInputActionMenuContent ,
2122 PromptInputActionMenuItem ,
@@ -159,6 +160,84 @@ const setupPromptInputTests = () => {
159160 } ) as unknown as typeof FileReader ;
160161} ;
161162
163+ const setupScreenshotCaptureMock = ( ) => {
164+ if ( ! navigator . mediaDevices ) {
165+ Object . defineProperty ( navigator , "mediaDevices" , {
166+ configurable : true ,
167+ value : { } ,
168+ writable : true ,
169+ } ) ;
170+ }
171+
172+ if ( ! ( "getDisplayMedia" in navigator . mediaDevices ) ) {
173+ Object . defineProperty ( navigator . mediaDevices , "getDisplayMedia" , {
174+ configurable : true ,
175+ value : vi . fn ( ) ,
176+ writable : true ,
177+ } ) ;
178+ }
179+
180+ const stopTrack = vi . fn ( ) ;
181+ const stream = {
182+ getTracks : ( ) => [ { stop : stopTrack } ] ,
183+ } as unknown as MediaStream ;
184+
185+ const getDisplayMedia = vi
186+ . spyOn ( navigator . mediaDevices , "getDisplayMedia" )
187+ . mockResolvedValue ( stream ) ;
188+
189+ const originalCreateElement = document . createElement . bind ( document ) ;
190+ const video = originalCreateElement ( "video" ) ;
191+ const canvas = originalCreateElement ( "canvas" ) ;
192+ const drawImage = vi . fn ( ) ;
193+ const play = vi . spyOn ( video , "play" ) . mockResolvedValue ( ) ;
194+ const pause = vi . spyOn ( video , "pause" ) . mockImplementation ( vi . fn ( ) ) ;
195+
196+ Object . defineProperty ( video , "videoWidth" , {
197+ configurable : true ,
198+ value : 1280 ,
199+ } ) ;
200+ Object . defineProperty ( video , "videoHeight" , {
201+ configurable : true ,
202+ value : 720 ,
203+ } ) ;
204+
205+ let srcObject : unknown = null ;
206+ Object . defineProperty ( video , "srcObject" , {
207+ configurable : true ,
208+ get : ( ) => srcObject ,
209+ set : ( value ) => {
210+ srcObject = value ;
211+ if ( value ) {
212+ setTimeout ( ( ) => {
213+ video . onloadedmetadata ?.( new Event ( "loadedmetadata" ) ) ;
214+ } , 0 ) ;
215+ }
216+ } ,
217+ } ) ;
218+
219+ vi . spyOn ( canvas , "getContext" ) . mockReturnValue ( {
220+ drawImage,
221+ } as unknown as CanvasRenderingContext2D ) ;
222+ // oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-callbacks)
223+ const toBlob = vi . spyOn ( canvas , "toBlob" ) . mockImplementation ( ( callback ) => {
224+ // oxlint-disable-next-line eslint-plugin-promise(prefer-await-to-callbacks)
225+ callback ?.( new Blob ( [ "mock-screenshot" ] , { type : "image/png" } ) ) ;
226+ } ) ;
227+
228+ vi . spyOn ( document , "createElement" ) . mockImplementation ( ( ( tagName : string ) => {
229+ if ( tagName === "video" ) {
230+ return video ;
231+ }
232+ if ( tagName === "canvas" ) {
233+ return canvas ;
234+ }
235+ return originalCreateElement ( tagName as keyof HTMLElementTagNameMap ) ;
236+ } ) as typeof document . createElement ) ;
237+
238+ return { getDisplayMedia, pause, play, stopTrack, toBlob } ;
239+ } ;
240+
162241describe ( "promptInput" , ( ) => {
163242 it ( "renders form" , ( ) => {
164243 setupPromptInputTests ( ) ;
@@ -2712,6 +2791,86 @@ describe("promptInputActionAddAttachments", () => {
27122791 } ) ;
27132792} ) ;
27142793
2794+ describe ( "promptInputActionAddScreenshot" , ( ) => {
2795+ it ( "captures a screenshot and adds it as attachment" , async ( ) => {
2796+ setupPromptInputTests ( ) ;
2797+ const onSubmit = vi . fn ( ) ;
2798+ const user = userEvent . setup ( ) ;
2799+ const { getDisplayMedia, pause, play, stopTrack, toBlob } =
2800+ setupScreenshotCaptureMock ( ) ;
2801+
2802+ render (
2803+ < PromptInput onSubmit = { onSubmit } >
2804+ < PromptInputBody >
2805+ < PromptInputAttachments >
2806+ { ( attachment ) => (
2807+ < div key = { attachment . id } > { attachment . filename } </ div >
2808+ ) }
2809+ </ PromptInputAttachments >
2810+ < PromptInputActionMenu >
2811+ < PromptInputActionMenuTrigger />
2812+ < PromptInputActionMenuContent >
2813+ < PromptInputActionAddScreenshot />
2814+ </ PromptInputActionMenuContent >
2815+ </ PromptInputActionMenu >
2816+ < PromptInputTextarea />
2817+ </ PromptInputBody >
2818+ </ PromptInput >
2819+ ) ;
2820+
2821+ await user . click ( screen . getByRole ( "button" ) ) ;
2822+ await user . click ( screen . getByText ( "Take screenshot" ) ) ;
2823+
2824+ await vi . waitFor ( ( ) => {
2825+ expect ( screen . getByText ( / ^ s c r e e n s h o t - .* \. p n g $ / ) ) . toBeInTheDocument ( ) ;
2826+ } ) ;
2827+ expect ( getDisplayMedia ) . toHaveBeenCalledOnce ( ) ;
2828+ expect ( play ) . toHaveBeenCalledOnce ( ) ;
2829+ expect ( toBlob ) . toHaveBeenCalledOnce ( ) ;
2830+ expect ( stopTrack ) . toHaveBeenCalledOnce ( ) ;
2831+ expect ( pause ) . toHaveBeenCalledOnce ( ) ;
2832+ } ) ;
2833+
2834+ it ( "ignores denied capture permission" , async ( ) => {
2835+ setupPromptInputTests ( ) ;
2836+ const onSubmit = vi . fn ( ) ;
2837+ const user = userEvent . setup ( ) ;
2838+ const consoleError = vi . spyOn ( console , "error" ) . mockImplementation ( vi . fn ( ) ) ;
2839+ const { getDisplayMedia } = setupScreenshotCaptureMock ( ) ;
2840+ getDisplayMedia . mockRejectedValue (
2841+ new DOMException ( "Permission denied" , "NotAllowedError" )
2842+ ) ;
2843+
2844+ render (
2845+ < PromptInput onSubmit = { onSubmit } >
2846+ < PromptInputBody >
2847+ < PromptInputAttachments >
2848+ { ( attachment ) => (
2849+ < div key = { attachment . id } > { attachment . filename } </ div >
2850+ ) }
2851+ </ PromptInputAttachments >
2852+ < PromptInputActionMenu >
2853+ < PromptInputActionMenuTrigger />
2854+ < PromptInputActionMenuContent >
2855+ < PromptInputActionAddScreenshot />
2856+ </ PromptInputActionMenuContent >
2857+ </ PromptInputActionMenu >
2858+ < PromptInputTextarea />
2859+ </ PromptInputBody >
2860+ </ PromptInput >
2861+ ) ;
2862+
2863+ await user . click ( screen . getByRole ( "button" ) ) ;
2864+ await user . click ( screen . getByText ( "Take screenshot" ) ) ;
2865+
2866+ await vi . waitFor ( ( ) => {
2867+ expect ( getDisplayMedia ) . toHaveBeenCalledOnce ( ) ;
2868+ } ) ;
2869+ expect ( screen . queryByText ( / ^ s c r e e n s h o t - .* \. p n g $ / ) ) . not . toBeInTheDocument ( ) ;
2870+ expect ( consoleError ) . not . toHaveBeenCalled ( ) ;
2871+ } ) ;
2872+ } ) ;
2873+
27152874describe ( "promptInputHeader" , ( ) => {
27162875 it ( "renders header content" , async ( ) => {
27172876 setupPromptInputTests ( ) ;
0 commit comments