RestoringVersions.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 chai = require('chai')
  14. chai.should()
  15. const { expect } = chai
  16. const { ObjectId } = require('../../../app/js/mongodb')
  17. const Settings = require('settings-sharelatex')
  18. const TrackChangesApp = require('./helpers/TrackChangesApp')
  19. const TrackChangesClient = require('./helpers/TrackChangesClient')
  20. const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
  21. const MockWebApi = require('./helpers/MockWebApi')
  22. describe('Restoring a version', function () {
  23. before(function (done) {
  24. sinon.spy(MockDocUpdaterApi, 'setDoc')
  25. this.now = Date.now()
  26. this.user_id = ObjectId().toString()
  27. this.doc_id = ObjectId().toString()
  28. this.project_id = ObjectId().toString()
  29. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  30. const minutes = 60 * 1000
  31. this.updates = [
  32. {
  33. op: [{ i: 'one ', p: 0 }],
  34. meta: { ts: this.now - 6 * minutes, user_id: this.user_id },
  35. v: 3
  36. },
  37. {
  38. op: [{ i: 'two ', p: 4 }],
  39. meta: { ts: this.now - 4 * minutes, user_id: this.user_id },
  40. v: 4
  41. },
  42. {
  43. op: [{ i: 'three ', p: 8 }],
  44. meta: { ts: this.now - 2 * minutes, user_id: this.user_id },
  45. v: 5
  46. },
  47. {
  48. op: [{ i: 'four', p: 14 }],
  49. meta: { ts: this.now, user_id: this.user_id },
  50. v: 6
  51. }
  52. ]
  53. this.lines = ['one two three four']
  54. this.restored_lines = ['one two ']
  55. this.beforeVersion = 5
  56. MockWebApi.users[this.user_id] = this.user = {
  57. email: 'user@sharelatex.com',
  58. first_name: 'Leo',
  59. last_name: 'Lion',
  60. id: this.user_id
  61. }
  62. MockDocUpdaterApi.docs[this.doc_id] = {
  63. lines: this.lines,
  64. version: 7
  65. }
  66. TrackChangesApp.ensureRunning(() => {
  67. return TrackChangesClient.pushRawUpdates(
  68. this.project_id,
  69. this.doc_id,
  70. this.updates,
  71. (error) => {
  72. if (error != null) {
  73. throw error
  74. }
  75. return TrackChangesClient.restoreDoc(
  76. this.project_id,
  77. this.doc_id,
  78. this.beforeVersion,
  79. this.user_id,
  80. (error) => {
  81. if (error != null) {
  82. throw error
  83. }
  84. return done()
  85. }
  86. )
  87. }
  88. )
  89. })
  90. return null
  91. })
  92. after(function () {
  93. MockDocUpdaterApi.setDoc.restore()
  94. return null
  95. })
  96. return it('should set the doc in the doc updater', function () {
  97. MockDocUpdaterApi.setDoc
  98. .calledWith(
  99. this.project_id,
  100. this.doc_id,
  101. this.restored_lines,
  102. this.user_id,
  103. true
  104. )
  105. .should.equal(true)
  106. return null
  107. })
  108. })