DocumentUpdaterHandler.test.mjs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. import mongodb from 'mongodb-legacy'
  5. import nock from 'nock'
  6. const { ObjectId } = mongodb
  7. const modulePath = path.join(
  8. import.meta.dirname,
  9. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler'
  10. )
  11. describe('DocumentUpdaterHandler', function () {
  12. beforeEach(async function (ctx) {
  13. ctx.project_id = 'project-id-923'
  14. ctx.projectHistoryId = 'ol-project-id-1'
  15. ctx.doc_id = 'doc-id-394'
  16. ctx.lines = ['one', 'two', 'three']
  17. ctx.version = 42
  18. ctx.user_id = 'mock-user-id-123'
  19. ctx.project = { _id: ctx.project_id }
  20. ctx.projectEntityHandler = {}
  21. ctx.settings = {
  22. apis: {
  23. documentupdater: {
  24. url: 'http://document_updater.example.com',
  25. },
  26. project_history: {
  27. url: 'http://project_history.example.com',
  28. },
  29. },
  30. moduleImportSequence: [],
  31. }
  32. ctx.source = 'dropbox'
  33. ctx.docUpdaterMock = nock(ctx.settings.apis.documentupdater.url)
  34. ctx.ProjectGetter = {
  35. promises: {
  36. getProjectWithoutLock: sinon.stub(),
  37. },
  38. }
  39. ctx.ProjectGetter.promises.getProjectWithoutLock
  40. .withArgs(ctx.project_id)
  41. .resolves(ctx.project)
  42. vi.doMock('@overleaf/settings', () => ({
  43. default: ctx.settings,
  44. }))
  45. vi.doMock(
  46. '../../../../app/src/Features/Project/ProjectEntityHandler',
  47. () => ({
  48. default: ctx.projectEntityHandler,
  49. })
  50. )
  51. vi.doMock('../../../../app/src/models/Project', () => ({
  52. Project: (ctx.Project = {}),
  53. }))
  54. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  55. default: ctx.ProjectGetter,
  56. }))
  57. vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
  58. default: {},
  59. }))
  60. vi.doMock('@overleaf/metrics', () => ({
  61. default: {
  62. Timer: class {
  63. done() {}
  64. },
  65. },
  66. }))
  67. vi.doMock('../../../../app/src/infrastructure/Modules', () => ({
  68. default: {
  69. promises: {
  70. hooks: {
  71. fire: sinon.stub().resolves(),
  72. },
  73. },
  74. },
  75. }))
  76. ctx.handler = (await import(modulePath)).default
  77. })
  78. afterEach(function () {
  79. nock.cleanAll()
  80. })
  81. describe('flushProjectToMongo', function () {
  82. describe('successfully', function () {
  83. beforeEach(async function (ctx) {
  84. ctx.docUpdaterMock.post(`/project/${ctx.project_id}/flush`).reply(204)
  85. await ctx.handler.promises.flushProjectToMongo(ctx.project_id)
  86. })
  87. it('should flush the document from the document updater', function (ctx) {
  88. expect(ctx.docUpdaterMock.isDone()).to.be.true
  89. })
  90. })
  91. describe('when the document updater API returns an error', function () {
  92. beforeEach(function (ctx) {
  93. ctx.docUpdaterMock
  94. .post(`/project/${ctx.project_id}/flush`)
  95. .replyWithError('boom')
  96. })
  97. it('should reject with an error', async function (ctx) {
  98. await expect(ctx.handler.promises.flushProjectToMongo(ctx.project_id))
  99. .to.be.rejected
  100. })
  101. })
  102. describe('when the document updater returns a failure error code', function () {
  103. beforeEach(function (ctx) {
  104. ctx.docUpdaterMock.post(`/project/${ctx.project_id}/flush`).reply(500)
  105. })
  106. it('should reject with an error', async function (ctx) {
  107. await expect(ctx.handler.promises.flushProjectToMongo(ctx.project_id))
  108. .to.be.rejected
  109. })
  110. })
  111. })
  112. describe('flushProjectToMongoAndDelete', function () {
  113. describe('successfully', function () {
  114. beforeEach(async function (ctx) {
  115. ctx.docUpdaterMock.delete(`/project/${ctx.project_id}`).reply(204)
  116. await ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
  117. })
  118. it('should delete the project from the document updater', function (ctx) {
  119. expect(ctx.docUpdaterMock.isDone()).to.be.true
  120. })
  121. })
  122. describe('when the document updater API returns an error', function () {
  123. beforeEach(function (ctx) {
  124. ctx.docUpdaterMock
  125. .delete(`/project/${ctx.project_id}`)
  126. .replyWithError('boom')
  127. })
  128. it('should reject with an error', async function (ctx) {
  129. await expect(
  130. ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
  131. ).to.be.rejected
  132. })
  133. })
  134. describe('when the document updater returns a failure error code', function () {
  135. beforeEach(function (ctx) {
  136. ctx.docUpdaterMock.delete(`/project/${ctx.project_id}`).reply(500)
  137. })
  138. it('should reject with an error', async function (ctx) {
  139. await expect(
  140. ctx.handler.promises.flushProjectToMongoAndDelete(ctx.project_id)
  141. ).to.be.rejected
  142. })
  143. })
  144. })
  145. describe('flushDocToMongo', function () {
  146. describe('successfully', function () {
  147. beforeEach(function (ctx) {
  148. ctx.docUpdaterMock
  149. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
  150. .reply(204)
  151. })
  152. it('should flush the document from the document updater', async function (ctx) {
  153. await ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
  154. expect(ctx.docUpdaterMock.isDone()).to.be.true
  155. })
  156. })
  157. describe('when the document updater API returns an error', function () {
  158. beforeEach(function (ctx) {
  159. ctx.docUpdaterMock
  160. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
  161. .replyWithError('boom')
  162. })
  163. it('should reject with an error', async function (ctx) {
  164. await expect(
  165. ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
  166. ).to.be.rejected
  167. })
  168. })
  169. describe('when the document updater returns a failure error code', function () {
  170. beforeEach(function (ctx) {
  171. ctx.docUpdaterMock
  172. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/flush`)
  173. .reply(500)
  174. })
  175. it('should reject with an error', async function (ctx) {
  176. await expect(
  177. ctx.handler.promises.flushDocToMongo(ctx.project_id, ctx.doc_id)
  178. ).to.be.rejected
  179. })
  180. })
  181. })
  182. describe('deleteDoc', function () {
  183. describe('successfully', function () {
  184. beforeEach(function (ctx) {
  185. ctx.docUpdaterMock
  186. .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
  187. .reply(204)
  188. })
  189. it('should delete the document from the document updater', async function (ctx) {
  190. await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id)
  191. expect(ctx.docUpdaterMock.isDone()).to.be.true
  192. })
  193. })
  194. describe('when the document updater API returns an error', function () {
  195. beforeEach(function (ctx) {
  196. ctx.docUpdaterMock
  197. .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
  198. .replyWithError('boom')
  199. })
  200. it('should reject with an error', async function (ctx) {
  201. await expect(ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id))
  202. .to.be.rejected
  203. })
  204. })
  205. describe('when the document updater returns a failure error code', function () {
  206. beforeEach(function (ctx) {
  207. ctx.docUpdaterMock
  208. .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
  209. .reply(500)
  210. })
  211. it('should reject with an error', async function (ctx) {
  212. await expect(ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id))
  213. .to.be.rejected
  214. })
  215. })
  216. describe("with 'ignoreFlushErrors' option", function () {
  217. it('when option is true, should send a `ignore_flush_errors=true` URL query to document-updater', async function (ctx) {
  218. ctx.docUpdaterMock
  219. .delete(
  220. `/project/${ctx.project_id}/doc/${ctx.doc_id}?ignore_flush_errors=true`
  221. )
  222. .reply(204)
  223. await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id, true)
  224. expect(ctx.docUpdaterMock.isDone()).to.be.true
  225. })
  226. it("when option is false, shouldn't send any URL query to document-updater", async function (ctx) {
  227. ctx.docUpdaterMock
  228. .delete(`/project/${ctx.project_id}/doc/${ctx.doc_id}`)
  229. .reply(204)
  230. await ctx.handler.promises.deleteDoc(ctx.project_id, ctx.doc_id, false)
  231. expect(ctx.docUpdaterMock.isDone()).to.be.true
  232. })
  233. })
  234. })
  235. describe('setDocument', function () {
  236. describe('successfully', function () {
  237. beforeEach(function (ctx) {
  238. ctx.docUpdaterMock
  239. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
  240. lines: ctx.lines,
  241. source: ctx.source,
  242. user_id: ctx.user_id,
  243. })
  244. .reply(204)
  245. })
  246. it('should set the document in the document updater', async function (ctx) {
  247. await ctx.handler.promises.setDocument(
  248. ctx.project_id,
  249. ctx.doc_id,
  250. ctx.user_id,
  251. ctx.lines,
  252. ctx.source
  253. )
  254. expect(ctx.docUpdaterMock.isDone()).to.be.true
  255. })
  256. })
  257. describe('when the document updater API returns an error', function () {
  258. beforeEach(function (ctx) {
  259. ctx.docUpdaterMock
  260. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
  261. lines: ctx.lines,
  262. source: ctx.source,
  263. user_id: ctx.user_id,
  264. })
  265. .replyWithError('boom')
  266. })
  267. it('should reject with an error', async function (ctx) {
  268. await expect(
  269. ctx.handler.promises.setDocument(
  270. ctx.project_id,
  271. ctx.doc_id,
  272. ctx.user_id,
  273. ctx.lines,
  274. ctx.source
  275. )
  276. ).to.be.rejected
  277. })
  278. })
  279. describe('when the document updater returns a failure error code', function () {
  280. beforeEach(function (ctx) {
  281. ctx.docUpdaterMock
  282. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}`, {
  283. lines: ctx.lines,
  284. source: ctx.source,
  285. user_id: ctx.user_id,
  286. })
  287. .reply(500)
  288. })
  289. it('should reject with an error', async function (ctx) {
  290. await expect(
  291. ctx.handler.promises.setDocument(
  292. ctx.project_id,
  293. ctx.doc_id,
  294. ctx.user_id,
  295. ctx.lines,
  296. ctx.source
  297. )
  298. ).to.be.rejected
  299. })
  300. })
  301. })
  302. describe('getComment', function () {
  303. beforeEach(function (ctx) {
  304. ctx.comment = { id: new ObjectId().toString() }
  305. ctx.docUpdaterMock
  306. .get(
  307. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.comment.id}`
  308. )
  309. .reply(200, ctx.comment)
  310. })
  311. it('should get the comment from the document updater', async function (ctx) {
  312. const body = await ctx.handler.promises.getComment(
  313. ctx.project_id,
  314. ctx.doc_id,
  315. ctx.comment.id
  316. )
  317. expect(body).to.deep.equal(ctx.comment)
  318. })
  319. })
  320. describe('getDocument', function () {
  321. beforeEach(function (ctx) {
  322. ctx.doc = {
  323. lines: ctx.lines,
  324. version: ctx.version,
  325. ops: ['mock-op-1', 'mock-op-2'],
  326. ranges: { mock: 'ranges' },
  327. }
  328. ctx.fromVersion = 2
  329. })
  330. describe('successfully', function () {
  331. beforeEach(function (ctx) {
  332. ctx.docUpdaterMock
  333. .get(
  334. `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
  335. )
  336. .reply(200, ctx.doc)
  337. })
  338. it('should return the lines and version', async function (ctx) {
  339. const doc = await ctx.handler.promises.getDocument(
  340. ctx.project_id,
  341. ctx.doc_id,
  342. ctx.fromVersion
  343. )
  344. expect(doc).to.deep.equal(ctx.doc)
  345. })
  346. })
  347. describe('when the document updater API returns an error', function () {
  348. beforeEach(function (ctx) {
  349. ctx.docUpdaterMock
  350. .get(
  351. `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
  352. )
  353. .replyWithError('boom')
  354. })
  355. it('should reject with an error', async function (ctx) {
  356. await expect(
  357. ctx.handler.promises.getDocument(
  358. ctx.project_id,
  359. ctx.doc_id,
  360. ctx.fromVersion
  361. )
  362. ).to.be.rejected
  363. })
  364. })
  365. describe('when the document updater returns a failure error code', function () {
  366. beforeEach(function (ctx) {
  367. ctx.docUpdaterMock
  368. .get(
  369. `/project/${ctx.project_id}/doc/${ctx.doc_id}?fromVersion=${ctx.fromVersion}`
  370. )
  371. .reply(500)
  372. })
  373. it('should reject with an error', async function (ctx) {
  374. await expect(
  375. ctx.handler.promises.getDocument(
  376. ctx.project_id,
  377. ctx.doc_id,
  378. ctx.fromVersion
  379. )
  380. ).to.be.rejected
  381. })
  382. })
  383. })
  384. describe('getProjectDocsIfMatch', function () {
  385. beforeEach(function (ctx) {
  386. ctx.project_state_hash = '1234567890abcdef'
  387. ctx.doc0 = {
  388. _id: ctx.doc_id,
  389. lines: ctx.lines,
  390. v: ctx.version,
  391. }
  392. ctx.docs = [ctx.doc0, ctx.doc0, ctx.doc0]
  393. })
  394. describe('successfully', function () {
  395. beforeEach(function (ctx) {
  396. ctx.docUpdaterMock
  397. .post(
  398. `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
  399. )
  400. .reply(200, ctx.docs)
  401. })
  402. it('should call the callback with the documents', async function (ctx) {
  403. const docs = await ctx.handler.promises.getProjectDocsIfMatch(
  404. ctx.project_id,
  405. ctx.project_state_hash
  406. )
  407. expect(docs).to.deep.equal(ctx.docs)
  408. })
  409. })
  410. describe('when the document updater API returns an error', function () {
  411. beforeEach(function (ctx) {
  412. ctx.docUpdaterMock
  413. .post(
  414. `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
  415. )
  416. .replyWithError('boom')
  417. })
  418. it('should reject with an error', async function (ctx) {
  419. await expect(
  420. ctx.handler.promises.getProjectDocsIfMatch(
  421. ctx.project_id,
  422. ctx.project_state_hash
  423. )
  424. ).to.be.rejected
  425. })
  426. })
  427. describe('when the document updater returns a conflict error code', function () {
  428. beforeEach(function (ctx) {
  429. ctx.docUpdaterMock
  430. .post(
  431. `/project/${ctx.project_id}/get_and_flush_if_old?state=${ctx.project_state_hash}`
  432. )
  433. .reply(409)
  434. })
  435. it('should return no documents', async function (ctx) {
  436. const response = await ctx.handler.promises.getProjectDocsIfMatch(
  437. ctx.project_id,
  438. ctx.project_state_hash
  439. )
  440. expect(response).to.be.undefined
  441. })
  442. })
  443. })
  444. describe('clearProjectState', function () {
  445. describe('successfully', function () {
  446. beforeEach(function (ctx) {
  447. ctx.docUpdaterMock
  448. .post(`/project/${ctx.project_id}/clearState`)
  449. .reply(200)
  450. })
  451. it('should clear the project state from the document updater', async function (ctx) {
  452. await ctx.handler.promises.clearProjectState(ctx.project_id)
  453. expect(ctx.docUpdaterMock.isDone()).to.be.true
  454. })
  455. })
  456. describe('when the document updater API returns an error', function () {
  457. beforeEach(function (ctx) {
  458. ctx.docUpdaterMock
  459. .post(`/project/${ctx.project_id}/clearState`)
  460. .replyWithError('boom')
  461. })
  462. it('should reject with an error', async function (ctx) {
  463. await expect(ctx.handler.promises.clearProjectState(ctx.project_id)).to
  464. .be.rejected
  465. })
  466. })
  467. describe('when the document updater returns an error code', function () {
  468. beforeEach(function (ctx) {
  469. ctx.docUpdaterMock
  470. .post(`/project/${ctx.project_id}/clearState`)
  471. .reply(500)
  472. })
  473. it('should reject with an error', async function (ctx) {
  474. await expect(ctx.handler.promises.clearProjectState(ctx.project_id)).to
  475. .be.rejected
  476. })
  477. })
  478. })
  479. describe('acceptChanges', function () {
  480. beforeEach(function (ctx) {
  481. ctx.change_id = 'mock-change-id-1'
  482. })
  483. describe('successfully', function () {
  484. beforeEach(function (ctx) {
  485. ctx.docUpdaterMock
  486. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`, {
  487. change_ids: [ctx.change_id],
  488. })
  489. .reply(200)
  490. })
  491. it('should accept the change in the document updater', async function (ctx) {
  492. await ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
  493. ctx.change_id,
  494. ])
  495. expect(ctx.docUpdaterMock.isDone()).to.be.true
  496. })
  497. })
  498. describe('when the document updater API returns an error', function () {
  499. beforeEach(function (ctx) {
  500. ctx.docUpdaterMock
  501. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`)
  502. .replyWithError('boom')
  503. })
  504. it('should reject with an error', async function (ctx) {
  505. await expect(
  506. ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
  507. ctx.change_id,
  508. ])
  509. ).to.be.rejected
  510. })
  511. })
  512. describe('when the document updater returns a failure error code', function () {
  513. beforeEach(function (ctx) {
  514. ctx.docUpdaterMock
  515. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/change/accept`)
  516. .reply(500)
  517. })
  518. it('should reject with an error', async function (ctx) {
  519. await expect(
  520. ctx.handler.promises.acceptChanges(ctx.project_id, ctx.doc_id, [
  521. ctx.change_id,
  522. ])
  523. ).to.be.rejected
  524. })
  525. })
  526. })
  527. describe('deleteThread', function () {
  528. beforeEach(function (ctx) {
  529. ctx.thread_id = 'mock-thread-id-1'
  530. })
  531. describe('successfully', function () {
  532. beforeEach(function (ctx) {
  533. ctx.docUpdaterMock
  534. .delete(
  535. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
  536. )
  537. .reply(200)
  538. })
  539. it('should delete the thread in the document updater', async function (ctx) {
  540. await ctx.handler.promises.deleteThread(
  541. ctx.project_id,
  542. ctx.doc_id,
  543. ctx.thread_id,
  544. ctx.user_id
  545. )
  546. expect(ctx.docUpdaterMock.isDone()).to.be.true
  547. })
  548. })
  549. describe('when the document updater API returns an error', function () {
  550. beforeEach(function (ctx) {
  551. ctx.docUpdaterMock
  552. .delete(
  553. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
  554. )
  555. .replyWithError('boom')
  556. })
  557. it('should reject with an error', async function (ctx) {
  558. await expect(
  559. ctx.handler.promises.deleteThread(
  560. ctx.project_id,
  561. ctx.doc_id,
  562. ctx.thread_id,
  563. ctx.user_id
  564. )
  565. ).to.be.rejected
  566. })
  567. })
  568. describe('when the document updater returns a failure error code', function () {
  569. beforeEach(function (ctx) {
  570. ctx.docUpdaterMock
  571. .delete(
  572. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}`
  573. )
  574. .reply(500)
  575. })
  576. it('should reject with an error', async function (ctx) {
  577. await expect(
  578. ctx.handler.promises.deleteThread(
  579. ctx.project_id,
  580. ctx.doc_id,
  581. ctx.thread_id,
  582. ctx.user_id
  583. )
  584. ).to.be.rejected
  585. })
  586. })
  587. })
  588. describe('resolveThread', function () {
  589. beforeEach(function (ctx) {
  590. ctx.thread_id = 'mock-thread-id-1'
  591. })
  592. describe('successfully', function () {
  593. beforeEach(function (ctx) {
  594. ctx.docUpdaterMock
  595. .post(
  596. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
  597. )
  598. .reply(200)
  599. })
  600. it('should resolve the thread in the document updater', async function (ctx) {
  601. await ctx.handler.promises.resolveThread(
  602. ctx.project_id,
  603. ctx.doc_id,
  604. ctx.thread_id,
  605. ctx.user_id
  606. )
  607. expect(ctx.docUpdaterMock.isDone()).to.be.true
  608. })
  609. })
  610. describe('when the document updater API returns an error', function () {
  611. beforeEach(function (ctx) {
  612. ctx.docUpdaterMock
  613. .post(
  614. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
  615. )
  616. .replyWithError('boom')
  617. })
  618. it('should reject with an error', async function (ctx) {
  619. await expect(
  620. ctx.handler.promises.resolveThread(
  621. ctx.project_id,
  622. ctx.doc_id,
  623. ctx.thread_id,
  624. ctx.user_id
  625. )
  626. ).to.be.rejected
  627. })
  628. })
  629. describe('when the document updater returns a failure error code', function () {
  630. beforeEach(function (ctx) {
  631. ctx.docUpdaterMock
  632. .post(
  633. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/resolve`
  634. )
  635. .reply(500)
  636. })
  637. it('should reject with an error', async function (ctx) {
  638. await expect(
  639. ctx.handler.promises.resolveThread(
  640. ctx.project_id,
  641. ctx.doc_id,
  642. ctx.thread_id,
  643. ctx.user_id
  644. )
  645. ).to.be.rejected
  646. })
  647. })
  648. })
  649. describe('reopenThread', function () {
  650. beforeEach(function (ctx) {
  651. ctx.thread_id = 'mock-thread-id-1'
  652. })
  653. describe('successfully', function () {
  654. beforeEach(function (ctx) {
  655. ctx.docUpdaterMock
  656. .post(
  657. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
  658. )
  659. .reply(200)
  660. })
  661. it('should reopen the thread in the document updater', async function (ctx) {
  662. await ctx.handler.promises.reopenThread(
  663. ctx.project_id,
  664. ctx.doc_id,
  665. ctx.thread_id,
  666. ctx.user_id
  667. )
  668. expect(ctx.docUpdaterMock.isDone()).to.be.true
  669. })
  670. })
  671. describe('when the document updater API returns an error', function () {
  672. beforeEach(function (ctx) {
  673. ctx.docUpdaterMock
  674. .post(
  675. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
  676. )
  677. .replyWithError('boom')
  678. })
  679. it('should reject with an error', async function (ctx) {
  680. await expect(
  681. ctx.handler.promises.reopenThread(
  682. ctx.project_id,
  683. ctx.doc_id,
  684. ctx.thread_id,
  685. ctx.user_id
  686. )
  687. ).to.be.rejected
  688. })
  689. })
  690. describe('when the document updater returns a failure error code', function () {
  691. beforeEach(function (ctx) {
  692. ctx.docUpdaterMock
  693. .post(
  694. `/project/${ctx.project_id}/doc/${ctx.doc_id}/comment/${ctx.thread_id}/reopen`
  695. )
  696. .reply(500)
  697. })
  698. it('should reject with an error', async function (ctx) {
  699. await expect(
  700. ctx.handler.promises.reopenThread(
  701. ctx.project_id,
  702. ctx.doc_id,
  703. ctx.thread_id,
  704. ctx.user_id
  705. )
  706. ).to.be.rejected
  707. })
  708. })
  709. })
  710. describe('updateProjectStructure ', function () {
  711. beforeEach(function (ctx) {
  712. ctx.user_id = 1234
  713. ctx.version = 999
  714. })
  715. describe('with project history disabled', function () {
  716. beforeEach(function (ctx) {
  717. ctx.settings.apis.project_history.sendProjectStructureOps = false
  718. })
  719. it('returns early', async function (ctx) {
  720. await ctx.handler.promises.updateProjectStructure(
  721. ctx.project_id,
  722. ctx.projectHistoryId,
  723. ctx.user_id,
  724. {},
  725. ctx.source
  726. )
  727. })
  728. })
  729. describe('with project history enabled', function () {
  730. beforeEach(function (ctx) {
  731. ctx.settings.apis.project_history.sendProjectStructureOps = true
  732. })
  733. describe('when an entity has changed name', function () {
  734. it('should send the structure update to the document updater', async function (ctx) {
  735. const docIdA = new ObjectId()
  736. const docIdB = new ObjectId()
  737. const changes = {
  738. oldDocs: [
  739. { path: '/old_a', doc: { _id: docIdA } },
  740. { path: '/old_b', doc: { _id: docIdB } },
  741. ],
  742. // create new instances of the same ObjectIds so that == doesn't pass
  743. newDocs: [
  744. {
  745. path: '/old_a',
  746. doc: { _id: new ObjectId(docIdA.toString()) },
  747. },
  748. {
  749. path: '/new_b',
  750. doc: { _id: new ObjectId(docIdB.toString()) },
  751. },
  752. ],
  753. newProject: { version: ctx.version },
  754. }
  755. const updates = [
  756. {
  757. type: 'rename-doc',
  758. id: docIdB.toString(),
  759. pathname: '/old_b',
  760. newPathname: '/new_b',
  761. },
  762. ]
  763. ctx.docUpdaterMock
  764. .post(`/project/${ctx.project_id}`, {
  765. updates,
  766. userId: ctx.user_id,
  767. version: ctx.version,
  768. projectHistoryId: ctx.projectHistoryId,
  769. source: ctx.source,
  770. })
  771. .reply(204)
  772. await ctx.handler.promises.updateProjectStructure(
  773. ctx.project_id,
  774. ctx.projectHistoryId,
  775. ctx.user_id,
  776. changes,
  777. ctx.source
  778. )
  779. expect(ctx.docUpdaterMock.isDone()).to.be.true
  780. })
  781. })
  782. describe('when a doc has been added', function () {
  783. it('should send the structure update to the document updater', async function (ctx) {
  784. const docId = new ObjectId()
  785. const changes = {
  786. newDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
  787. newProject: { version: ctx.version },
  788. }
  789. const updates = [
  790. {
  791. type: 'add-doc',
  792. id: docId.toString(),
  793. pathname: '/foo',
  794. docLines: 'a\nb',
  795. historyRangesSupport: false,
  796. hash: undefined,
  797. ranges: undefined,
  798. metadata: undefined,
  799. createdBlob: true,
  800. },
  801. ]
  802. ctx.docUpdaterMock
  803. .post(`/project/${ctx.project_id}`, {
  804. updates,
  805. userId: ctx.user_id,
  806. version: ctx.version,
  807. projectHistoryId: ctx.projectHistoryId,
  808. source: ctx.source,
  809. })
  810. .reply(204)
  811. await ctx.handler.promises.updateProjectStructure(
  812. ctx.project_id,
  813. ctx.projectHistoryId,
  814. ctx.user_id,
  815. changes,
  816. ctx.source
  817. )
  818. expect(ctx.docUpdaterMock.isDone()).to.be.true
  819. })
  820. })
  821. describe('when a file has been added', function () {
  822. it('should send the structure update to the document updater', async function (ctx) {
  823. const fileId = new ObjectId()
  824. const changes = {
  825. newFiles: [
  826. {
  827. path: '/bar',
  828. file: { _id: fileId, hash: '12345' },
  829. },
  830. ],
  831. newProject: { version: ctx.version },
  832. }
  833. const updates = [
  834. {
  835. type: 'add-file',
  836. id: fileId.toString(),
  837. pathname: '/bar',
  838. docLines: undefined,
  839. historyRangesSupport: false,
  840. hash: '12345',
  841. ranges: undefined,
  842. createdBlob: true,
  843. metadata: undefined,
  844. },
  845. ]
  846. ctx.docUpdaterMock
  847. .post(`/project/${ctx.project_id}`, {
  848. updates,
  849. userId: ctx.user_id,
  850. version: ctx.version,
  851. projectHistoryId: ctx.projectHistoryId,
  852. source: ctx.source,
  853. })
  854. .reply(204)
  855. await ctx.handler.promises.updateProjectStructure(
  856. ctx.project_id,
  857. ctx.projectHistoryId,
  858. ctx.user_id,
  859. changes,
  860. ctx.source
  861. )
  862. expect(ctx.docUpdaterMock.isDone()).to.be.true
  863. })
  864. })
  865. describe('when an entity has been deleted', function () {
  866. it('should end the structure update to the document updater', async function (ctx) {
  867. const docId = new ObjectId()
  868. const changes = {
  869. oldDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
  870. newProject: { version: ctx.version },
  871. }
  872. const updates = [
  873. {
  874. type: 'rename-doc',
  875. id: docId.toString(),
  876. pathname: '/foo',
  877. newPathname: '',
  878. },
  879. ]
  880. ctx.docUpdaterMock
  881. .post(`/project/${ctx.project_id}`, {
  882. updates,
  883. userId: ctx.user_id,
  884. version: ctx.version,
  885. projectHistoryId: ctx.projectHistoryId,
  886. source: ctx.source,
  887. })
  888. .reply(204)
  889. await ctx.handler.promises.updateProjectStructure(
  890. ctx.project_id,
  891. ctx.projectHistoryId,
  892. ctx.user_id,
  893. changes,
  894. ctx.source
  895. )
  896. expect(ctx.docUpdaterMock.isDone()).to.be.true
  897. })
  898. })
  899. describe('when a file is converted to a doc', function () {
  900. it('should send the delete first', async function (ctx) {
  901. const docId = new ObjectId()
  902. const fileId = new ObjectId()
  903. const changes = {
  904. oldFiles: [
  905. {
  906. path: '/foo.doc',
  907. file: { _id: fileId },
  908. },
  909. ],
  910. newDocs: [
  911. {
  912. path: '/foo.doc',
  913. docLines: 'hello there',
  914. doc: { _id: docId },
  915. },
  916. ],
  917. newProject: { version: ctx.version },
  918. }
  919. const updates = [
  920. {
  921. type: 'rename-file',
  922. id: fileId.toString(),
  923. pathname: '/foo.doc',
  924. newPathname: '',
  925. },
  926. {
  927. type: 'add-doc',
  928. id: docId.toString(),
  929. pathname: '/foo.doc',
  930. docLines: 'hello there',
  931. historyRangesSupport: false,
  932. hash: undefined,
  933. ranges: undefined,
  934. metadata: undefined,
  935. createdBlob: true,
  936. },
  937. ]
  938. ctx.docUpdaterMock
  939. .post(`/project/${ctx.project_id}`, {
  940. updates,
  941. userId: ctx.user_id,
  942. version: ctx.version,
  943. projectHistoryId: ctx.projectHistoryId,
  944. source: ctx.source,
  945. })
  946. .reply(204)
  947. await ctx.handler.promises.updateProjectStructure(
  948. ctx.project_id,
  949. ctx.projectHistoryId,
  950. ctx.user_id,
  951. changes,
  952. ctx.source
  953. )
  954. expect(ctx.docUpdaterMock.isDone()).to.be.true
  955. })
  956. })
  957. describe('when the project version is missing', function () {
  958. it('should call the callback with an error', async function (ctx) {
  959. const docId = new ObjectId()
  960. const changes = {
  961. oldDocs: [{ path: '/foo', docLines: 'a\nb', doc: { _id: docId } }],
  962. }
  963. await expect(
  964. ctx.handler.promises.updateProjectStructure(
  965. ctx.project_id,
  966. ctx.projectHistoryId,
  967. ctx.user_id,
  968. changes,
  969. ctx.source
  970. )
  971. ).to.be.rejectedWith('did not receive project version in changes')
  972. })
  973. })
  974. describe('when ranges are present', function () {
  975. beforeEach(function (ctx) {
  976. ctx.docId = new ObjectId()
  977. ctx.ranges = {
  978. changes: [
  979. {
  980. op: { p: 0, i: 'foo' },
  981. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  982. },
  983. {
  984. op: { p: 7, d: ' baz' },
  985. metadata: { ts: '2024-02-01T00:00:00.000Z', user_id: 'user-1' },
  986. },
  987. ],
  988. comments: [
  989. {
  990. op: { p: 4, c: 'bar', t: 'comment-1' },
  991. metadata: { resolved: false },
  992. },
  993. ],
  994. }
  995. ctx.changes = {
  996. newDocs: [
  997. {
  998. path: '/foo',
  999. docLines: 'foo\nbar',
  1000. doc: { _id: ctx.docId },
  1001. ranges: ctx.ranges,
  1002. },
  1003. ],
  1004. newProject: { version: ctx.version },
  1005. }
  1006. })
  1007. it('should forward ranges', async function (ctx) {
  1008. const updates = [
  1009. {
  1010. type: 'add-doc',
  1011. id: ctx.docId.toString(),
  1012. pathname: '/foo',
  1013. docLines: 'foo\nbar',
  1014. historyRangesSupport: false,
  1015. hash: undefined,
  1016. ranges: ctx.ranges,
  1017. metadata: undefined,
  1018. createdBlob: true,
  1019. },
  1020. ]
  1021. ctx.docUpdaterMock
  1022. .post(`/project/${ctx.project_id}`, {
  1023. updates,
  1024. userId: ctx.user_id,
  1025. version: ctx.version,
  1026. projectHistoryId: ctx.projectHistoryId,
  1027. source: ctx.source,
  1028. })
  1029. .reply(204)
  1030. await ctx.handler.promises.updateProjectStructure(
  1031. ctx.project_id,
  1032. ctx.projectHistoryId,
  1033. ctx.user_id,
  1034. ctx.changes,
  1035. ctx.source
  1036. )
  1037. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1038. })
  1039. it('should include flag when history ranges support is enabled', async function (ctx) {
  1040. ctx.ProjectGetter.promises.getProjectWithoutLock
  1041. .withArgs(ctx.project_id)
  1042. .resolves({
  1043. _id: ctx.project_id,
  1044. overleaf: { history: { rangesSupportEnabled: true } },
  1045. })
  1046. const updates = [
  1047. {
  1048. type: 'add-doc',
  1049. id: ctx.docId.toString(),
  1050. pathname: '/foo',
  1051. docLines: 'foo\nbar',
  1052. historyRangesSupport: true,
  1053. hash: undefined,
  1054. ranges: ctx.ranges,
  1055. metadata: undefined,
  1056. createdBlob: true,
  1057. },
  1058. ]
  1059. ctx.docUpdaterMock
  1060. .post(`/project/${ctx.project_id}`, {
  1061. updates,
  1062. userId: ctx.user_id,
  1063. version: ctx.version,
  1064. projectHistoryId: ctx.projectHistoryId,
  1065. source: ctx.source,
  1066. })
  1067. .reply(204)
  1068. await ctx.handler.promises.updateProjectStructure(
  1069. ctx.project_id,
  1070. ctx.projectHistoryId,
  1071. ctx.user_id,
  1072. ctx.changes,
  1073. ctx.source
  1074. )
  1075. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1076. })
  1077. })
  1078. describe('with filestore disabled', function () {
  1079. beforeEach(function (ctx) {
  1080. ctx.fileId = new ObjectId()
  1081. const updates = [
  1082. {
  1083. type: 'add-file',
  1084. id: ctx.fileId.toString(),
  1085. pathname: '/bar',
  1086. docLines: undefined,
  1087. historyRangesSupport: false,
  1088. hash: '12345',
  1089. ranges: undefined,
  1090. createdBlob: true,
  1091. metadata: undefined,
  1092. },
  1093. ]
  1094. ctx.docUpdaterMock
  1095. .post(`/project/${ctx.project_id}`, {
  1096. updates,
  1097. userId: ctx.user_id,
  1098. version: ctx.version,
  1099. projectHistoryId: ctx.projectHistoryId,
  1100. source: ctx.source,
  1101. })
  1102. .reply(204)
  1103. })
  1104. it('should add files without URL and with createdBlob', async function (ctx) {
  1105. ctx.changes = {
  1106. newFiles: [
  1107. {
  1108. path: '/bar',
  1109. file: { _id: ctx.fileId, hash: '12345' },
  1110. },
  1111. ],
  1112. newProject: { version: ctx.version },
  1113. }
  1114. await ctx.handler.promises.updateProjectStructure(
  1115. ctx.project_id,
  1116. ctx.projectHistoryId,
  1117. ctx.user_id,
  1118. ctx.changes,
  1119. ctx.source
  1120. )
  1121. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1122. })
  1123. it('should flag files without hash', async function (ctx) {
  1124. ctx.fileId = new ObjectId()
  1125. ctx.changes = {
  1126. newFiles: [
  1127. {
  1128. path: '/bar',
  1129. file: { _id: ctx.fileId },
  1130. },
  1131. ],
  1132. newProject: { version: ctx.version },
  1133. }
  1134. await expect(
  1135. ctx.handler.promises.updateProjectStructure(
  1136. ctx.project_id,
  1137. ctx.projectHistoryId,
  1138. ctx.user_id,
  1139. ctx.changes,
  1140. ctx.source
  1141. )
  1142. ).to.be.rejected
  1143. })
  1144. })
  1145. })
  1146. })
  1147. describe('resyncProjectHistory', function () {
  1148. it('should add docs', async function (ctx) {
  1149. const docId1 = new ObjectId()
  1150. const docId2 = new ObjectId()
  1151. const docs = [
  1152. { doc: { _id: docId1 }, path: 'main.tex' },
  1153. { doc: { _id: docId2 }, path: 'references.bib' },
  1154. ]
  1155. const files = []
  1156. const projectId = new ObjectId()
  1157. const projectHistoryId = 99
  1158. ctx.docUpdaterMock
  1159. .post(`/project/${projectId}/history/resync`, {
  1160. docs: [
  1161. { doc: docId1.toString(), path: 'main.tex' },
  1162. { doc: docId2.toString(), path: 'references.bib' },
  1163. ],
  1164. files: [],
  1165. projectHistoryId,
  1166. })
  1167. .reply(200)
  1168. await ctx.handler.promises.resyncProjectHistory(
  1169. projectId,
  1170. projectHistoryId,
  1171. docs,
  1172. files,
  1173. {}
  1174. )
  1175. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1176. })
  1177. it('should add files', async function (ctx) {
  1178. const fileId1 = new ObjectId()
  1179. const fileId2 = new ObjectId()
  1180. const fileId3 = new ObjectId()
  1181. const fileCreated2 = new Date()
  1182. const fileCreated3 = new Date()
  1183. const otherProjectId = new ObjectId().toString()
  1184. const files = [
  1185. { file: { _id: fileId1, hash: '42' }, path: '1.png' },
  1186. {
  1187. file: {
  1188. _id: fileId2,
  1189. hash: '1337',
  1190. created: fileCreated2,
  1191. linkedFileData: {
  1192. provider: 'references-provider',
  1193. },
  1194. },
  1195. path: '1.bib',
  1196. },
  1197. {
  1198. file: {
  1199. _id: fileId3,
  1200. hash: '21',
  1201. created: fileCreated3,
  1202. linkedFileData: {
  1203. provider: 'project_output_file',
  1204. build_id: '1234-abc',
  1205. clsiServerId: 'server-1',
  1206. source_project_id: otherProjectId,
  1207. source_output_file_path: 'foo/bar.txt',
  1208. },
  1209. },
  1210. path: 'bar.txt',
  1211. },
  1212. ]
  1213. const docs = []
  1214. const projectId = new ObjectId()
  1215. const projectHistoryId = 99
  1216. ctx.docUpdaterMock
  1217. .post(`/project/${projectId}/history/resync`, {
  1218. docs: [],
  1219. files: [
  1220. {
  1221. file: fileId1.toString(),
  1222. _hash: '42',
  1223. path: '1.png',
  1224. createdBlob: true,
  1225. },
  1226. {
  1227. file: fileId2.toString(),
  1228. _hash: '1337',
  1229. path: '1.bib',
  1230. createdBlob: true,
  1231. metadata: {
  1232. importedAt: fileCreated2.toISOString(),
  1233. provider: 'references-provider',
  1234. },
  1235. },
  1236. {
  1237. file: fileId3.toString(),
  1238. _hash: '21',
  1239. path: 'bar.txt',
  1240. createdBlob: true,
  1241. metadata: {
  1242. importedAt: fileCreated3.toISOString(),
  1243. provider: 'project_output_file',
  1244. source_project_id: otherProjectId.toString(),
  1245. source_output_file_path: 'foo/bar.txt',
  1246. // build_id and clsiServerId are omitted
  1247. },
  1248. },
  1249. ],
  1250. projectHistoryId,
  1251. })
  1252. .reply(200)
  1253. await ctx.handler.promises.resyncProjectHistory(
  1254. projectId,
  1255. projectHistoryId,
  1256. docs,
  1257. files,
  1258. {}
  1259. )
  1260. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1261. })
  1262. it('should add files without URL', async function (ctx) {
  1263. const fileId1 = new ObjectId()
  1264. const fileId2 = new ObjectId()
  1265. const fileId3 = new ObjectId()
  1266. const fileCreated2 = new Date()
  1267. const fileCreated3 = new Date()
  1268. const otherProjectId = new ObjectId().toString()
  1269. const files = [
  1270. { file: { _id: fileId1, hash: '42' }, path: '1.png' },
  1271. {
  1272. file: {
  1273. _id: fileId2,
  1274. hash: '1337',
  1275. created: fileCreated2,
  1276. linkedFileData: {
  1277. provider: 'references-provider',
  1278. },
  1279. },
  1280. path: '1.bib',
  1281. },
  1282. {
  1283. file: {
  1284. _id: fileId3,
  1285. hash: '21',
  1286. created: fileCreated3,
  1287. linkedFileData: {
  1288. provider: 'project_output_file',
  1289. build_id: '1234-abc',
  1290. clsiServerId: 'server-1',
  1291. source_project_id: otherProjectId,
  1292. source_output_file_path: 'foo/bar.txt',
  1293. },
  1294. },
  1295. path: 'bar.txt',
  1296. },
  1297. ]
  1298. const docs = []
  1299. const projectId = new ObjectId()
  1300. const projectHistoryId = 99
  1301. ctx.docUpdaterMock
  1302. .post(`/project/${projectId}/history/resync`, {
  1303. docs: [],
  1304. files: [
  1305. {
  1306. file: fileId1.toString(),
  1307. _hash: '42',
  1308. path: '1.png',
  1309. createdBlob: true,
  1310. },
  1311. {
  1312. file: fileId2.toString(),
  1313. _hash: '1337',
  1314. path: '1.bib',
  1315. createdBlob: true,
  1316. metadata: {
  1317. importedAt: fileCreated2.toISOString(),
  1318. provider: 'references-provider',
  1319. },
  1320. },
  1321. {
  1322. file: fileId3.toString(),
  1323. _hash: '21',
  1324. path: 'bar.txt',
  1325. createdBlob: true,
  1326. metadata: {
  1327. importedAt: fileCreated3.toISOString(),
  1328. provider: 'project_output_file',
  1329. source_project_id: otherProjectId,
  1330. source_output_file_path: 'foo/bar.txt',
  1331. // build_id and clsiServerId are omitted
  1332. },
  1333. },
  1334. ],
  1335. projectHistoryId,
  1336. })
  1337. .reply(200)
  1338. await ctx.handler.promises.resyncProjectHistory(
  1339. projectId,
  1340. projectHistoryId,
  1341. docs,
  1342. files,
  1343. {}
  1344. )
  1345. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1346. })
  1347. it('should flag files with missing hashes', async function (ctx) {
  1348. const fileId1 = new ObjectId()
  1349. const fileId2 = new ObjectId()
  1350. const fileId3 = new ObjectId()
  1351. const fileCreated2 = new Date()
  1352. const fileCreated3 = new Date()
  1353. const otherProjectId = new ObjectId().toString()
  1354. const files = [
  1355. { file: { _id: fileId1, hash: '42' }, path: '1.png' },
  1356. {
  1357. file: {
  1358. _id: fileId2,
  1359. created: fileCreated2,
  1360. linkedFileData: {
  1361. provider: 'references-provider',
  1362. },
  1363. },
  1364. path: '1.bib',
  1365. },
  1366. {
  1367. file: {
  1368. _id: fileId3,
  1369. hash: '21',
  1370. created: fileCreated3,
  1371. linkedFileData: {
  1372. provider: 'project_output_file',
  1373. build_id: '1234-abc',
  1374. clsiServerId: 'server-1',
  1375. source_project_id: otherProjectId,
  1376. source_output_file_path: 'foo/bar.txt',
  1377. },
  1378. },
  1379. path: 'bar.txt',
  1380. },
  1381. ]
  1382. const docs = []
  1383. const projectId = new ObjectId()
  1384. const projectHistoryId = 99
  1385. await expect(
  1386. ctx.handler.promises.resyncProjectHistory(
  1387. projectId,
  1388. projectHistoryId,
  1389. docs,
  1390. files,
  1391. {}
  1392. )
  1393. ).to.be.rejected
  1394. })
  1395. })
  1396. describe('appendToDocument', function () {
  1397. describe('successfully', function () {
  1398. beforeEach(function (ctx) {
  1399. ctx.body = { rev: 1 }
  1400. ctx.docUpdaterMock
  1401. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`, {
  1402. lines: ctx.lines,
  1403. source: ctx.source,
  1404. user_id: ctx.user_id,
  1405. })
  1406. .reply(200)
  1407. })
  1408. it('should append to the document in the document updater', async function (ctx) {
  1409. await ctx.handler.promises.appendToDocument(
  1410. ctx.project_id,
  1411. ctx.doc_id,
  1412. ctx.user_id,
  1413. ctx.lines,
  1414. ctx.source
  1415. )
  1416. expect(ctx.docUpdaterMock.isDone()).to.be.true
  1417. })
  1418. })
  1419. describe('when the document updater API returns an error', function () {
  1420. beforeEach(function (ctx) {
  1421. ctx.docUpdaterMock
  1422. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`)
  1423. .replyWithError('boom')
  1424. })
  1425. it('should reject with an error', async function (ctx) {
  1426. await expect(
  1427. ctx.handler.promises.appendToDocument(
  1428. ctx.project_id,
  1429. ctx.doc_id,
  1430. ctx.user_id,
  1431. ctx.lines,
  1432. ctx.source
  1433. )
  1434. ).to.be.rejected
  1435. })
  1436. })
  1437. describe('when the document updater returns a failure error code', function () {
  1438. beforeEach(function (ctx) {
  1439. ctx.docUpdaterMock
  1440. .post(`/project/${ctx.project_id}/doc/${ctx.doc_id}/append`)
  1441. .reply(500)
  1442. })
  1443. it('should reject with an error', async function (ctx) {
  1444. await expect(
  1445. ctx.handler.promises.appendToDocument(
  1446. ctx.project_id,
  1447. ctx.doc_id,
  1448. ctx.user_id,
  1449. ctx.lines,
  1450. ctx.source
  1451. )
  1452. ).to.be.rejected
  1453. })
  1454. })
  1455. })
  1456. })