2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
node/no-deprecated-api,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/OutputFileOptimiser'
|
|
|
|
)
|
|
|
|
const path = require('path')
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('OutputFileOptimiser', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.OutputFileOptimiser = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
fs: (this.fs = {}),
|
|
|
|
path: (this.Path = {}),
|
|
|
|
child_process: { spawn: (this.spawn = sinon.stub()) },
|
|
|
|
'logger-sharelatex': { log: sinon.stub(), warn: sinon.stub() },
|
|
|
|
'./Metrics': {}
|
2020-06-15 04:52:21 -04:00
|
|
|
},
|
|
|
|
globals: { Math } // used by lodash
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
this.directory = '/test/dir'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('optimiseFile', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.src = './output.pdf'
|
|
|
|
return (this.dst = './output.pdf')
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the file is not a pdf file', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.src = './output.log'
|
|
|
|
this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, false)
|
|
|
|
this.OutputFileOptimiser.optimisePDF = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null)
|
|
|
|
return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not check if the file is optimised', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised
|
|
|
|
.calledWith(this.src)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not optimise the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.optimisePDF
|
|
|
|
.calledWith(this.src, this.dst)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the pdf file is not optimised', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, false)
|
|
|
|
this.OutputFileOptimiser.optimisePDF = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null)
|
|
|
|
return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check if the pdf is optimised', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised
|
|
|
|
.calledWith(this.src)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should optimise the pdf', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.optimisePDF
|
|
|
|
.calledWith(this.src, this.dst)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the pdf file is optimised', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, true)
|
|
|
|
this.OutputFileOptimiser.optimisePDF = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null)
|
|
|
|
return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check if the pdf is optimised', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised
|
|
|
|
.calledWith(this.src)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not optimise the pdf', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.optimisePDF
|
|
|
|
.calledWith(this.src, this.dst)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('checkIfPDFISOptimised', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.fd = 1234
|
|
|
|
this.fs.open = sinon.stub().yields(null, this.fd)
|
|
|
|
this.fs.read = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.fd)
|
2020-05-07 05:42:05 -04:00
|
|
|
.yields(null, 100, Buffer.from('hello /Linearized 1'))
|
2020-08-10 12:01:11 -04:00
|
|
|
this.fs.close = sinon.stub().withArgs(this.fd).yields(null)
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised(
|
|
|
|
this.src,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('for a linearised file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.read = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.fd)
|
2020-05-07 05:42:05 -04:00
|
|
|
.yields(null, 100, Buffer.from('hello /Linearized 1'))
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised(
|
|
|
|
this.src,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should open the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.open.calledWith(this.src, 'r').should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should read the header', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.read.calledWith(this.fd).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should close the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.close.calledWith(this.fd).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with a true result', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('for an unlinearised file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.fs.read = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.fd)
|
2020-05-07 05:42:05 -04:00
|
|
|
.yields(null, 100, Buffer.from('hello not linearized 1'))
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileOptimiser.checkIfPDFIsOptimised(
|
|
|
|
this.src,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should open the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.open.calledWith(this.src, 'r').should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should read the header', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.read.calledWith(this.fd).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should close the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.close.calledWith(this.fd).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with a false result', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|