signal-desktop/ts/test-electron/util/scaleImageToLevel_test.ts
Scott Nonnenberg 99b2bc304e
Use streams to download attachments directly to disk
Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
2023-10-30 09:24:28 -07:00

69 lines
2.1 KiB
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import loadImage from 'blueimp-load-image';
import { IMAGE_JPEG, IMAGE_PNG } from '../../types/MIME';
import { scaleImageToLevel } from '../../util/scaleImageToLevel';
describe('scaleImageToLevel', () => {
// NOTE: These tests are incomplete.
async function getBlob(path: string): Promise<Blob> {
const response = await fetch(path);
return response.blob();
}
it("doesn't scale images that are already small enough", async () => {
const testCases = [
{
path: '../fixtures/kitten-1-64-64.jpg',
contentType: IMAGE_JPEG,
expectedWidth: 64,
expectedHeight: 64,
},
{
path: '../fixtures/20x200-yellow.png',
contentType: IMAGE_PNG,
expectedWidth: 20,
expectedHeight: 200,
},
];
await Promise.all(
testCases.map(
async ({ path, contentType, expectedWidth, expectedHeight }) => {
const blob = await getBlob(path);
const scaled = await scaleImageToLevel(
blob,
contentType,
blob.size,
true
);
const data = await loadImage(scaled.blob, { orientation: true });
const { originalWidth: width, originalHeight: height } = data;
assert.strictEqual(width, expectedWidth);
assert.strictEqual(height, expectedHeight);
assert.strictEqual(scaled.contentType, contentType);
assert.strictEqual(scaled.blob.type, contentType);
}
)
);
});
it('removes EXIF data from small images', async () => {
const original = await getBlob('../fixtures/kitten-2-64-64.jpg');
assert.isDefined(
(await loadImage(original, { meta: true, orientation: true })).exif,
'Test setup failure: expected fixture to have EXIF data'
);
const scaled = await scaleImageToLevel(original, IMAGE_JPEG, original.size);
assert.isUndefined(
(await loadImage(scaled.blob, { meta: true, orientation: true })).exif
);
});
});