@@ -121,6 +121,15 @@ vi.mock("./uuid", () => ({
121121
122122import { convertPastedImagesToAttachments , persistComposerAttachmentFile } from "./composerImages" ;
123123
124+ function oversizedPng ( ) : string {
125+ const png = Buffer . alloc ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1 ) ;
126+ Buffer . from ( [ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a ] ) . copy ( png ) ;
127+ png . write ( "IHDR" , 12 ) ;
128+ png . writeUInt32BE ( 2000 , 16 ) ;
129+ png . writeUInt32BE ( 2000 , 20 ) ;
130+ return png . toString ( "base64" ) ;
131+ }
132+
124133describe ( "native pasted image cleanup" , ( ) => {
125134 beforeEach ( ( ) => {
126135 files . clear ( ) ;
@@ -131,7 +140,10 @@ describe("native pasted image cleanup", () => {
131140 const uri = "content://media/external/images/media/42" ;
132141 files . set ( uri , { base64 : "aGVsbG8=" , deleted : false , mimeType : "image/jpeg" } ) ;
133142
134- const attachments = await convertPastedImagesToAttachments ( { uris : [ uri ] , existingCount : 0 } ) ;
143+ const { attachments } = await convertPastedImagesToAttachments ( {
144+ uris : [ uri ] ,
145+ existingCount : 0 ,
146+ } ) ;
135147
136148 expect ( attachments ) . toEqual ( [
137149 expect . objectContaining ( {
@@ -149,7 +161,10 @@ describe("native pasted image cleanup", () => {
149161 files . set ( uri , { base64 : "b3JpZ2luYWw=" , deleted : false , mimeType : "image/heic" } ) ;
150162 manipulator . state . sourceSize = { width : 100 , height : 100 } ;
151163
152- const attachments = await convertPastedImagesToAttachments ( { uris : [ uri ] , existingCount : 0 } ) ;
164+ const { attachments } = await convertPastedImagesToAttachments ( {
165+ uris : [ uri ] ,
166+ existingCount : 0 ,
167+ } ) ;
153168
154169 expect ( attachments ) . toEqual ( [
155170 expect . objectContaining ( {
@@ -171,7 +186,10 @@ describe("native pasted image cleanup", () => {
171186 Math . ceil ( ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1 ) / 3 ) * 4 ,
172187 ) ;
173188
174- const attachments = await convertPastedImagesToAttachments ( { uris : [ uri ] , existingCount : 0 } ) ;
189+ const { attachments } = await convertPastedImagesToAttachments ( {
190+ uris : [ uri ] ,
191+ existingCount : 0 ,
192+ } ) ;
175193
176194 expect ( attachments ) . toEqual ( [ ] ) ;
177195 expect ( files . get ( "file:///cache/rendered.jpeg" ) ?. deleted ) . toBe ( true ) ;
@@ -183,7 +201,7 @@ describe("native pasted image cleanup", () => {
183201 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png" ;
184202 files . set ( uri , { base64 : "aGVsbG8=" , deleted : false } ) ;
185203
186- const attachments = await convertPastedImagesToAttachments ( {
204+ const { attachments } = await convertPastedImagesToAttachments ( {
187205 uris : [ uri ] ,
188206 existingCount : 0 ,
189207 } ) ;
@@ -216,6 +234,53 @@ describe("native pasted image cleanup", () => {
216234 expect ( files . get ( overflow ) ?. deleted ) . toBe ( true ) ;
217235 expect ( files . get ( userOwned ) ?. deleted ) . toBe ( false ) ;
218236 } ) ;
237+
238+ it ( "re-renders an over-limit paste whose dimensions already fit, like the photo picker" , async ( ) => {
239+ const uri =
240+ "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/photo.png" ;
241+ files . set ( uri , { base64 : oversizedPng ( ) , deleted : false } ) ;
242+ manipulator . state . sourceSize = { width : 2000 , height : 2000 } ;
243+
244+ const { attachments } = await convertPastedImagesToAttachments ( {
245+ uris : [ uri ] ,
246+ existingCount : 0 ,
247+ } ) ;
248+
249+ expect ( attachments ) . toHaveLength ( 1 ) ;
250+ expect ( attachments [ 0 ] ?. sizeBytes ) . toBeLessThanOrEqual ( PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) ;
251+ expect ( files . get ( "file:///cache/rendered.jpeg" ) ?. deleted ) . toBe ( true ) ;
252+ } ) ;
253+
254+ it ( "keeps the readable images of a paste and still reports the one it dropped" , async ( ) => {
255+ const gone =
256+ "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/gone.png" ;
257+ const kept =
258+ "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/kept.png" ;
259+ files . set ( kept , { base64 : "aGVsbG8=" , deleted : false } ) ;
260+
261+ const result = await convertPastedImagesToAttachments ( { uris : [ gone , kept ] , existingCount : 0 } ) ;
262+
263+ expect ( result . attachments ) . toEqual ( [
264+ expect . objectContaining ( { dataUrl : "data:image/png;base64,aGVsbG8=" } ) ,
265+ ] ) ;
266+ expect ( result . error ) . toBe ( "Could not read a pasted image." ) ;
267+ } ) ;
268+
269+ it ( "reports the size limit when even the JPEG re-render is too large" , async ( ) => {
270+ const uri =
271+ "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/huge.png" ;
272+ files . set ( uri , { base64 : oversizedPng ( ) , deleted : false } ) ;
273+ manipulator . state . sourceSize = { width : 2000 , height : 2000 } ;
274+ manipulator . state . renderedBase64 = oversizedPng ( ) ;
275+
276+ const result = await convertPastedImagesToAttachments ( { uris : [ uri ] , existingCount : 0 } ) ;
277+
278+ expect ( result ) . toEqual ( {
279+ attachments : [ ] ,
280+ error : "A pasted image exceeds the 10 MB attachment limit." ,
281+ } ) ;
282+ expect ( files . get ( uri ) ?. deleted ) . toBe ( true ) ;
283+ } ) ;
219284} ) ;
220285
221286describe ( "persistComposerAttachmentFile" , ( ) => {
0 commit comments