ClsiManagerTests.js 39 KB

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