|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { encode as encodeJpeg } from 'jpeg-js'; |
| 3 | +import { PNG } from './png.ts'; |
| 4 | +import { detectScreenshotImageFormat, transcodeScreenshotToPng } from './png-transcode.ts'; |
| 5 | + |
| 6 | +function solidRgba(width: number, height: number, rgba: readonly [number, number, number, number]) { |
| 7 | + const data = Buffer.alloc(width * height * 4); |
| 8 | + for (let offset = 0; offset < data.length; offset += 4) data.set(rgba, offset); |
| 9 | + return data; |
| 10 | +} |
| 11 | + |
| 12 | +/** The value a synchronous call throws, so one assertion can check its code and message together. */ |
| 13 | +function thrownBy(run: () => unknown): unknown { |
| 14 | + try { |
| 15 | + run(); |
| 16 | + } catch (error) { |
| 17 | + return error; |
| 18 | + } |
| 19 | + throw new Error('expected the call to throw'); |
| 20 | +} |
| 21 | + |
| 22 | +test('a JPEG screenshot becomes a PNG of the same size with the same picture', () => { |
| 23 | + const jpeg = encodeJpeg({ width: 6, height: 4, data: solidRgba(6, 4, [200, 30, 30, 255]) }, 100); |
| 24 | + expect(detectScreenshotImageFormat(jpeg.data)).toBe('jpeg'); |
| 25 | + |
| 26 | + const png = transcodeScreenshotToPng(jpeg.data, 'test screenshot'); |
| 27 | + |
| 28 | + expect(detectScreenshotImageFormat(png)).toBe('png'); |
| 29 | + const decoded = PNG.sync.read(png); |
| 30 | + expect([decoded.width, decoded.height]).toEqual([6, 4]); |
| 31 | + const [r = -1, g = -1, b = -1, a = -1] = decoded.data.subarray(0, 4); |
| 32 | + // JPEG is lossy; a flat field survives within a few levels per channel. |
| 33 | + expect(Math.abs(r - 200)).toBeLessThanOrEqual(4); |
| 34 | + expect(Math.abs(g - 30)).toBeLessThanOrEqual(4); |
| 35 | + expect(Math.abs(b - 30)).toBeLessThanOrEqual(4); |
| 36 | + expect(a).toBe(255); |
| 37 | +}); |
| 38 | + |
| 39 | +test('a PNG screenshot passes through byte for byte', () => { |
| 40 | + const png = PNG.sync.write(new PNG({ width: 3, height: 2 })); |
| 41 | + |
| 42 | + expect(transcodeScreenshotToPng(png, 'test screenshot')).toBe(png); |
| 43 | +}); |
| 44 | + |
| 45 | +test('bytes that are neither container are refused with a typed error carrying the label', () => { |
| 46 | + expect( |
| 47 | + thrownBy(() => transcodeScreenshotToPng(Buffer.from('GIF89a'), 'Limrun iOS screenshot')), |
| 48 | + ).toMatchObject({ |
| 49 | + code: 'COMMAND_FAILED', |
| 50 | + message: 'Limrun iOS screenshot is neither PNG nor JPEG', |
| 51 | + details: { label: 'Limrun iOS screenshot', leadingBytes: '47494638' }, |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('a valid SOI marker followed by garbage is refused with the typed decode error, not the decoder exception', () => { |
| 56 | + const garbage = Buffer.concat([Buffer.from([0xff, 0xd8, 0xff, 0xe0]), Buffer.alloc(32, 0x41)]); |
| 57 | + expect(detectScreenshotImageFormat(garbage)).toBe('jpeg'); |
| 58 | + |
| 59 | + expect(thrownBy(() => transcodeScreenshotToPng(garbage, 'Limrun iOS screenshot'))).toMatchObject({ |
| 60 | + code: 'COMMAND_FAILED', |
| 61 | + message: 'Failed to decode Limrun iOS screenshot as JPEG', |
| 62 | + details: { label: 'Limrun iOS screenshot', reason: expect.any(String) }, |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +test('a truncated JPEG body is refused with the same typed decode error', () => { |
| 67 | + const jpeg = encodeJpeg({ width: 6, height: 4, data: solidRgba(6, 4, [10, 200, 30, 255]) }, 100); |
| 68 | + const truncated = jpeg.data.subarray(0, 24); |
| 69 | + |
| 70 | + expect( |
| 71 | + thrownBy(() => transcodeScreenshotToPng(truncated, 'Limrun iOS screenshot')), |
| 72 | + ).toMatchObject({ |
| 73 | + code: 'COMMAND_FAILED', |
| 74 | + message: 'Failed to decode Limrun iOS screenshot as JPEG', |
| 75 | + }); |
| 76 | +}); |
0 commit comments