@@ -124,28 +124,35 @@ describe("composer file attachments", () => {
124124 width : 4032 ,
125125 height : 3024 ,
126126 } ;
127+ type Size = { width : number ; height : number } ;
127128 const native = {
128- size : { width : 1 , height : 1 } ,
129- resizes : [ ] as Array < { width ?: number | null ; height ?: number | null } > ,
129+ size : { width : 1 , height : 1 } as Size ,
130+ resizes : [ ] as Size [ ] ,
130131 saved : rendered as { uri : string ; base64 ?: string } ,
132+ encode : null as ( ( size : Size , format : string ) => { uri : string ; base64 ?: string } ) | null ,
131133 } ;
132134
133135 beforeEach ( ( ) => {
134136 native . size = { width : 1 , height : 1 } ;
135137 native . resizes = [ ] ;
136138 native . saved = rendered ;
139+ native . encode = null ;
137140 mocks . manipulate . mockReset ( ) ;
138141 mocks . release . mockReset ( ) ;
139- mocks . manipulate . mockImplementation ( ( ) => {
142+ mocks . manipulate . mockImplementation ( ( source : string | Size ) => {
143+ let size = typeof source === "string" ? native . size : source ;
140144 const context = {
141- resize ( size : { width ?: number | null ; height ?: number | null } ) {
142- native . resizes . push ( size ) ;
145+ resize ( next : Size ) {
146+ native . resizes . push ( next ) ;
147+ size = next ;
143148 return context ;
144149 } ,
145150 renderAsync : async ( ) => ( {
146- ...native . size ,
151+ width : size . width ,
152+ height : size . height ,
147153 release : mocks . release ,
148- saveAsync : async ( ) => native . saved ,
154+ saveAsync : async ( options : { format : string } ) =>
155+ native . encode ?.( size , options . format ) ?? native . saved ,
149156 } ) ,
150157 } ;
151158 return context ;
@@ -210,6 +217,45 @@ describe("composer file attachments", () => {
210217 ] ) ;
211218 } ) ;
212219
220+ it ( "shrinks a fitted PNG further when re-encoding it exceeds the image limit" , async ( ) => {
221+ const header = Buffer . alloc ( 24 ) ;
222+ Buffer . from ( [ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a ] ) . copy ( header ) ;
223+ header . write ( "IHDR" , 12 ) ;
224+ header . writeUInt32BE ( 1668 , 16 ) ;
225+ header . writeUInt32BE ( 2388 , 20 ) ;
226+ native . size = { width : 1668 , height : 2388 } ;
227+ // Native PNG encoding of the 2000px resample is larger than the 7 MB original.
228+ native . encode = ( size , format ) => ( {
229+ uri : `file:///cache/fitted-${ size . width } x${ size . height } .${ format } ` ,
230+ base64 : Buffer . alloc ( size . width * size . height * 4 ) . toString ( "base64" ) ,
231+ } ) ;
232+ mocks . size . mockReturnValue ( 7 * 1024 * 1024 ) ;
233+ mocks . readBase64 . mockResolvedValue ( header . toString ( "base64" ) ) ;
234+ mocks . pickMedia . mockResolvedValue ( {
235+ canceled : false ,
236+ assets : [ { ...photo , fileName : "IMG_9280.png" , mimeType : "image/png" } ] ,
237+ } ) ;
238+
239+ const result = await pickComposerImages ( { existingCount : 0 } ) ;
240+
241+ expect ( result . error ) . toBeNull ( ) ;
242+ const [ image ] = result . images ;
243+ const last = native . resizes . at ( - 1 ) ! ;
244+ expect ( native . resizes [ 0 ] ) . toEqual ( { width : 1397 , height : 2000 } ) ;
245+ expect ( Math . max ( last . width , last . height ) ) . toBeLessThan ( 2000 ) ;
246+ expect ( image ) . toEqual (
247+ expect . objectContaining ( {
248+ name : "IMG_9280.png" ,
249+ mimeType : "image/png" ,
250+ sizeBytes : last . width * last . height * 4 ,
251+ previewUri : `file:///cache/fitted-${ last . width } x${ last . height } .png` ,
252+ } ) ,
253+ ) ;
254+ expect ( image ! . sizeBytes ) . toBeLessThanOrEqual ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) ;
255+ expect ( mocks . delete ) . toHaveBeenCalledWith ( "file:///cache/fitted-1397x2000.png" ) ;
256+ expect ( mocks . delete ) . not . toHaveBeenCalledWith ( image ! . previewUri ) ;
257+ } ) ;
258+
213259 it ( "measures the picker file instead of trusting the reported size" , async ( ) => {
214260 mocks . pickMedia . mockResolvedValue ( {
215261 canceled : false ,
0 commit comments