Files
smartjimp/test/test.node.ts
T

66 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartjimp from '../ts/index.js';
import * as smartfile from '@push.rocks/smartfile';
const smartFileFactory = smartfile.SmartFileFactory.nodeFs();
const sourceImageUrl = 'https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80';
let testSmartJimp: smartjimp.SmartJimp;
tap.test('first test', async () => {
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
});
tap.test('should be able to create a master', async () => {
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
const convertedAsset = await testSmartJimp.getFromSmartfile(smartfileInstance, { width: 500 });
await smartFileFactory.fromBuffer('.nogit/result.avif', convertedAsset).write();
});
tap.test('should be able to use jimp', async () => {
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'jimp' });
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
const convertedAsset = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
format: 'png',
});
await smartFileFactory.fromBuffer('.nogit/result2.png', convertedAsset).write();
});
tap.test('should create progressive JPEG with Sharp', async () => {
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
// Create progressive JPEG
const progressiveJpeg = await testSmartJimp.getFromSmartfile(smartfileInstance, {
format: 'jpeg',
progressive: true,
width: 800,
});
expect(progressiveJpeg).toBeInstanceOf(Buffer);
await smartFileFactory.fromBuffer('.nogit/progressive.jpg', progressiveJpeg).write();
});
tap.test('should convert PNG to progressive JPEG', async () => {
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp' });
// First create a PNG
const smartfileInstance = await smartFileFactory.fromUrl(sourceImageUrl);
const pngBuffer = await testSmartJimp.computeAssetVariation(smartfileInstance.contentBuffer, {
format: 'png',
width: 500,
});
// Then convert it to progressive JPEG
const progressiveJpeg = await testSmartJimp.computeAssetVariation(pngBuffer, {
format: 'jpeg',
progressive: true,
});
expect(progressiveJpeg).toBeInstanceOf(Buffer);
await smartFileFactory.fromBuffer('.nogit/png-to-progressive.jpg', progressiveJpeg).write();
});
export default tap.start();