55 type AuthSessionState ,
66} from "@t3tools/contracts" ;
77import { AsyncResult } from "effect/reactivity" ;
8+ import { mediaFileReference } from "@t3tools/client-runtime/media-reference" ;
89import { createElement , isValidElement , type ReactNode } from "react" ;
910import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
1011
@@ -24,6 +25,7 @@ const state = vi.hoisted(() => ({
2425 showMenu : vi . fn ( ) ,
2526 openFile : vi . fn ( ) ,
2627 clipboard : vi . fn ( ) ,
28+ copyText : vi . fn ( ) ,
2729 menuFinished : null as ( ( ) => void ) | null ,
2830} ) ) ;
2931
@@ -33,7 +35,7 @@ vi.mock("react", async (importOriginal) => ({
3335 useRef : < A , > ( current : A ) => ( { current } ) ,
3436 useState : < A , > ( initial : A ) => [ initial , ( ) => { } ] ,
3537} ) ) ;
36- vi . mock ( "../../hooks/useCopyToClipboard" , ( ) => ( { writeTextToClipboard : vi . fn ( ) } ) ) ;
38+ vi . mock ( "../../hooks/useCopyToClipboard" , ( ) => ( { writeTextToClipboard : state . copyText } ) ) ;
3739vi . mock ( "../../localApi" , ( ) => ( {
3840 readLocalApi : ( ) => ( { contextMenu : { show : state . showMenu } } ) ,
3941} ) ) ;
@@ -128,6 +130,7 @@ beforeEach(() => {
128130 state . png . mockReset ( ) . mockResolvedValue ( new Blob ( [ "png" ] , { type : "image/png" } ) ) ;
129131 state . showMenu . mockReset ( ) . mockResolvedValue ( null ) ;
130132 state . openFile . mockReset ( ) ;
133+ state . copyText . mockReset ( ) . mockResolvedValue ( true ) ;
131134 state . menuFinished = null ;
132135 class TestClipboardItem {
133136 constructor ( readonly items : Record < string , Promise < Blob > > ) { }
@@ -141,6 +144,72 @@ beforeEach(() => {
141144
142145afterEach ( ( ) => vi . unstubAllGlobals ( ) ) ;
143146
147+ it . each ( [
148+ [
149+ "image" ,
150+ "C:/repo/assets/shot.png" ,
151+ "C:/repo" ,
152+ String . raw `C:\repo\assets\shot.png` ,
153+ String . raw `assets\shot.png` ,
154+ ] ,
155+ [
156+ "video" ,
157+ "C:/repo/assets/clip with spaces.mp4" ,
158+ "C:/repo" ,
159+ String . raw `C:\repo\assets\clip with spaces.mp4` ,
160+ String . raw `assets\clip with spaces.mp4` ,
161+ ] ,
162+ [
163+ "image" ,
164+ String . raw `\\server\share\repo/assets/shot.png` ,
165+ String . raw `\\server\share\repo` ,
166+ String . raw `\\server\share\repo\assets\shot.png` ,
167+ String . raw `assets\shot.png` ,
168+ ] ,
169+ [
170+ "video" ,
171+ String . raw `\\server\share\repo/assets/clip.mp4` ,
172+ String . raw `\\server\share\repo` ,
173+ String . raw `\\server\share\repo\assets\clip.mp4` ,
174+ String . raw `assets\clip.mp4` ,
175+ ] ,
176+ [ "image" , "/repo/assets/shot.png" , "/repo" , "/repo/assets/shot.png" , "assets/shot.png" ] ,
177+ [ "video" , "/repo/assets/clip.mp4" , "/repo" , "/repo/assets/clip.mp4" , "assets/clip.mp4" ] ,
178+ ] as const ) (
179+ "copies full and relative paths for %s %s" ,
180+ async ( kind , path , root , full , relative ) => {
181+ const source : MediaActionSource = {
182+ ...hostSource ( ) ,
183+ kind,
184+ reference : mediaFileReference ( path , root ) ,
185+ } ;
186+ for ( const [ action , expected ] of [
187+ [ "copy-full-path" , full ] ,
188+ [ "copy-relative-path" , relative ] ,
189+ ] as const ) {
190+ const completed = deferred < void > ( ) ;
191+ state . menuFinished = ( ) => completed . resolve ( ) ;
192+ state . showMenu . mockResolvedValue ( action ) ;
193+ openMenu ( source ) ;
194+ await completed . promise ;
195+ expect ( state . copyText ) . toHaveBeenLastCalledWith ( expected , "file path" ) ;
196+ }
197+ expect ( state . mint ) . not . toHaveBeenCalled ( ) ;
198+ expect ( state . download ) . not . toHaveBeenCalled ( ) ;
199+ } ,
200+ ) ;
201+
202+ it . each ( [ "image" , "video" ] as const ) ( "preserves the %s URL when copying" , async ( kind ) => {
203+ const url = `https://cdn.test/assets/${ kind === "image" ? "shot.png" : "clip.mp4" } ?path=C:/repo/media` ;
204+ const completed = deferred < void > ( ) ;
205+ state . menuFinished = ( ) => completed . resolve ( ) ;
206+ state . showMenu . mockResolvedValue ( "copy-url" ) ;
207+ openMenu ( { ...hostSource ( ) , kind, src : url , reference : { kind : "url" , url } } ) ;
208+ await completed . promise ;
209+ expect ( state . copyText ) . toHaveBeenCalledWith ( url , "URL" ) ;
210+ expect ( state . mint ) . not . toHaveBeenCalled ( ) ;
211+ } ) ;
212+
144213it . each ( [ "workspace-file" , "media-file" ] as const ) (
145214 "waits for the %s grant before enabling host menu actions" ,
146215 ( _tag ) => {
0 commit comments