diff --git a/test/fixtures/test.avif b/test/fixtures/test.avif new file mode 100644 index 0000000..b0e6691 Binary files /dev/null and b/test/fixtures/test.avif differ diff --git a/test/fixtures/test.jpeg b/test/fixtures/test.jpeg new file mode 100644 index 0000000..f9b7e07 Binary files /dev/null and b/test/fixtures/test.jpeg differ diff --git a/test/fixtures/test.jxl b/test/fixtures/test.jxl new file mode 100644 index 0000000..0d7f469 Binary files /dev/null and b/test/fixtures/test.jxl differ diff --git a/test/fixtures/test.png b/test/fixtures/test.png new file mode 100644 index 0000000..ce3dcf2 Binary files /dev/null and b/test/fixtures/test.png differ diff --git a/test/fixtures/test.webp b/test/fixtures/test.webp new file mode 100644 index 0000000..eed315c Binary files /dev/null and b/test/fixtures/test.webp differ diff --git a/test/node/avif.test.js b/test/node/avif.test.js new file mode 100644 index 0000000..338faad --- /dev/null +++ b/test/node/avif.test.js @@ -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); +}); diff --git a/test/node/jpeg.test.js b/test/node/jpeg.test.js new file mode 100644 index 0000000..2a703f5 --- /dev/null +++ b/test/node/jpeg.test.js @@ -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); +}); diff --git a/test/node/jxl.test.js b/test/node/jxl.test.js new file mode 100644 index 0000000..79e77e6 --- /dev/null +++ b/test/node/jxl.test.js @@ -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); +}); diff --git a/test/node/png.test.js b/test/node/png.test.js new file mode 100644 index 0000000..9f8e873 --- /dev/null +++ b/test/node/png.test.js @@ -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)); +}); diff --git a/test/node/utils.js b/test/node/utils.js new file mode 100644 index 0000000..d7e3dbb --- /dev/null +++ b/test/node/utils.js @@ -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); +} diff --git a/test/node/webp.test.js b/test/node/webp.test.js new file mode 100644 index 0000000..437160b --- /dev/null +++ b/test/node/webp.test.js @@ -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); +}); diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..7639ddf --- /dev/null +++ b/test/package.json @@ -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" +}