CompileControllerTests.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. require('chai').should()
  15. const modulePath = require('path').join(
  16. __dirname,
  17. '../../../app/js/CompileController'
  18. )
  19. const tk = require('timekeeper')
  20. describe('CompileController', function() {
  21. beforeEach(function() {
  22. this.CompileController = SandboxedModule.require(modulePath, {
  23. requires: {
  24. './CompileManager': (this.CompileManager = {}),
  25. './RequestParser': (this.RequestParser = {}),
  26. 'settings-sharelatex': (this.Settings = {
  27. apis: {
  28. clsi: {
  29. url: 'http://clsi.example.com'
  30. }
  31. }
  32. }),
  33. './ProjectPersistenceManager': (this.ProjectPersistenceManager = {}),
  34. 'logger-sharelatex': (this.logger = {
  35. log: sinon.stub(),
  36. error: sinon.stub(),
  37. err: sinon.stub(),
  38. warn: sinon.stub()
  39. })
  40. }
  41. })
  42. this.Settings.externalUrl = 'http://www.example.com'
  43. this.req = {}
  44. this.res = {}
  45. return (this.next = sinon.stub())
  46. })
  47. describe('compile', function() {
  48. beforeEach(function() {
  49. this.req.body = {
  50. compile: 'mock-body'
  51. }
  52. this.req.params = { project_id: (this.project_id = 'project-id-123') }
  53. this.request = {
  54. compile: 'mock-parsed-request'
  55. }
  56. this.request_with_project_id = {
  57. compile: this.request.compile,
  58. project_id: this.project_id
  59. }
  60. this.output_files = [
  61. {
  62. path: 'output.pdf',
  63. type: 'pdf',
  64. build: 1234
  65. },
  66. {
  67. path: 'output.log',
  68. type: 'log',
  69. build: 1234
  70. }
  71. ]
  72. this.RequestParser.parse = sinon
  73. .stub()
  74. .callsArgWith(1, null, this.request)
  75. this.ProjectPersistenceManager.markProjectAsJustAccessed = sinon
  76. .stub()
  77. .callsArg(1)
  78. this.res.status = sinon.stub().returnsThis()
  79. return (this.res.send = sinon.stub())
  80. })
  81. describe('successfully', function() {
  82. beforeEach(function() {
  83. this.CompileManager.doCompileWithLock = sinon
  84. .stub()
  85. .callsArgWith(1, null, this.output_files)
  86. return this.CompileController.compile(this.req, this.res)
  87. })
  88. it('should parse the request', function() {
  89. return this.RequestParser.parse
  90. .calledWith(this.req.body)
  91. .should.equal(true)
  92. })
  93. it('should run the compile for the specified project', function() {
  94. return this.CompileManager.doCompileWithLock
  95. .calledWith(this.request_with_project_id)
  96. .should.equal(true)
  97. })
  98. it('should mark the project as accessed', function() {
  99. return this.ProjectPersistenceManager.markProjectAsJustAccessed
  100. .calledWith(this.project_id)
  101. .should.equal(true)
  102. })
  103. return it('should return the JSON response', function() {
  104. this.res.status.calledWith(200).should.equal(true)
  105. return this.res.send
  106. .calledWith({
  107. compile: {
  108. status: 'success',
  109. error: null,
  110. outputFiles: this.output_files.map(file => {
  111. return {
  112. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  113. path: file.path,
  114. type: file.type,
  115. build: file.build
  116. }
  117. })
  118. }
  119. })
  120. .should.equal(true)
  121. })
  122. })
  123. describe('with an error', function() {
  124. beforeEach(function() {
  125. this.CompileManager.doCompileWithLock = sinon
  126. .stub()
  127. .callsArgWith(1, new Error((this.message = 'error message')), null)
  128. return this.CompileController.compile(this.req, this.res)
  129. })
  130. return it('should return the JSON response with the error', function() {
  131. this.res.status.calledWith(500).should.equal(true)
  132. return this.res.send
  133. .calledWith({
  134. compile: {
  135. status: 'error',
  136. error: this.message,
  137. outputFiles: []
  138. }
  139. })
  140. .should.equal(true)
  141. })
  142. })
  143. describe('when the request times out', function() {
  144. beforeEach(function() {
  145. this.error = new Error((this.message = 'container timed out'))
  146. this.error.timedout = true
  147. this.CompileManager.doCompileWithLock = sinon
  148. .stub()
  149. .callsArgWith(1, this.error, null)
  150. return this.CompileController.compile(this.req, this.res)
  151. })
  152. return it('should return the JSON response with the timeout status', function() {
  153. this.res.status.calledWith(200).should.equal(true)
  154. return this.res.send
  155. .calledWith({
  156. compile: {
  157. status: 'timedout',
  158. error: this.message,
  159. outputFiles: []
  160. }
  161. })
  162. .should.equal(true)
  163. })
  164. })
  165. return describe('when the request returns no output files', function() {
  166. beforeEach(function() {
  167. this.CompileManager.doCompileWithLock = sinon
  168. .stub()
  169. .callsArgWith(1, null, [])
  170. return this.CompileController.compile(this.req, this.res)
  171. })
  172. return it('should return the JSON response with the failure status', function() {
  173. this.res.status.calledWith(200).should.equal(true)
  174. return this.res.send
  175. .calledWith({
  176. compile: {
  177. error: null,
  178. status: 'failure',
  179. outputFiles: []
  180. }
  181. })
  182. .should.equal(true)
  183. })
  184. })
  185. })
  186. describe('syncFromCode', function() {
  187. beforeEach(function() {
  188. this.file = 'main.tex'
  189. this.line = 42
  190. this.column = 5
  191. this.project_id = 'mock-project-id'
  192. this.req.params = { project_id: this.project_id }
  193. this.req.query = {
  194. file: this.file,
  195. line: this.line.toString(),
  196. column: this.column.toString()
  197. }
  198. this.res.json = sinon.stub()
  199. this.CompileManager.syncFromCode = sinon
  200. .stub()
  201. .callsArgWith(5, null, (this.pdfPositions = ['mock-positions']))
  202. return this.CompileController.syncFromCode(this.req, this.res, this.next)
  203. })
  204. it('should find the corresponding location in the PDF', function() {
  205. return this.CompileManager.syncFromCode
  206. .calledWith(
  207. this.project_id,
  208. undefined,
  209. this.file,
  210. this.line,
  211. this.column
  212. )
  213. .should.equal(true)
  214. })
  215. return it('should return the positions', function() {
  216. return this.res.json
  217. .calledWith({
  218. pdf: this.pdfPositions
  219. })
  220. .should.equal(true)
  221. })
  222. })
  223. describe('syncFromPdf', function() {
  224. beforeEach(function() {
  225. this.page = 5
  226. this.h = 100.23
  227. this.v = 45.67
  228. this.project_id = 'mock-project-id'
  229. this.req.params = { project_id: this.project_id }
  230. this.req.query = {
  231. page: this.page.toString(),
  232. h: this.h.toString(),
  233. v: this.v.toString()
  234. }
  235. this.res.json = sinon.stub()
  236. this.CompileManager.syncFromPdf = sinon
  237. .stub()
  238. .callsArgWith(5, null, (this.codePositions = ['mock-positions']))
  239. return this.CompileController.syncFromPdf(this.req, this.res, this.next)
  240. })
  241. it('should find the corresponding location in the code', function() {
  242. return this.CompileManager.syncFromPdf
  243. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  244. .should.equal(true)
  245. })
  246. return it('should return the positions', function() {
  247. return this.res.json
  248. .calledWith({
  249. code: this.codePositions
  250. })
  251. .should.equal(true)
  252. })
  253. })
  254. return describe('wordcount', function() {
  255. beforeEach(function() {
  256. this.file = 'main.tex'
  257. this.project_id = 'mock-project-id'
  258. this.req.params = { project_id: this.project_id }
  259. this.req.query = {
  260. file: this.file,
  261. image: (this.image = 'example.com/image')
  262. }
  263. this.res.json = sinon.stub()
  264. this.CompileManager.wordcount = sinon
  265. .stub()
  266. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  267. return this.CompileController.wordcount(this.req, this.res, this.next)
  268. })
  269. it('should return the word count of a file', function() {
  270. return this.CompileManager.wordcount
  271. .calledWith(this.project_id, undefined, this.file, this.image)
  272. .should.equal(true)
  273. })
  274. return it('should return the texcount info', function() {
  275. return this.res.json
  276. .calledWith({
  277. texcount: this.texcount
  278. })
  279. .should.equal(true)
  280. })
  281. })
  282. })