@@ -169,6 +169,48 @@ describe("Image", () => {
169169 } ) ,
170170 )
171171
172+ describe ( "OpenAI-style url results" , ( ) => {
173+ const generate = ( data : unknown ) =>
174+ Image . generate ( {
175+ model : OpenAI . configure ( { apiKey : "test" , baseURL : "https://openai.test/v1" } ) . image ( "dall-e-3" ) ,
176+ prompt : "A lighthouse" ,
177+ } ) . pipe ( Effect . provide ( layer ( ( input ) => Effect . succeed ( json ( input , { data } ) ) ) ) )
178+
179+ it . effect ( "keeps an http url as a URL asset" , ( ) =>
180+ Effect . gen ( function * ( ) {
181+ const response = yield * generate ( [ { url : "https://cdn.image.test/result.png" } ] )
182+ expect ( response . image . source ) . toEqual ( { type : "url" , url : "https://cdn.image.test/result.png" } )
183+ } ) ,
184+ )
185+
186+ it . effect ( "decodes a data url inline with its media type" , ( ) =>
187+ Effect . gen ( function * ( ) {
188+ const response = yield * generate ( [ { url : "data:image/png;base64,iVBORw==" } ] )
189+ expect ( response . image . source ) . toEqual ( { type : "base64" , data : "iVBORw==" , mediaType : "image/png" } )
190+ expect ( response . image . mediaType ) . toBe ( "image/png" )
191+ } ) ,
192+ )
193+
194+ it . effect ( "prefers b64_json when url is null" , ( ) =>
195+ Effect . gen ( function * ( ) {
196+ const response = yield * generate ( [ { b64_json : "/9j/4AAQ" , url : null } ] )
197+ expect ( response . image . source ) . toEqual ( {
198+ type : "bytes" ,
199+ data : Uint8Array . from ( [ 0xff , 0xd8 , 0xff , 0xe0 , 0x00 , 0x10 ] ) ,
200+ mediaType : "image/jpeg" ,
201+ } )
202+ } ) ,
203+ )
204+
205+ it . effect ( "rejects an item with neither b64_json nor url" , ( ) =>
206+ Effect . gen ( function * ( ) {
207+ const error = yield * generate ( [ { } ] ) . pipe ( Effect . flip )
208+ expect ( error . reason . _tag ) . toBe ( "InvalidProviderOutput" )
209+ expect ( error . message ) . toContain ( "has neither image data nor a URL" )
210+ } ) ,
211+ )
212+ } )
213+
172214 it . effect ( "classifies an OpenAI stream moderation error event as a content policy rejection" , ( ) =>
173215 Effect . gen ( function * ( ) {
174216 const error = yield * Image . stream ( {
0 commit comments