GettingUpdatesTests.js 5.0 KB

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