RestoreManagerTests.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../../app/src/Features/History/RestoreManager'
  6. )
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const tk = require('timekeeper')
  9. const moment = require('moment')
  10. const { expect } = require('chai')
  11. describe('RestoreManager', function () {
  12. beforeEach(function () {
  13. tk.freeze(Date.now()) // freeze the time for these tests
  14. this.RestoreManager = SandboxedModule.require(modulePath, {
  15. requires: {
  16. '@overleaf/settings': {},
  17. '../../infrastructure/FileWriter': (this.FileWriter = { promises: {} }),
  18. '../Uploads/FileSystemImportManager': (this.FileSystemImportManager = {
  19. promises: {},
  20. }),
  21. '../Editor/EditorController': (this.EditorController = {
  22. promises: {},
  23. }),
  24. '../Project/ProjectLocator': (this.ProjectLocator = { promises: {} }),
  25. '../DocumentUpdater/DocumentUpdaterHandler':
  26. (this.DocumentUpdaterHandler = {
  27. promises: { flushProjectToMongo: sinon.stub().resolves() },
  28. }),
  29. '../Docstore/DocstoreManager': (this.DocstoreManager = {
  30. promises: {},
  31. }),
  32. '../Chat/ChatApiHandler': (this.ChatApiHandler = { promises: {} }),
  33. '../Chat/ChatManager': (this.ChatManager = { promises: {} }),
  34. '../Editor/EditorRealTimeController': (this.EditorRealTimeController =
  35. {}),
  36. '../Project/ProjectGetter': (this.ProjectGetter = { promises: {} }),
  37. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {
  38. promises: {},
  39. }),
  40. },
  41. })
  42. this.user_id = 'mock-user-id'
  43. this.project_id = 'mock-project-id'
  44. this.version = 42
  45. })
  46. afterEach(function () {
  47. tk.reset()
  48. })
  49. describe('restoreFileFromV2', function () {
  50. beforeEach(function () {
  51. this.RestoreManager.promises._writeFileVersionToDisk = sinon
  52. .stub()
  53. .resolves((this.fsPath = '/tmp/path/on/disk'))
  54. this.RestoreManager.promises._findOrCreateFolder = sinon
  55. .stub()
  56. .resolves((this.folder_id = 'mock-folder-id'))
  57. this.FileSystemImportManager.promises.addEntity = sinon
  58. .stub()
  59. .resolves((this.entity = 'mock-entity'))
  60. })
  61. describe('with a file not in a folder', function () {
  62. beforeEach(async function () {
  63. this.pathname = 'foo.tex'
  64. this.result = await this.RestoreManager.promises.restoreFileFromV2(
  65. this.user_id,
  66. this.project_id,
  67. this.version,
  68. this.pathname
  69. )
  70. })
  71. it('should write the file version to disk', function () {
  72. this.RestoreManager.promises._writeFileVersionToDisk
  73. .calledWith(this.project_id, this.version, this.pathname)
  74. .should.equal(true)
  75. })
  76. it('should find the root folder', function () {
  77. this.RestoreManager.promises._findOrCreateFolder
  78. .calledWith(this.project_id, '')
  79. .should.equal(true)
  80. })
  81. it('should add the entity', function () {
  82. this.FileSystemImportManager.promises.addEntity
  83. .calledWith(
  84. this.user_id,
  85. this.project_id,
  86. this.folder_id,
  87. 'foo.tex',
  88. this.fsPath,
  89. false
  90. )
  91. .should.equal(true)
  92. })
  93. it('should return the entity', function () {
  94. expect(this.result).to.equal(this.entity)
  95. })
  96. })
  97. describe('with a file in a folder', function () {
  98. beforeEach(async function () {
  99. this.pathname = 'foo/bar.tex'
  100. await this.RestoreManager.promises.restoreFileFromV2(
  101. this.user_id,
  102. this.project_id,
  103. this.version,
  104. this.pathname
  105. )
  106. })
  107. it('should find the folder', function () {
  108. this.RestoreManager.promises._findOrCreateFolder
  109. .calledWith(this.project_id, 'foo')
  110. .should.equal(true)
  111. })
  112. it('should add the entity by its basename', function () {
  113. this.FileSystemImportManager.promises.addEntity
  114. .calledWith(
  115. this.user_id,
  116. this.project_id,
  117. this.folder_id,
  118. 'bar.tex',
  119. this.fsPath,
  120. false
  121. )
  122. .should.equal(true)
  123. })
  124. })
  125. })
  126. describe('_findOrCreateFolder', function () {
  127. beforeEach(async function () {
  128. this.EditorController.promises.mkdirp = sinon.stub().resolves({
  129. newFolders: [],
  130. lastFolder: { _id: (this.folder_id = 'mock-folder-id') },
  131. })
  132. this.result = await this.RestoreManager.promises._findOrCreateFolder(
  133. this.project_id,
  134. 'folder/name'
  135. )
  136. })
  137. it('should look up or create the folder', function () {
  138. this.EditorController.promises.mkdirp
  139. .calledWith(this.project_id, 'folder/name')
  140. .should.equal(true)
  141. })
  142. it('should return the folder_id', function () {
  143. expect(this.result).to.equal(this.folder_id)
  144. })
  145. })
  146. describe('_addEntityWithUniqueName', function () {
  147. beforeEach(function () {
  148. this.addEntityWithName = sinon.stub()
  149. this.name = 'foo.tex'
  150. })
  151. describe('with a valid name', function () {
  152. beforeEach(async function () {
  153. this.addEntityWithName.resolves((this.entity = 'mock-entity'))
  154. this.result =
  155. await this.RestoreManager.promises._addEntityWithUniqueName(
  156. this.addEntityWithName,
  157. this.name
  158. )
  159. })
  160. it('should add the entity', function () {
  161. this.addEntityWithName.calledWith(this.name).should.equal(true)
  162. })
  163. it('should return the entity', function () {
  164. expect(this.result).to.equal(this.entity)
  165. })
  166. })
  167. describe('with a duplicate name', function () {
  168. beforeEach(async function () {
  169. this.addEntityWithName.rejects(new Errors.DuplicateNameError())
  170. this.addEntityWithName
  171. .onSecondCall()
  172. .resolves((this.entity = 'mock-entity'))
  173. this.result =
  174. await this.RestoreManager.promises._addEntityWithUniqueName(
  175. this.addEntityWithName,
  176. this.name
  177. )
  178. })
  179. it('should try to add the entity with its original name', function () {
  180. this.addEntityWithName.calledWith('foo.tex').should.equal(true)
  181. })
  182. it('should try to add the entity with a unique name', function () {
  183. const date = moment(new Date()).format('Do MMM YY H:mm:ss')
  184. this.addEntityWithName
  185. .calledWith(`foo (Restored on ${date}).tex`)
  186. .should.equal(true)
  187. })
  188. it('should return the entity', function () {
  189. expect(this.result).to.equal(this.entity)
  190. })
  191. })
  192. })
  193. describe('revertFile', function () {
  194. beforeEach(function () {
  195. this.ProjectGetter.promises.getProject = sinon.stub()
  196. this.ProjectGetter.promises.getProject
  197. .withArgs(this.project_id)
  198. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  199. this.RestoreManager.promises._writeFileVersionToDisk = sinon
  200. .stub()
  201. .resolves((this.fsPath = '/tmp/path/on/disk'))
  202. this.RestoreManager.promises._findOrCreateFolder = sinon
  203. .stub()
  204. .resolves((this.folder_id = 'mock-folder-id'))
  205. this.FileSystemImportManager.promises.addEntity = sinon
  206. .stub()
  207. .resolves((this.entity = 'mock-entity'))
  208. this.RestoreManager.promises._getRangesFromHistory = sinon
  209. .stub()
  210. .rejects()
  211. this.RestoreManager.promises._getMetadataFromHistory = sinon
  212. .stub()
  213. .resolves({ metadata: undefined })
  214. })
  215. describe('reverting a project without ranges support', function () {
  216. beforeEach(function () {
  217. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  218. overleaf: { history: { rangesSupportEnabled: false } },
  219. })
  220. })
  221. it('should throw an error', async function () {
  222. await expect(
  223. this.RestoreManager.promises.revertFile(
  224. this.user_id,
  225. this.project_id,
  226. this.version,
  227. this.pathname
  228. )
  229. ).to.eventually.be.rejectedWith('project does not have ranges support')
  230. })
  231. })
  232. describe('reverting a document with ranges', function () {
  233. beforeEach(function () {
  234. this.pathname = 'foo.tex'
  235. this.comments = [
  236. { op: { t: 'comment-in-other-doc', p: 0, c: 'foo' } },
  237. { op: { t: 'single-comment', p: 10, c: 'bar' } },
  238. { op: { t: 'deleted-comment', p: 20, c: 'baz' } },
  239. ]
  240. this.remappedComments = [
  241. { op: { t: 'duplicate-comment', p: 0, c: 'foo' } },
  242. { op: { t: 'single-comment', p: 10, c: 'bar' } },
  243. ]
  244. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  245. this.DocstoreManager.promises.getAllRanges = sinon.stub().resolves([
  246. {
  247. ranges: {
  248. comments: this.comments.slice(0, 1),
  249. },
  250. },
  251. ])
  252. this.ChatApiHandler.promises.duplicateCommentThreads = sinon
  253. .stub()
  254. .resolves({
  255. newThreads: {
  256. 'comment-in-other-doc': {
  257. duplicateId: 'duplicate-comment',
  258. },
  259. },
  260. })
  261. this.ChatApiHandler.promises.generateThreadData = sinon.stub().resolves(
  262. (this.threadData = {
  263. 'single-comment': {
  264. messages: [
  265. {
  266. content: 'message-content',
  267. timestamp: '2024-01-01T00:00:00.000Z',
  268. user_id: 'user-1',
  269. },
  270. ],
  271. },
  272. 'duplicate-comment': {
  273. messages: [
  274. {
  275. content: 'another message',
  276. timestamp: '2024-01-01T00:00:00.000Z',
  277. user_id: 'user-1',
  278. },
  279. ],
  280. },
  281. })
  282. )
  283. this.ChatManager.promises.injectUserInfoIntoThreads = sinon
  284. .stub()
  285. .resolves(this.threadData)
  286. this.EditorRealTimeController.emitToRoom = sinon.stub()
  287. this.tracked_changes = [
  288. {
  289. op: { pos: 4, i: 'bar' },
  290. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  291. },
  292. {
  293. op: { pos: 8, d: 'qux' },
  294. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-2' },
  295. },
  296. ]
  297. this.FileSystemImportManager.promises.importFile = sinon
  298. .stub()
  299. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  300. this.RestoreManager.promises._getRangesFromHistory = sinon
  301. .stub()
  302. .resolves({
  303. changes: this.tracked_changes,
  304. comments: this.comments,
  305. })
  306. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  307. .stub()
  308. .resolves([
  309. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  310. ])
  311. this.EditorController.promises.addDocWithRanges = sinon
  312. .stub()
  313. .resolves((this.addedFile = { _id: 'mock-doc', type: 'doc' }))
  314. })
  315. describe("when reverting a file that doesn't current exist", function () {
  316. beforeEach(async function () {
  317. this.data = await this.RestoreManager.promises.revertFile(
  318. this.user_id,
  319. this.project_id,
  320. this.version,
  321. this.pathname
  322. )
  323. })
  324. it('should flush the document before fetching ranges', function () {
  325. expect(
  326. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  327. ).to.have.been.calledBefore(
  328. this.DocstoreManager.promises.getAllRanges
  329. )
  330. })
  331. it('should import the file', function () {
  332. expect(
  333. this.EditorController.promises.addDocWithRanges
  334. ).to.have.been.calledWith(
  335. this.project_id,
  336. this.folder_id,
  337. 'foo.tex',
  338. ['foo', 'bar', 'baz'],
  339. { changes: this.tracked_changes, comments: this.remappedComments }
  340. )
  341. })
  342. it('should return the created entity', function () {
  343. expect(this.data).to.deep.equal(this.addedFile)
  344. })
  345. it('should look up ranges', function () {
  346. expect(
  347. this.RestoreManager.promises._getRangesFromHistory
  348. ).to.have.been.calledWith(
  349. this.project_id,
  350. this.version,
  351. this.pathname
  352. )
  353. })
  354. })
  355. describe('with an existing file in the current project', function () {
  356. beforeEach(async function () {
  357. this.ProjectLocator.promises.findElementByPath = sinon
  358. .stub()
  359. .resolves({ type: 'file', element: { _id: 'mock-file-id' } })
  360. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  361. this.data = await this.RestoreManager.promises.revertFile(
  362. this.user_id,
  363. this.project_id,
  364. this.version,
  365. this.pathname
  366. )
  367. })
  368. it('should delete the existing file', async function () {
  369. expect(
  370. this.EditorController.promises.deleteEntity
  371. ).to.have.been.calledWith(
  372. this.project_id,
  373. 'mock-file-id',
  374. 'file',
  375. {
  376. kind: 'file-restore',
  377. path: this.pathname,
  378. version: this.version,
  379. timestamp: new Date(this.endTs).toISOString(),
  380. },
  381. this.user_id
  382. )
  383. })
  384. })
  385. describe('with an existing document in the current project', function () {
  386. beforeEach(async function () {
  387. this.ProjectLocator.promises.findElementByPath = sinon
  388. .stub()
  389. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  390. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  391. this.data = await this.RestoreManager.promises.revertFile(
  392. this.user_id,
  393. this.project_id,
  394. this.version,
  395. this.pathname
  396. )
  397. })
  398. it('should delete the existing document', async function () {
  399. expect(
  400. this.EditorController.promises.deleteEntity
  401. ).to.have.been.calledWith(
  402. this.project_id,
  403. 'mock-file-id',
  404. 'doc',
  405. {
  406. kind: 'file-restore',
  407. path: this.pathname,
  408. version: this.version,
  409. timestamp: new Date(this.endTs).toISOString(),
  410. },
  411. this.user_id
  412. )
  413. })
  414. it('should delete the document before flushing', function () {
  415. expect(
  416. this.EditorController.promises.deleteEntity
  417. ).to.have.been.calledBefore(
  418. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  419. )
  420. })
  421. it('should flush the document before fetching ranges', function () {
  422. expect(
  423. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  424. ).to.have.been.calledBefore(
  425. this.DocstoreManager.promises.getAllRanges
  426. )
  427. })
  428. it('should import the file', function () {
  429. expect(
  430. this.EditorController.promises.addDocWithRanges
  431. ).to.have.been.calledWith(
  432. this.project_id,
  433. this.folder_id,
  434. 'foo.tex',
  435. ['foo', 'bar', 'baz'],
  436. { changes: this.tracked_changes, comments: this.remappedComments },
  437. {
  438. kind: 'file-restore',
  439. path: this.pathname,
  440. version: this.version,
  441. timestamp: new Date(this.endTs).toISOString(),
  442. }
  443. )
  444. })
  445. it('should return the created entity', function () {
  446. expect(this.data).to.deep.equal(this.addedFile)
  447. })
  448. it('should look up ranges', function () {
  449. expect(
  450. this.RestoreManager.promises._getRangesFromHistory
  451. ).to.have.been.calledWith(
  452. this.project_id,
  453. this.version,
  454. this.pathname
  455. )
  456. })
  457. })
  458. })
  459. describe('reverting a document with metadata', function () {
  460. beforeEach(async function () {
  461. this.pathname = 'foo.tex'
  462. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  463. this.EditorController.promises.addDocWithRanges = sinon.stub()
  464. this.FileSystemImportManager.promises.importFile = sinon
  465. .stub()
  466. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  467. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  468. .stub()
  469. .resolves([
  470. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  471. ])
  472. this.EditorController.promises.upsertFile = sinon
  473. .stub()
  474. .resolves({ _id: 'mock-file-id', type: 'file' })
  475. this.RestoreManager.promises._getMetadataFromHistory = sinon
  476. .stub()
  477. .resolves({ metadata: { foo: 'bar' } })
  478. this.result = await this.RestoreManager.promises.revertFile(
  479. this.user_id,
  480. this.project_id,
  481. this.version,
  482. this.pathname
  483. )
  484. })
  485. it('should revert it as a file', function () {
  486. expect(this.result).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  487. })
  488. it('should upload to the project as a file', function () {
  489. expect(
  490. this.EditorController.promises.upsertFile
  491. ).to.have.been.calledWith(
  492. this.project_id,
  493. 'mock-folder-id',
  494. 'foo.tex',
  495. this.fsPath,
  496. { foo: 'bar' },
  497. {
  498. kind: 'file-restore',
  499. path: this.pathname,
  500. version: this.version,
  501. timestamp: new Date(this.endTs).toISOString(),
  502. },
  503. this.user_id
  504. )
  505. })
  506. it('should not look up ranges', function () {
  507. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  508. .been.called
  509. })
  510. it('should not try to add a file', function () {
  511. expect(this.EditorController.promises.addDocWithRanges).to.not.have.been
  512. .called
  513. })
  514. })
  515. describe('reverting a file with metadata', function () {
  516. beforeEach(async function () {
  517. this.pathname = 'foo.png'
  518. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  519. this.EditorController.promises.addDocWithRanges = sinon.stub()
  520. this.FileSystemImportManager.promises.importFile = sinon
  521. .stub()
  522. .resolves({ type: 'file' })
  523. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  524. .stub()
  525. .resolves([
  526. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  527. ])
  528. this.EditorController.promises.upsertFile = sinon
  529. .stub()
  530. .resolves({ _id: 'mock-file-id', type: 'file' })
  531. this.RestoreManager.promises._getMetadataFromHistory = sinon
  532. .stub()
  533. .resolves({ metadata: { foo: 'bar' } })
  534. this.result = await this.RestoreManager.promises.revertFile(
  535. this.user_id,
  536. this.project_id,
  537. this.version,
  538. this.pathname
  539. )
  540. })
  541. it('should revert it as a file', function () {
  542. expect(this.result).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  543. })
  544. it('should upload to the project as a file', function () {
  545. expect(
  546. this.EditorController.promises.upsertFile
  547. ).to.have.been.calledWith(
  548. this.project_id,
  549. 'mock-folder-id',
  550. 'foo.png',
  551. this.fsPath,
  552. { foo: 'bar' },
  553. {
  554. kind: 'file-restore',
  555. path: this.pathname,
  556. version: this.version,
  557. timestamp: new Date(this.endTs).toISOString(),
  558. },
  559. this.user_id
  560. )
  561. })
  562. it('should not look up ranges', function () {
  563. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  564. .been.called
  565. })
  566. it('should not try to add a file', function () {
  567. expect(this.EditorController.promises.addDocWithRanges).to.not.have.been
  568. .called
  569. })
  570. })
  571. describe('when reverting a binary file', function () {
  572. beforeEach(async function () {
  573. this.pathname = 'foo.png'
  574. this.FileSystemImportManager.promises.importFile = sinon
  575. .stub()
  576. .resolves({ type: 'file' })
  577. this.EditorController.promises.upsertFile = sinon
  578. .stub()
  579. .resolves({ _id: 'mock-file-id', type: 'file' })
  580. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  581. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  582. .stub()
  583. .resolves([{ toV: this.version, meta: { end_ts: Date.now() } }])
  584. })
  585. it('should return the created entity if file exists', async function () {
  586. this.ProjectLocator.promises.findElementByPath = sinon
  587. .stub()
  588. .resolves({ type: 'file', element: { _id: 'existing-file-id' } })
  589. const revertRes = await this.RestoreManager.promises.revertFile(
  590. this.user_id,
  591. this.project_id,
  592. this.version,
  593. this.pathname
  594. )
  595. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  596. })
  597. it('should return the created entity if file does not exists', async function () {
  598. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  599. const revertRes = await this.RestoreManager.promises.revertFile(
  600. this.user_id,
  601. this.project_id,
  602. this.version,
  603. this.pathname
  604. )
  605. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  606. })
  607. })
  608. })
  609. describe('revertProject', function () {
  610. beforeEach(function () {
  611. this.ProjectGetter.promises.getProject = sinon.stub()
  612. this.ProjectGetter.promises.getProject
  613. .withArgs(this.project_id)
  614. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  615. this.RestoreManager.promises.revertFile = sinon.stub().resolves()
  616. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  617. .stub()
  618. .resolves([])
  619. this.ProjectEntityHandler.promises.getAllEntities = sinon
  620. .stub()
  621. .resolves({ docs: [], files: [] })
  622. this.EditorController.promises.deleteEntityWithPath = sinon
  623. .stub()
  624. .resolves()
  625. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  626. .stub()
  627. .resolves([
  628. { toV: this.version, meta: { end_ts: (this.end_ts = Date.now()) } },
  629. ])
  630. })
  631. describe('reverting a project without ranges support', function () {
  632. beforeEach(function () {
  633. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  634. overleaf: { history: { rangesSupportEnabled: false } },
  635. })
  636. })
  637. it('should throw an error', async function () {
  638. await expect(
  639. this.RestoreManager.promises.revertProject(
  640. this.user_id,
  641. this.project_id,
  642. this.version
  643. )
  644. ).to.eventually.be.rejectedWith('project does not have ranges support')
  645. })
  646. })
  647. describe('for a project with overlap in current files and old files', function () {
  648. beforeEach(async function () {
  649. this.ProjectEntityHandler.promises.getAllEntities = sinon
  650. .stub()
  651. .resolves({
  652. docs: [{ path: '/main.tex' }, { path: '/new-file.tex' }],
  653. files: [{ path: '/figures/image.png' }],
  654. })
  655. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  656. .stub()
  657. .resolves(['main.tex', 'figures/image.png', 'since-deleted.tex'])
  658. await this.RestoreManager.promises.revertProject(
  659. this.user_id,
  660. this.project_id,
  661. this.version
  662. )
  663. this.origin = {
  664. kind: 'project-restore',
  665. version: this.version,
  666. timestamp: new Date(this.end_ts).toISOString(),
  667. }
  668. })
  669. it('should delete the old files', function () {
  670. expect(
  671. this.EditorController.promises.deleteEntityWithPath
  672. ).to.have.been.calledWith(
  673. this.project_id,
  674. 'new-file.tex',
  675. this.origin,
  676. this.user_id
  677. )
  678. })
  679. it('should not delete the current files', function () {
  680. expect(
  681. this.EditorController.promises.deleteEntityWithPath
  682. ).to.not.have.been.calledWith(
  683. this.project_id,
  684. 'main.tex',
  685. this.origin,
  686. this.user_id
  687. )
  688. expect(
  689. this.EditorController.promises.deleteEntityWithPath
  690. ).to.not.have.been.calledWith(
  691. this.project_id,
  692. 'figures/image.png',
  693. this.origin,
  694. this.user_id
  695. )
  696. })
  697. it('should revert the old files', function () {
  698. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  699. this.user_id,
  700. this.project_id,
  701. this.version,
  702. 'main.tex'
  703. )
  704. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  705. this.user_id,
  706. this.project_id,
  707. this.version,
  708. 'figures/image.png'
  709. )
  710. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  711. this.user_id,
  712. this.project_id,
  713. this.version,
  714. 'since-deleted.tex'
  715. )
  716. })
  717. it('should not revert the current files', function () {
  718. expect(
  719. this.RestoreManager.promises.revertFile
  720. ).to.not.have.been.calledWith(
  721. this.user_id,
  722. this.project_id,
  723. this.version,
  724. 'new-file.tex'
  725. )
  726. })
  727. })
  728. })
  729. })