mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-29 20:11:32 +02:00
[filestore] convert to ES modules GitOrigin-RevId: 404905973548bb6e437fff66b368e87be8249b73
79 lines
2.0 KiB
JavaScript
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
|
|
})
|
|
})
|
|
})
|