RestoringVersions.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const { expect } = require('chai')
  14. const { ObjectId } = require('../../../app/js/mongodb')
  15. const Settings = require('@overleaf/settings')
  16. const TrackChangesApp = require('./helpers/TrackChangesApp')
  17. const TrackChangesClient = require('./helpers/TrackChangesClient')
  18. const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
  19. const MockWebApi = require('./helpers/MockWebApi')
  20. describe('Restoring a version', function () {
  21. before(function (done) {
  22. sinon.spy(MockDocUpdaterApi, 'setDoc')
  23. this.now = Date.now()
  24. this.user_id = ObjectId().toString()
  25. this.doc_id = ObjectId().toString()
  26. this.project_id = ObjectId().toString()
  27. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  28. const minutes = 60 * 1000
  29. this.updates = [
  30. {
  31. op: [{ i: 'one ', p: 0 }],
  32. meta: { ts: this.now - 6 * minutes, user_id: this.user_id },
  33. v: 3,
  34. },
  35. {
  36. op: [{ i: 'two ', p: 4 }],
  37. meta: { ts: this.now - 4 * minutes, user_id: this.user_id },
  38. v: 4,
  39. },
  40. {
  41. op: [{ i: 'three ', p: 8 }],
  42. meta: { ts: this.now - 2 * minutes, user_id: this.user_id },
  43. v: 5,
  44. },
  45. {
  46. op: [{ i: 'four', p: 14 }],
  47. meta: { ts: this.now, user_id: this.user_id },
  48. v: 6,
  49. },
  50. ]
  51. this.lines = ['one two three four']
  52. this.restored_lines = ['one two ']
  53. this.beforeVersion = 5
  54. MockWebApi.users[this.user_id] = this.user = {
  55. email: 'user@sharelatex.com',
  56. first_name: 'Leo',
  57. last_name: 'Lion',
  58. id: this.user_id,
  59. }
  60. MockDocUpdaterApi.docs[this.doc_id] = {
  61. lines: this.lines,
  62. version: 7,
  63. }
  64. TrackChangesApp.ensureRunning(() => {
  65. return TrackChangesClient.pushRawUpdates(
  66. this.project_id,
  67. this.doc_id,
  68. this.updates,
  69. error => {
  70. if (error != null) {
  71. throw error
  72. }
  73. return TrackChangesClient.restoreDoc(
  74. this.project_id,
  75. this.doc_id,
  76. this.beforeVersion,
  77. this.user_id,
  78. error => {
  79. if (error != null) {
  80. throw error
  81. }
  82. return done()
  83. }
  84. )
  85. }
  86. )
  87. })
  88. return null
  89. })
  90. after(function () {
  91. MockDocUpdaterApi.setDoc.restore()
  92. return null
  93. })
  94. return it('should set the doc in the doc updater', function () {
  95. MockDocUpdaterApi.setDoc
  96. .calledWith(
  97. this.project_id,
  98. this.doc_id,
  99. this.restored_lines,
  100. this.user_id,
  101. true
  102. )
  103. .should.equal(true)
  104. return null
  105. })
  106. })