GettingUpdatesTests.js 5.0 KB

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