11// Media parse tests cover media reference parsing from text and payloads.
2+ import path from "node:path" ;
3+ import { pathToFileURL } from "node:url" ;
24import { describe , expect , it } from "vitest" ;
35import { splitMediaFromOutput } from "./parse.js" ;
46
@@ -88,15 +90,29 @@ describe("splitMediaFromOutput", () => {
8890 String . raw `MEDIA:/path/to/image.png\"}],\"details\":{\"provider\":\"openai\"}` ,
8991 ] ,
9092 [ "/tmp/render,final.png" , "MEDIA:/tmp/render,final.png" ] ,
91- [ "/tmp/generated.png" , "MEDIA:FILE:///tmp/generated.png" ] ,
92- [ "/tmp/generated.png" , "MEDIA:FILE:/tmp/generated.png" ] ,
93- [ "/tmp/generated.png" , "MEDIA:file:///tmp/generated.png" ] ,
94- [ "/Users/pete/My File.png" , "MEDIA:FILE:///Users/pete/My File.png" ] ,
95- [ "/Users/pete/My File.png" , "MEDIA:file:///Users/pete/My File.png" ] ,
9693 ] as const ) ( "accepts supported media path variant: %s" , ( expectedPath , input ) => {
9794 expectAcceptedMediaPathCase ( expectedPath , input ) ;
9895 } ) ;
9996
97+ const nativeFilePath = path . resolve ( "media" , "café 100% image.png" ) ;
98+ const nativeFileUrl = pathToFileURL ( nativeFilePath ) . href ;
99+ it . each ( [
100+ nativeFileUrl ,
101+ nativeFileUrl . replace ( / ^ f i l e : \/ \/ / u, "FILE:" ) ,
102+ nativeFileUrl . replace ( / ^ f i l e : / u, "FILE:" ) ,
103+ nativeFileUrl . replace ( / ^ f i l e : \/ \/ / u, "file://localhost" ) ,
104+ nativeFileUrl . replace ( / % 2 0 / gu, " " ) ,
105+ ] ) ( "preserves file URLs for native media loading: %s" , ( fileUrl ) => {
106+ expectAcceptedMediaPathCase ( fileUrl , `MEDIA:${ fileUrl } ` ) ;
107+ } ) ;
108+
109+ it . each ( [ nativeFileUrl , nativeFilePath ] ) ( "keeps file URL siblings separate from %s" , ( first ) => {
110+ const secondPath = path . resolve ( "media" , "second image.png" ) ;
111+ expectParsedMediaOutputCase ( `MEDIA:${ first } ${ pathToFileURL ( secondPath ) . href } ` , {
112+ mediaUrls : [ first , pathToFileURL ( secondPath ) . href ] ,
113+ } ) ;
114+ } ) ;
115+
100116 it . each ( [
101117 [ "bare image" , "Generated image\nMEDIA:image.png" , [ "image.png" ] ] ,
102118 [ "bare audio" , "Generated audio\nMEDIA:voice.ogg" , [ "voice.ogg" ] ] ,
@@ -156,10 +172,6 @@ describe("splitMediaFromOutput", () => {
156172 "MEDIA:/tmp/project screenshots/shot.png media\\second.png" ,
157173 [ "/tmp/project screenshots/shot.png" , "media\\second.png" ] ,
158174 ] ,
159- [
160- "MEDIA:/tmp/project screenshots/shot.png file:///tmp/second.png" ,
161- [ "/tmp/project screenshots/shot.png" , "/tmp/second.png" ] ,
162- ] ,
163175 [ "MEDIA:C:\\Users\\First Last\\..\\secret.png D:\\safe\\second.png" , [ "D:\\safe\\second.png" ] ] ,
164176 [ "MEDIA:/tmp/project screenshots/../../.env /tmp/safe/second.png" , [ "/tmp/safe/second.png" ] ] ,
165177 ] as const ) ( "keeps separate media items on one directive line: %s" , ( input , mediaUrls ) => {
@@ -174,6 +186,7 @@ describe("splitMediaFromOutput", () => {
174186 "MEDIA:./foo/../../../etc/shadow" ,
175187 "MEDIA:C:\\Users\\First Last\\..\\secret.png" ,
176188 "MEDIA:/tmp/project screenshots/../../.env" ,
189+ "MEDIA:file:///tmp/../secret.png" ,
177190 ] as const ) ( "rejects traversal and unsupported home-dir path: %s" , ( input ) => {
178191 expectRejectedMediaPathCase ( input ) ;
179192 } ) ;
0 commit comments