GettingUpdatesTests.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. mongojs = require "../../../app/js/mongojs"
  6. db = mongojs.db
  7. ObjectId = mongojs.ObjectId
  8. Settings = require "settings-sharelatex"
  9. TrackChangesClient = require "./helpers/TrackChangesClient"
  10. MockWebApi = require "./helpers/MockWebApi"
  11. describe "Getting updates", ->
  12. before (done) ->
  13. @now = Date.now()
  14. @to = @now
  15. @user_id = ObjectId().toString()
  16. @deleted_user_id = 'deleted_user'
  17. @doc_id = ObjectId().toString()
  18. @project_id = ObjectId().toString()
  19. @minutes = 60 * 1000
  20. @hours = 60 * @minutes
  21. MockWebApi.projects[@project_id] =
  22. features:
  23. versioning: true
  24. MockWebApi.users[@user_id] = @user =
  25. email: "user@sharelatex.com"
  26. first_name: "Leo"
  27. last_name: "Lion"
  28. id: @user_id
  29. sinon.spy MockWebApi, "getUser"
  30. @updates = []
  31. for i in [0..9]
  32. @updates.push {
  33. op: [{ i: "a", p: 0 }]
  34. meta: { ts: @now - (9 - i) * @hours - 2 * @minutes, user_id: @user_id }
  35. v: 2 * i + 1
  36. }
  37. @updates.push {
  38. op: [{ i: "b", p: 0 }]
  39. meta: { ts: @now - (9 - i) * @hours, user_id: @user_id }
  40. v: 2 * i + 2
  41. }
  42. @updates[0].meta.user_id = @deleted_user_id
  43. TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
  44. throw error if error?
  45. done()
  46. after: () ->
  47. MockWebApi.getUser.restore()
  48. describe "getting updates up to the limit", ->
  49. before (done) ->
  50. TrackChangesClient.getUpdates @project_id, { before: @to + 1, min_count: 3 }, (error, body) =>
  51. throw error if error?
  52. @updates = body.updates
  53. done()
  54. it "should fetch the user details from the web api", ->
  55. MockWebApi.getUser
  56. .calledWith(@user_id)
  57. .should.equal true
  58. it "should return at least the min_count number of summarized updates", ->
  59. docs1 = {}
  60. docs1[@doc_id] = toV: 20, fromV: 19
  61. docs2 = {}
  62. docs2[@doc_id] = toV: 18, fromV: 17
  63. docs3 = {}
  64. docs3[@doc_id] = toV: 16, fromV: 15
  65. expect(@updates.slice(0,3)).to.deep.equal [{
  66. docs: docs1
  67. meta:
  68. start_ts: @to - 2 * @minutes
  69. end_ts: @to
  70. users: [@user]
  71. }, {
  72. docs: docs2
  73. meta:
  74. start_ts: @to - 1 * @hours - 2 * @minutes
  75. end_ts: @to - 1 * @hours
  76. users: [@user]
  77. }, {
  78. docs: docs3
  79. meta:
  80. start_ts: @to - 2 * @hours - 2 * @minutes
  81. end_ts: @to - 2 * @hours
  82. users: [@user]
  83. }]
  84. describe "getting updates beyond the end of the database", ->
  85. before (done) ->
  86. TrackChangesClient.getUpdates @project_id, { before: @to - 8 * @hours + 1, min_count: 30 }, (error, body) =>
  87. throw error if error?
  88. @updates = body.updates
  89. done()
  90. it "should return as many updates as it can", ->
  91. docs1 = {}
  92. docs1[@doc_id] = toV: 4, fromV: 3
  93. docs2 = {}
  94. docs2[@doc_id] = toV: 2, fromV: 1
  95. expect(@updates).to.deep.equal [{
  96. docs: docs1
  97. meta:
  98. start_ts: @to - 8 * @hours - 2 * @minutes
  99. end_ts: @to - 8 * @hours
  100. users: [@user]
  101. }, {
  102. docs: docs2
  103. meta:
  104. start_ts: @to - 9 * @hours - 2 * @minutes
  105. end_ts: @to - 9 * @hours
  106. users: [@user, null]
  107. }]