ClsiManagerTests.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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().returns('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':
  57. this.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. metricsMethod: 'standard',
  577. }, // "01234567890abcdef"
  578. rootResourcePath: 'main.tex',
  579. resources: [
  580. {
  581. path: 'main.tex',
  582. content: this.doc_1.lines.join('\n'),
  583. },
  584. {
  585. path: 'chapters/chapter1.tex',
  586. content: this.doc_2.lines.join('\n'),
  587. },
  588. {
  589. path: 'images/image.png',
  590. url: `${this.settings.apis.filestore.url}/project/${this.project_id}/file/${this.file_1._id}`,
  591. modified: this.file_1.created.getTime(),
  592. },
  593. ],
  594. },
  595. })
  596. })
  597. })
  598. describe('with the incremental compile option', function () {
  599. beforeEach(function (done) {
  600. this.project_state_hash = '01234567890abcdef'
  601. this.ClsiStateManager.computeHash = sinon
  602. .stub()
  603. .returns(this.project_state_hash)
  604. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  605. .stub()
  606. .callsArgWith(2, null, [
  607. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 },
  608. ])
  609. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  610. .stub()
  611. .returns({ 'mock-doc-id-1': 'main.tex' })
  612. this.ClsiManager._buildRequest(
  613. this.project_id,
  614. {
  615. timeout: 100,
  616. incrementalCompilesEnabled: true,
  617. compileGroup: 'priority',
  618. },
  619. (err, request) => {
  620. if (err != null) {
  621. return done(err)
  622. }
  623. this.request = request
  624. done()
  625. }
  626. )
  627. })
  628. it('should get the project with the required fields', function () {
  629. this.ProjectGetter.getProject
  630. .calledWith(this.project_id, {
  631. compiler: 1,
  632. rootDoc_id: 1,
  633. imageName: 1,
  634. rootFolder: 1,
  635. })
  636. .should.equal(true)
  637. })
  638. it('should not explicitly flush the project to the database', function () {
  639. this.DocumentUpdaterHandler.flushProjectToMongo
  640. .calledWith(this.project_id)
  641. .should.equal(false)
  642. })
  643. it('should get only the live docs from the docupdater with a background flush in docupdater', function () {
  644. this.DocumentUpdaterHandler.getProjectDocsIfMatch
  645. .calledWith(this.project_id)
  646. .should.equal(true)
  647. })
  648. it('should not get any of the files', function () {
  649. this.ProjectEntityHandler.getAllFiles.called.should.equal(false)
  650. })
  651. it('should build up the CLSI request', function () {
  652. expect(this.request).to.deep.equal({
  653. compile: {
  654. options: {
  655. compiler: this.compiler,
  656. timeout: 100,
  657. imageName: this.image,
  658. draft: false,
  659. check: undefined,
  660. syncType: 'incremental',
  661. syncState: '01234567890abcdef',
  662. compileGroup: 'priority',
  663. enablePdfCaching: false,
  664. flags: undefined,
  665. metricsMethod: 'priority',
  666. },
  667. rootResourcePath: 'main.tex',
  668. resources: [
  669. {
  670. path: 'main.tex',
  671. content: this.doc_1.lines.join('\n'),
  672. },
  673. ],
  674. },
  675. })
  676. })
  677. describe('when the root doc is set and not in the docupdater', function () {
  678. beforeEach(function (done) {
  679. this.project_state_hash = '01234567890abcdef'
  680. this.ClsiStateManager.computeHash = sinon
  681. .stub()
  682. .returns(this.project_state_hash)
  683. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  684. .stub()
  685. .callsArgWith(2, null, [
  686. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 },
  687. ])
  688. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  689. .stub()
  690. .returns({
  691. 'mock-doc-id-1': 'main.tex',
  692. 'mock-doc-id-2': '/chapters/chapter1.tex',
  693. })
  694. this.ClsiManager._buildRequest(
  695. this.project_id,
  696. {
  697. timeout: 100,
  698. incrementalCompilesEnabled: true,
  699. rootDoc_id: 'mock-doc-id-2',
  700. },
  701. (err, request) => {
  702. if (err != null) {
  703. return done(err)
  704. }
  705. this.request = request
  706. done()
  707. }
  708. )
  709. })
  710. it('should still change the root path', function () {
  711. this.request.compile.rootResourcePath.should.equal(
  712. 'chapters/chapter1.tex'
  713. )
  714. })
  715. })
  716. })
  717. describe('when root doc override is valid', function () {
  718. beforeEach(function (done) {
  719. this.ClsiManager._buildRequest(
  720. this.project_id,
  721. { rootDoc_id: 'mock-doc-id-2' },
  722. (err, request) => {
  723. if (err != null) {
  724. return done(err)
  725. }
  726. this.request = request
  727. done()
  728. }
  729. )
  730. })
  731. it('should change root path', function () {
  732. this.request.compile.rootResourcePath.should.equal(
  733. 'chapters/chapter1.tex'
  734. )
  735. })
  736. })
  737. describe('when root doc override is invalid', function () {
  738. beforeEach(function (done) {
  739. this.ClsiManager._buildRequest(
  740. this.project_id,
  741. { rootDoc_id: 'invalid-id' },
  742. (err, request) => {
  743. if (err != null) {
  744. return done(err)
  745. }
  746. this.request = request
  747. done()
  748. }
  749. )
  750. })
  751. it('should fallback to default root doc', function () {
  752. this.request.compile.rootResourcePath.should.equal('main.tex')
  753. })
  754. })
  755. describe('when the project has an invalid compiler', function () {
  756. beforeEach(function (done) {
  757. this.project.compiler = 'context'
  758. this.ClsiManager._buildRequest(this.project, null, (err, request) => {
  759. if (err != null) {
  760. return done(err)
  761. }
  762. this.request = request
  763. done()
  764. })
  765. })
  766. it('should set the compiler to pdflatex', function () {
  767. this.request.compile.options.compiler.should.equal('pdflatex')
  768. })
  769. })
  770. describe('when there is no valid root document', function () {
  771. beforeEach(function (done) {
  772. this.project.rootDoc_id = 'not-valid'
  773. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  774. this.error = error
  775. this.request = request
  776. done()
  777. })
  778. })
  779. it('should set to main.tex', function () {
  780. this.request.compile.rootResourcePath.should.equal('main.tex')
  781. })
  782. })
  783. describe('when there is no valid root document and no main.tex document', function () {
  784. beforeEach(function () {
  785. this.project.rootDoc_id = 'not-valid'
  786. this.docs = {
  787. '/other.tex': (this.doc_1 = {
  788. name: 'other.tex',
  789. _id: 'mock-doc-id-1',
  790. lines: ['Hello', 'world'],
  791. }),
  792. '/chapters/chapter1.tex': (this.doc_2 = {
  793. name: 'chapter1.tex',
  794. _id: 'mock-doc-id-2',
  795. lines: ['Chapter 1'],
  796. }),
  797. }
  798. this.ProjectEntityHandler.getAllDocs = sinon
  799. .stub()
  800. .callsArgWith(1, null, this.docs)
  801. this.ClsiManager._buildRequest(this.project, null, this.callback)
  802. })
  803. it('should report an error', function () {
  804. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  805. })
  806. })
  807. describe('when there is no valid root document and a single document which is not main.tex', function () {
  808. beforeEach(function (done) {
  809. this.project.rootDoc_id = 'not-valid'
  810. this.docs = {
  811. '/other.tex': (this.doc_1 = {
  812. name: 'other.tex',
  813. _id: 'mock-doc-id-1',
  814. lines: ['Hello', 'world'],
  815. }),
  816. }
  817. this.ProjectEntityHandler.getAllDocs = sinon
  818. .stub()
  819. .callsArgWith(1, null, this.docs)
  820. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  821. this.error = error
  822. this.request = request
  823. done()
  824. })
  825. })
  826. it('should set io to the only file', function () {
  827. this.request.compile.rootResourcePath.should.equal('other.tex')
  828. })
  829. })
  830. describe('with the draft option', function () {
  831. it('should add the draft option into the request', function (done) {
  832. this.ClsiManager._buildRequest(
  833. this.project_id,
  834. { timeout: 100, draft: true },
  835. (err, request) => {
  836. if (err != null) {
  837. return done(err)
  838. }
  839. request.compile.options.draft.should.equal(true)
  840. done()
  841. }
  842. )
  843. })
  844. })
  845. })
  846. describe('_postToClsi', function () {
  847. beforeEach(function () {
  848. this.req = { mock: 'req', compile: {} }
  849. })
  850. describe('successfully', function () {
  851. beforeEach(function () {
  852. this.ClsiManager._makeRequest = sinon
  853. .stub()
  854. .callsArgWith(
  855. 3,
  856. null,
  857. { statusCode: 204 },
  858. (this.body = { mock: 'foo' })
  859. )
  860. this.ClsiManager._postToClsi(
  861. this.project_id,
  862. this.user_id,
  863. this.req,
  864. 'standard',
  865. this.callback
  866. )
  867. })
  868. it('should send the request to the CLSI', function () {
  869. const url = `${this.settings.apis.clsi.url}/project/${this.project_id}/user/${this.user_id}/compile`
  870. this.ClsiManager._makeRequest
  871. .calledWith(this.project_id, this.user_id, {
  872. method: 'POST',
  873. url,
  874. json: this.req,
  875. })
  876. .should.equal(true)
  877. })
  878. it('should call the callback with the body and no error', function () {
  879. this.callback.calledWith(null, this.body).should.equal(true)
  880. })
  881. })
  882. describe('when the CLSI returns an error', function () {
  883. beforeEach(function () {
  884. this.ClsiManager._makeRequest = sinon
  885. .stub()
  886. .callsArgWith(
  887. 3,
  888. null,
  889. { statusCode: 500 },
  890. (this.body = { mock: 'foo' })
  891. )
  892. this.ClsiManager._postToClsi(
  893. this.project_id,
  894. this.user_id,
  895. this.req,
  896. 'standard',
  897. this.callback
  898. )
  899. })
  900. it('should call the callback with an error', function () {
  901. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  902. })
  903. })
  904. })
  905. describe('wordCount', function () {
  906. beforeEach(function () {
  907. this.ClsiManager._makeRequestWithClsiServerId = sinon
  908. .stub()
  909. .yields(null, { statusCode: 200 }, (this.body = { mock: 'foo' }))
  910. this.ClsiManager._buildRequest = sinon.stub().callsArgWith(
  911. 2,
  912. null,
  913. (this.req = {
  914. compile: { rootResourcePath: 'rootfile.text', options: {} },
  915. })
  916. )
  917. })
  918. describe('with root file', function () {
  919. beforeEach(function () {
  920. this.ClsiManager.wordCount(
  921. this.project_id,
  922. this.user_id,
  923. false,
  924. {},
  925. 'node-1',
  926. this.callback
  927. )
  928. })
  929. it('should call wordCount with root file', function () {
  930. this.ClsiManager._makeRequestWithClsiServerId
  931. .calledWith(
  932. this.project_id,
  933. this.user_id,
  934. {
  935. method: 'GET',
  936. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  937. qs: {
  938. file: 'rootfile.text',
  939. image: undefined,
  940. },
  941. json: true,
  942. },
  943. 'node-1'
  944. )
  945. .should.equal(true)
  946. })
  947. it('should call the callback', function () {
  948. this.callback.called.should.equal(true)
  949. })
  950. })
  951. describe('with param file', function () {
  952. beforeEach(function () {
  953. this.ClsiManager.wordCount(
  954. this.project_id,
  955. this.user_id,
  956. 'main.tex',
  957. {},
  958. 'node-2',
  959. this.callback
  960. )
  961. })
  962. it('should call wordCount with param file', function () {
  963. this.ClsiManager._makeRequestWithClsiServerId
  964. .calledWith(
  965. this.project_id,
  966. this.user_id,
  967. {
  968. method: 'GET',
  969. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  970. qs: { file: 'main.tex', image: undefined },
  971. json: true,
  972. },
  973. 'node-2'
  974. )
  975. .should.equal(true)
  976. })
  977. })
  978. describe('with image', function () {
  979. beforeEach(function () {
  980. this.req.compile.options.imageName = this.image =
  981. 'example.com/mock/image'
  982. this.ClsiManager.wordCount(
  983. this.project_id,
  984. this.user_id,
  985. 'main.tex',
  986. {},
  987. 'node-3',
  988. this.callback
  989. )
  990. })
  991. it('should call wordCount with file and image', function () {
  992. this.ClsiManager._makeRequestWithClsiServerId
  993. .calledWith(
  994. this.project_id,
  995. this.user_id,
  996. {
  997. method: 'GET',
  998. url: `http://clsi.example.com/project/${this.project_id}/user/${this.user_id}/wordcount`,
  999. qs: { file: 'main.tex', image: this.image },
  1000. json: true,
  1001. },
  1002. 'node-3'
  1003. )
  1004. .should.equal(true)
  1005. })
  1006. })
  1007. })
  1008. describe('_makeRequest', function () {
  1009. beforeEach(function () {
  1010. this.response = { there: 'something' }
  1011. this.request.callsArgWith(1, null, this.response)
  1012. this.opts = {
  1013. method: 'SOMETHIGN',
  1014. url: 'http://a place on the web',
  1015. }
  1016. })
  1017. it('should process a request with a cookie jar', function (done) {
  1018. this.ClsiManager._makeRequest(
  1019. this.project_id,
  1020. this.user_id,
  1021. this.opts,
  1022. () => {
  1023. const args = this.request.args[0]
  1024. args[0].method.should.equal(this.opts.method)
  1025. args[0].url.should.equal(this.opts.url)
  1026. args[0].jar.should.equal(this.jar)
  1027. done()
  1028. }
  1029. )
  1030. })
  1031. it('should set the cookie again on response as it might have changed', function (done) {
  1032. this.ClsiManager._makeRequest(
  1033. this.project_id,
  1034. this.user_id,
  1035. this.opts,
  1036. () => {
  1037. this.ClsiCookieManager.setServerId
  1038. .calledWith(this.project_id, this.user_id, this.response)
  1039. .should.equal(true)
  1040. done()
  1041. }
  1042. )
  1043. })
  1044. })
  1045. describe('_makeRequestWithClsiServerId', function () {
  1046. beforeEach(function () {
  1047. this.response = { statusCode: 200 }
  1048. this.request.yields(null, this.response)
  1049. this.opts = {
  1050. method: 'GET',
  1051. url: 'http://clsi',
  1052. }
  1053. })
  1054. describe('with a regular request', function () {
  1055. it('should process a request with a cookie jar', function (done) {
  1056. this.ClsiManager._makeRequestWithClsiServerId(
  1057. this.project_id,
  1058. this.user_id,
  1059. this.opts,
  1060. undefined,
  1061. err => {
  1062. if (err) return done(err)
  1063. const args = this.request.args[0]
  1064. args[0].method.should.equal(this.opts.method)
  1065. args[0].url.should.equal(this.opts.url)
  1066. args[0].jar.should.equal(this.jar)
  1067. expect(args[0].qs).to.not.exist
  1068. done()
  1069. }
  1070. )
  1071. })
  1072. it('should persist the cookie from the response', function (done) {
  1073. this.ClsiManager._makeRequestWithClsiServerId(
  1074. this.project_id,
  1075. this.user_id,
  1076. this.opts,
  1077. undefined,
  1078. err => {
  1079. if (err) return done(err)
  1080. this.ClsiCookieManager.setServerId
  1081. .calledWith(this.project_id, this.user_id, this.response)
  1082. .should.equal(true)
  1083. done()
  1084. }
  1085. )
  1086. })
  1087. })
  1088. describe('with a persistent request', function () {
  1089. it('should not add a cookie jar', function (done) {
  1090. this.ClsiManager._makeRequestWithClsiServerId(
  1091. this.project_id,
  1092. this.user_id,
  1093. this.opts,
  1094. 'node-1',
  1095. err => {
  1096. if (err) return done(err)
  1097. const requestOpts = this.request.args[0][0]
  1098. expect(requestOpts.method).to.equal(this.opts.method)
  1099. expect(requestOpts.url).to.equal(this.opts.url)
  1100. expect(requestOpts.jar).to.not.exist
  1101. expect(requestOpts.qs).to.deep.equal({ clsiserverid: 'node-1' })
  1102. done()
  1103. }
  1104. )
  1105. })
  1106. it('should not persist a cookie on response', function (done) {
  1107. this.ClsiManager._makeRequestWithClsiServerId(
  1108. this.project_id,
  1109. this.user_id,
  1110. this.opts,
  1111. 'node-1',
  1112. err => {
  1113. if (err) return done(err)
  1114. expect(this.ClsiCookieManager.setServerId.called).to.equal(false)
  1115. done()
  1116. }
  1117. )
  1118. })
  1119. })
  1120. })
  1121. describe('_makeGoogleCloudRequest', function () {
  1122. beforeEach(function () {
  1123. this.settings.apis.clsi_new = { url: 'https://compiles.somewhere.test' }
  1124. this.response = { there: 'something' }
  1125. this.request.callsArgWith(1, null, this.response)
  1126. this.opts = {
  1127. url: this.ClsiManager._getCompilerUrl(null, this.project_id),
  1128. }
  1129. })
  1130. it('should change the domain on the url', function (done) {
  1131. this.ClsiManager._makeNewBackendRequest(
  1132. this.project_id,
  1133. this.user_id,
  1134. this.opts,
  1135. () => {
  1136. const args = this.request.args[0]
  1137. args[0].url.should.equal(
  1138. `https://compiles.somewhere.test/project/${this.project_id}`
  1139. )
  1140. done()
  1141. }
  1142. )
  1143. })
  1144. it('should not make a request if there is not clsi_new url', function (done) {
  1145. this.settings.apis.clsi_new = undefined
  1146. this.ClsiManager._makeNewBackendRequest(
  1147. this.project_id,
  1148. this.user_id,
  1149. this.opts,
  1150. err => {
  1151. expect(err).to.equal(undefined)
  1152. this.request.callCount.should.equal(0)
  1153. done()
  1154. }
  1155. )
  1156. })
  1157. })
  1158. })