OutputFileOptimiserTests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. const modulePath = require('path').join(
  16. __dirname,
  17. '../../../app/js/OutputFileOptimiser'
  18. )
  19. const path = require('path')
  20. const { expect } = require('chai')
  21. const { EventEmitter } = require('events')
  22. describe('OutputFileOptimiser', function () {
  23. beforeEach(function () {
  24. this.OutputFileOptimiser = SandboxedModule.require(modulePath, {
  25. requires: {
  26. fs: (this.fs = {}),
  27. path: (this.Path = {}),
  28. child_process: { spawn: (this.spawn = sinon.stub()) },
  29. './Metrics': {},
  30. },
  31. globals: { Math }, // used by lodash
  32. })
  33. this.directory = '/test/dir'
  34. return (this.callback = sinon.stub())
  35. })
  36. describe('optimiseFile', function () {
  37. beforeEach(function () {
  38. this.src = './output.pdf'
  39. return (this.dst = './output.pdf')
  40. })
  41. describe('when the file is not a pdf file', function () {
  42. beforeEach(function (done) {
  43. this.src = './output.log'
  44. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  45. .stub()
  46. .callsArgWith(1, null, false)
  47. this.OutputFileOptimiser.optimisePDF = sinon
  48. .stub()
  49. .callsArgWith(2, null)
  50. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  51. })
  52. it('should not check if the file is optimised', function () {
  53. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  54. .calledWith(this.src)
  55. .should.equal(false)
  56. })
  57. return it('should not optimise the file', function () {
  58. return this.OutputFileOptimiser.optimisePDF
  59. .calledWith(this.src, this.dst)
  60. .should.equal(false)
  61. })
  62. })
  63. describe('when the pdf file is not optimised', function () {
  64. beforeEach(function (done) {
  65. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  66. .stub()
  67. .callsArgWith(1, null, false)
  68. this.OutputFileOptimiser.optimisePDF = sinon
  69. .stub()
  70. .callsArgWith(2, null)
  71. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  72. })
  73. it('should check if the pdf is optimised', function () {
  74. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  75. .calledWith(this.src)
  76. .should.equal(true)
  77. })
  78. return it('should optimise the pdf', function () {
  79. return this.OutputFileOptimiser.optimisePDF
  80. .calledWith(this.src, this.dst)
  81. .should.equal(true)
  82. })
  83. })
  84. return describe('when the pdf file is optimised', function () {
  85. beforeEach(function (done) {
  86. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  87. .stub()
  88. .callsArgWith(1, null, true)
  89. this.OutputFileOptimiser.optimisePDF = sinon
  90. .stub()
  91. .callsArgWith(2, null)
  92. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  93. })
  94. it('should check if the pdf is optimised', function () {
  95. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  96. .calledWith(this.src)
  97. .should.equal(true)
  98. })
  99. return it('should not optimise the pdf', function () {
  100. return this.OutputFileOptimiser.optimisePDF
  101. .calledWith(this.src, this.dst)
  102. .should.equal(false)
  103. })
  104. })
  105. })
  106. return describe('checkIfPDFISOptimised', function () {
  107. beforeEach(function () {
  108. this.callback = sinon.stub()
  109. this.fd = 1234
  110. this.fs.open = sinon.stub().yields(null, this.fd)
  111. this.fs.read = sinon
  112. .stub()
  113. .withArgs(this.fd)
  114. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  115. this.fs.close = sinon.stub().withArgs(this.fd).yields(null)
  116. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  117. this.src,
  118. this.callback
  119. )
  120. })
  121. describe('for a linearised file', function () {
  122. beforeEach(function () {
  123. this.fs.read = sinon
  124. .stub()
  125. .withArgs(this.fd)
  126. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  127. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  128. this.src,
  129. this.callback
  130. )
  131. })
  132. it('should open the file', function () {
  133. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  134. })
  135. it('should read the header', function () {
  136. return this.fs.read.calledWith(this.fd).should.equal(true)
  137. })
  138. it('should close the file', function () {
  139. return this.fs.close.calledWith(this.fd).should.equal(true)
  140. })
  141. return it('should call the callback with a true result', function () {
  142. return this.callback.calledWith(null, true).should.equal(true)
  143. })
  144. })
  145. return describe('for an unlinearised file', function () {
  146. beforeEach(function () {
  147. this.fs.read = sinon
  148. .stub()
  149. .withArgs(this.fd)
  150. .yields(null, 100, Buffer.from('hello not linearized 1'))
  151. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  152. this.src,
  153. this.callback
  154. )
  155. })
  156. it('should open the file', function () {
  157. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  158. })
  159. it('should read the header', function () {
  160. return this.fs.read.calledWith(this.fd).should.equal(true)
  161. })
  162. it('should close the file', function () {
  163. return this.fs.close.calledWith(this.fd).should.equal(true)
  164. })
  165. return it('should call the callback with a false result', function () {
  166. return this.callback.calledWith(null, false).should.equal(true)
  167. })
  168. })
  169. })
  170. })