Image/packages/binding/__test__/transformer.spec.mjs
2024-02-19 22:45:21 +08:00

71 lines
2.3 KiB
JavaScript

import { promises as fs } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import test from 'ava'
import { decode } from 'blurhash'
import { Transformer } from '../index.js'
const __DIRNAME = join(fileURLToPath(import.meta.url), '..')
const ROOT_DIR = join(__DIRNAME, '..', '..', '..')
const PNG = await fs.readFile(join(ROOT_DIR, 'un-optimized.png'))
const JPEG = await fs.readFile(join(ROOT_DIR, 'un-optimized.jpg'))
const WITH_EXIF_JPG = await fs.readFile(join(ROOT_DIR, 'with-exif.jpg'))
const SVG = await fs.readFile(join(ROOT_DIR, 'input-debian.svg'))
test('should be able to get metadata from png', async (t) => {
const decoder = new Transformer(PNG)
const metadata = await decoder.metadata()
t.is(metadata.width, 1024)
t.is(metadata.height, 681)
})
test('should be able to get metadata from jpg', async (t) => {
const decoder = new Transformer(JPEG)
const metadata = await decoder.metadata()
t.is(metadata.width, 1024)
t.is(metadata.height, 678)
})
test('should be able to get exif from jpg', async (t) => {
const decoder = new Transformer(WITH_EXIF_JPG)
const metadata = await decoder.metadata(true)
t.snapshot(metadata)
t.is(metadata.orientation, 5)
t.is(metadata.format, 'jpeg')
})
test('should be able to encode into webp', async (t) => {
const decoder = new Transformer(PNG)
await t.notThrowsAsync(() => decoder.webp(75))
})
test('should be able to decode from avif', async (t) => {
if (process.env.NAPI_RS_FORCE_WASI) {
t.pass()
return
}
const decoder = new Transformer(PNG)
const AVIF = await decoder.avif()
const avifDecoder = new Transformer(AVIF)
await t.notThrowsAsync(() => avifDecoder.png())
})
test('should be able to decode from webp', async (t) => {
const decoder = new Transformer(PNG)
const WEBP = await decoder.webp()
const webpDecoder = new Transformer(WEBP)
await t.notThrowsAsync(() => webpDecoder.png())
})
test('should be able to create transformer from raw rgba pixels', async (t) => {
const pixels = decode('LEHV6nWB2yk8pyo0adR*.7kCMdnj', 32, 32)
await t.notThrowsAsync(() => Transformer.fromRgbaPixels(pixels, 32, 32).webp())
})
test('should be able to create transformer from SVG', async (t) => {
await t.notThrowsAsync(() => Transformer.fromSvg(SVG).jpeg())
})