Files
overleaf-cep/services/filestore/test/unit/js/ImageOptimiser.test.js
Andrew Rumble 90cf4b6a0a Merge pull request #29841 from overleaf/ar-convert-filestore-to-esm
[filestore] convert to ES modules

GitOrigin-RevId: 404905973548bb6e437fff66b368e87be8249b73
2025-12-05 09:05:35 +00:00

79 lines
2.0 KiB
JavaScript

import sinon from 'sinon'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import Errors from '../../../app/js/Errors.js'
const { FailedCommandError } = Errors
const modulePath = '../../../app/js/ImageOptimiser.js'
describe('ImageOptimiser', function () {
let ImageOptimiser, SafeExec
const sourcePath = '/wombat/potato.eps'
beforeEach(async function () {
SafeExec = {
promises: sinon.stub().resolves(),
}
vi.doMock('../../../app/js/SafeExec', () => ({
default: SafeExec,
}))
vi.doMock('@overleaf/metrics', () => ({
default: {
Timer: sinon.stub().returns({ done: sinon.stub() }),
},
}))
ImageOptimiser = (await import(modulePath)).default
})
describe('compressPng', function () {
it('should convert the file', async function () {
await new Promise(resolve => {
ImageOptimiser.compressPng(sourcePath, err => {
expect(err).not.to.exist
expect(SafeExec.promises).to.have.been.calledWith([
'optipng',
sourcePath,
])
resolve()
})
})
})
it('should return the error', async function () {
await new Promise(resolve => {
SafeExec.promises.rejects('wombat herding failure')
ImageOptimiser.compressPng(sourcePath, err => {
expect(err.toString()).to.equal('wombat herding failure')
resolve()
})
})
})
})
describe('when optimiser is sigkilled', function () {
const expectedError = new FailedCommandError('', 'SIGKILL', '', '')
let error
beforeEach(async function () {
await new Promise(resolve => {
SafeExec.promises.rejects(expectedError)
ImageOptimiser.compressPng(sourcePath, err => {
error = err
resolve()
})
})
})
it('should not produce an error', function () {
expect(error).not.to.exist
})
it('should log a warning', function (ctx) {
expect(ctx.logger.warn).to.have.been.calledOnce
})
})
})