RestoreManager.test.mjs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  4. import tk from 'timekeeper'
  5. import moment from 'moment'
  6. const modulePath = '../../../../app/src/Features/History/RestoreManager'
  7. function nestedMapWithSetToObject(m) {
  8. return Object.fromEntries(
  9. Array.from(m.entries()).map(([key, set]) => [key, Array.from(set)])
  10. )
  11. }
  12. describe('RestoreManager', function () {
  13. beforeEach(async function (ctx) {
  14. tk.freeze(Date.now()) // freeze the time for these tests
  15. ctx.fsPath = '/tmp/path/on/disk'
  16. ctx.blobStream = 'blob-stream'
  17. vi.doMock('../../../../app/src/Features/Errors/Errors.js', () => ({
  18. default: Errors,
  19. }))
  20. vi.doMock(
  21. '../../../../app/src/Features/History/HistoryManager.mjs',
  22. () => ({
  23. default: (ctx.HistoryManager = {
  24. promises: {
  25. getContentAtVersion: sinon.stub().resolves({
  26. // Raw snapshot data that will be passed to Snapshot.fromRaw
  27. files: {
  28. 'main.tex': {
  29. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  30. stringLength: 100,
  31. metadata: {
  32. editorId: 'test-editor',
  33. },
  34. },
  35. 'foo.tex': {
  36. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  37. stringLength: 100,
  38. metadata: {
  39. editorId: 'test-editor',
  40. },
  41. },
  42. 'folder/file.tex': {
  43. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  44. stringLength: 100,
  45. metadata: {
  46. editorId: 'test-editor',
  47. },
  48. },
  49. 'foo.png': {
  50. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  51. stringLength: 100,
  52. metadata: {
  53. provider: 'bar',
  54. },
  55. },
  56. 'linkedFile.bib': {
  57. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  58. stringLength: 100,
  59. metadata: {
  60. provider: 'mendeley',
  61. },
  62. },
  63. 'withMainTrue.tex': {
  64. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  65. stringLength: 100,
  66. metadata: {
  67. main: true,
  68. },
  69. },
  70. },
  71. timestamp: new Date().toISOString(),
  72. }),
  73. requestBlob: sinon.stub().resolves({ stream: ctx.blobStream }),
  74. },
  75. }),
  76. })
  77. )
  78. vi.doMock('../../../../app/src/infrastructure/Metrics.js', () => ({
  79. default: {
  80. revertFileDurationSeconds: {
  81. startTimer: sinon.stub().returns(sinon.stub()),
  82. },
  83. revertProjectDurationSeconds: {
  84. startTimer: sinon.stub().returns(sinon.stub()),
  85. },
  86. },
  87. }))
  88. vi.doMock('@overleaf/settings', () => ({
  89. default: {},
  90. }))
  91. vi.doMock('../../../../app/src/infrastructure/FileWriter', () => ({
  92. default: (ctx.FileWriter = {
  93. promises: {
  94. writeStreamToDisk: sinon.stub().resolves(ctx.fsPath),
  95. writeContentToDisk: sinon.stub().resolves(ctx.fsPath),
  96. },
  97. }),
  98. }))
  99. vi.doMock(
  100. '../../../../app/src/Features/Uploads/FileSystemImportManager',
  101. () => ({
  102. default: (ctx.FileSystemImportManager = {
  103. promises: {},
  104. }),
  105. })
  106. )
  107. vi.doMock('../../../../app/src/Features/Editor/EditorController', () => ({
  108. default: (ctx.EditorController = {
  109. promises: {},
  110. }),
  111. }))
  112. vi.doMock('../../../../app/src/Features/Project/ProjectLocator', () => ({
  113. default: (ctx.ProjectLocator = { promises: {} }),
  114. }))
  115. vi.doMock(
  116. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
  117. () => ({
  118. default: (ctx.DocumentUpdaterHandler = {
  119. promises: { flushProjectToMongo: sinon.stub().resolves() },
  120. }),
  121. })
  122. )
  123. vi.doMock('../../../../app/src/Features/Docstore/DocstoreManager', () => ({
  124. default: (ctx.DocstoreManager = {
  125. promises: { getCommentThreadIds: sinon.stub().resolves({}) },
  126. }),
  127. }))
  128. vi.doMock('../../../../app/src/Features/Chat/ChatApiHandler', () => ({
  129. default: (ctx.ChatApiHandler = { promises: {} }),
  130. }))
  131. vi.doMock('../../../../app/src/Features/Chat/ChatManager', () => ({
  132. default: (ctx.ChatManager = { promises: {} }),
  133. }))
  134. vi.doMock(
  135. '../../../../app/src/Features/Editor/EditorRealTimeController',
  136. () => ({
  137. default: (ctx.EditorRealTimeController = {}),
  138. })
  139. )
  140. vi.doMock('../../../../app/src/Features/Project/ProjectGetter', () => ({
  141. default: (ctx.ProjectGetter = { promises: {} }),
  142. }))
  143. vi.doMock(
  144. '../../../../app/src/Features/Project/ProjectEntityHandler',
  145. () => ({
  146. default: (ctx.ProjectEntityHandler = {
  147. promises: {},
  148. }),
  149. })
  150. )
  151. vi.doMock('overleaf-editor-core', () => ({
  152. Snapshot: {
  153. fromRaw: sinon.stub().callsFake(snapshotData => ({
  154. getFile: pathname => ({
  155. getStringLength: sinon.stub().returns(100),
  156. getByteLength: sinon.stub().returns(100),
  157. getContent: sinon.stub().returns('foo\nbar\nbaz'),
  158. isEditable: sinon.stub().returns(true),
  159. getMetadata: sinon
  160. .stub()
  161. .returns(snapshotData?.files?.[pathname]?.metadata),
  162. getHash: sinon.stub().returns((ctx.hash = 'somehash')),
  163. }),
  164. getFilePathnames: sinon
  165. .stub()
  166. .returns(Object.keys(snapshotData.files || {})),
  167. getTimestamp: sinon
  168. .stub()
  169. .returns(
  170. snapshotData?.timestamp
  171. ? new Date(snapshotData.timestamp)
  172. : new Date()
  173. ),
  174. })),
  175. },
  176. getDocUpdaterCompatibleRanges: (ctx.getDocUpdaterCompatibleRanges = sinon
  177. .stub()
  178. .returns({
  179. changes: ctx.tracked_changes || [],
  180. comments: ctx.comments || [],
  181. })),
  182. }))
  183. ctx.RestoreManager = (await import(modulePath)).default
  184. ctx.user_id = 'mock-user-id'
  185. ctx.project_id = 'mock-project-id'
  186. ctx.version = 42
  187. // Add missing method mocks to RestoreManager
  188. ctx.RestoreManager.promises._getUpdatesFromHistory = sinon.stub().resolves([
  189. {
  190. toV: ctx.version,
  191. meta: { end_ts: new Date('2024-01-01T00:00:00.000Z') },
  192. },
  193. ])
  194. ctx.RestoreManager.promises._writeFileVersionToDisk = sinon
  195. .stub()
  196. .resolves('/tmp/mock-file-path')
  197. })
  198. afterEach(function () {
  199. tk.reset()
  200. })
  201. describe('restoreFileFromV2', function () {
  202. beforeEach(function (ctx) {
  203. ctx.RestoreManager.promises._writeFileVersionToDisk = sinon
  204. .stub()
  205. .resolves((ctx.fsPath = '/tmp/path/on/disk'))
  206. ctx.RestoreManager.promises._findOrCreateFolder = sinon
  207. .stub()
  208. .resolves((ctx.folder_id = 'mock-folder-id'))
  209. ctx.FileSystemImportManager.promises.addEntity = sinon
  210. .stub()
  211. .resolves((ctx.entity = 'mock-entity'))
  212. })
  213. describe('with a file not in a folder', function () {
  214. beforeEach(async function (ctx) {
  215. ctx.pathname = 'foo.tex'
  216. ctx.result = await ctx.RestoreManager.promises.restoreFileFromV2(
  217. ctx.user_id,
  218. ctx.project_id,
  219. ctx.version,
  220. ctx.pathname
  221. )
  222. })
  223. it('should write the file version to disk', function (ctx) {
  224. ctx.RestoreManager.promises._writeFileVersionToDisk
  225. .calledWith(ctx.project_id, ctx.version, ctx.pathname)
  226. .should.equal(true)
  227. })
  228. it('should find the root folder', function (ctx) {
  229. ctx.RestoreManager.promises._findOrCreateFolder
  230. .calledWith(ctx.project_id, '', ctx.user_id)
  231. .should.equal(true)
  232. })
  233. it('should add the entity', function (ctx) {
  234. ctx.FileSystemImportManager.promises.addEntity
  235. .calledWith(
  236. ctx.user_id,
  237. ctx.project_id,
  238. ctx.folder_id,
  239. 'foo.tex',
  240. ctx.fsPath,
  241. false
  242. )
  243. .should.equal(true)
  244. })
  245. it('should return the entity', function (ctx) {
  246. expect(ctx.result).to.equal(ctx.entity)
  247. })
  248. })
  249. describe('with a file in a folder', function () {
  250. beforeEach(async function (ctx) {
  251. ctx.pathname = 'foo/bar.tex'
  252. await ctx.RestoreManager.promises.restoreFileFromV2(
  253. ctx.user_id,
  254. ctx.project_id,
  255. ctx.version,
  256. ctx.pathname
  257. )
  258. })
  259. it('should find the folder', function (ctx) {
  260. ctx.RestoreManager.promises._findOrCreateFolder
  261. .calledWith(ctx.project_id, 'foo', ctx.user_id)
  262. .should.equal(true)
  263. })
  264. it('should add the entity by its basename', function (ctx) {
  265. ctx.FileSystemImportManager.promises.addEntity
  266. .calledWith(
  267. ctx.user_id,
  268. ctx.project_id,
  269. ctx.folder_id,
  270. 'bar.tex',
  271. ctx.fsPath,
  272. false
  273. )
  274. .should.equal(true)
  275. })
  276. })
  277. })
  278. describe('_findOrCreateFolder', function () {
  279. beforeEach(async function (ctx) {
  280. ctx.EditorController.promises.mkdirp = sinon.stub().resolves({
  281. newFolders: [],
  282. lastFolder: { _id: (ctx.folder_id = 'mock-folder-id') },
  283. })
  284. ctx.result = await ctx.RestoreManager.promises._findOrCreateFolder(
  285. ctx.project_id,
  286. 'folder/name',
  287. ctx.user_id
  288. )
  289. })
  290. it('should look up or create the folder', function (ctx) {
  291. ctx.EditorController.promises.mkdirp
  292. .calledWith(ctx.project_id, 'folder/name', ctx.user_id)
  293. .should.equal(true)
  294. })
  295. it('should return the folder_id', function (ctx) {
  296. expect(ctx.result).to.equal(ctx.folder_id)
  297. })
  298. })
  299. describe('_addEntityWithUniqueName', function () {
  300. beforeEach(function (ctx) {
  301. ctx.addEntityWithName = sinon.stub()
  302. ctx.filename = 'foo.tex'
  303. })
  304. describe('with a valid name', function () {
  305. beforeEach(async function (ctx) {
  306. ctx.addEntityWithName.resolves((ctx.entity = 'mock-entity'))
  307. ctx.result = await ctx.RestoreManager.promises._addEntityWithUniqueName(
  308. ctx.addEntityWithName,
  309. ctx.filename
  310. )
  311. })
  312. it('should add the entity', function (ctx) {
  313. ctx.addEntityWithName.calledWith(ctx.filename).should.equal(true)
  314. })
  315. it('should return the entity', function (ctx) {
  316. expect(ctx.result).to.equal(ctx.entity)
  317. })
  318. })
  319. describe('with a duplicate name', function () {
  320. beforeEach(async function (ctx) {
  321. ctx.addEntityWithName.rejects(new Errors.DuplicateNameError())
  322. ctx.addEntityWithName
  323. .onSecondCall()
  324. .resolves((ctx.entity = 'mock-entity'))
  325. ctx.result = await ctx.RestoreManager.promises._addEntityWithUniqueName(
  326. ctx.addEntityWithName,
  327. ctx.filename
  328. )
  329. })
  330. it('should try to add the entity with its original name', function (ctx) {
  331. ctx.addEntityWithName.calledWith('foo.tex').should.equal(true)
  332. })
  333. it('should try to add the entity with a unique name', function (ctx) {
  334. const date = moment(new Date()).format('Do MMM YY H:mm:ss')
  335. ctx.addEntityWithName
  336. .calledWith(`foo (Restored on ${date}).tex`)
  337. .should.equal(true)
  338. })
  339. it('should return the entity', function (ctx) {
  340. expect(ctx.result).to.equal(ctx.entity)
  341. })
  342. })
  343. })
  344. describe('revertFile', function () {
  345. beforeEach(function (ctx) {
  346. ctx.ProjectGetter.promises.getProject = sinon.stub()
  347. ctx.ProjectGetter.promises.getProject.withArgs(ctx.project_id).resolves({
  348. overleaf: { history: { rangesSupportEnabled: true } },
  349. rootDoc_id: 'root-doc-id',
  350. })
  351. ctx.RestoreManager.promises._findOrCreateFolder = sinon
  352. .stub()
  353. .resolves((ctx.folder_id = 'mock-folder-id'))
  354. ctx.FileSystemImportManager.promises.addEntity = sinon
  355. .stub()
  356. .resolves((ctx.entity = 'mock-entity'))
  357. })
  358. describe('reverting a project without ranges support', function () {
  359. beforeEach(function (ctx) {
  360. ctx.ProjectGetter.promises.getProject = sinon.stub().resolves({
  361. overleaf: { history: { rangesSupportEnabled: false } },
  362. })
  363. })
  364. it('should throw an error', async function (ctx) {
  365. await expect(
  366. ctx.RestoreManager.promises.revertFile(
  367. ctx.user_id,
  368. ctx.project_id,
  369. ctx.version,
  370. ctx.pathname
  371. )
  372. ).to.eventually.be.rejectedWith('project does not have ranges support')
  373. })
  374. })
  375. describe('reverting a document with ranges', function () {
  376. beforeEach(function (ctx) {
  377. ctx.pathname = 'foo.tex'
  378. ctx.comments = [
  379. {
  380. id: 'comment-in-other-doc',
  381. op: { t: 'comment-in-other-doc', p: 0, c: 'foo' },
  382. },
  383. {
  384. id: 'single-comment',
  385. op: { t: 'single-comment', p: 10, c: 'bar' },
  386. },
  387. {
  388. id: 'deleted-comment',
  389. op: { t: 'deleted-comment', p: 20, c: 'baz' },
  390. },
  391. ]
  392. ctx.remappedComments = [
  393. {
  394. id: 'duplicate-comment',
  395. op: { t: 'duplicate-comment', p: 0, c: 'foo' },
  396. },
  397. {
  398. id: 'single-comment',
  399. op: { t: 'single-comment', p: 10, c: 'bar' },
  400. },
  401. ]
  402. ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  403. ctx.DocstoreManager.promises.getCommentThreadIds = sinon
  404. .stub()
  405. .resolves({ 'other-doc': [ctx.comments[0].op.t] })
  406. ctx.ChatApiHandler.promises.duplicateCommentThreads = sinon
  407. .stub()
  408. .resolves({
  409. newThreads: {
  410. 'comment-in-other-doc': {
  411. duplicateId: 'duplicate-comment',
  412. },
  413. },
  414. })
  415. ctx.ChatApiHandler.promises.generateThreadData = sinon.stub().resolves(
  416. (ctx.threadData = {
  417. 'single-comment': {
  418. messages: [
  419. {
  420. content: 'message-content',
  421. timestamp: '2024-01-01T00:00:00.000Z',
  422. user_id: 'user-1',
  423. },
  424. ],
  425. },
  426. 'duplicate-comment': {
  427. messages: [
  428. {
  429. content: 'another message',
  430. timestamp: '2024-01-01T00:00:00.000Z',
  431. user_id: 'user-1',
  432. },
  433. ],
  434. },
  435. })
  436. )
  437. ctx.ChatManager.promises.injectUserInfoIntoThreads = sinon
  438. .stub()
  439. .resolves(ctx.threadData)
  440. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  441. ctx.tracked_changes = [
  442. {
  443. op: { pos: 4, i: 'bar' },
  444. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  445. },
  446. {
  447. op: { pos: 8, d: 'qux' },
  448. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-2' },
  449. },
  450. ]
  451. ctx.getDocUpdaterCompatibleRanges.returns({
  452. changes: ctx.tracked_changes,
  453. comments: ctx.comments,
  454. })
  455. ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
  456. .stub()
  457. .resolves([
  458. { toV: ctx.version, meta: { end_ts: (ctx.endTs = new Date()) } },
  459. ])
  460. ctx.EditorController.promises.addDocWithRanges = sinon
  461. .stub()
  462. .resolves((ctx.addedFile = { _id: 'mock-doc', type: 'doc' }))
  463. })
  464. describe("when reverting a file that doesn't current exist", function () {
  465. beforeEach(async function (ctx) {
  466. ctx.data = await ctx.RestoreManager.promises.revertFile(
  467. ctx.user_id,
  468. ctx.project_id,
  469. ctx.version,
  470. ctx.pathname
  471. )
  472. })
  473. it('should flush the document before fetching ranges', function (ctx) {
  474. expect(
  475. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
  476. ).to.have.been.calledBefore(
  477. ctx.DocstoreManager.promises.getCommentThreadIds
  478. )
  479. })
  480. it('should import the file', function (ctx) {
  481. expect(
  482. ctx.EditorController.promises.addDocWithRanges
  483. ).to.have.been.calledWith(
  484. ctx.project_id,
  485. ctx.folder_id,
  486. 'foo.tex',
  487. ['foo', 'bar', 'baz'],
  488. { changes: ctx.tracked_changes, comments: ctx.remappedComments }
  489. )
  490. })
  491. it('should return the created entity', function (ctx) {
  492. expect(ctx.data).to.deep.equal(ctx.addedFile)
  493. })
  494. })
  495. describe('with an existing file in the current project', function () {
  496. beforeEach(async function (ctx) {
  497. ctx.ProjectGetter.promises.getProject = sinon.stub()
  498. ctx.ProjectGetter.promises.getProject
  499. .withArgs(ctx.project_id)
  500. .resolves({
  501. overleaf: { history: { rangesSupportEnabled: true } },
  502. rootDoc_id: 'root-doc-id',
  503. })
  504. ctx.ProjectLocator.promises.findElementByPath = sinon
  505. .stub()
  506. .resolves({ type: 'file', element: { _id: 'mock-file-id' } })
  507. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  508. ctx.data = await ctx.RestoreManager.promises.revertFile(
  509. ctx.user_id,
  510. ctx.project_id,
  511. ctx.version,
  512. ctx.pathname
  513. )
  514. })
  515. it('should delete the existing file', async function (ctx) {
  516. expect(
  517. ctx.EditorController.promises.deleteEntity
  518. ).to.have.been.calledWith(
  519. ctx.project_id,
  520. 'mock-file-id',
  521. 'file',
  522. {
  523. kind: 'file-restore',
  524. path: ctx.pathname,
  525. version: ctx.version,
  526. timestamp: new Date(ctx.endTs).toISOString(),
  527. },
  528. ctx.user_id
  529. )
  530. })
  531. })
  532. describe('with an existing document in the current project', function () {
  533. beforeEach(async function (ctx) {
  534. ctx.ProjectGetter.promises.getProject = sinon.stub()
  535. ctx.ProjectGetter.promises.getProject
  536. .withArgs(ctx.project_id)
  537. .resolves({
  538. overleaf: { history: { rangesSupportEnabled: true } },
  539. rootDoc_id: 'root-doc-id',
  540. })
  541. ctx.ProjectLocator.promises.findElementByPath = sinon
  542. .stub()
  543. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  544. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  545. ctx.data = await ctx.RestoreManager.promises.revertFile(
  546. ctx.user_id,
  547. ctx.project_id,
  548. ctx.version,
  549. ctx.pathname
  550. )
  551. })
  552. it('should delete the existing document', async function (ctx) {
  553. expect(
  554. ctx.EditorController.promises.deleteEntity
  555. ).to.have.been.calledWith(
  556. ctx.project_id,
  557. 'mock-file-id',
  558. 'doc',
  559. {
  560. kind: 'file-restore',
  561. path: ctx.pathname,
  562. version: ctx.version,
  563. timestamp: new Date(ctx.endTs).toISOString(),
  564. },
  565. ctx.user_id
  566. )
  567. })
  568. it('should flush the document before fetching ranges', function (ctx) {
  569. expect(
  570. ctx.DocumentUpdaterHandler.promises.flushProjectToMongo
  571. ).to.have.been.calledBefore(
  572. ctx.DocstoreManager.promises.getCommentThreadIds
  573. )
  574. })
  575. it('should import the file', function (ctx) {
  576. expect(
  577. ctx.EditorController.promises.addDocWithRanges
  578. ).to.have.been.calledWith(
  579. ctx.project_id,
  580. ctx.folder_id,
  581. 'foo.tex',
  582. ['foo', 'bar', 'baz'],
  583. { changes: ctx.tracked_changes, comments: ctx.remappedComments },
  584. {
  585. kind: 'file-restore',
  586. path: ctx.pathname,
  587. version: ctx.version,
  588. timestamp: new Date(ctx.endTs).toISOString(),
  589. }
  590. )
  591. })
  592. it('should return the created entity', function (ctx) {
  593. expect(ctx.data).to.deep.equal(ctx.addedFile)
  594. })
  595. })
  596. describe('with comments in same doc', function () {
  597. // copy of the above, addition: inject and later inspect threadIds set
  598. beforeEach(async function (ctx) {
  599. ctx.ProjectGetter.promises.getProject = sinon.stub()
  600. ctx.ProjectGetter.promises.getProject
  601. .withArgs(ctx.project_id)
  602. .resolves({
  603. overleaf: { history: { rangesSupportEnabled: true } },
  604. rootDoc_id: 'root-doc-id',
  605. })
  606. ctx.ProjectLocator.promises.findElementByPath = sinon
  607. .stub()
  608. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  609. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  610. ctx.ChatApiHandler.promises.generateThreadData = sinon
  611. .stub()
  612. .resolves(
  613. (ctx.threadData = {
  614. [ctx.comments[0].op.t]: {
  615. messages: [
  616. {
  617. content: 'message',
  618. timestamp: '2024-01-01T00:00:00.000Z',
  619. user_id: 'user-1',
  620. },
  621. ],
  622. },
  623. [ctx.comments[1].op.t]: {
  624. messages: [
  625. {
  626. content: 'other message',
  627. timestamp: '2024-01-01T00:00:00.000Z',
  628. user_id: 'user-1',
  629. },
  630. ],
  631. },
  632. })
  633. )
  634. ctx.threadIds = new Map([
  635. [
  636. 'mock-file-id',
  637. new Set([ctx.comments[0].op.t, ctx.comments[1].op.t]),
  638. ],
  639. ])
  640. // Comments are updated in-place. Look up threads before reverting.
  641. ctx.afterThreadIds = {
  642. // mock-file-id removed
  643. [ctx.addedFile._id]: [ctx.comments[0].op.t, ctx.comments[1].op.t],
  644. }
  645. ctx.data = await ctx.RestoreManager.promises._revertSingleFile(
  646. ctx.user_id,
  647. ctx.project_id,
  648. ctx.version,
  649. ctx.pathname,
  650. ctx.threadIds,
  651. {
  652. getFile: sinon.stub().returns({
  653. getMetadata: sinon.stub().returns(undefined),
  654. getContent: sinon.stub().returns('foo\nbar\nbaz'),
  655. isEditable: sinon.stub().returns(true),
  656. }),
  657. },
  658. {
  659. origin: {
  660. kind: 'file-restore',
  661. path: 'foo.tex',
  662. timestamp: new Date(ctx.endTs).toISOString(),
  663. version: 42,
  664. },
  665. }
  666. )
  667. })
  668. it('should import the file with original comments minus the deleted one', function (ctx) {
  669. expect(
  670. ctx.EditorController.promises.addDocWithRanges
  671. ).to.have.been.calledWith(
  672. ctx.project_id,
  673. ctx.folder_id,
  674. 'foo.tex',
  675. ['foo', 'bar', 'baz'],
  676. {
  677. changes: ctx.tracked_changes,
  678. comments: ctx.comments.slice(0, 2),
  679. },
  680. {
  681. kind: 'file-restore',
  682. path: ctx.pathname,
  683. version: ctx.version,
  684. timestamp: new Date(ctx.endTs).toISOString(),
  685. }
  686. )
  687. })
  688. it('should add the seen thread ids to the map', function (ctx) {
  689. expect(nestedMapWithSetToObject(ctx.threadIds)).to.deep.equal(
  690. ctx.afterThreadIds
  691. )
  692. })
  693. })
  694. describe('with remapped comments during revertProject', function () {
  695. // copy of the above, addition: inject and later inspect threadIds set
  696. beforeEach(async function (ctx) {
  697. ctx.ProjectGetter.promises.getProject = sinon.stub()
  698. ctx.ProjectGetter.promises.getProject
  699. .withArgs(ctx.project_id)
  700. .resolves({
  701. overleaf: { history: { rangesSupportEnabled: true } },
  702. rootDoc_id: 'root-doc-id',
  703. })
  704. ctx.ProjectLocator.promises.findElementByPath = sinon
  705. .stub()
  706. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  707. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  708. ctx.threadIds = new Map([
  709. ['other-doc', new Set([ctx.comments[0].op.t])],
  710. ])
  711. // Comments are updated in-place. Look up threads before reverting.
  712. ctx.afterThreadIds = {
  713. // mock-file-id removed
  714. 'other-doc': [ctx.comments[0].op.t],
  715. [ctx.addedFile._id]: [
  716. ctx.remappedComments[0].op.t,
  717. ctx.remappedComments[1].op.t,
  718. ],
  719. }
  720. ctx.data = await ctx.RestoreManager.promises._revertSingleFile(
  721. ctx.user_id,
  722. ctx.project_id,
  723. ctx.version,
  724. ctx.pathname,
  725. ctx.threadIds,
  726. {
  727. getFile: sinon.stub().returns({
  728. getMetadata: sinon.stub().returns(undefined),
  729. getContent: sinon.stub().returns('foo\nbar\nbaz'),
  730. isEditable: sinon.stub().returns(true),
  731. }),
  732. },
  733. {
  734. origin: {
  735. kind: 'file-restore',
  736. path: 'foo.tex',
  737. timestamp: new Date(ctx.endTs).toISOString(),
  738. version: 42,
  739. },
  740. }
  741. )
  742. })
  743. it('should import the file', function (ctx) {
  744. expect(
  745. ctx.EditorController.promises.addDocWithRanges
  746. ).to.have.been.calledWith(
  747. ctx.project_id,
  748. ctx.folder_id,
  749. 'foo.tex',
  750. ['foo', 'bar', 'baz'],
  751. { changes: ctx.tracked_changes, comments: ctx.remappedComments },
  752. {
  753. kind: 'file-restore',
  754. path: ctx.pathname,
  755. version: ctx.version,
  756. timestamp: new Date(ctx.endTs).toISOString(),
  757. }
  758. )
  759. })
  760. it('should add the seen thread ids to the map', function (ctx) {
  761. expect(nestedMapWithSetToObject(ctx.threadIds)).to.deep.equal(
  762. ctx.afterThreadIds
  763. )
  764. })
  765. })
  766. describe('when restored file has the same id as root doc', function () {
  767. beforeEach(async function (ctx) {
  768. ctx.ProjectGetter.promises.getProject = sinon.stub()
  769. ctx.ProjectGetter.promises.getProject
  770. .withArgs(ctx.project_id)
  771. .resolves({
  772. overleaf: { history: { rangesSupportEnabled: true } },
  773. rootDoc_id: 'root-doc-id',
  774. })
  775. ctx.ProjectLocator.promises.findElementByPath = sinon
  776. .stub()
  777. .resolves({ type: 'doc', element: { _id: 'root-doc-id' } })
  778. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  779. ctx.EditorController.promises.addDocWithRanges = sinon
  780. .stub()
  781. .resolves((ctx.addedFile = { _id: 'new-doc-id', type: 'doc' }))
  782. ctx.EditorController.promises.setRootDoc = sinon.stub().resolves()
  783. ctx.data = await ctx.RestoreManager.promises.revertFile(
  784. ctx.user_id,
  785. ctx.project_id,
  786. ctx.version,
  787. ctx.pathname
  788. )
  789. })
  790. it('should delete the existing root document', async function (ctx) {
  791. expect(
  792. ctx.EditorController.promises.deleteEntity
  793. ).to.have.been.calledWith(
  794. ctx.project_id,
  795. 'root-doc-id',
  796. 'doc',
  797. {
  798. kind: 'file-restore',
  799. path: ctx.pathname,
  800. version: ctx.version,
  801. timestamp: new Date(ctx.endTs).toISOString(),
  802. },
  803. ctx.user_id
  804. )
  805. })
  806. it('should import the file', function (ctx) {
  807. expect(
  808. ctx.EditorController.promises.addDocWithRanges
  809. ).to.have.been.calledWith(
  810. ctx.project_id,
  811. ctx.folder_id,
  812. 'foo.tex',
  813. ['foo', 'bar', 'baz'],
  814. { changes: ctx.tracked_changes, comments: ctx.remappedComments },
  815. {
  816. kind: 'file-restore',
  817. path: ctx.pathname,
  818. version: ctx.version,
  819. timestamp: new Date(ctx.endTs).toISOString(),
  820. }
  821. )
  822. })
  823. it('should return the created entity with root doc id', function (ctx) {
  824. expect(ctx.data).to.deep.equal(ctx.addedFile)
  825. expect(ctx.data._id).to.equal('new-doc-id')
  826. })
  827. it('should set the restored document as the new root doc', function (ctx) {
  828. expect(
  829. ctx.EditorController.promises.setRootDoc
  830. ).to.have.been.calledWith(ctx.project_id, ctx.addedFile._id)
  831. })
  832. })
  833. })
  834. describe('reverting a file or document with metadata', function () {
  835. beforeEach(function (ctx) {
  836. ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  837. ctx.EditorController.promises.addDocWithRanges = sinon.stub()
  838. ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
  839. .stub()
  840. .resolves([
  841. { toV: ctx.version, meta: { end_ts: (ctx.endTs = new Date()) } },
  842. ])
  843. ctx.EditorController.promises.upsertFile = sinon
  844. .stub()
  845. .resolves({ _id: 'mock-file-id', type: 'file' })
  846. ctx.EditorController.promises.addDocWithRanges = sinon
  847. .stub()
  848. .resolves((ctx.addedFile = { _id: 'mock-doc-id', type: 'doc' }))
  849. ctx.DocstoreManager.promises.getCommentThreadIds = sinon
  850. .stub()
  851. .resolves({})
  852. ctx.ChatApiHandler.promises.generateThreadData = sinon
  853. .stub()
  854. .resolves({})
  855. ctx.ChatManager.promises.injectUserInfoIntoThreads = sinon
  856. .stub()
  857. .resolves({})
  858. ctx.EditorRealTimeController.emitToRoom = sinon.stub()
  859. })
  860. describe('when reverting a linked file', function () {
  861. beforeEach(async function (ctx) {
  862. ctx.pathname = 'foo.png'
  863. ctx.result = await ctx.RestoreManager.promises.revertFile(
  864. ctx.user_id,
  865. ctx.project_id,
  866. ctx.version,
  867. ctx.pathname
  868. )
  869. })
  870. it('should revert it as a file', function (ctx) {
  871. expect(ctx.result).to.deep.equal({
  872. _id: 'mock-file-id',
  873. type: 'file',
  874. })
  875. })
  876. it('should upload to the project as a file', function (ctx) {
  877. expect(
  878. ctx.EditorController.promises.upsertFile
  879. ).to.have.been.calledWith(
  880. ctx.project_id,
  881. 'mock-folder-id',
  882. 'foo.png',
  883. ctx.fsPath,
  884. { provider: 'bar' },
  885. {
  886. kind: 'file-restore',
  887. path: ctx.pathname,
  888. version: ctx.version,
  889. timestamp: new Date(ctx.endTs).toISOString(),
  890. },
  891. ctx.user_id
  892. )
  893. })
  894. it('should not try to add a document', function (ctx) {
  895. expect(ctx.EditorController.promises.addDocWithRanges).to.not.have
  896. .been.called
  897. })
  898. })
  899. describe('when reverting a linked document with provider', function () {
  900. beforeEach(async function (ctx) {
  901. ctx.pathname = 'linkedFile.bib'
  902. ctx.result = await ctx.RestoreManager.promises.revertFile(
  903. ctx.user_id,
  904. ctx.project_id,
  905. ctx.version,
  906. ctx.pathname
  907. )
  908. })
  909. it('should revert it as a file', function (ctx) {
  910. expect(ctx.result).to.deep.equal({
  911. _id: 'mock-file-id',
  912. type: 'file',
  913. })
  914. })
  915. it('should upload to the project as a file', function (ctx) {
  916. expect(
  917. ctx.EditorController.promises.upsertFile
  918. ).to.have.been.calledWith(
  919. ctx.project_id,
  920. 'mock-folder-id',
  921. 'linkedFile.bib',
  922. ctx.fsPath,
  923. { provider: 'mendeley' },
  924. {
  925. kind: 'file-restore',
  926. path: ctx.pathname,
  927. version: ctx.version,
  928. timestamp: new Date(ctx.endTs).toISOString(),
  929. },
  930. ctx.user_id
  931. )
  932. })
  933. it('should not try to add a document', function (ctx) {
  934. expect(ctx.EditorController.promises.addDocWithRanges).to.not.have
  935. .been.called
  936. })
  937. })
  938. describe('when reverting a linked document with { main: true }', function () {
  939. beforeEach(async function (ctx) {
  940. ctx.pathname = 'withMainTrue.tex'
  941. ctx.result = await ctx.RestoreManager.promises.revertFile(
  942. ctx.user_id,
  943. ctx.project_id,
  944. ctx.version,
  945. ctx.pathname
  946. )
  947. })
  948. it('should revert it as a document', function (ctx) {
  949. expect(ctx.result).to.deep.equal({
  950. _id: 'mock-doc-id',
  951. type: 'doc',
  952. })
  953. })
  954. it('should not upload to the project as a file', function (ctx) {
  955. expect(ctx.EditorController.promises.upsertFile).to.not.have.been
  956. .called
  957. })
  958. it('should add the document', function (ctx) {
  959. expect(
  960. ctx.EditorController.promises.addDocWithRanges
  961. ).to.have.been.calledWith(
  962. ctx.project_id,
  963. ctx.folder_id,
  964. 'withMainTrue.tex',
  965. ['foo', 'bar', 'baz'],
  966. { changes: [], comments: [] }
  967. )
  968. })
  969. })
  970. })
  971. describe('when reverting a binary file', function () {
  972. beforeEach(async function (ctx) {
  973. ctx.pathname = 'foo.png'
  974. ctx.EditorController.promises.upsertFile = sinon
  975. .stub()
  976. .resolves({ _id: 'mock-file-id', type: 'file' })
  977. ctx.EditorController.promises.deleteEntity = sinon.stub().resolves()
  978. ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
  979. .stub()
  980. .resolves([{ toV: ctx.version, meta: { end_ts: Date.now() } }])
  981. })
  982. it('should return the created entity if file exists', async function (ctx) {
  983. ctx.ProjectLocator.promises.findElementByPath = sinon
  984. .stub()
  985. .resolves({ type: 'file', element: { _id: 'existing-file-id' } })
  986. const revertRes = await ctx.RestoreManager.promises.revertFile(
  987. ctx.user_id,
  988. ctx.project_id,
  989. ctx.version,
  990. ctx.pathname
  991. )
  992. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  993. })
  994. it('should return the created entity if file does not exists', async function (ctx) {
  995. ctx.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  996. const revertRes = await ctx.RestoreManager.promises.revertFile(
  997. ctx.user_id,
  998. ctx.project_id,
  999. ctx.version,
  1000. ctx.pathname
  1001. )
  1002. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  1003. })
  1004. })
  1005. })
  1006. describe('revertProject', function () {
  1007. beforeEach(function (ctx) {
  1008. ctx.ProjectGetter.promises.getProject = sinon.stub()
  1009. ctx.ProjectGetter.promises.getProject
  1010. .withArgs(ctx.project_id)
  1011. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  1012. ctx.RestoreManager.promises._revertSingleFile = sinon.stub().resolves({
  1013. _id: 'mock-doc-id',
  1014. type: 'doc',
  1015. })
  1016. ctx.RestoreManager.promises._getProjectPathsAtVersion = sinon
  1017. .stub()
  1018. .resolves([])
  1019. ctx.ProjectEntityHandler.promises.getAllEntities = sinon
  1020. .stub()
  1021. .resolves({ docs: [], files: [] })
  1022. ctx.EditorController.promises.deleteEntityWithPath = sinon
  1023. .stub()
  1024. .resolves()
  1025. ctx.RestoreManager.promises._getUpdatesFromHistory = sinon
  1026. .stub()
  1027. .resolves([
  1028. { toV: ctx.version, meta: { end_ts: (ctx.end_ts = Date.now()) } },
  1029. ])
  1030. })
  1031. describe('reverting a project without ranges support', function () {
  1032. beforeEach(function (ctx) {
  1033. ctx.ProjectGetter.promises.getProject = sinon.stub().resolves({
  1034. overleaf: { history: { rangesSupportEnabled: false } },
  1035. })
  1036. })
  1037. it('should throw an error', async function (ctx) {
  1038. await expect(
  1039. ctx.RestoreManager.promises.revertProject(
  1040. ctx.user_id,
  1041. ctx.project_id,
  1042. ctx.version
  1043. )
  1044. ).to.eventually.be.rejectedWith('project does not have ranges support')
  1045. })
  1046. })
  1047. describe('for a project with overlap in current files and old files', function () {
  1048. beforeEach(async function (ctx) {
  1049. ctx.HistoryManager.promises.getContentAtVersion = sinon
  1050. .stub()
  1051. .resolves({
  1052. files: {
  1053. 'main.tex': {
  1054. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  1055. stringLength: 100,
  1056. metadata: {
  1057. editorId: 'test-editor',
  1058. },
  1059. },
  1060. 'figures/image.png': {
  1061. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  1062. stringLength: 100,
  1063. metadata: {
  1064. provider: 'bar',
  1065. },
  1066. },
  1067. 'since-deleted.tex': {
  1068. hash: 'abcdef1234567890abcdef1234567890abcdef12',
  1069. stringLength: 100,
  1070. metadata: {
  1071. editorId: 'test-editor',
  1072. },
  1073. },
  1074. },
  1075. timestamp: new Date().toISOString(),
  1076. })
  1077. ctx.ProjectEntityHandler.promises.getAllEntities = sinon
  1078. .stub()
  1079. .resolves({
  1080. docs: [{ path: '/main.tex' }, { path: '/new-file.tex' }],
  1081. files: [{ path: '/figures/image.png' }],
  1082. })
  1083. await ctx.RestoreManager.promises.revertProject(
  1084. ctx.user_id,
  1085. ctx.project_id,
  1086. ctx.version
  1087. )
  1088. ctx.origin = {
  1089. kind: 'project-restore',
  1090. version: ctx.version,
  1091. timestamp: new Date(ctx.end_ts).toISOString(),
  1092. }
  1093. })
  1094. it('should delete the old files', function (ctx) {
  1095. expect(
  1096. ctx.EditorController.promises.deleteEntityWithPath
  1097. ).to.have.been.calledWith(
  1098. ctx.project_id,
  1099. 'new-file.tex',
  1100. ctx.origin,
  1101. ctx.user_id
  1102. )
  1103. })
  1104. it('should not delete the current files', function (ctx) {
  1105. expect(
  1106. ctx.EditorController.promises.deleteEntityWithPath
  1107. ).to.not.have.been.calledWith(
  1108. ctx.project_id,
  1109. 'main.tex',
  1110. ctx.origin,
  1111. ctx.user_id
  1112. )
  1113. expect(
  1114. ctx.EditorController.promises.deleteEntityWithPath
  1115. ).to.not.have.been.calledWith(
  1116. ctx.project_id,
  1117. 'figures/image.png',
  1118. ctx.origin,
  1119. ctx.user_id
  1120. )
  1121. })
  1122. it('should revert the old files', function (ctx) {
  1123. expect(
  1124. ctx.RestoreManager.promises._revertSingleFile
  1125. ).to.have.been.calledWith(
  1126. ctx.user_id,
  1127. ctx.project_id,
  1128. ctx.version,
  1129. 'main.tex'
  1130. )
  1131. expect(
  1132. ctx.RestoreManager.promises._revertSingleFile
  1133. ).to.have.been.calledWith(
  1134. ctx.user_id,
  1135. ctx.project_id,
  1136. ctx.version,
  1137. 'figures/image.png'
  1138. )
  1139. expect(
  1140. ctx.RestoreManager.promises._revertSingleFile
  1141. ).to.have.been.calledWith(
  1142. ctx.user_id,
  1143. ctx.project_id,
  1144. ctx.version,
  1145. 'since-deleted.tex'
  1146. )
  1147. })
  1148. it('should not revert the current files', function (ctx) {
  1149. expect(
  1150. ctx.RestoreManager.promises._revertSingleFile
  1151. ).to.not.have.been.calledWith(
  1152. ctx.user_id,
  1153. ctx.project_id,
  1154. ctx.version,
  1155. 'new-file.tex'
  1156. )
  1157. })
  1158. })
  1159. })
  1160. })