| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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
- })
- })
- })
|