ClsiManagerTests.js 39 KB

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