ClsiManagerTests.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/Compile/ClsiManager.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. describe('ClsiManager', function () {
  6. beforeEach(function () {
  7. this.jar = { cookie: 'stuff' }
  8. this.ClsiCookieManager = {
  9. clearServerId: sinon.stub().yields(),
  10. getCookieJar: sinon.stub().callsArgWith(2, null, this.jar),
  11. setServerId: sinon.stub().yields(null),
  12. _getServerId: sinon.stub(),
  13. }
  14. this.ClsiStateManager = {
  15. computeHash: sinon.stub().callsArgWith(2, null, '01234567890abcdef'),
  16. }
  17. this.ClsiFormatChecker = {
  18. checkRecoursesForProblems: sinon.stub().callsArgWith(1),
  19. }
  20. this.Project = {}
  21. this.ProjectEntityHandler = {}
  22. this.ProjectGetter = {}
  23. this.DocumentUpdaterHandler = {
  24. getProjectDocsIfMatch: sinon.stub().callsArgWith(2, null, null),
  25. }
  26. this.request = sinon.stub()
  27. this.Metrics = {
  28. Timer: class Metrics {
  29. constructor() {
  30. this.done = sinon.stub()
  31. }
  32. },
  33. inc: sinon.stub(),
  34. }
  35. this.ClsiManager = SandboxedModule.require(modulePath, {
  36. requires: {
  37. '@overleaf/settings': (this.settings = {
  38. apis: {
  39. filestore: {
  40. url: 'filestore.example.com',
  41. secret: 'secret',
  42. },
  43. clsi: {
  44. url: 'http://clsi.example.com',
  45. },
  46. clsi_priority: {
  47. url: 'https://clsipremium.example.com',
  48. },
  49. },
  50. }),
  51. '../../models/Project': {
  52. Project: this.Project,
  53. },
  54. '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
  55. '../Project/ProjectGetter': this.ProjectGetter,
  56. '../DocumentUpdater/DocumentUpdaterHandler': this
  57. .DocumentUpdaterHandler,
  58. './ClsiCookieManager': () => this.ClsiCookieManager,
  59. './ClsiStateManager': this.ClsiStateManager,
  60. request: this.request,
  61. './ClsiFormatChecker': this.ClsiFormatChecker,
  62. '@overleaf/metrics': this.Metrics,
  63. },
  64. })
  65. this.project_id = 'project-id'
  66. this.user_id = 'user-id'
  67. this.callback = sinon.stub()
  68. })
  69. describe('sendRequest', function () {
  70. beforeEach(function () {
  71. this.ClsiManager._buildRequest = sinon
  72. .stub()
  73. .callsArgWith(2, null, (this.request = 'mock-request'))
  74. this.ClsiCookieManager._getServerId.callsArgWith(2, null, 'clsi3')
  75. })
  76. describe('with a successful compile', function () {
  77. beforeEach(function () {
  78. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  79. compile: {
  80. status: (this.status = 'success'),
  81. outputFiles: [
  82. {
  83. url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.pdf`,
  84. path: 'output.pdf',
  85. type: 'pdf',
  86. build: 1234,
  87. },
  88. {
  89. url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.log`,
  90. path: 'output.log',
  91. type: 'log',
  92. build: 1234,
  93. },
  94. ],
  95. },
  96. })
  97. this.ClsiManager.sendRequest(
  98. this.project_id,
  99. this.user_id,
  100. { compileGroup: 'standard' },
  101. this.callback
  102. )
  103. })
  104. it('should build the request', function () {
  105. this.ClsiManager._buildRequest
  106. .calledWith(this.project_id)
  107. .should.equal(true)
  108. })
  109. it('should send the request to the CLSI', function () {
  110. this.ClsiManager._postToClsi
  111. .calledWith(this.project_id, this.user_id, this.request, 'standard')
  112. .should.equal(true)
  113. })
  114. it('should call the callback with the status and output files', function () {
  115. const outputFiles = [
  116. {
  117. url: `/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.pdf`,
  118. path: 'output.pdf',
  119. type: 'pdf',
  120. build: 1234,
  121. // gets dropped by JSON.stringify
  122. contentId: undefined,
  123. ranges: undefined,
  124. size: undefined,
  125. },
  126. {
  127. url: `/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.log`,
  128. path: 'output.log',
  129. type: 'log',
  130. build: 1234,
  131. // gets dropped by JSON.stringify
  132. contentId: undefined,
  133. ranges: undefined,
  134. size: undefined,
  135. },
  136. ]
  137. this.callback
  138. .calledWith(null, this.status, outputFiles)
  139. .should.equal(true)
  140. })
  141. })
  142. describe('with ranges on the pdf and stats/timings details', function () {
  143. beforeEach(function () {
  144. this.ClsiManager._postToClsi = sinon.stub().yields(
  145. null,
  146. {
  147. compile: {
  148. status: 'success',
  149. stats: { fooStat: 1 },
  150. timings: { barTiming: 2 },
  151. outputFiles: [
  152. {
  153. url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.pdf`,
  154. path: 'output.pdf',
  155. type: 'pdf',
  156. build: 1234,
  157. contentId: '123-321',
  158. ranges: [{ start: 1, end: 42, hash: 'foo' }],
  159. size: 42,
  160. },
  161. {
  162. url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.log`,
  163. path: 'output.log',
  164. type: 'log',
  165. build: 1234,
  166. },
  167. ],
  168. },
  169. },
  170. 'clsi-server-id-43'
  171. )
  172. this.ClsiCookieManager._getServerId.yields(null, 'clsi-server-id-42')
  173. this.ClsiManager.sendRequest(
  174. this.project_id,
  175. this.user_id,
  176. { compileGroup: 'standard' },
  177. this.callback
  178. )
  179. })
  180. it('should emit the caching details and stats/timings', function () {
  181. const outputFiles = [
  182. {
  183. url: `/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.pdf`,
  184. path: 'output.pdf',
  185. type: 'pdf',
  186. build: 1234,
  187. contentId: '123-321',
  188. ranges: [{ start: 1, end: 42, hash: 'foo' }],
  189. size: 42,
  190. },
  191. {
  192. url: `/project/${this.project_id}/user/${this.user_id}/build/1234/output/output.log`,
  193. path: 'output.log',
  194. type: 'log',
  195. build: 1234,
  196. // gets dropped by JSON.stringify
  197. contentId: undefined,
  198. ranges: undefined,
  199. size: undefined,
  200. },
  201. ]
  202. const validationError = undefined
  203. expect(this.callback).to.have.been.calledWith(
  204. null,
  205. 'success',
  206. outputFiles,
  207. 'clsi-server-id-43',
  208. validationError,
  209. { fooStat: 1 },
  210. { barTiming: 2 }
  211. )
  212. })
  213. })
  214. describe('with a failed compile', function () {
  215. beforeEach(function () {
  216. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  217. compile: {
  218. status: (this.status = 'failure'),
  219. },
  220. })
  221. this.ClsiManager.sendRequest(
  222. this.project_id,
  223. this.user_id,
  224. {},
  225. this.callback
  226. )
  227. })
  228. it('should call the callback with a failure status', function () {
  229. this.callback.calledWith(null, this.status).should.equal(true)
  230. })
  231. })
  232. describe('with a sync conflict', function () {
  233. beforeEach(function () {
  234. this.ClsiManager.sendRequestOnce = sinon.stub()
  235. this.ClsiManager.sendRequestOnce
  236. .withArgs(this.project_id, this.user_id, { syncType: 'full' })
  237. .callsArgWith(3, null, (this.status = 'success'))
  238. this.ClsiManager.sendRequestOnce
  239. .withArgs(this.project_id, this.user_id, {})
  240. .callsArgWith(3, null, 'conflict')
  241. this.ClsiManager.sendRequest(
  242. this.project_id,
  243. this.user_id,
  244. {},
  245. this.callback
  246. )
  247. })
  248. it('should call the sendRequestOnce method twice', function () {
  249. this.ClsiManager.sendRequestOnce.calledTwice.should.equal(true)
  250. })
  251. it('should call the sendRequestOnce method with syncType:full', function () {
  252. this.ClsiManager.sendRequestOnce
  253. .calledWith(this.project_id, this.user_id, { syncType: 'full' })
  254. .should.equal(true)
  255. })
  256. it('should call the sendRequestOnce method without syncType:full', function () {
  257. this.ClsiManager.sendRequestOnce
  258. .calledWith(this.project_id, this.user_id, {})
  259. .should.equal(true)
  260. })
  261. it('should call the callback with a success status', function () {
  262. this.callback.calledWith(null, this.status).should.equal(true)
  263. })
  264. })
  265. describe('with an unavailable response', function () {
  266. beforeEach(function () {
  267. this.ClsiManager.sendRequestOnce = sinon.stub()
  268. this.ClsiManager.sendRequestOnce
  269. .withArgs(this.project_id, this.user_id, {
  270. syncType: 'full',
  271. forceNewClsiServer: true,
  272. })
  273. .callsArgWith(3, null, (this.status = 'success'))
  274. this.ClsiManager.sendRequestOnce
  275. .withArgs(this.project_id, this.user_id, {})
  276. .callsArgWith(3, null, 'unavailable')
  277. this.ClsiManager.sendRequest(
  278. this.project_id,
  279. this.user_id,
  280. {},
  281. this.callback
  282. )
  283. })
  284. it('should call the sendRequestOnce method twice', function () {
  285. this.ClsiManager.sendRequestOnce.calledTwice.should.equal(true)
  286. })
  287. it('should call the sendRequestOnce method with forceNewClsiServer:true', function () {
  288. this.ClsiManager.sendRequestOnce
  289. .calledWith(this.project_id, this.user_id, {
  290. forceNewClsiServer: true,
  291. syncType: 'full',
  292. })
  293. .should.equal(true)
  294. })
  295. it('should call the sendRequestOnce method without forceNewClsiServer:true', function () {
  296. this.ClsiManager.sendRequestOnce
  297. .calledWith(this.project_id, this.user_id, {})
  298. .should.equal(true)
  299. })
  300. it('should call the callback with a success status', function () {
  301. this.callback.calledWith(null, this.status).should.equal(true)
  302. })
  303. })
  304. describe('when the resources fail the precompile check', function () {
  305. beforeEach(function () {
  306. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  307. .stub()
  308. .callsArgWith(1, new Error('failed'))
  309. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  310. compile: {
  311. status: (this.status = 'failure'),
  312. },
  313. })
  314. this.ClsiManager.sendRequest(
  315. this.project_id,
  316. this.user_id,
  317. {},
  318. this.callback
  319. )
  320. })
  321. it('should call the callback only once', function () {
  322. this.callback.calledOnce.should.equal(true)
  323. })
  324. it('should call the callback with an error', function () {
  325. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  326. })
  327. })
  328. })
  329. describe('sendExternalRequest', function () {
  330. beforeEach(function () {
  331. this.submission_id = 'submission-id'
  332. this.clsi_request = 'mock-request'
  333. this.ClsiCookieManager._getServerId.callsArgWith(2, null, 'clsi3')
  334. })
  335. describe('with a successful compile', function () {
  336. beforeEach(function () {
  337. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  338. compile: {
  339. status: (this.status = 'success'),
  340. outputFiles: [
  341. {
  342. url: `${this.settings.apis.clsi.url}/project/${this.submission_id}/build/1234/output/output.pdf`,
  343. path: 'output.pdf',
  344. type: 'pdf',
  345. build: 1234,
  346. },
  347. {
  348. url: `${this.settings.apis.clsi.url}/project/${this.submission_id}/build/1234/output/output.log`,
  349. path: 'output.log',
  350. type: 'log',
  351. build: 1234,
  352. },
  353. ],
  354. },
  355. })
  356. this.ClsiManager.sendExternalRequest(
  357. this.submission_id,
  358. this.clsi_request,
  359. { compileGroup: 'standard' },
  360. this.callback
  361. )
  362. })
  363. it('should send the request to the CLSI', function () {
  364. this.ClsiManager._postToClsi
  365. .calledWith(this.submission_id, null, this.clsi_request, 'standard')
  366. .should.equal(true)
  367. })
  368. it('should call the callback with the status and output files', function () {
  369. const outputFiles = [
  370. {
  371. url: `/project/${this.submission_id}/build/1234/output/output.pdf`,
  372. path: 'output.pdf',
  373. type: 'pdf',
  374. build: 1234,
  375. // gets dropped by JSON.stringify
  376. contentId: undefined,
  377. ranges: undefined,
  378. size: undefined,
  379. },
  380. {
  381. url: `/project/${this.submission_id}/build/1234/output/output.log`,
  382. path: 'output.log',
  383. type: 'log',
  384. build: 1234,
  385. // gets dropped by JSON.stringify
  386. contentId: undefined,
  387. ranges: undefined,
  388. size: undefined,
  389. },
  390. ]
  391. this.callback
  392. .calledWith(null, this.status, outputFiles)
  393. .should.equal(true)
  394. })
  395. })
  396. describe('with a failed compile', function () {
  397. beforeEach(function () {
  398. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  399. compile: {
  400. status: (this.status = 'failure'),
  401. },
  402. })
  403. this.ClsiManager.sendExternalRequest(
  404. this.submission_id,
  405. this.clsi_request,
  406. {},
  407. this.callback
  408. )
  409. })
  410. it('should call the callback with a failure status', function () {
  411. this.callback.calledWith(null, this.status).should.equal(true)
  412. })
  413. })
  414. describe('when the resources fail the precompile check', function () {
  415. beforeEach(function () {
  416. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  417. .stub()
  418. .callsArgWith(1, new Error('failed'))
  419. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  420. compile: {
  421. status: (this.status = 'failure'),
  422. },
  423. })
  424. this.ClsiManager.sendExternalRequest(
  425. this.submission_id,
  426. this.clsi_request,
  427. {},
  428. this.callback
  429. )
  430. })
  431. it('should call the callback only once', function () {
  432. this.callback.calledOnce.should.equal(true)
  433. })
  434. it('should call the callback with an error', function () {
  435. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  436. })
  437. })
  438. })
  439. describe('deleteAuxFiles', function () {
  440. beforeEach(function () {
  441. this.ClsiManager._makeRequestWithClsiServerId = sinon.stub().yields(null)
  442. this.DocumentUpdaterHandler.clearProjectState = sinon.stub().callsArg(1)
  443. })
  444. describe('with the standard compileGroup', function () {
  445. beforeEach(function () {
  446. this.ClsiManager.deleteAuxFiles(
  447. this.project_id,
  448. this.user_id,
  449. { compileGroup: 'standard' },
  450. 'node-1',
  451. this.callback
  452. )
  453. })
  454. it('should call the delete method in the standard CLSI', function () {
  455. this.ClsiManager._makeRequestWithClsiServerId
  456. .calledWith(
  457. this.project_id,
  458. this.user_id,
  459. {
  460. method: 'DELETE',
  461. url: `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}`,
  462. },
  463. 'node-1'
  464. )
  465. .should.equal(true)
  466. })
  467. it('should clear the project state from the docupdater', function () {
  468. this.DocumentUpdaterHandler.clearProjectState
  469. .calledWith(this.project_id)
  470. .should.equal(true)
  471. })
  472. it('should clear the clsi persistance', function () {
  473. this.ClsiCookieManager.clearServerId
  474. .calledWith(this.project_id, this.user_id)
  475. .should.equal(true)
  476. })
  477. it('should call the callback', function () {
  478. this.callback.called.should.equal(true)
  479. })
  480. })
  481. })
  482. describe('_buildRequest', function () {
  483. beforeEach(function () {
  484. this.project = {
  485. _id: this.project_id,
  486. compiler: (this.compiler = 'latex'),
  487. rootDoc_id: 'mock-doc-id-1',
  488. imageName: (this.image = 'mock-image-name'),
  489. }
  490. this.docs = {
  491. '/main.tex': (this.doc_1 = {
  492. name: 'main.tex',
  493. _id: 'mock-doc-id-1',
  494. lines: ['Hello', 'world'],
  495. }),
  496. '/chapters/chapter1.tex': (this.doc_2 = {
  497. name: 'chapter1.tex',
  498. _id: 'mock-doc-id-2',
  499. lines: ['Chapter 1'],
  500. }),
  501. }
  502. this.files = {
  503. '/images/image.png': (this.file_1 = {
  504. name: 'image.png',
  505. _id: 'mock-file-id-1',
  506. created: new Date(),
  507. }),
  508. }
  509. this.Project.findById = sinon.stub().callsArgWith(2, null, this.project)
  510. this.ProjectEntityHandler.getAllDocs = sinon
  511. .stub()
  512. .callsArgWith(1, null, this.docs)
  513. this.ProjectEntityHandler.getAllFiles = sinon
  514. .stub()
  515. .callsArgWith(1, null, this.files)
  516. this.ProjectGetter.getProject = sinon
  517. .stub()
  518. .callsArgWith(2, null, this.project)
  519. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  520. .stub()
  521. .callsArgWith(1, null)
  522. })
  523. describe('with a valid project', function () {
  524. beforeEach(function (done) {
  525. this.ClsiManager._buildRequest(
  526. this.project_id,
  527. { timeout: 100, compileGroup: 'standard' },
  528. (err, request) => {
  529. if (err != null) {
  530. return done(err)
  531. }
  532. this.request = request
  533. done()
  534. }
  535. )
  536. })
  537. it('should get the project with the required fields', function () {
  538. this.ProjectGetter.getProject
  539. .calledWith(this.project_id, {
  540. compiler: 1,
  541. rootDoc_id: 1,
  542. imageName: 1,
  543. rootFolder: 1,
  544. })
  545. .should.equal(true)
  546. })
  547. it('should flush the project to the database', function () {
  548. this.DocumentUpdaterHandler.flushProjectToMongo
  549. .calledWith(this.project_id)
  550. .should.equal(true)
  551. })
  552. it('should get all the docs', function () {
  553. this.ProjectEntityHandler.getAllDocs
  554. .calledWith(this.project_id)
  555. .should.equal(true)
  556. })
  557. it('should get all the files', function () {
  558. this.ProjectEntityHandler.getAllFiles
  559. .calledWith(this.project_id)
  560. .should.equal(true)
  561. })
  562. it('should build up the CLSI request', function () {
  563. expect(this.request).to.deep.equal({
  564. compile: {
  565. options: {
  566. compiler: this.compiler,
  567. timeout: 100,
  568. imageName: this.image,
  569. draft: false,
  570. check: undefined,
  571. syncType: undefined, // "full"
  572. syncState: undefined,
  573. compileGroup: 'standard',
  574. enablePdfCaching: false,
  575. flags: undefined,
  576. }, // "01234567890abcdef"
  577. rootResourcePath: 'main.tex',
  578. resources: [
  579. {
  580. path: 'main.tex',
  581. content: this.doc_1.lines.join('\n'),
  582. },
  583. {
  584. path: 'chapters/chapter1.tex',
  585. content: this.doc_2.lines.join('\n'),
  586. },
  587. {
  588. path: 'images/image.png',
  589. url: `${this.settings.apis.filestore.url}/project/${this.project_id}/file/${this.file_1._id}`,
  590. modified: this.file_1.created.getTime(),
  591. },
  592. ],
  593. },
  594. })
  595. })
  596. })
  597. describe('with the incremental compile option', function () {
  598. beforeEach(function (done) {
  599. this.ClsiStateManager.computeHash = sinon
  600. .stub()
  601. .callsArgWith(
  602. 2,
  603. null,
  604. (this.project_state_hash = '01234567890abcdef')
  605. )
  606. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  607. .stub()
  608. .callsArgWith(2, null, [
  609. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 },
  610. ])
  611. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  612. .stub()
  613. .callsArgWith(1, null, { 'mock-doc-id-1': 'main.tex' })
  614. this.ClsiManager._buildRequest(
  615. this.project_id,
  616. {
  617. timeout: 100,
  618. incrementalCompilesEnabled: true,
  619. compileGroup: 'priority',
  620. },
  621. (err, request) => {
  622. if (err != null) {
  623. return done(err)
  624. }
  625. this.request = request
  626. done()
  627. }
  628. )
  629. })
  630. it('should get the project with the required fields', function () {
  631. this.ProjectGetter.getProject
  632. .calledWith(this.project_id, {
  633. compiler: 1,
  634. rootDoc_id: 1,
  635. imageName: 1,
  636. rootFolder: 1,
  637. })
  638. .should.equal(true)
  639. })
  640. it('should not explicitly flush the project to the database', function () {
  641. this.DocumentUpdaterHandler.flushProjectToMongo
  642. .calledWith(this.project_id)
  643. .should.equal(false)
  644. })
  645. it('should get only the live docs from the docupdater with a background flush in docupdater', function () {
  646. this.DocumentUpdaterHandler.getProjectDocsIfMatch
  647. .calledWith(this.project_id)
  648. .should.equal(true)
  649. })
  650. it('should not get any of the files', function () {
  651. this.ProjectEntityHandler.getAllFiles.called.should.equal(false)
  652. })
  653. it('should build up the CLSI request', function () {
  654. expect(this.request).to.deep.equal({
  655. compile: {
  656. options: {
  657. compiler: this.compiler,
  658. timeout: 100,
  659. imageName: this.image,
  660. draft: false,
  661. check: undefined,
  662. syncType: 'incremental',
  663. syncState: '01234567890abcdef',
  664. compileGroup: 'priority',
  665. enablePdfCaching: false,
  666. flags: undefined,
  667. },
  668. rootResourcePath: 'main.tex',
  669. resources: [
  670. {
  671. path: 'main.tex',
  672. content: this.doc_1.lines.join('\n'),
  673. },
  674. ],
  675. },
  676. })
  677. })
  678. describe('when the root doc is set and not in the docupdater', function () {
  679. beforeEach(function (done) {
  680. this.ClsiStateManager.computeHash = sinon
  681. .stub()
  682. .callsArgWith(
  683. 2,
  684. null,
  685. (this.project_state_hash = '01234567890abcdef')
  686. )
  687. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  688. .stub()
  689. .callsArgWith(2, null, [
  690. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 },
  691. ])
  692. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  693. .stub()
  694. .callsArgWith(1, null, {
  695. 'mock-doc-id-1': 'main.tex',
  696. 'mock-doc-id-2': '/chapters/chapter1.tex',
  697. })
  698. this.ClsiManager._buildRequest(
  699. this.project_id,
  700. {
  701. timeout: 100,
  702. incrementalCompilesEnabled: true,
  703. rootDoc_id: 'mock-doc-id-2',
  704. },
  705. (err, request) => {
  706. if (err != null) {
  707. return done(err)
  708. }
  709. this.request = request
  710. done()
  711. }
  712. )
  713. })
  714. it('should still change the root path', function () {
  715. this.request.compile.rootResourcePath.should.equal(
  716. 'chapters/chapter1.tex'
  717. )
  718. })
  719. })
  720. })
  721. describe('when root doc override is valid', function () {
  722. beforeEach(function (done) {
  723. this.ClsiManager._buildRequest(
  724. this.project_id,
  725. { rootDoc_id: 'mock-doc-id-2' },
  726. (err, request) => {
  727. if (err != null) {
  728. return done(err)
  729. }
  730. this.request = request
  731. done()
  732. }
  733. )
  734. })
  735. it('should change root path', function () {
  736. this.request.compile.rootResourcePath.should.equal(
  737. 'chapters/chapter1.tex'
  738. )
  739. })
  740. })
  741. describe('when root doc override is invalid', function () {
  742. beforeEach(function (done) {
  743. this.ClsiManager._buildRequest(
  744. this.project_id,
  745. { rootDoc_id: 'invalid-id' },
  746. (err, request) => {
  747. if (err != null) {
  748. return done(err)
  749. }
  750. this.request = request
  751. done()
  752. }
  753. )
  754. })
  755. it('should fallback to default root doc', function () {
  756. this.request.compile.rootResourcePath.should.equal('main.tex')
  757. })
  758. })
  759. describe('when the project has an invalid compiler', function () {
  760. beforeEach(function (done) {
  761. this.project.compiler = 'context'
  762. this.ClsiManager._buildRequest(this.project, null, (err, request) => {
  763. if (err != null) {
  764. return done(err)
  765. }
  766. this.request = request
  767. done()
  768. })
  769. })
  770. it('should set the compiler to pdflatex', function () {
  771. this.request.compile.options.compiler.should.equal('pdflatex')
  772. })
  773. })
  774. describe('when there is no valid root document', function () {
  775. beforeEach(function (done) {
  776. this.project.rootDoc_id = 'not-valid'
  777. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  778. this.error = error
  779. this.request = request
  780. done()
  781. })
  782. })
  783. it('should set to main.tex', function () {
  784. this.request.compile.rootResourcePath.should.equal('main.tex')
  785. })
  786. })
  787. describe('when there is no valid root document and no main.tex document', function () {
  788. beforeEach(function () {
  789. this.project.rootDoc_id = 'not-valid'
  790. this.docs = {
  791. '/other.tex': (this.doc_1 = {
  792. name: 'other.tex',
  793. _id: 'mock-doc-id-1',
  794. lines: ['Hello', 'world'],
  795. }),
  796. '/chapters/chapter1.tex': (this.doc_2 = {
  797. name: 'chapter1.tex',
  798. _id: 'mock-doc-id-2',
  799. lines: ['Chapter 1'],
  800. }),
  801. }
  802. this.ProjectEntityHandler.getAllDocs = sinon
  803. .stub()
  804. .callsArgWith(1, null, this.docs)
  805. this.ClsiManager._buildRequest(this.project, null, this.callback)
  806. })
  807. it('should report an error', function () {
  808. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  809. })
  810. })
  811. describe('when there is no valid root document and a single document which is not main.tex', function () {
  812. beforeEach(function (done) {
  813. this.project.rootDoc_id = 'not-valid'
  814. this.docs = {
  815. '/other.tex': (this.doc_1 = {
  816. name: 'other.tex',
  817. _id: 'mock-doc-id-1',
  818. lines: ['Hello', 'world'],
  819. }),
  820. }
  821. this.ProjectEntityHandler.getAllDocs = sinon
  822. .stub()
  823. .callsArgWith(1, null, this.docs)
  824. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  825. this.error = error
  826. this.request = request
  827. done()
  828. })
  829. })
  830. it('should set io to the only file', function () {
  831. this.request.compile.rootResourcePath.should.equal('other.tex')
  832. })
  833. })
  834. describe('with the draft option', function () {
  835. it('should add the draft option into the request', function (done) {
  836. this.ClsiManager._buildRequest(
  837. this.project_id,
  838. { timeout: 100, draft: true },
  839. (err, request) => {
  840. if (err != null) {
  841. return done(err)
  842. }
  843. request.compile.options.draft.should.equal(true)
  844. done()
  845. }
  846. )
  847. })
  848. })
  849. })
  850. describe('_postToClsi', function () {
  851. beforeEach(function () {
  852. this.req = { mock: 'req', compile: {} }
  853. })
  854. describe('successfully', function () {
  855. beforeEach(function () {
  856. this.ClsiManager._makeRequest = sinon
  857. .stub()
  858. .callsArgWith(
  859. 3,
  860. null,
  861. { statusCode: 204 },
  862. (this.body = { mock: 'foo' })
  863. )
  864. this.ClsiManager._postToClsi(
  865. this.project_id,
  866. this.user_id,
  867. this.req,
  868. 'standard',
  869. this.callback
  870. )
  871. })
  872. it('should send the request to the CLSI', function () {
  873. const url = `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/compile`
  874. this.ClsiManager._makeRequest
  875. .calledWith(this.project_id, this.user_id, {
  876. method: 'POST',
  877. url,
  878. json: this.req,
  879. })
  880. .should.equal(true)
  881. })
  882. it('should call the callback with the body and no error', function () {
  883. this.callback.calledWith(null, this.body).should.equal(true)
  884. })
  885. })
  886. describe('when the CLSI returns an error', function () {
  887. beforeEach(function () {
  888. this.ClsiManager._makeRequest = sinon
  889. .stub()
  890. .callsArgWith(
  891. 3,
  892. null,
  893. { statusCode: 500 },
  894. (this.body = { mock: 'foo' })
  895. )
  896. this.ClsiManager._postToClsi(
  897. this.project_id,
  898. this.user_id,
  899. this.req,
  900. 'standard',
  901. this.callback
  902. )
  903. })
  904. it('should call the callback with an error', function () {
  905. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  906. })
  907. })
  908. })
  909. describe('wordCount', function () {
  910. beforeEach(function () {
  911. this.ClsiManager._makeRequestWithClsiServerId = sinon
  912. .stub()
  913. .yields(null, { statusCode: 200 }, (this.body = { mock: 'foo' }))
  914. this.ClsiManager._buildRequest = sinon.stub().callsArgWith(
  915. 2,
  916. null,
  917. (this.req = {
  918. compile: { rootResourcePath: 'rootfile.text', options: {} },
  919. })
  920. )
  921. })
  922. describe('with root file', function () {
  923. beforeEach(function () {
  924. this.ClsiManager.wordCount(
  925. this.project_id,
  926. this.user_id,
  927. false,
  928. {},
  929. 'node-1',
  930. this.callback
  931. )
  932. })
  933. it('should call wordCount with root file', function () {
  934. this.ClsiManager._makeRequestWithClsiServerId
  935. .calledWith(
  936. this.project_id,
  937. this.user_id,
  938. {
  939. method: 'GET',
  940. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  941. qs: {
  942. file: 'rootfile.text',
  943. image: undefined,
  944. },
  945. },
  946. 'node-1'
  947. )
  948. .should.equal(true)
  949. })
  950. it('should call the callback', function () {
  951. this.callback.called.should.equal(true)
  952. })
  953. })
  954. describe('with param file', function () {
  955. beforeEach(function () {
  956. this.ClsiManager.wordCount(
  957. this.project_id,
  958. this.user_id,
  959. 'main.tex',
  960. {},
  961. 'node-2',
  962. this.callback
  963. )
  964. })
  965. it('should call wordCount with param file', function () {
  966. this.ClsiManager._makeRequestWithClsiServerId
  967. .calledWith(
  968. this.project_id,
  969. this.user_id,
  970. {
  971. method: 'GET',
  972. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  973. qs: { file: 'main.tex', image: undefined },
  974. },
  975. 'node-2'
  976. )
  977. .should.equal(true)
  978. })
  979. })
  980. describe('with image', function () {
  981. beforeEach(function () {
  982. this.req.compile.options.imageName = this.image =
  983. 'example.com/mock/image'
  984. this.ClsiManager.wordCount(
  985. this.project_id,
  986. this.user_id,
  987. 'main.tex',
  988. {},
  989. 'node-3',
  990. this.callback
  991. )
  992. })
  993. it('should call wordCount with file and image', function () {
  994. this.ClsiManager._makeRequestWithClsiServerId
  995. .calledWith(
  996. this.project_id,
  997. this.user_id,
  998. {
  999. method: 'GET',
  1000. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  1001. qs: { file: 'main.tex', image: this.image },
  1002. },
  1003. 'node-3'
  1004. )
  1005. .should.equal(true)
  1006. })
  1007. })
  1008. })
  1009. describe('_makeRequest', function () {
  1010. beforeEach(function () {
  1011. this.response = { there: 'something' }
  1012. this.request.callsArgWith(1, null, this.response)
  1013. this.opts = {
  1014. method: 'SOMETHIGN',
  1015. url: 'http://a place on the web',
  1016. }
  1017. })
  1018. it('should process a request with a cookie jar', function (done) {
  1019. this.ClsiManager._makeRequest(
  1020. this.project_id,
  1021. this.user_id,
  1022. this.opts,
  1023. () => {
  1024. const args = this.request.args[0]
  1025. args[0].method.should.equal(this.opts.method)
  1026. args[0].url.should.equal(this.opts.url)
  1027. args[0].jar.should.equal(this.jar)
  1028. done()
  1029. }
  1030. )
  1031. })
  1032. it('should set the cookie again on response as it might have changed', function (done) {
  1033. this.ClsiManager._makeRequest(
  1034. this.project_id,
  1035. this.user_id,
  1036. this.opts,
  1037. () => {
  1038. this.ClsiCookieManager.setServerId
  1039. .calledWith(this.project_id, this.user_id, this.response)
  1040. .should.equal(true)
  1041. done()
  1042. }
  1043. )
  1044. })
  1045. })
  1046. describe('_makeRequestWithClsiServerId', function () {
  1047. beforeEach(function () {
  1048. this.response = { statusCode: 200 }
  1049. this.request.yields(null, this.response)
  1050. this.opts = {
  1051. method: 'GET',
  1052. url: 'http://clsi',
  1053. }
  1054. })
  1055. describe('with a regular request', function () {
  1056. it('should process a request with a cookie jar', function (done) {
  1057. this.ClsiManager._makeRequestWithClsiServerId(
  1058. this.project_id,
  1059. this.user_id,
  1060. this.opts,
  1061. undefined,
  1062. err => {
  1063. if (err) return done(err)
  1064. const args = this.request.args[0]
  1065. args[0].method.should.equal(this.opts.method)
  1066. args[0].url.should.equal(this.opts.url)
  1067. args[0].jar.should.equal(this.jar)
  1068. expect(args[0].qs).to.not.exist
  1069. done()
  1070. }
  1071. )
  1072. })
  1073. it('should persist the cookie from the response', function (done) {
  1074. this.ClsiManager._makeRequestWithClsiServerId(
  1075. this.project_id,
  1076. this.user_id,
  1077. this.opts,
  1078. undefined,
  1079. err => {
  1080. if (err) return done(err)
  1081. this.ClsiCookieManager.setServerId
  1082. .calledWith(this.project_id, this.user_id, this.response)
  1083. .should.equal(true)
  1084. done()
  1085. }
  1086. )
  1087. })
  1088. })
  1089. describe('with a persistent request', function () {
  1090. it('should not add a cookie jar', function (done) {
  1091. this.ClsiManager._makeRequestWithClsiServerId(
  1092. this.project_id,
  1093. this.user_id,
  1094. this.opts,
  1095. 'node-1',
  1096. err => {
  1097. if (err) return done(err)
  1098. const requestOpts = this.request.args[0][0]
  1099. expect(requestOpts.method).to.equal(this.opts.method)
  1100. expect(requestOpts.url).to.equal(this.opts.url)
  1101. expect(requestOpts.jar).to.not.exist
  1102. expect(requestOpts.qs).to.deep.equal({ clsiserverid: 'node-1' })
  1103. done()
  1104. }
  1105. )
  1106. })
  1107. it('should not persist a cookie on response', function (done) {
  1108. this.ClsiManager._makeRequestWithClsiServerId(
  1109. this.project_id,
  1110. this.user_id,
  1111. this.opts,
  1112. 'node-1',
  1113. err => {
  1114. if (err) return done(err)
  1115. expect(this.ClsiCookieManager.setServerId.called).to.equal(false)
  1116. done()
  1117. }
  1118. )
  1119. })
  1120. })
  1121. })
  1122. describe('_makeGoogleCloudRequest', function () {
  1123. beforeEach(function () {
  1124. this.settings.apis.clsi_new = { url: 'https://compiles.somewhere.test' }
  1125. this.response = { there: 'something' }
  1126. this.request.callsArgWith(1, null, this.response)
  1127. this.opts = {
  1128. url: this.ClsiManager._getCompilerUrl(null, this.project_id),
  1129. }
  1130. })
  1131. it('should change the domain on the url', function (done) {
  1132. this.ClsiManager._makeNewBackendRequest(
  1133. this.project_id,
  1134. this.user_id,
  1135. this.opts,
  1136. () => {
  1137. const args = this.request.args[0]
  1138. args[0].url.should.equal(
  1139. `https://compiles.somewhere.test/project/${this.project_id}`
  1140. )
  1141. done()
  1142. }
  1143. )
  1144. })
  1145. it('should not make a request if there is not clsi_new url', function (done) {
  1146. this.settings.apis.clsi_new = undefined
  1147. this.ClsiManager._makeNewBackendRequest(
  1148. this.project_id,
  1149. this.user_id,
  1150. this.opts,
  1151. err => {
  1152. expect(err).to.equal(undefined)
  1153. this.request.callCount.should.equal(0)
  1154. done()
  1155. }
  1156. )
  1157. })
  1158. })
  1159. })