Skip to content

Commit

Permalink
chore: add initial node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamsinclair committed Nov 20, 2023
1 parent cf856b5 commit 074d308
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 0 deletions.
Binary file added test/fixtures/test.avif
Binary file not shown.
Binary file added test/fixtures/test.jpeg
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 test/fixtures/test.jxl
Binary file not shown.
Binary file added test/fixtures/test.png
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 test/fixtures/test.webp
Binary file not shown.
26 changes: 26 additions & 0 deletions test/node/avif.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import { imageDataPolyfill, importWasmModule, getFixturesImage } from './utils.js';

import decode, { init as initDecode } from '@jsquash/avif/decode.js';
import encode, { init as initEncode } from '@jsquash/avif/encode.js';

imageDataPolyfill();

test('can successfully decode image', async t => {
const [testImage, decodeWasmModule] = await Promise.all([
getFixturesImage('test.avif'),
importWasmModule('node_modules/@jsquash/avif/codec/dec/avif_dec.wasm'),
]);
initDecode(decodeWasmModule);
const data = await decode(testImage);
t.is(data.width, 50);
t.is(data.height, 50);
t.is(data.data.length, 4 * 50 * 50);
});

test('can successfully encode image', async t => {
const encodeWasmModule = await importWasmModule('node_modules/@jsquash/avif/codec/enc/avif_enc.wasm');
await initEncode(encodeWasmModule);
const data = await encode(new ImageData(new Uint8ClampedArray(4 * 50 * 50), 50, 50));
t.assert(data instanceof ArrayBuffer);
});
26 changes: 26 additions & 0 deletions test/node/jpeg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import { imageDataPolyfill, importWasmModule, getFixturesImage } from './utils.js';

import decode, { init as initDecode } from '@jsquash/jpeg/decode.js';
import encode, { init as initEncode } from '@jsquash/jpeg/encode.js';

imageDataPolyfill();

test('can successfully decode image', async t => {
const [testImage, decodeWasmModule] = await Promise.all([
getFixturesImage('test.jpeg'),
importWasmModule('node_modules/@jsquash/jpeg/codec/dec/mozjpeg_dec.wasm'),
]);
initDecode(decodeWasmModule);
const data = await decode(testImage);
t.is(data.width, 50);
t.is(data.height, 50);
t.is(data.data.length, 4 * 50 * 50);
});

test('can successfully encode image', async t => {
const encodeWasmModule = await importWasmModule('node_modules/@jsquash/jpeg/codec/enc/mozjpeg_enc.wasm');
await initEncode(encodeWasmModule);
const data = await encode(new ImageData(new Uint8ClampedArray(4 * 50 * 50), 50, 50));
t.assert(data instanceof ArrayBuffer);
});
26 changes: 26 additions & 0 deletions test/node/jxl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import { imageDataPolyfill, importWasmModule, getFixturesImage } from './utils.js';

import decode, { init as initDecode } from '@jsquash/jxl/decode.js';
import encode, { init as initEncode } from '@jsquash/jxl/encode.js';

imageDataPolyfill();

test('can successfully decode image', async t => {
const [testImage, decodeWasmModule] = await Promise.all([
getFixturesImage('test.jxl'),
importWasmModule('node_modules/@jsquash/jxl/codec/dec/jxl_dec.wasm'),
]);
initDecode(decodeWasmModule);
const data = await decode(testImage);
t.is(data.width, 50);
t.is(data.height, 50);
t.is(data.data.length, 4 * 50 * 50);
});

test('can successfully encode image', async t => {
const encodeWasmModule = await importWasmModule('node_modules/@jsquash/jxl/codec/enc/jxl_enc.wasm');
await initEncode(encodeWasmModule);
const data = await encode(new ImageData(new Uint8ClampedArray(4 * 50 * 50), 50, 50));
t.assert(data instanceof ArrayBuffer);
});
27 changes: 27 additions & 0 deletions test/node/png.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'ava';
import { imageDataPolyfill, importWasmModule, getFixturesImage } from './utils.js';

import decode, { init as initDecode } from '@jsquash/png/decode.js';
import encode, { init as initEncode } from '@jsquash/png/encode.js';

imageDataPolyfill();

test('can successfully decode image', async t => {
const [testImage, decodeWasmModule] = await Promise.all([
getFixturesImage('test.png'),
importWasmModule('node_modules/@jsquash/png/codec/squoosh_png_bg.wasm'),
]);
initDecode(decodeWasmModule);
const data = await decode(testImage);
t.is(data.width, 50);
t.is(data.height, 50);
t.is(data.data.length, 4 * 50 * 50);
});

test('can successfully encode image', async t => {
const encodeWasmModule = await importWasmModule('node_modules/@jsquash/png/codec/squoosh_png_bg.wasm');
await initEncode(encodeWasmModule);
const data = await encode(new ImageData(new Uint8ClampedArray(4 * 50 * 50), 50, 50));
// @TODO Next breaking change, make PNG encode return an ArrayBuffer to match other packages
t.assert(!(data instanceof ArrayBuffer));
});
24 changes: 24 additions & 0 deletions test/node/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { promises as fs } from 'fs';
import path from 'path';

export function imageDataPolyfill() {
if (typeof ImageData === 'undefined') {
globalThis.ImageData = class ImageData {
constructor(data, width, height) {
this.data = data;
this.width = width;
this.height = height;
}
};
}
}

export async function importWasmModule(path) {
const fileBuffer = await fs.readFile(path);
return WebAssembly.compile(fileBuffer);
}

export function getFixturesImage(imagePath) {
const filePath = path.resolve(`fixtures/${imagePath}`);
return fs.readFile(filePath);
}
26 changes: 26 additions & 0 deletions test/node/webp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import { imageDataPolyfill, importWasmModule, getFixturesImage } from './utils.js';

import decode, { init as initDecode } from '@jsquash/webp/decode.js';
import encode, { init as initEncode } from '@jsquash/webp/encode.js';

imageDataPolyfill();

test('can successfully decode image', async t => {
const [testImage, decodeWasmModule] = await Promise.all([
getFixturesImage('test.webp'),
importWasmModule('node_modules/@jsquash/webp/codec/dec/webp_dec.wasm'),
]);
initDecode(decodeWasmModule);
const data = await decode(testImage);
t.is(data.width, 50);
t.is(data.height, 50);
t.is(data.data.length, 4 * 50 * 50);
});

test('can successfully encode image', async t => {
const encodeWasmModule = await importWasmModule('node_modules/@jsquash/webp/codec/enc/webp_enc.wasm');
await initEncode(encodeWasmModule);
const data = await encode(new ImageData(new Uint8ClampedArray(4 * 50 * 50), 50, 50));
t.assert(data instanceof ArrayBuffer);
});
18 changes: 18 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "integration-tests",
"scripts": {
"test": "ava node/*.test.js"
},
"devDependencies": {
"ava": "^5.3.1",
"@jsquash/avif": "file:../packages/avif/dist",
"@jsquash/jpeg": "file:../packages/jpeg/dist",
"@jsquash/jxl": "file:../packages/jxl/dist",
"@jsquash/oxipng": "file:../packages/oxipng/dist",
"@jsquash/png": "file:../packages/png/dist",
"@jsquash/resize": "file:../packages/resize/dist",
"@jsquash/webp": "file:../packages/webp/dist"
},
"private": true,
"type": "module"
}

0 comments on commit 074d308

Please sign in to comment.