OutputFileOptimiserTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(__dirname, '../../../app/js/OutputFileOptimiser');
  17. const path = require("path");
  18. const { expect } = require("chai");
  19. const { EventEmitter } = require("events");
  20. describe("OutputFileOptimiser", function() {
  21. beforeEach(function() {
  22. this.OutputFileOptimiser = SandboxedModule.require(modulePath, { requires: {
  23. "fs": (this.fs = {}),
  24. "path": (this.Path = {}),
  25. "child_process": { spawn: (this.spawn = sinon.stub())
  26. },
  27. "logger-sharelatex": { log: sinon.stub(), warn: sinon.stub() },
  28. "./Metrics" : {}
  29. }
  30. });
  31. this.directory = "/test/dir";
  32. return this.callback = sinon.stub();
  33. });
  34. describe("optimiseFile", function() {
  35. beforeEach(function() {
  36. this.src = "./output.pdf";
  37. return this.dst = "./output.pdf";
  38. });
  39. describe("when the file is not a pdf file", function() {
  40. beforeEach(function(done){
  41. this.src = "./output.log";
  42. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false);
  43. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  44. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  45. });
  46. it("should not check if the file is optimised", function() {
  47. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(false);
  48. });
  49. return it("should not optimise the file", function() {
  50. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(false);
  51. });
  52. });
  53. describe("when the pdf file is not optimised", function() {
  54. beforeEach(function(done) {
  55. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false);
  56. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  57. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  58. });
  59. it("should check if the pdf is optimised", function() {
  60. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(true);
  61. });
  62. return it("should optimise the pdf", function() {
  63. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(true);
  64. });
  65. });
  66. return describe("when the pdf file is optimised", function() {
  67. beforeEach(function(done) {
  68. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, true);
  69. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  70. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  71. });
  72. it("should check if the pdf is optimised", function() {
  73. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(true);
  74. });
  75. return it("should not optimise the pdf", function() {
  76. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(false);
  77. });
  78. });
  79. });
  80. return describe("checkIfPDFISOptimised", function() {
  81. beforeEach(function() {
  82. this.callback = sinon.stub();
  83. this.fd = 1234;
  84. this.fs.open = sinon.stub().yields(null, this.fd);
  85. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello /Linearized 1"));
  86. this.fs.close = sinon.stub().withArgs(this.fd).yields(null);
  87. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  88. });
  89. describe("for a linearised file", function() {
  90. beforeEach(function() {
  91. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello /Linearized 1"));
  92. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  93. });
  94. it("should open the file", function() {
  95. return this.fs.open.calledWith(this.src, "r").should.equal(true);
  96. });
  97. it("should read the header", function() {
  98. return this.fs.read.calledWith(this.fd).should.equal(true);
  99. });
  100. it("should close the file", function() {
  101. return this.fs.close.calledWith(this.fd).should.equal(true);
  102. });
  103. return it("should call the callback with a true result", function() {
  104. return this.callback.calledWith(null, true).should.equal(true);
  105. });
  106. });
  107. return describe("for an unlinearised file", function() {
  108. beforeEach(function() {
  109. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello not linearized 1"));
  110. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  111. });
  112. it("should open the file", function() {
  113. return this.fs.open.calledWith(this.src, "r").should.equal(true);
  114. });
  115. it("should read the header", function() {
  116. return this.fs.read.calledWith(this.fd).should.equal(true);
  117. });
  118. it("should close the file", function() {
  119. return this.fs.close.calledWith(this.fd).should.equal(true);
  120. });
  121. return it("should call the callback with a false result", function() {
  122. return this.callback.calledWith(null, false).should.equal(true);
  123. });
  124. });
  125. });
  126. });