CompileControllerTests.coffee 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/CompileController'
  5. tk = require("timekeeper")
  6. describe "CompileController", ->
  7. beforeEach ->
  8. @CompileController = SandboxedModule.require modulePath, requires:
  9. "./CompileManager": @CompileManager = {}
  10. "./RequestParser": @RequestParser = {}
  11. "settings-sharelatex": @Settings =
  12. apis:
  13. clsi:
  14. url: "http://clsi.example.com"
  15. "./ProjectPersistenceManager": @ProjectPersistenceManager = {}
  16. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  17. @Settings.externalUrl = "http://www.example.com"
  18. @req = {}
  19. @res = {}
  20. @next = sinon.stub()
  21. describe "compile", ->
  22. beforeEach ->
  23. @req.body = {
  24. compile: "mock-body"
  25. }
  26. @req.params =
  27. project_id: @project_id = "project-id-123"
  28. @request = {
  29. compile: "mock-parsed-request"
  30. }
  31. @request_with_project_id =
  32. compile: @request.compile
  33. project_id: @project_id
  34. @output_files = [{
  35. path: "output.pdf"
  36. type: "pdf"
  37. build: 1234
  38. }, {
  39. path: "output.log"
  40. type: "log"
  41. build: 1234
  42. }]
  43. @RequestParser.parse = sinon.stub().callsArgWith(1, null, @request)
  44. @ProjectPersistenceManager.markProjectAsJustAccessed = sinon.stub().callsArg(1)
  45. @res.status = sinon.stub().returnsThis()
  46. @res.send = sinon.stub()
  47. describe "successfully", ->
  48. beforeEach ->
  49. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, @output_files)
  50. @CompileController.compile @req, @res
  51. it "should parse the request", ->
  52. @RequestParser.parse
  53. .calledWith(@req.body)
  54. .should.equal true
  55. it "should run the compile for the specified project", ->
  56. @CompileManager.doCompile
  57. .calledWith(@request_with_project_id)
  58. .should.equal true
  59. it "should mark the project as accessed", ->
  60. @ProjectPersistenceManager.markProjectAsJustAccessed
  61. .calledWith(@project_id)
  62. .should.equal true
  63. it "should return the JSON response", ->
  64. @res.status.calledWith(200).should.equal true
  65. @res.send
  66. .calledWith(
  67. compile:
  68. status: "success"
  69. error: null
  70. outputFiles: @output_files.map (file) =>
  71. url: "#{@Settings.apis.clsi.url}/project/#{@project_id}/output/#{file.path}"
  72. type: file.type
  73. build: file.build
  74. )
  75. .should.equal true
  76. describe "with an error", ->
  77. beforeEach ->
  78. @CompileManager.doCompile = sinon.stub().callsArgWith(1, new Error(@message = "error message"), null)
  79. @CompileController.compile @req, @res
  80. it "should return the JSON response with the error", ->
  81. @res.status.calledWith(500).should.equal true
  82. @res.send
  83. .calledWith(
  84. compile:
  85. status: "error"
  86. error: @message
  87. outputFiles: []
  88. )
  89. .should.equal true
  90. describe "when the request times out", ->
  91. beforeEach ->
  92. @error = new Error(@message = "container timed out")
  93. @error.timedout = true
  94. @CompileManager.doCompile = sinon.stub().callsArgWith(1, @error, null)
  95. @CompileController.compile @req, @res
  96. it "should return the JSON response with the timeout status", ->
  97. @res.status.calledWith(200).should.equal true
  98. @res.send
  99. .calledWith(
  100. compile:
  101. status: "timedout"
  102. error: @message
  103. outputFiles: []
  104. )
  105. .should.equal true
  106. describe "when the request returns no output files", ->
  107. beforeEach ->
  108. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, [])
  109. @CompileController.compile @req, @res
  110. it "should return the JSON response with the failure status", ->
  111. @res.status.calledWith(200).should.equal true
  112. @res.send
  113. .calledWith(
  114. compile:
  115. error: null
  116. status: "failure"
  117. outputFiles: []
  118. )
  119. .should.equal true
  120. describe "syncFromCode", ->
  121. beforeEach ->
  122. @file = "main.tex"
  123. @line = 42
  124. @column = 5
  125. @project_id = "mock-project-id"
  126. @req.params =
  127. project_id: @project_id
  128. @req.query =
  129. file: @file
  130. line: @line.toString()
  131. column: @column.toString()
  132. @res.send = sinon.stub()
  133. @CompileManager.syncFromCode = sinon.stub().callsArgWith(4, null, @pdfPositions = ["mock-positions"])
  134. @CompileController.syncFromCode @req, @res, @next
  135. it "should find the corresponding location in the PDF", ->
  136. @CompileManager.syncFromCode
  137. .calledWith(@project_id, @file, @line, @column)
  138. .should.equal true
  139. it "should return the positions", ->
  140. @res.send
  141. .calledWith(JSON.stringify
  142. pdf: @pdfPositions
  143. )
  144. .should.equal true
  145. describe "syncFromPdf", ->
  146. beforeEach ->
  147. @page = 5
  148. @h = 100.23
  149. @v = 45.67
  150. @project_id = "mock-project-id"
  151. @req.params =
  152. project_id: @project_id
  153. @req.query =
  154. page: @page.toString()
  155. h: @h.toString()
  156. v: @v.toString()
  157. @res.send = sinon.stub()
  158. @CompileManager.syncFromPdf = sinon.stub().callsArgWith(4, null, @codePositions = ["mock-positions"])
  159. @CompileController.syncFromPdf @req, @res, @next
  160. it "should find the corresponding location in the code", ->
  161. @CompileManager.syncFromPdf
  162. .calledWith(@project_id, @page, @h, @v)
  163. .should.equal true
  164. it "should return the positions", ->
  165. @res.send
  166. .calledWith(JSON.stringify
  167. code: @codePositions
  168. )
  169. .should.equal true
  170. describe "wordcount", ->
  171. beforeEach ->
  172. @file = "main.tex"
  173. @project_id = "mock-project-id"
  174. @req.params =
  175. project_id: @project_id
  176. @req.query =
  177. file: @file
  178. @res.send = sinon.stub()
  179. @CompileManager.wordcount = sinon.stub().callsArgWith(2, null, @texcount = ["mock-texcount"])
  180. @CompileController.wordcount @req, @res, @next
  181. it "should return the word count of a file", ->
  182. @CompileManager.wordcount
  183. .calledWith(@project_id, @file)
  184. .should.equal true
  185. it "should return the texcount info", ->
  186. @res.send
  187. .calledWith(JSON.stringify
  188. texcount: @texcount
  189. )
  190. .should.equal true