RestoringVersions.js 3.1 KB

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