ClsiManagerTests.js 39 KB

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