Image/packages/binding/__test__/transformer.spec.mjs
2022-04-19 16:23:07 +08:00

48 lines
1.5 KiB
JavaScript

import { promises as fs } from 'fs'
import { join } from 'path'
import { fileURLToPath } from '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'))
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, 1052)
t.is(metadata.height, 744)
})
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 create transformer from raw rgba pixels', async (t) => {
const pixels = decode('LEHV6nWB2yk8pyo0adR*.7kCMdnj', 32, 32)
await t.notThrowsAsync(() => Transformer.fromRgbaPixels(pixels, 32, 32).webpLossless())
})