From 1664bc60eb7aa3fa3792b6acff50f9bbabd3d877 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Feb 2021 10:19:09 +0100 Subject: Optimize remote image processing --- server/tests/helpers/image.ts | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 server/tests/helpers/image.ts (limited to 'server/tests/helpers/image.ts') diff --git a/server/tests/helpers/image.ts b/server/tests/helpers/image.ts new file mode 100644 index 000000000..54911697f --- /dev/null +++ b/server/tests/helpers/image.ts @@ -0,0 +1,59 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import { readFile, remove } from 'fs-extra' +import { join } from 'path' +import { processImage } from '../../../server/helpers/image-utils' +import { buildAbsoluteFixturePath, root } from '../../../shared/extra-utils' +import { expect } from 'chai' + +async function checkBuffers (path1: string, path2: string, equals: boolean) { + const [ buf1, buf2 ] = await Promise.all([ + readFile(path1), + readFile(path2) + ]) + + if (equals) { + expect(buf1.equals(buf2)).to.be.true + } else { + expect(buf1.equals(buf2)).to.be.false + } +} + +describe('Image helpers', function () { + const imageDestDir = join(root(), 'test-images') + const imageDest = join(imageDestDir, 'test.jpg') + const thumbnailSize = { width: 223, height: 122 } + + it('Should skip processing if the source image is okay', async function () { + const input = buildAbsoluteFixturePath('thumbnail.jpg') + await processImage(input, imageDest, thumbnailSize, true) + + await checkBuffers(input, imageDest, true) + }) + + it('Should not skip processing if the source image does not have the appropriate extension', async function () { + const input = buildAbsoluteFixturePath('thumbnail.png') + await processImage(input, imageDest, thumbnailSize, true) + + await checkBuffers(input, imageDest, false) + }) + + it('Should not skip processing if the source image does not have the appropriate size', async function () { + const input = buildAbsoluteFixturePath('preview.jpg') + await processImage(input, imageDest, thumbnailSize, true) + + await checkBuffers(input, imageDest, false) + }) + + it('Should not skip processing if the source image does not have the appropriate size', async function () { + const input = buildAbsoluteFixturePath('thumbnail-big.jpg') + await processImage(input, imageDest, thumbnailSize, true) + + await checkBuffers(input, imageDest, false) + }) + + after(async function () { + await remove(imageDest) + }) +}) -- cgit v1.2.3