RestoreManagerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. (this.comment = { op: { t: 'comment-1', p: 0, c: 'foo' } }),
  237. ]
  238. this.remappedComments = [{ op: { t: 'comment-2', p: 0, c: 'foo' } }]
  239. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  240. this.DocstoreManager.promises.getAllRanges = sinon.stub().resolves([
  241. {
  242. ranges: {
  243. comments: [this.comment],
  244. },
  245. },
  246. ])
  247. this.ChatApiHandler.promises.duplicateCommentThreads = sinon
  248. .stub()
  249. .resolves({
  250. newThreads: {
  251. 'comment-1': {
  252. duplicateId: 'comment-2',
  253. },
  254. },
  255. })
  256. this.ChatApiHandler.promises.generateThreadData = sinon.stub().resolves(
  257. (this.threadData = {
  258. 'comment-2': {
  259. messages: [
  260. {
  261. content: 'message-content',
  262. timestamp: '2024-01-01T00:00:00.000Z',
  263. user_id: 'user-1',
  264. },
  265. ],
  266. },
  267. })
  268. )
  269. this.ChatManager.promises.injectUserInfoIntoThreads = sinon
  270. .stub()
  271. .resolves(this.threadData)
  272. this.EditorRealTimeController.emitToRoom = sinon.stub()
  273. this.tracked_changes = [
  274. {
  275. op: { pos: 4, i: 'bar' },
  276. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  277. },
  278. {
  279. op: { pos: 8, d: 'qux' },
  280. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-2' },
  281. },
  282. ]
  283. this.FileSystemImportManager.promises.importFile = sinon
  284. .stub()
  285. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  286. this.RestoreManager.promises._getRangesFromHistory = sinon
  287. .stub()
  288. .resolves({
  289. changes: this.tracked_changes,
  290. comments: this.comments,
  291. })
  292. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  293. .stub()
  294. .resolves([
  295. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  296. ])
  297. this.EditorController.promises.addDocWithRanges = sinon
  298. .stub()
  299. .resolves((this.addedFile = { _id: 'mock-doc', type: 'doc' }))
  300. })
  301. describe("when reverting a file that doesn't current exist", function () {
  302. beforeEach(async function () {
  303. this.data = await this.RestoreManager.promises.revertFile(
  304. this.user_id,
  305. this.project_id,
  306. this.version,
  307. this.pathname
  308. )
  309. })
  310. it('should flush the document before fetching ranges', function () {
  311. expect(
  312. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  313. ).to.have.been.calledBefore(
  314. this.DocstoreManager.promises.getAllRanges
  315. )
  316. })
  317. it('should import the file', function () {
  318. expect(
  319. this.EditorController.promises.addDocWithRanges
  320. ).to.have.been.calledWith(
  321. this.project_id,
  322. this.folder_id,
  323. 'foo.tex',
  324. ['foo', 'bar', 'baz'],
  325. { changes: this.tracked_changes, comments: this.remappedComments }
  326. )
  327. })
  328. it('should return the created entity', function () {
  329. expect(this.data).to.deep.equal(this.addedFile)
  330. })
  331. it('should look up ranges', function () {
  332. expect(
  333. this.RestoreManager.promises._getRangesFromHistory
  334. ).to.have.been.calledWith(
  335. this.project_id,
  336. this.version,
  337. this.pathname
  338. )
  339. })
  340. })
  341. describe('with an existing file in the current project', function () {
  342. beforeEach(async function () {
  343. this.ProjectLocator.promises.findElementByPath = sinon
  344. .stub()
  345. .resolves({ type: 'file', element: { _id: 'mock-file-id' } })
  346. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  347. this.data = await this.RestoreManager.promises.revertFile(
  348. this.user_id,
  349. this.project_id,
  350. this.version,
  351. this.pathname
  352. )
  353. })
  354. it('should delete the existing file', async function () {
  355. expect(
  356. this.EditorController.promises.deleteEntity
  357. ).to.have.been.calledWith(
  358. this.project_id,
  359. 'mock-file-id',
  360. 'file',
  361. {
  362. kind: 'file-restore',
  363. path: this.pathname,
  364. version: this.version,
  365. timestamp: new Date(this.endTs).toISOString(),
  366. },
  367. this.user_id
  368. )
  369. })
  370. })
  371. describe('with an existing document in the current project', function () {
  372. beforeEach(async function () {
  373. this.ProjectLocator.promises.findElementByPath = sinon
  374. .stub()
  375. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  376. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  377. this.data = await this.RestoreManager.promises.revertFile(
  378. this.user_id,
  379. this.project_id,
  380. this.version,
  381. this.pathname
  382. )
  383. })
  384. it('should delete the existing document', async function () {
  385. expect(
  386. this.EditorController.promises.deleteEntity
  387. ).to.have.been.calledWith(
  388. this.project_id,
  389. 'mock-file-id',
  390. 'doc',
  391. {
  392. kind: 'file-restore',
  393. path: this.pathname,
  394. version: this.version,
  395. timestamp: new Date(this.endTs).toISOString(),
  396. },
  397. this.user_id
  398. )
  399. })
  400. it('should delete the document before flushing', function () {
  401. expect(
  402. this.EditorController.promises.deleteEntity
  403. ).to.have.been.calledBefore(
  404. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  405. )
  406. })
  407. it('should flush the document before fetching ranges', function () {
  408. expect(
  409. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  410. ).to.have.been.calledBefore(
  411. this.DocstoreManager.promises.getAllRanges
  412. )
  413. })
  414. it('should import the file', function () {
  415. expect(
  416. this.EditorController.promises.addDocWithRanges
  417. ).to.have.been.calledWith(
  418. this.project_id,
  419. this.folder_id,
  420. 'foo.tex',
  421. ['foo', 'bar', 'baz'],
  422. { changes: this.tracked_changes, comments: this.remappedComments },
  423. {
  424. kind: 'file-restore',
  425. path: this.pathname,
  426. version: this.version,
  427. timestamp: new Date(this.endTs).toISOString(),
  428. }
  429. )
  430. })
  431. it('should return the created entity', function () {
  432. expect(this.data).to.deep.equal(this.addedFile)
  433. })
  434. it('should look up ranges', function () {
  435. expect(
  436. this.RestoreManager.promises._getRangesFromHistory
  437. ).to.have.been.calledWith(
  438. this.project_id,
  439. this.version,
  440. this.pathname
  441. )
  442. })
  443. })
  444. })
  445. describe('reverting a document with metadata', function () {
  446. beforeEach(async function () {
  447. this.pathname = 'foo.tex'
  448. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  449. this.EditorController.promises.addDocWithRanges = sinon.stub()
  450. this.FileSystemImportManager.promises.importFile = sinon
  451. .stub()
  452. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  453. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  454. .stub()
  455. .resolves([
  456. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  457. ])
  458. this.EditorController.promises.upsertFile = sinon
  459. .stub()
  460. .resolves({ _id: 'mock-file-id', type: 'file' })
  461. this.RestoreManager.promises._getMetadataFromHistory = sinon
  462. .stub()
  463. .resolves({ metadata: { foo: 'bar' } })
  464. this.result = await this.RestoreManager.promises.revertFile(
  465. this.user_id,
  466. this.project_id,
  467. this.version,
  468. this.pathname
  469. )
  470. })
  471. it('should revert it as a file', function () {
  472. expect(this.result).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  473. })
  474. it('should upload to the project as a file', function () {
  475. expect(
  476. this.EditorController.promises.upsertFile
  477. ).to.have.been.calledWith(
  478. this.project_id,
  479. 'mock-folder-id',
  480. 'foo.tex',
  481. this.fsPath,
  482. { foo: 'bar' },
  483. {
  484. kind: 'file-restore',
  485. path: this.pathname,
  486. version: this.version,
  487. timestamp: new Date(this.endTs).toISOString(),
  488. },
  489. this.user_id
  490. )
  491. })
  492. it('should not look up ranges', function () {
  493. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  494. .been.called
  495. })
  496. it('should not try to add a file', function () {
  497. expect(this.EditorController.promises.addDocWithRanges).to.not.have.been
  498. .called
  499. })
  500. })
  501. describe('reverting a file with metadata', function () {
  502. beforeEach(async function () {
  503. this.pathname = 'foo.png'
  504. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  505. this.EditorController.promises.addDocWithRanges = sinon.stub()
  506. this.FileSystemImportManager.promises.importFile = sinon
  507. .stub()
  508. .resolves({ type: 'file' })
  509. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  510. .stub()
  511. .resolves([
  512. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  513. ])
  514. this.EditorController.promises.upsertFile = sinon
  515. .stub()
  516. .resolves({ _id: 'mock-file-id', type: 'file' })
  517. this.RestoreManager.promises._getMetadataFromHistory = sinon
  518. .stub()
  519. .resolves({ metadata: { foo: 'bar' } })
  520. this.result = await this.RestoreManager.promises.revertFile(
  521. this.user_id,
  522. this.project_id,
  523. this.version,
  524. this.pathname
  525. )
  526. })
  527. it('should revert it as a file', function () {
  528. expect(this.result).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  529. })
  530. it('should upload to the project as a file', function () {
  531. expect(
  532. this.EditorController.promises.upsertFile
  533. ).to.have.been.calledWith(
  534. this.project_id,
  535. 'mock-folder-id',
  536. 'foo.png',
  537. this.fsPath,
  538. { foo: 'bar' },
  539. {
  540. kind: 'file-restore',
  541. path: this.pathname,
  542. version: this.version,
  543. timestamp: new Date(this.endTs).toISOString(),
  544. },
  545. this.user_id
  546. )
  547. })
  548. it('should not look up ranges', function () {
  549. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  550. .been.called
  551. })
  552. it('should not try to add a file', function () {
  553. expect(this.EditorController.promises.addDocWithRanges).to.not.have.been
  554. .called
  555. })
  556. })
  557. describe('when reverting a binary file', function () {
  558. beforeEach(async function () {
  559. this.pathname = 'foo.png'
  560. this.FileSystemImportManager.promises.importFile = sinon
  561. .stub()
  562. .resolves({ type: 'file' })
  563. this.EditorController.promises.upsertFile = sinon
  564. .stub()
  565. .resolves({ _id: 'mock-file-id', type: 'file' })
  566. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  567. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  568. .stub()
  569. .resolves([{ toV: this.version, meta: { end_ts: Date.now() } }])
  570. })
  571. it('should return the created entity if file exists', async function () {
  572. this.ProjectLocator.promises.findElementByPath = sinon
  573. .stub()
  574. .resolves({ type: 'file', element: { _id: 'existing-file-id' } })
  575. const revertRes = await this.RestoreManager.promises.revertFile(
  576. this.user_id,
  577. this.project_id,
  578. this.version,
  579. this.pathname
  580. )
  581. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  582. })
  583. it('should return the created entity if file does not exists', async function () {
  584. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  585. const revertRes = await this.RestoreManager.promises.revertFile(
  586. this.user_id,
  587. this.project_id,
  588. this.version,
  589. this.pathname
  590. )
  591. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  592. })
  593. })
  594. })
  595. describe('revertProject', function () {
  596. beforeEach(function () {
  597. this.ProjectGetter.promises.getProject = sinon.stub()
  598. this.ProjectGetter.promises.getProject
  599. .withArgs(this.project_id)
  600. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  601. this.RestoreManager.promises.revertFile = sinon.stub().resolves()
  602. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  603. .stub()
  604. .resolves([])
  605. this.ProjectEntityHandler.promises.getAllEntities = sinon
  606. .stub()
  607. .resolves({ docs: [], files: [] })
  608. this.EditorController.promises.deleteEntityWithPath = sinon
  609. .stub()
  610. .resolves()
  611. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  612. .stub()
  613. .resolves([
  614. { toV: this.version, meta: { end_ts: (this.end_ts = Date.now()) } },
  615. ])
  616. })
  617. describe('reverting a project without ranges support', function () {
  618. beforeEach(function () {
  619. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  620. overleaf: { history: { rangesSupportEnabled: false } },
  621. })
  622. })
  623. it('should throw an error', async function () {
  624. await expect(
  625. this.RestoreManager.promises.revertProject(
  626. this.user_id,
  627. this.project_id,
  628. this.version
  629. )
  630. ).to.eventually.be.rejectedWith('project does not have ranges support')
  631. })
  632. })
  633. describe('for a project with overlap in current files and old files', function () {
  634. beforeEach(async function () {
  635. this.ProjectEntityHandler.promises.getAllEntities = sinon
  636. .stub()
  637. .resolves({
  638. docs: [{ path: '/main.tex' }, { path: '/new-file.tex' }],
  639. files: [{ path: '/figures/image.png' }],
  640. })
  641. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  642. .stub()
  643. .resolves(['main.tex', 'figures/image.png', 'since-deleted.tex'])
  644. await this.RestoreManager.promises.revertProject(
  645. this.user_id,
  646. this.project_id,
  647. this.version
  648. )
  649. this.origin = {
  650. kind: 'project-restore',
  651. version: this.version,
  652. timestamp: new Date(this.end_ts).toISOString(),
  653. }
  654. })
  655. it('should delete the old files', function () {
  656. expect(
  657. this.EditorController.promises.deleteEntityWithPath
  658. ).to.have.been.calledWith(
  659. this.project_id,
  660. 'new-file.tex',
  661. this.origin,
  662. this.user_id
  663. )
  664. })
  665. it('should not delete the current files', function () {
  666. expect(
  667. this.EditorController.promises.deleteEntityWithPath
  668. ).to.not.have.been.calledWith(
  669. this.project_id,
  670. 'main.tex',
  671. this.origin,
  672. this.user_id
  673. )
  674. expect(
  675. this.EditorController.promises.deleteEntityWithPath
  676. ).to.not.have.been.calledWith(
  677. this.project_id,
  678. 'figures/image.png',
  679. this.origin,
  680. this.user_id
  681. )
  682. })
  683. it('should revert the old files', function () {
  684. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  685. this.user_id,
  686. this.project_id,
  687. this.version,
  688. 'main.tex'
  689. )
  690. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  691. this.user_id,
  692. this.project_id,
  693. this.version,
  694. 'figures/image.png'
  695. )
  696. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  697. this.user_id,
  698. this.project_id,
  699. this.version,
  700. 'since-deleted.tex'
  701. )
  702. })
  703. it('should not revert the current files', function () {
  704. expect(
  705. this.RestoreManager.promises.revertFile
  706. ).to.not.have.been.calledWith(
  707. this.user_id,
  708. this.project_id,
  709. this.version,
  710. 'new-file.tex'
  711. )
  712. })
  713. })
  714. })
  715. })