@@ -2,11 +2,7 @@ import { expect, test } from 'vitest';
22import fc from 'fast-check' ;
33import { encode as encodeJpeg } from 'jpeg-js' ;
44import { PNG } from './png.ts' ;
5- import {
6- decodeScreenshotImage ,
7- detectScreenshotImageFormat ,
8- transcodeScreenshotToPng ,
9- } from './screenshot-image.ts' ;
5+ import { decodeScreenshotImage , detectScreenshotImageFormat } from './screenshot-image.ts' ;
106
117function solidRgba ( width : number , height : number , rgba : readonly [ number , number , number , number ] ) {
128 const data = Buffer . alloc ( width * height * 4 ) ;
@@ -24,32 +20,9 @@ function thrownBy(run: () => unknown): unknown {
2420 throw new Error ( 'expected the call to throw' ) ;
2521}
2622
27- test ( 'a JPEG screenshot becomes a PNG of the same size with the same picture' , ( ) => {
28- const jpeg = encodeJpeg ( { width : 6 , height : 4 , data : solidRgba ( 6 , 4 , [ 200 , 30 , 30 , 255 ] ) } , 100 ) ;
29- expect ( detectScreenshotImageFormat ( jpeg . data ) ) . toBe ( 'jpeg' ) ;
30-
31- const png = transcodeScreenshotToPng ( jpeg . data , 'test screenshot' ) ;
32-
33- expect ( detectScreenshotImageFormat ( png ) ) . toBe ( 'png' ) ;
34- const decoded = PNG . sync . read ( png ) ;
35- expect ( [ decoded . width , decoded . height ] ) . toEqual ( [ 6 , 4 ] ) ;
36- const [ r = - 1 , g = - 1 , b = - 1 , a = - 1 ] = decoded . data . subarray ( 0 , 4 ) ;
37- // JPEG is lossy; a flat field survives within a few levels per channel.
38- expect ( Math . abs ( r - 200 ) ) . toBeLessThanOrEqual ( 4 ) ;
39- expect ( Math . abs ( g - 30 ) ) . toBeLessThanOrEqual ( 4 ) ;
40- expect ( Math . abs ( b - 30 ) ) . toBeLessThanOrEqual ( 4 ) ;
41- expect ( a ) . toBe ( 255 ) ;
42- } ) ;
43-
44- test ( 'a PNG screenshot passes through byte for byte' , ( ) => {
45- const png = PNG . sync . write ( new PNG ( { width : 3 , height : 2 } ) ) ;
46-
47- expect ( transcodeScreenshotToPng ( png , 'test screenshot' ) ) . toBe ( png ) ;
48- } ) ;
49-
5023test ( 'bytes that are neither container are refused with a typed error carrying the label' , ( ) => {
5124 expect (
52- thrownBy ( ( ) => transcodeScreenshotToPng ( Buffer . from ( 'GIF89a' ) , 'Limrun iOS screenshot' ) ) ,
25+ thrownBy ( ( ) => decodeScreenshotImage ( Buffer . from ( 'GIF89a' ) , 'Limrun iOS screenshot' ) ) ,
5326 ) . toMatchObject ( {
5427 code : 'COMMAND_FAILED' ,
5528 message : 'Limrun iOS screenshot is neither PNG nor JPEG' ,
@@ -61,7 +34,7 @@ test('a valid SOI marker followed by garbage is refused with the typed decode er
6134 const garbage = Buffer . concat ( [ Buffer . from ( [ 0xff , 0xd8 , 0xff , 0xe0 ] ) , Buffer . alloc ( 32 , 0x41 ) ] ) ;
6235 expect ( detectScreenshotImageFormat ( garbage ) ) . toBe ( 'jpeg' ) ;
6336
64- expect ( thrownBy ( ( ) => transcodeScreenshotToPng ( garbage , 'Limrun iOS screenshot' ) ) ) . toMatchObject ( {
37+ expect ( thrownBy ( ( ) => decodeScreenshotImage ( garbage , 'Limrun iOS screenshot' ) ) ) . toMatchObject ( {
6538 code : 'COMMAND_FAILED' ,
6639 message : 'Failed to decode Limrun iOS screenshot as JPEG' ,
6740 details : { label : 'Limrun iOS screenshot' , reason : expect . any ( String ) } ,
@@ -72,9 +45,7 @@ test('a truncated JPEG body is refused with the same typed decode error', () =>
7245 const jpeg = encodeJpeg ( { width : 6 , height : 4 , data : solidRgba ( 6 , 4 , [ 10 , 200 , 30 , 255 ] ) } , 100 ) ;
7346 const truncated = jpeg . data . subarray ( 0 , 24 ) ;
7447
75- expect (
76- thrownBy ( ( ) => transcodeScreenshotToPng ( truncated , 'Limrun iOS screenshot' ) ) ,
77- ) . toMatchObject ( {
48+ expect ( thrownBy ( ( ) => decodeScreenshotImage ( truncated , 'Limrun iOS screenshot' ) ) ) . toMatchObject ( {
7849 code : 'COMMAND_FAILED' ,
7950 message : 'Failed to decode Limrun iOS screenshot as JPEG' ,
8051 } ) ;
0 commit comments