@@ -11,15 +11,20 @@ function png(width: number, height: number, alpha: number, colorType: 0 | 6 = 6)
1111 return PNG . sync . write ( image , { colorType } ) ;
1212}
1313
14+ function fitted ( mimeType : string , bytes : Uint8Array ) {
15+ const result = fitClaudeImage ( mimeType , bytes ) ;
16+ if ( ! result ) throw new Error ( "expected a fitted image" ) ;
17+ return Buffer . from ( result ) ;
18+ }
19+
1420describe ( "fitClaudeImage" , ( ) => {
1521 it ( "leaves an image within 2000px untouched" , ( ) => {
1622 const bytes = png ( 2000 , 1200 , 255 ) ;
1723 expect ( fitClaudeImage ( "image/png" , bytes ) ) . toBe ( bytes ) ;
1824 } ) ;
1925
2026 it ( "scales a tall phone screenshot to 2000px and keeps its transparency" , ( ) => {
21- const fitted = fitClaudeImage ( "image/png" , png ( 1080 , 2424 , 128 ) ) ;
22- const decoded = PNG . sync . read ( Buffer . from ( fitted ) ) ;
27+ const decoded = PNG . sync . read ( fitted ( "image/png" , png ( 1080 , 2424 , 128 ) ) ) ;
2328 expect ( { width : decoded . width , height : decoded . height } ) . toEqual ( { width : 891 , height : 2000 } ) ;
2429 expect ( decoded . data [ 3 ] ) . toBe ( 128 ) ;
2530 } ) ;
@@ -30,21 +35,21 @@ describe("fitClaudeImage", () => {
3035 for ( let offset = 0 ; offset < image . data . length ; offset += 4 ) {
3136 image . data . fill ( ( offset / 4 ) % 2 === 0 ? 255 : 0 , offset , offset + 4 ) ;
3237 }
33- const decoded = PNG . sync . read ( Buffer . from ( fitClaudeImage ( "image/png" , PNG . sync . write ( image ) ) ) ) ;
38+ const decoded = PNG . sync . read ( fitted ( "image/png" , PNG . sync . write ( image ) ) ) ;
3439 expect ( [ ...decoded . data . subarray ( 0 , 4 ) ] ) . toEqual ( [ 255 , 255 , 255 , 128 ] ) ;
3540 } ) ;
3641
37- it ( "sends an image too large to decode safely as is " , ( ) => {
42+ it ( "refuses a PNG too large to decode safely" , ( ) => {
3843 const header = Buffer . alloc ( 24 ) ;
3944 header . set ( [ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a , 0 , 0 , 0 , 13 ] , 0 ) ;
4045 header . write ( "IHDR" , 12 , "ascii" ) ;
4146 header . writeUInt32BE ( 20_000 , 16 ) ;
4247 header . writeUInt32BE ( 20_000 , 20 ) ;
43- expect ( fitClaudeImage ( "image/png" , header ) ) . toBe ( header ) ;
48+ expect ( fitClaudeImage ( "image/png" , header ) ) . toBeNull ( ) ;
4449 } ) ;
4550
4651 it ( "keeps a grayscale PNG grayscale" , ( ) => {
47- const decoded = PNG . sync . read ( Buffer . from ( fitClaudeImage ( "image/png" , png ( 2400 , 100 , 90 , 0 ) ) ) ) ;
52+ const decoded = PNG . sync . read ( fitted ( "image/png" , png ( 2400 , 100 , 90 , 0 ) ) ) ;
4853 expect ( { width : decoded . width , colorType : decoded . colorType } ) . toEqual ( {
4954 width : 2000 ,
5055 colorType : 0 ,
@@ -67,7 +72,7 @@ describe("fitClaudeImage", () => {
6772 90 ,
6873 ) ;
6974 // Orientation 6 swaps the reported sides.
70- expect ( readImageDimensions ( fitClaudeImage ( "image/jpeg" , data ) ) ) . toEqual ( {
75+ expect ( readImageDimensions ( fitted ( "image/jpeg" , data ) ) ) . toEqual ( {
7176 width : 667 ,
7277 height : 2000 ,
7378 } ) ;
0 commit comments