ClsiManagerTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "logger-sharelatex": @logger = { log: sinon.stub() }
  19. @project_id = "project-id"
  20. @callback = sinon.stub()
  21. describe "sendRequest", ->
  22. beforeEach ->
  23. @Project.findById = sinon.stub().callsArgWith(1, null, @project = "mock-project")
  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 look up the project", ->
  40. @Project.findById
  41. .calledWith(@project_id)
  42. .should.equal true
  43. it "should convert the project to a request", ->
  44. @ClsiManager._buildRequest
  45. .calledWith(@project)
  46. .should.equal true
  47. it "should send the request to the CLSI", ->
  48. @ClsiManager._postToClsi
  49. .calledWith(@project_id, @request)
  50. .should.equal true
  51. it "should call the callback with the status and output files", ->
  52. outputFiles = [{
  53. path: "output.pdf"
  54. type: "pdf"
  55. },{
  56. path: "output.log"
  57. type: "log"
  58. }]
  59. @callback.calledWith(null, true, outputFiles).should.equal true
  60. describe "with a failed compile", ->
  61. beforeEach ->
  62. @ClsiManager._postToClsi = sinon.stub().callsArgWith(2, null, {
  63. compile:
  64. status: "failure"
  65. })
  66. @ClsiManager.sendRequest @project_id, @callback
  67. it "should call the callback with a failure statue", ->
  68. @callback.calledWith(null, false).should.equal true
  69. describe "_buildRequest", ->
  70. beforeEach ->
  71. @project =
  72. _id: @project_id
  73. compiler: @compiler = "latex"
  74. rootDoc_id: "mock-doc-id-1"
  75. rootFolder: [
  76. docs: [
  77. @doc_1 = {
  78. name: "main.tex"
  79. _id: "mock-doc-id-1"
  80. lines: ["Hello", "world"]
  81. }
  82. ]
  83. fileRefs: []
  84. folders: [
  85. @chapters_folder = {
  86. name: "chapters"
  87. docs: [
  88. @doc_2 = {
  89. name: "chapter1.tex"
  90. _id: "mock-doc-id-2"
  91. lines: [
  92. "Chapter 1"
  93. ]
  94. }
  95. ]
  96. fileRefs: []
  97. folders: []
  98. },
  99. @images_folder = {
  100. name: "images"
  101. docs: []
  102. fileRefs: [
  103. @file_1 = {
  104. name: "image.png"
  105. _id: "mock-file-id-1"
  106. created: new Date()
  107. }
  108. ]
  109. folders: []
  110. }
  111. ]
  112. ]
  113. describe "with a valid project", ->
  114. beforeEach (done) ->
  115. @ClsiManager._buildRequest @project, (error, request) =>
  116. @request = request
  117. done()
  118. it "should build up the CLSI request", ->
  119. expect(@request).to.deep.equal(
  120. compile:
  121. options:
  122. compiler: @compiler
  123. rootResourcePath: "main.tex"
  124. resources: [{
  125. path: @doc_1.name
  126. content: @doc_1.lines.join("\n")
  127. }, {
  128. path: "#{@chapters_folder.name}/#{@doc_2.name}"
  129. content: @doc_2.lines.join("\n")
  130. }, {
  131. path: "#{@images_folder.name}/#{@file_1.name}"
  132. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  133. modified: @file_1.created.getTime()
  134. }]
  135. )
  136. describe "when the project has an invalid compiler", ->
  137. beforeEach (done) ->
  138. @project.compiler = "context"
  139. @ClsiManager._buildRequest @project, (error, request) =>
  140. @request = request
  141. done()
  142. it "should set the compiler to pdflatex", ->
  143. @request.compile.options.compiler.should.equal "pdflatex"
  144. describe "when there is no valid root document", ->
  145. beforeEach (done) ->
  146. @project.rootDoc_id = "not-valid"
  147. @ClsiManager._buildRequest @project, (@error, @request) =>
  148. done()
  149. it "should return an error", ->
  150. expect(@error).to.exist