Skip to content

Commit

Permalink
Add gifsicle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilwanner committed Aug 5, 2020
1 parent 9f7f4fb commit b3078b9
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
Binary file added packages/gifsicle/__tests__/images/large.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/gifsicle/__tests__/images/medium.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/gifsicle/__tests__/images/small.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/gifsicle/__tests__/images/tiny.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions packages/gifsicle/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
initTestUtils,
cleanup,
getImageFile,
writeTmpBuffer,
getFileSize,
getImageMetadata,
} from '@wasm-codecs/test-utils';
import gifInfo from 'gif-info';
import encode from '../lib';

describe('gifsicle', () => {
beforeAll(() => {
initTestUtils(__dirname);
});

afterAll(() => cleanup());

// test all image sizes
['tiny', 'small', 'medium', 'large'].forEach((size) => {
it(`encodes a ${size} image`, async () => {
const data = getImageFile(`images/${size}.gif`);
const originalSize = getFileSize(`images/${size}.gif`);
const originalMetadata = await getImageMetadata(`images/${size}.gif`);

const result = await encode(data);
const encodedPath = writeTmpBuffer(result, `${size}.gif`);
const encodedSize = getFileSize(encodedPath);

// expect the image size to be between 0.25 and 0.995 of the original image
expect(encodedSize).toBeLessThan(originalSize * 0.995);
expect(encodedSize).toBeGreaterThan(originalSize * 0.25);

// check if the image is still a valid jpeg image
const metadata = await getImageMetadata(encodedPath);
expect(metadata.format).toBe('gif');
expect(metadata.width).toBe(originalMetadata.width);
expect(metadata.height).toBe(originalMetadata.height);
});
});

it('respects optimization level option', async () => {
const data = getImageFile('images/small.gif');

// with level 1
const resultLevel1 = await encode(data, { optimizationLevel1 });
const pathLevel1 = writeTmpBuffer(resultLevel1, 'small-level1.gif');
const sizeLevel1 = getFileSize(pathLevel1);

// with level 3
const resultLevel3 = await encode(data, { optimizationLevel3 });
const pathLevel3 = writeTmpBuffer(resultLevel3, 'small-level3.gif');
const sizeLevel3 = getFileSize(pathLevel3);

expect(sizeLevel1).toBeGreaterThan(sizeLevel3);
});

it('resizes correctly with both sides given', async () => {
const data = getImageFile('images/medium.gif');
const result = await encode(data, { width20, height30 });
const encodedPath = writeTmpBuffer(result, 'medium-20x30.gif');

const metadata = await getImageMetadata(encodedPath);
expect(metadata.format).toBe('gif');
expect(metadata.width).toBe(20);
expect(metadata.height).toBe(30);
});

it('resizes correctly with only width given', async () => {
const data = getImageFile('images/medium.gif');
const result = await encode(data, { width50 });
const encodedPath = writeTmpBuffer(result, 'medium-50x_.gif');

const metadata = await getImageMetadata(encodedPath);
expect(metadata.format).toBe('gif');
expect(metadata.width).toBe(50);
expect(metadata.height).toBe(28);
});

it('resizes correctly with only height given', async () => {
const data = getImageFile('images/medium.gif');
const result = await encode(data, { height25 });
const encodedPath = writeTmpBuffer(result, 'medium-_x25.gif');

const metadata = await getImageMetadata(encodedPath);
expect(metadata.format).toBe('gif');
expect(metadata.width).toBe(44);
expect(metadata.height).toBe(25);
});

it('respects colors option', async () => {
const data = getImageFile('images/small.gif');
const originalInfo = gifInfo(new Uint8Array(data).buffer);
expect(originalInfo.globalPaletteSize).toBe(256);

const result = await encode(data, { colors: 32 });
writeTmpBuffer(result, `small-32colors.gif`);

// check if new gif has only 32 colors
const encodeInfo = gifInfo(new Uint8Array(result).buffer);
expect(encodeInfo.globalPaletteSize).toBe(32);
});
});
34 changes: 34 additions & 0 deletions packages/gifsicle/__tests__/performance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { initTestUtils, getImageFile, getFileSize, getImageMetadata } from '@wasm-codecs/test-utils';
import encode from '../lib';

describe('gifsicle performance', () => {
beforeAll(() => {
initTestUtils(__dirname);
});

it('encodes many images', async () => {
jest.setTimeout(100000);
process.setMaxListeners(0);

const data = getImageFile('images/medium.gif');
const originalSize = getFileSize('images/medium.gif');

const promises = [];
for (let i = 0; i < 15; i += 1) {
promises.push(encode(data));
}

const results = await Promise.all(promises);
let resultSize = -1;
for (let i = 0; i < 15; i += 1) {
expect(results[i].length).toBeLessThan(originalSize * 0.995);
expect(results[i].length).toBeGreaterThan(originalSize * 0.25);

if (resultSize < 0) {
resultSize = results[i].length;
} else {
expect(resultSize).toBe(results[i].length);
}
}
});
});
6 changes: 6 additions & 0 deletions packages/gifsicle/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/gifsicle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@babel/core": "^7.10.5",
"@wasm-codecs/test-utils": "^1.0.0",
"eslint": "^7.5.0",
"gif-info": "^1.0.1",
"jest": "^26.1.0",
"mkdirp": "^1.0.4",
"rimraf": "^3.0.2",
Expand Down
10 changes: 10 additions & 0 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export const getImage = async (fileName: string): Promise<{ data: Buffer; info:
return sharp(path.resolve(basePath, fileName)).toBuffer({ resolveWithObject: true });
};

/**
* Load image data from a file without sharp
*
* @param {string} fileName Image file name
* @returns {Buffer} Image data
*/
export const getImageFile = (fileName: string): Buffer => {
return fs.readFileSync(path.resolve(basePath, fileName));
};

/**
* Load raw image data from a file
*
Expand Down

0 comments on commit b3078b9

Please sign in to comment.