11import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
2+ import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts" ;
23import type { ImagePickerAsset } from "expo-image-picker" ;
34
45const mocks = vi . hoisted ( ( ) => ( {
@@ -9,6 +10,7 @@ const mocks = vi.hoisted(() => ({
910 delete : vi . fn ( ) ,
1011 open : vi . fn ( ) ,
1112 size : vi . fn ( ) ,
13+ readBase64 : vi . fn ( ) ,
1214} ) ) ;
1315
1416vi . mock ( "expo-file-system" , ( ) => {
@@ -55,6 +57,10 @@ vi.mock("expo-file-system", () => {
5557 mocks . copy ( this . uri , destination . uri ) ;
5658 }
5759
60+ async base64 ( ) : Promise < string > {
61+ return mocks . readBase64 ( this . uri ) ;
62+ }
63+
5864 delete ( ) : void {
5965 mocks . delete ( this . uri ) ;
6066 }
@@ -95,9 +101,115 @@ describe("composer file attachments", () => {
95101 mocks . delete . mockReset ( ) ;
96102 mocks . open . mockReset ( ) ;
97103 mocks . size . mockReset ( ) ;
104+ mocks . readBase64 . mockReset ( ) ;
98105 mocks . size . mockImplementation ( ( uri : string ) => ( uri . startsWith ( "content:" ) ? null : 42 ) ) ;
99106 } ) ;
100107
108+ describe ( "photo library image conversion" , ( ) => {
109+ const jpeg = "/9j/2Q==" ;
110+ const photo : ImagePickerAsset = {
111+ uri : "file:///picker/photo.heic" ,
112+ type : "image" ,
113+ fileName : "photo.HEIC" ,
114+ mimeType : "image/heic" ,
115+ fileSize : 20 * 1024 * 1024 ,
116+ base64 : jpeg ,
117+ width : 1 ,
118+ height : 1 ,
119+ } ;
120+
121+ it . each ( [ "image/heic" , "image/heif" , undefined ] ) (
122+ "attaches the native JPEG conversion with matching metadata when the source MIME is %s" ,
123+ async ( mimeType ) => {
124+ mocks . pickMedia . mockResolvedValue ( {
125+ canceled : false ,
126+ assets : [ { ...photo , mimeType } ] ,
127+ } ) ;
128+
129+ const result = await pickComposerImages ( { existingCount : 0 } ) ;
130+
131+ expect ( result ) . toEqual ( {
132+ images : [
133+ {
134+ id : "attachment-id" ,
135+ type : "image" ,
136+ name : "photo.jpg" ,
137+ mimeType : "image/jpeg" ,
138+ sizeBytes : 4 ,
139+ dataUrl : `data:image/jpeg;base64,${ jpeg } ` ,
140+ previewUri : `data:image/jpeg;base64,${ jpeg } ` ,
141+ } ,
142+ ] ,
143+ error : null ,
144+ } ) ;
145+ } ,
146+ ) ;
147+
148+ it . each ( [
149+ { extension : "png" , mimeType : "image/png" , base64 : "iVBORw0KGgo=" } ,
150+ { extension : "gif" , mimeType : "image/gif" , base64 : "R0lGODlh" } ,
151+ { extension : "webp" , mimeType : "image/webp" , base64 : "UklGRgQAAABXRUJQ" } ,
152+ ] ) ( "preserves original $extension bytes instead of the picker's JPEG" , async ( original ) => {
153+ const name = `photo.${ original . extension } ` ;
154+ mocks . pickMedia . mockResolvedValue ( {
155+ canceled : false ,
156+ assets : [ { ...photo , fileName : name , mimeType : original . mimeType } ] ,
157+ } ) ;
158+ mocks . readBase64 . mockResolvedValue ( original . base64 ) ;
159+
160+ const result = await pickComposerImages ( { existingCount : 0 } ) ;
161+
162+ expect ( result . error ) . toBeNull ( ) ;
163+ expect ( result . images ) . toEqual ( [
164+ expect . objectContaining ( {
165+ name,
166+ mimeType : original . mimeType ,
167+ dataUrl : `data:${ original . mimeType } ;base64,${ original . base64 } ` ,
168+ sizeBytes : Buffer . from ( original . base64 , "base64" ) . byteLength ,
169+ } ) ,
170+ ] ) ;
171+ } ) ;
172+
173+ it ( "checks the converted JPEG size even when the HEIC source was smaller" , async ( ) => {
174+ const oversized =
175+ jpeg . slice ( 0 , 4 ) + "A" . repeat ( Math . ceil ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3 ) * 4 ) ;
176+ mocks . pickMedia . mockResolvedValue ( {
177+ canceled : false ,
178+ assets : [ { ...photo , fileSize : 42 , base64 : oversized } ] ,
179+ } ) ;
180+
181+ await expect ( pickComposerImages ( { existingCount : 0 } ) ) . resolves . toEqual ( {
182+ images : [ ] ,
183+ error : "'photo.HEIC' exceeds the 10 MB attachment limit." ,
184+ } ) ;
185+ } ) ;
186+
187+ it ( "does not relabel unconverted HEIC bytes as JPEG" , async ( ) => {
188+ mocks . pickMedia . mockResolvedValue ( {
189+ canceled : false ,
190+ assets : [ { ...photo , base64 : "AAAAGGZ0eXBoZWlj" } ] ,
191+ } ) ;
192+
193+ const result = await pickComposerImages ( { existingCount : 0 } ) ;
194+
195+ expect ( result . images ) . toEqual ( [ ] ) ;
196+ expect ( result . error ) . toContain ( "not a supported image type" ) ;
197+ } ) ;
198+
199+ it ( "retains a converted photo when another original cannot be read" , async ( ) => {
200+ mocks . pickMedia . mockResolvedValue ( {
201+ canceled : false ,
202+ assets : [ { ...photo , fileName : "missing.gif" , mimeType : "image/gif" } , photo ] ,
203+ } ) ;
204+ mocks . readBase64 . mockRejectedValue ( new Error ( "missing file" ) ) ;
205+
206+ const result = await pickComposerImages ( { existingCount : 0 } ) ;
207+
208+ expect ( result . images ) . toEqual ( [ expect . objectContaining ( { name : "photo.jpg" } ) ] ) ;
209+ expect ( result . error ) . toBe ( "Failed to read 'missing.gif'." ) ;
210+ } ) ;
211+ } ) ;
212+
101213 describe ( "photo library videos" , ( ) => {
102214 const image : ImagePickerAsset = {
103215 uri : "file:///picker/photo.png" ,
0 commit comments