OutputFileOptimiserTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/OutputFileOptimiser'
  5. path = require "path"
  6. expect = require("chai").expect
  7. EventEmitter = require("events").EventEmitter
  8. describe "OutputFileOptimiser", ->
  9. beforeEach ->
  10. @OutputFileOptimiser = SandboxedModule.require modulePath, requires:
  11. "fs": @fs = {}
  12. "path": @Path = {}
  13. "child_process": spawn: @spawn = sinon.stub()
  14. "logger-sharelatex": { log: sinon.stub(), warn: sinon.stub() }
  15. "./Metrics" : {}
  16. @directory = "/test/dir"
  17. @callback = sinon.stub()
  18. describe "optimiseFile", ->
  19. beforeEach ->
  20. @src = "./output.pdf"
  21. @dst = "./output.pdf"
  22. describe "when the file is not a pdf file", ->
  23. beforeEach (done)->
  24. @src = "./output.log"
  25. @OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false)
  26. @OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null)
  27. @OutputFileOptimiser.optimiseFile @src, @dst, done
  28. it "should not check if the file is optimised", ->
  29. @OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(@src).should.equal false
  30. it "should not optimise the file", ->
  31. @OutputFileOptimiser.optimisePDF.calledWith(@src, @dst).should.equal false
  32. describe "when the pdf file is not optimised", ->
  33. beforeEach (done) ->
  34. @OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false)
  35. @OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null)
  36. @OutputFileOptimiser.optimiseFile @src, @dst, done
  37. it "should check if the pdf is optimised", ->
  38. @OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(@src).should.equal true
  39. it "should optimise the pdf", ->
  40. @OutputFileOptimiser.optimisePDF.calledWith(@src, @dst).should.equal true
  41. describe "when the pdf file is optimised", ->
  42. beforeEach (done) ->
  43. @OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, true)
  44. @OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null)
  45. @OutputFileOptimiser.optimiseFile @src, @dst, done
  46. it "should check if the pdf is optimised", ->
  47. @OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(@src).should.equal true
  48. it "should not optimise the pdf", ->
  49. @OutputFileOptimiser.optimisePDF.calledWith(@src, @dst).should.equal false
  50. describe "checkIfPDFISOptimised", ->
  51. beforeEach () ->
  52. @callback = sinon.stub()
  53. @fd = 1234
  54. @fs.open = sinon.stub().yields(null, @fd)
  55. @fs.read = sinon.stub().withArgs(@fd).yields(null, 100, new Buffer("hello /Linearized 1"))
  56. @fs.close = sinon.stub().withArgs(@fd).yields(null)
  57. @OutputFileOptimiser.checkIfPDFIsOptimised @src, @callback
  58. describe "for a linearised file", ->
  59. beforeEach () ->
  60. @fs.read = sinon.stub().withArgs(@fd).yields(null, 100, new Buffer("hello /Linearized 1"))
  61. @OutputFileOptimiser.checkIfPDFIsOptimised @src, @callback
  62. it "should open the file", ->
  63. @fs.open.calledWith(@src, "r").should.equal true
  64. it "should read the header", ->
  65. @fs.read.calledWith(@fd).should.equal true
  66. it "should close the file", ->
  67. @fs.close.calledWith(@fd).should.equal true
  68. it "should call the callback with a true result", ->
  69. @callback.calledWith(null, true).should.equal true
  70. describe "for an unlinearised file", ->
  71. beforeEach () ->
  72. @fs.read = sinon.stub().withArgs(@fd).yields(null, 100, new Buffer("hello not linearized 1"))
  73. @OutputFileOptimiser.checkIfPDFIsOptimised @src, @callback
  74. it "should open the file", ->
  75. @fs.open.calledWith(@src, "r").should.equal true
  76. it "should read the header", ->
  77. @fs.read.calledWith(@fd).should.equal true
  78. it "should close the file", ->
  79. @fs.close.calledWith(@fd).should.equal true
  80. it "should call the callback with a false result", ->
  81. @callback.calledWith(null, false).should.equal true