GettingUpdatesTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* eslint-disable
  2. chai-friendly/no-unused-expressions,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const { expect } = require('chai')
  15. const { ObjectId } = require('../../../app/js/mongodb')
  16. const Settings = require('@overleaf/settings')
  17. const TrackChangesApp = require('./helpers/TrackChangesApp')
  18. const TrackChangesClient = require('./helpers/TrackChangesClient')
  19. const MockWebApi = require('./helpers/MockWebApi')
  20. describe('Getting updates', function () {
  21. before(function (done) {
  22. this.now = Date.now()
  23. this.to = this.now
  24. this.user_id = ObjectId().toString()
  25. this.deleted_user_id = 'deleted_user'
  26. this.doc_id = ObjectId().toString()
  27. this.project_id = ObjectId().toString()
  28. this.minutes = 60 * 1000
  29. this.hours = 60 * this.minutes
  30. MockWebApi.projects[this.project_id] = {
  31. features: {
  32. versioning: true,
  33. },
  34. }
  35. MockWebApi.users[this.user_id] = this.user = {
  36. email: 'user@sharelatex.com',
  37. first_name: 'Leo',
  38. last_name: 'Lion',
  39. id: this.user_id,
  40. }
  41. sinon.spy(MockWebApi, 'getUserInfo')
  42. this.updates = []
  43. for (let i = 0; i <= 9; i++) {
  44. this.updates.push({
  45. op: [{ i: 'a', p: 0 }],
  46. meta: {
  47. ts: this.now - (9 - i) * this.hours - 2 * this.minutes,
  48. user_id: this.user_id,
  49. },
  50. v: 2 * i + 1,
  51. })
  52. this.updates.push({
  53. op: [{ i: 'b', p: 0 }],
  54. meta: { ts: this.now - (9 - i) * this.hours, user_id: this.user_id },
  55. v: 2 * i + 2,
  56. })
  57. }
  58. this.updates[0].meta.user_id = this.deleted_user_id
  59. TrackChangesApp.ensureRunning(() => {
  60. return TrackChangesClient.pushRawUpdates(
  61. this.project_id,
  62. this.doc_id,
  63. this.updates,
  64. error => {
  65. if (error != null) {
  66. throw error
  67. }
  68. return done()
  69. }
  70. )
  71. })
  72. return null
  73. })
  74. ;({
  75. after() {
  76. MockWebApi.getUserInfo.restore()
  77. return null
  78. },
  79. })
  80. describe('getting updates up to the limit', function () {
  81. before(function (done) {
  82. TrackChangesClient.getUpdates(
  83. this.project_id,
  84. { before: this.to + 1, min_count: 3 },
  85. (error, body) => {
  86. if (error != null) {
  87. throw error
  88. }
  89. this.updates = body.updates
  90. return done()
  91. }
  92. )
  93. return null
  94. })
  95. it('should fetch the user details from the web api', function () {
  96. return MockWebApi.getUserInfo.calledWith(this.user_id).should.equal(true)
  97. })
  98. return it('should return at least the min_count number of summarized updates', function () {
  99. const docs1 = {}
  100. docs1[this.doc_id] = { toV: 20, fromV: 19 }
  101. const docs2 = {}
  102. docs2[this.doc_id] = { toV: 18, fromV: 17 }
  103. const docs3 = {}
  104. docs3[this.doc_id] = { toV: 16, fromV: 15 }
  105. return expect(this.updates.slice(0, 3)).to.deep.equal([
  106. {
  107. docs: docs1,
  108. meta: {
  109. start_ts: this.to - 2 * this.minutes,
  110. end_ts: this.to,
  111. users: [this.user],
  112. },
  113. },
  114. {
  115. docs: docs2,
  116. meta: {
  117. start_ts: this.to - 1 * this.hours - 2 * this.minutes,
  118. end_ts: this.to - 1 * this.hours,
  119. users: [this.user],
  120. },
  121. },
  122. {
  123. docs: docs3,
  124. meta: {
  125. start_ts: this.to - 2 * this.hours - 2 * this.minutes,
  126. end_ts: this.to - 2 * this.hours,
  127. users: [this.user],
  128. },
  129. },
  130. ])
  131. })
  132. })
  133. return describe('getting updates beyond the end of the database', function () {
  134. before(function (done) {
  135. TrackChangesClient.getUpdates(
  136. this.project_id,
  137. { before: this.to - 8 * this.hours + 1, min_count: 30 },
  138. (error, body) => {
  139. if (error != null) {
  140. throw error
  141. }
  142. this.updates = body.updates
  143. return done()
  144. }
  145. )
  146. return null
  147. })
  148. return it('should return as many updates as it can', function () {
  149. const docs1 = {}
  150. docs1[this.doc_id] = { toV: 4, fromV: 3 }
  151. const docs2 = {}
  152. docs2[this.doc_id] = { toV: 2, fromV: 1 }
  153. return expect(this.updates).to.deep.equal([
  154. {
  155. docs: docs1,
  156. meta: {
  157. start_ts: this.to - 8 * this.hours - 2 * this.minutes,
  158. end_ts: this.to - 8 * this.hours,
  159. users: [this.user],
  160. },
  161. },
  162. {
  163. docs: docs2,
  164. meta: {
  165. start_ts: this.to - 9 * this.hours - 2 * this.minutes,
  166. end_ts: this.to - 9 * this.hours,
  167. users: [this.user, null],
  168. },
  169. },
  170. ])
  171. })
  172. })
  173. })