ClsiManagerTests.coffee 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. clsi_priority:
  18. url: "https://clsipremium.example.com"
  19. "../../models/Project": Project: @Project = {}
  20. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  21. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  22. "request": @request = {}
  23. @project_id = "project-id"
  24. @callback = sinon.stub()
  25. describe "sendRequest", ->
  26. beforeEach ->
  27. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
  28. describe "with a successful compile", ->
  29. beforeEach ->
  30. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  31. compile:
  32. status: @status = "success"
  33. outputFiles: [{
  34. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.pdf"
  35. type: "pdf"
  36. build: 1234
  37. },{
  38. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.log"
  39. type: "log"
  40. build: 1234
  41. }]
  42. })
  43. @ClsiManager.sendRequest @project_id, {compileGroup:"standard"}, @callback
  44. it "should build the request", ->
  45. @ClsiManager._buildRequest
  46. .calledWith(@project_id)
  47. .should.equal true
  48. it "should send the request to the CLSI", ->
  49. @ClsiManager._postToClsi
  50. .calledWith(@project_id, @request, "standard")
  51. .should.equal true
  52. it "should call the callback with the status and output files", ->
  53. outputFiles = [{
  54. path: "output.pdf"
  55. type: "pdf"
  56. build: 1234
  57. },{
  58. path: "output.log"
  59. type: "log"
  60. build: 1234
  61. }]
  62. @callback.calledWith(null, @status, outputFiles).should.equal true
  63. describe "with a failed compile", ->
  64. beforeEach ->
  65. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  66. compile:
  67. status: @status = "failure"
  68. })
  69. @ClsiManager.sendRequest @project_id, {}, @callback
  70. it "should call the callback with a failure statue", ->
  71. @callback.calledWith(null, @status).should.equal true
  72. describe "deleteAuxFiles", ->
  73. beforeEach ->
  74. @request.del = sinon.stub().callsArg(1)
  75. describe "with the standard compileGroup", ->
  76. beforeEach ->
  77. @ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
  78. it "should call the delete method in the standard CLSI", ->
  79. @request.del
  80. .calledWith("#{@settings.apis.clsi.url}/project/#{@project_id}")
  81. .should.equal true
  82. it "should call the callback", ->
  83. @callback.called.should.equal true
  84. describe "with the priority compileGroup", ->
  85. beforeEach ->
  86. @ClsiManager.deleteAuxFiles @project_id, {compileGroup: "priority"}, @callback
  87. it "should call the delete method in the CLSI", ->
  88. @request.del
  89. .calledWith("#{@settings.apis.clsi_priority.url}/project/#{@project_id}")
  90. .should.equal true
  91. describe "_buildRequest", ->
  92. beforeEach ->
  93. @project =
  94. _id: @project_id
  95. compiler: @compiler = "latex"
  96. rootDoc_id: "mock-doc-id-1"
  97. imageName: @image = "mock-image-name"
  98. @docs = {
  99. "/main.tex": @doc_1 = {
  100. name: "main.tex"
  101. _id: "mock-doc-id-1"
  102. lines: ["Hello", "world"]
  103. },
  104. "/chapters/chapter1.tex": @doc_2 = {
  105. name: "chapter1.tex"
  106. _id: "mock-doc-id-2"
  107. lines: [
  108. "Chapter 1"
  109. ]
  110. }
  111. }
  112. @files = {
  113. "/images/image.png": @file_1 = {
  114. name: "image.png"
  115. _id: "mock-file-id-1"
  116. created: new Date()
  117. }
  118. }
  119. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  120. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  121. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  122. describe "with a valid project", ->
  123. beforeEach (done) ->
  124. @ClsiManager._buildRequest @project_id, {timeout:100}, (error, request) =>
  125. @request = request
  126. done()
  127. it "should get the project with the required fields", ->
  128. @Project.findById
  129. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1})
  130. .should.equal true
  131. it "should get all the docs", ->
  132. @ProjectEntityHandler.getAllDocs
  133. .calledWith(@project_id)
  134. .should.equal true
  135. it "should get all the files", ->
  136. @ProjectEntityHandler.getAllFiles
  137. .calledWith(@project_id)
  138. .should.equal true
  139. it "should build up the CLSI request", ->
  140. expect(@request).to.deep.equal(
  141. compile:
  142. options:
  143. compiler: @compiler
  144. timeout : 100
  145. imageName: @image
  146. draft: false
  147. rootResourcePath: "main.tex"
  148. resources: [{
  149. path: "main.tex"
  150. content: @doc_1.lines.join("\n")
  151. }, {
  152. path: "chapters/chapter1.tex"
  153. content: @doc_2.lines.join("\n")
  154. }, {
  155. path: "images/image.png"
  156. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  157. modified: @file_1.created.getTime()
  158. }]
  159. )
  160. describe "when root doc override is valid", ->
  161. beforeEach (done) ->
  162. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  163. @request = request
  164. done()
  165. it "should change root path", ->
  166. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  167. describe "when root doc override is invalid", ->
  168. beforeEach (done) ->
  169. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  170. @request = request
  171. done()
  172. it "should fallback to default root doc", ->
  173. @request.compile.rootResourcePath.should.equal "main.tex"
  174. describe "when the project has an invalid compiler", ->
  175. beforeEach (done) ->
  176. @project.compiler = "context"
  177. @ClsiManager._buildRequest @project, null, (error, request) =>
  178. @request = request
  179. done()
  180. it "should set the compiler to pdflatex", ->
  181. @request.compile.options.compiler.should.equal "pdflatex"
  182. describe "when there is no valid root document", ->
  183. beforeEach (done) ->
  184. @project.rootDoc_id = "not-valid"
  185. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  186. done()
  187. it "should return an error", ->
  188. expect(@error).to.exist
  189. describe "with the draft option", ->
  190. it "should add the draft option into the request", (done) ->
  191. @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>
  192. request.compile.options.draft.should.equal true
  193. done()
  194. describe '_postToClsi', ->
  195. beforeEach ->
  196. @req = { mock: "req" }
  197. describe "successfully", ->
  198. beforeEach ->
  199. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, @body = { mock: "foo" })
  200. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  201. it 'should send the request to the CLSI', ->
  202. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
  203. @request.post.calledWith({
  204. url: url
  205. json: @req
  206. jar: false
  207. }).should.equal true
  208. it "should call the callback with the body and no error", ->
  209. @callback.calledWith(null, @body).should.equal true
  210. describe "when the CLSI returns an error", ->
  211. beforeEach ->
  212. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
  213. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  214. it "should call the callback with the body and the error", ->
  215. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  216. describe "when the compiler is priority", ->
  217. beforeEach ->
  218. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
  219. @ClsiManager._postToClsi @project_id, @req, "priority", @callback
  220. it "should use the clsi_priority url", ->
  221. url = "#{@settings.apis.clsi_priority.url}/project/#{@project_id}/compile"
  222. @request.post.calledWith({
  223. url: url
  224. json: @req
  225. jar: false
  226. }).should.equal true
  227. describe "wordCount", ->
  228. beforeEach ->
  229. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body = { mock: "foo" })
  230. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
  231. @ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url"
  232. describe "with root file", ->
  233. beforeEach ->
  234. @ClsiManager.wordCount @project_id, false, {}, @callback
  235. it "should call wordCount with root file", ->
  236. @request.get
  237. .calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
  238. .should.equal true
  239. it "should call the callback", ->
  240. @callback.called.should.equal true
  241. describe "with param file", ->
  242. beforeEach ->
  243. @ClsiManager.wordCount @project_id, "main.tex", {}, @callback
  244. it "should call wordCount with param file", ->
  245. @request.get
  246. .calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
  247. .should.equal true
  248. describe "with image", ->
  249. beforeEach ->
  250. @req.compile.options.imageName = @image = "example.com/mock/image"
  251. @ClsiManager.wordCount @project_id, "main.tex", {}, @callback
  252. it "should call wordCount with file and image", ->
  253. @request.get
  254. .calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
  255. .should.equal true