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