OutputFileOptimiserTests.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. node/no-deprecated-api,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. require('chai').should()
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../app/js/OutputFileOptimiser'
  19. )
  20. const path = require('path')
  21. const { expect } = require('chai')
  22. const { EventEmitter } = require('events')
  23. describe('OutputFileOptimiser', function() {
  24. beforeEach(function() {
  25. this.OutputFileOptimiser = SandboxedModule.require(modulePath, {
  26. requires: {
  27. fs: (this.fs = {}),
  28. path: (this.Path = {}),
  29. child_process: { spawn: (this.spawn = sinon.stub()) },
  30. 'logger-sharelatex': { log: sinon.stub(), warn: sinon.stub() },
  31. './Metrics': {}
  32. }
  33. })
  34. this.directory = '/test/dir'
  35. return (this.callback = sinon.stub())
  36. })
  37. describe('optimiseFile', function() {
  38. beforeEach(function() {
  39. this.src = './output.pdf'
  40. return (this.dst = './output.pdf')
  41. })
  42. describe('when the file is not a pdf file', function() {
  43. beforeEach(function(done) {
  44. this.src = './output.log'
  45. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  46. .stub()
  47. .callsArgWith(1, null, false)
  48. this.OutputFileOptimiser.optimisePDF = sinon
  49. .stub()
  50. .callsArgWith(2, null)
  51. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  52. })
  53. it('should not check if the file is optimised', function() {
  54. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  55. .calledWith(this.src)
  56. .should.equal(false)
  57. })
  58. return it('should not optimise the file', function() {
  59. return this.OutputFileOptimiser.optimisePDF
  60. .calledWith(this.src, this.dst)
  61. .should.equal(false)
  62. })
  63. })
  64. describe('when the pdf file is not optimised', function() {
  65. beforeEach(function(done) {
  66. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  67. .stub()
  68. .callsArgWith(1, null, false)
  69. this.OutputFileOptimiser.optimisePDF = sinon
  70. .stub()
  71. .callsArgWith(2, null)
  72. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  73. })
  74. it('should check if the pdf is optimised', function() {
  75. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  76. .calledWith(this.src)
  77. .should.equal(true)
  78. })
  79. return it('should optimise the pdf', function() {
  80. return this.OutputFileOptimiser.optimisePDF
  81. .calledWith(this.src, this.dst)
  82. .should.equal(true)
  83. })
  84. })
  85. return describe('when the pdf file is optimised', function() {
  86. beforeEach(function(done) {
  87. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  88. .stub()
  89. .callsArgWith(1, null, true)
  90. this.OutputFileOptimiser.optimisePDF = sinon
  91. .stub()
  92. .callsArgWith(2, null)
  93. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  94. })
  95. it('should check if the pdf is optimised', function() {
  96. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  97. .calledWith(this.src)
  98. .should.equal(true)
  99. })
  100. return it('should not optimise the pdf', function() {
  101. return this.OutputFileOptimiser.optimisePDF
  102. .calledWith(this.src, this.dst)
  103. .should.equal(false)
  104. })
  105. })
  106. })
  107. return describe('checkIfPDFISOptimised', function() {
  108. beforeEach(function() {
  109. this.callback = sinon.stub()
  110. this.fd = 1234
  111. this.fs.open = sinon.stub().yields(null, this.fd)
  112. this.fs.read = sinon
  113. .stub()
  114. .withArgs(this.fd)
  115. .yields(null, 100, new Buffer('hello /Linearized 1'))
  116. this.fs.close = sinon
  117. .stub()
  118. .withArgs(this.fd)
  119. .yields(null)
  120. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  121. this.src,
  122. this.callback
  123. )
  124. })
  125. describe('for a linearised file', function() {
  126. beforeEach(function() {
  127. this.fs.read = sinon
  128. .stub()
  129. .withArgs(this.fd)
  130. .yields(null, 100, new Buffer('hello /Linearized 1'))
  131. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  132. this.src,
  133. this.callback
  134. )
  135. })
  136. it('should open the file', function() {
  137. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  138. })
  139. it('should read the header', function() {
  140. return this.fs.read.calledWith(this.fd).should.equal(true)
  141. })
  142. it('should close the file', function() {
  143. return this.fs.close.calledWith(this.fd).should.equal(true)
  144. })
  145. return it('should call the callback with a true result', function() {
  146. return this.callback.calledWith(null, true).should.equal(true)
  147. })
  148. })
  149. return describe('for an unlinearised file', function() {
  150. beforeEach(function() {
  151. this.fs.read = sinon
  152. .stub()
  153. .withArgs(this.fd)
  154. .yields(null, 100, new Buffer('hello not linearized 1'))
  155. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  156. this.src,
  157. this.callback
  158. )
  159. })
  160. it('should open the file', function() {
  161. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  162. })
  163. it('should read the header', function() {
  164. return this.fs.read.calledWith(this.fd).should.equal(true)
  165. })
  166. it('should close the file', function() {
  167. return this.fs.close.calledWith(this.fd).should.equal(true)
  168. })
  169. return it('should call the callback with a false result', function() {
  170. return this.callback.calledWith(null, false).should.equal(true)
  171. })
  172. })
  173. })
  174. })