ClsiManagerTests.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/ClsiManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "ClsiManager", ->
  8. beforeEach ->
  9. @ClsiManager = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": @settings =
  11. apis:
  12. filestore:
  13. url: "filestore.example.com"
  14. secret: "secret"
  15. clsi:
  16. url: "http://clsi.example.com"
  17. "../../models/Project": Project: @Project = {}
  18. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  19. "logger-sharelatex": @logger = { log: sinon.stub() }
  20. @project_id = "project-id"
  21. @callback = sinon.stub()
  22. describe "sendRequest", ->
  23. beforeEach ->
  24. @ClsiManager._buildRequest = sinon.stub().callsArgWith(1, null, @request = "mock-request")
  25. describe "with a successful compile", ->
  26. beforeEach ->
  27. @ClsiManager._postToClsi = sinon.stub().callsArgWith(2, null, {
  28. compile:
  29. status: "success"
  30. outputFiles: [{
  31. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.pdf"
  32. type: "pdf"
  33. },{
  34. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.log"
  35. type: "log"
  36. }]
  37. })
  38. @ClsiManager.sendRequest @project_id, @callback
  39. it "should build the request", ->
  40. @ClsiManager._buildRequest
  41. .calledWith(@project_id)
  42. .should.equal true
  43. it "should send the request to the CLSI", ->
  44. @ClsiManager._postToClsi
  45. .calledWith(@project_id, @request)
  46. .should.equal true
  47. it "should call the callback with the status and output files", ->
  48. outputFiles = [{
  49. path: "output.pdf"
  50. type: "pdf"
  51. },{
  52. path: "output.log"
  53. type: "log"
  54. }]
  55. @callback.calledWith(null, true, outputFiles).should.equal true
  56. describe "with a failed compile", ->
  57. beforeEach ->
  58. @ClsiManager._postToClsi = sinon.stub().callsArgWith(2, null, {
  59. compile:
  60. status: "failure"
  61. })
  62. @ClsiManager.sendRequest @project_id, @callback
  63. it "should call the callback with a failure statue", ->
  64. @callback.calledWith(null, false).should.equal true
  65. describe "_buildRequest", ->
  66. beforeEach ->
  67. @project =
  68. _id: @project_id
  69. compiler: @compiler = "latex"
  70. rootDoc_id: "mock-doc-id-1"
  71. @docs = {
  72. "/main.tex": @doc_1 = {
  73. name: "main.tex"
  74. _id: "mock-doc-id-1"
  75. lines: ["Hello", "world"]
  76. },
  77. "/chapters/chapter1.tex": @doc_2 = {
  78. name: "chapter1.tex"
  79. _id: "mock-doc-id-2"
  80. lines: [
  81. "Chapter 1"
  82. ]
  83. }
  84. }
  85. @files = {
  86. "/images/image.png": @file_1 = {
  87. name: "image.png"
  88. _id: "mock-file-id-1"
  89. created: new Date()
  90. }
  91. }
  92. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  93. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  94. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  95. describe "with a valid project", ->
  96. beforeEach (done) ->
  97. @ClsiManager._buildRequest @project_id, (error, request) =>
  98. @request = request
  99. done()
  100. it "should get the project with the required fields", ->
  101. @Project.findById
  102. .calledWith(@project_id, {compiler:1, rootDoc_id: 1})
  103. .should.equal true
  104. it "should get all the docs", ->
  105. @ProjectEntityHandler.getAllDocs
  106. .calledWith(@project_id)
  107. .should.equal true
  108. it "should get all the files", ->
  109. @ProjectEntityHandler.getAllFiles
  110. .calledWith(@project_id)
  111. .should.equal true
  112. it "should build up the CLSI request", ->
  113. expect(@request).to.deep.equal(
  114. compile:
  115. options:
  116. compiler: @compiler
  117. rootResourcePath: "main.tex"
  118. resources: [{
  119. path: "main.tex"
  120. content: @doc_1.lines.join("\n")
  121. }, {
  122. path: "chapters/chapter1.tex"
  123. content: @doc_2.lines.join("\n")
  124. }, {
  125. path: "images/image.png"
  126. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  127. modified: @file_1.created.getTime()
  128. }]
  129. )
  130. describe "when the project has an invalid compiler", ->
  131. beforeEach (done) ->
  132. @project.compiler = "context"
  133. @ClsiManager._buildRequest @project, (error, request) =>
  134. @request = request
  135. done()
  136. it "should set the compiler to pdflatex", ->
  137. @request.compile.options.compiler.should.equal "pdflatex"
  138. describe "when there is no valid root document", ->
  139. beforeEach (done) ->
  140. @project.rootDoc_id = "not-valid"
  141. @ClsiManager._buildRequest @project, (@error, @request) =>
  142. done()
  143. it "should return an error", ->
  144. expect(@error).to.exist