OutputFileOptimiserTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  5. */
  6. const SandboxedModule = require('sandboxed-module');
  7. const sinon = require('sinon');
  8. require('chai').should();
  9. const modulePath = require('path').join(__dirname, '../../../app/js/OutputFileOptimiser');
  10. const path = require("path");
  11. const { expect } = require("chai");
  12. const { EventEmitter } = require("events");
  13. describe("OutputFileOptimiser", function() {
  14. beforeEach(function() {
  15. this.OutputFileOptimiser = SandboxedModule.require(modulePath, { requires: {
  16. "fs": (this.fs = {}),
  17. "path": (this.Path = {}),
  18. "child_process": { spawn: (this.spawn = sinon.stub())
  19. },
  20. "logger-sharelatex": { log: sinon.stub(), warn: sinon.stub() },
  21. "./Metrics" : {}
  22. }
  23. });
  24. this.directory = "/test/dir";
  25. return this.callback = sinon.stub();
  26. });
  27. describe("optimiseFile", function() {
  28. beforeEach(function() {
  29. this.src = "./output.pdf";
  30. return this.dst = "./output.pdf";
  31. });
  32. describe("when the file is not a pdf file", function() {
  33. beforeEach(function(done){
  34. this.src = "./output.log";
  35. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false);
  36. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  37. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  38. });
  39. it("should not check if the file is optimised", function() {
  40. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(false);
  41. });
  42. return it("should not optimise the file", function() {
  43. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(false);
  44. });
  45. });
  46. describe("when the pdf file is not optimised", function() {
  47. beforeEach(function(done) {
  48. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, false);
  49. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  50. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  51. });
  52. it("should check if the pdf is optimised", function() {
  53. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(true);
  54. });
  55. return it("should optimise the pdf", function() {
  56. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(true);
  57. });
  58. });
  59. return describe("when the pdf file is optimised", function() {
  60. beforeEach(function(done) {
  61. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon.stub().callsArgWith(1, null, true);
  62. this.OutputFileOptimiser.optimisePDF = sinon.stub().callsArgWith(2, null);
  63. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done);
  64. });
  65. it("should check if the pdf is optimised", function() {
  66. return this.OutputFileOptimiser.checkIfPDFIsOptimised.calledWith(this.src).should.equal(true);
  67. });
  68. return it("should not optimise the pdf", function() {
  69. return this.OutputFileOptimiser.optimisePDF.calledWith(this.src, this.dst).should.equal(false);
  70. });
  71. });
  72. });
  73. return describe("checkIfPDFISOptimised", function() {
  74. beforeEach(function() {
  75. this.callback = sinon.stub();
  76. this.fd = 1234;
  77. this.fs.open = sinon.stub().yields(null, this.fd);
  78. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello /Linearized 1"));
  79. this.fs.close = sinon.stub().withArgs(this.fd).yields(null);
  80. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  81. });
  82. describe("for a linearised file", function() {
  83. beforeEach(function() {
  84. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello /Linearized 1"));
  85. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  86. });
  87. it("should open the file", function() {
  88. return this.fs.open.calledWith(this.src, "r").should.equal(true);
  89. });
  90. it("should read the header", function() {
  91. return this.fs.read.calledWith(this.fd).should.equal(true);
  92. });
  93. it("should close the file", function() {
  94. return this.fs.close.calledWith(this.fd).should.equal(true);
  95. });
  96. return it("should call the callback with a true result", function() {
  97. return this.callback.calledWith(null, true).should.equal(true);
  98. });
  99. });
  100. return describe("for an unlinearised file", function() {
  101. beforeEach(function() {
  102. this.fs.read = sinon.stub().withArgs(this.fd).yields(null, 100, new Buffer("hello not linearized 1"));
  103. return this.OutputFileOptimiser.checkIfPDFIsOptimised(this.src, this.callback);
  104. });
  105. it("should open the file", function() {
  106. return this.fs.open.calledWith(this.src, "r").should.equal(true);
  107. });
  108. it("should read the header", function() {
  109. return this.fs.read.calledWith(this.fd).should.equal(true);
  110. });
  111. it("should close the file", function() {
  112. return this.fs.close.calledWith(this.fd).should.equal(true);
  113. });
  114. return it("should call the callback with a false result", function() {
  115. return this.callback.calledWith(null, false).should.equal(true);
  116. });
  117. });
  118. });
  119. });