CompileControllerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. }, {
  38. path: "output.log"
  39. type: "log"
  40. }]
  41. @RequestParser.parse = sinon.stub().callsArgWith(1, null, @request)
  42. @ProjectPersistenceManager.markProjectAsJustAccessed = sinon.stub().callsArg(1)
  43. @res.send = sinon.stub()
  44. describe "successfully", ->
  45. beforeEach ->
  46. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, @output_files)
  47. @CompileController.compile @req, @res
  48. it "should parse the request", ->
  49. @RequestParser.parse
  50. .calledWith(@req.body)
  51. .should.equal true
  52. it "should run the compile for the specified project", ->
  53. @CompileManager.doCompile
  54. .calledWith(@request_with_project_id)
  55. .should.equal true
  56. it "should mark the project as accessed", ->
  57. @ProjectPersistenceManager.markProjectAsJustAccessed
  58. .calledWith(@project_id)
  59. .should.equal true
  60. it "should return the JSON response", ->
  61. @res.send
  62. .calledWith(200,
  63. compile:
  64. status: "success"
  65. error: null
  66. outputFiles: @output_files.map (file) =>
  67. url: "#{@Settings.apis.clsi.url}/project/#{@project_id}/output/#{file.path}"
  68. type: file.type
  69. )
  70. .should.equal true
  71. describe "with an error", ->
  72. beforeEach ->
  73. @CompileManager.doCompile = sinon.stub().callsArgWith(1, new Error(@message = "error message"), null)
  74. @CompileController.compile @req, @res
  75. it "should return the JSON response with the error", ->
  76. @res.send
  77. .calledWith(500,
  78. compile:
  79. status: "error"
  80. error: @message
  81. outputFiles: []
  82. )
  83. .should.equal true
  84. describe "when the request times out", ->
  85. beforeEach ->
  86. @error = new Error(@message = "container timed out")
  87. @error.timedout = true
  88. @CompileManager.doCompile = sinon.stub().callsArgWith(1, @error, null)
  89. @CompileController.compile @req, @res
  90. it "should return the JSON response with the timeout status", ->
  91. @res.send
  92. .calledWith(200,
  93. compile:
  94. status: "timedout"
  95. error: @message
  96. outputFiles: []
  97. )
  98. .should.equal true
  99. describe "when the request returns no output files", ->
  100. beforeEach ->
  101. @CompileManager.doCompile = sinon.stub().callsArgWith(1, null, [])
  102. @CompileController.compile @req, @res
  103. it "should return the JSON response with the failure status", ->
  104. @res.send
  105. .calledWith(200,
  106. compile:
  107. error: null
  108. status: "failure"
  109. outputFiles: []
  110. )
  111. .should.equal true
  112. describe "syncFromCode", ->
  113. beforeEach ->
  114. @file = "main.tex"
  115. @line = 42
  116. @column = 5
  117. @project_id = "mock-project-id"
  118. @req.params =
  119. project_id: @project_id
  120. @req.query =
  121. file: @file
  122. line: @line.toString()
  123. column: @column.toString()
  124. @res.send = sinon.stub()
  125. @CompileManager.syncFromCode = sinon.stub().callsArgWith(4, null, @pdfPositions = ["mock-positions"])
  126. @CompileController.syncFromCode @req, @res, @next
  127. it "should find the corresponding location in the PDF", ->
  128. @CompileManager.syncFromCode
  129. .calledWith(@project_id, @file, @line, @column)
  130. .should.equal true
  131. it "should return the positions", ->
  132. @res.send
  133. .calledWith(JSON.stringify
  134. pdf: @pdfPositions
  135. )
  136. .should.equal true
  137. describe "syncFromPdf", ->
  138. beforeEach ->
  139. @page = 5
  140. @h = 100.23
  141. @v = 45.67
  142. @project_id = "mock-project-id"
  143. @req.params =
  144. project_id: @project_id
  145. @req.query =
  146. page: @page.toString()
  147. h: @h.toString()
  148. v: @v.toString()
  149. @res.send = sinon.stub()
  150. @CompileManager.syncFromPdf = sinon.stub().callsArgWith(4, null, @codePositions = ["mock-positions"])
  151. @CompileController.syncFromPdf @req, @res, @next
  152. it "should find the corresponding location in the code", ->
  153. @CompileManager.syncFromPdf
  154. .calledWith(@project_id, @page, @h, @v)
  155. .should.equal true
  156. it "should return the positions", ->
  157. @res.send
  158. .calledWith(JSON.stringify
  159. code: @codePositions
  160. )
  161. .should.equal true