overleaf/services/track-changes/test/acceptance/coffee/GettingUpdatesTests.coffee

112 lines
2.6 KiB
CoffeeScript
Raw Normal View History

sinon = require "sinon"
chai = require("chai")
chai.should()
expect = chai.expect
mongojs = require "../../../app/js/mongojs"
db = mongojs.db
ObjectId = mongojs.ObjectId
Settings = require "settings-sharelatex"
TrackChangesClient = require "./helpers/TrackChangesClient"
MockWebApi = require "./helpers/MockWebApi"
describe "Getting updates", ->
before (done) ->
@now = Date.now()
@to = @now
@user_id = ObjectId().toString()
@doc_id = ObjectId().toString()
@project_id = ObjectId().toString()
@minutes = 60 * 1000
@days = 24 * 60 * @minutes
MockWebApi.users[@user_id] = @user =
email: "user@sharelatex.com"
first_name: "Leo"
last_name: "Lion"
id: @user_id
sinon.spy MockWebApi, "getUser"
@updates = []
for i in [0..9]
@updates.push {
op: [{ i: "a", p: 0 }]
meta: { ts: @now - (9 - i) * @days - 2 * @minutes, user_id: @user_id }
v: 2 * i + 1
}
@updates.push {
op: [{ i: "b", p: 0 }]
meta: { ts: @now - (9 - i) * @days, user_id: @user_id }
v: 2 * i + 2
}
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if error?
done()
2014-03-11 13:39:40 -04:00
after: () ->
MockWebApi.getUser.restore()
describe "getting updates up to the limit", ->
2014-03-11 13:39:40 -04:00
before (done) ->
TrackChangesClient.getUpdates @project_id, @doc_id, { to: 20, limit: 3 }, (error, body) =>
throw error if error?
@updates = body.updates
done()
2014-03-11 13:39:40 -04:00
it "should fetch the user details from the web api", ->
MockWebApi.getUser
.calledWith(@user_id)
.should.equal true
it "should return the same number of summarized updates as the limit", ->
2014-03-11 13:39:40 -04:00
expect(@updates).to.deep.equal [{
meta:
start_ts: @to - 2 * @minutes
2014-03-11 13:39:40 -04:00
end_ts: @to
users: [@user]
toV: 20
fromV: 19
2014-03-11 13:39:40 -04:00
}, {
meta:
start_ts: @to - 1 * @days - 2 * @minutes
end_ts: @to - 1 * @days
users: [@user]
toV: 18
fromV: 17
2014-03-11 13:39:40 -04:00
}, {
meta:
start_ts: @to - 2 * @days - 2 * @minutes
end_ts: @to - 2 * @days
users: [@user]
toV: 16
fromV: 15
2014-03-11 13:39:40 -04:00
}]
describe "getting updates beyond the end of the database", ->
before (done) ->
TrackChangesClient.getUpdates @project_id, @doc_id, { to: 4, limit: 30 }, (error, body) =>
2014-03-11 13:39:40 -04:00
throw error if error?
@updates = body.updates
done()
it "should return as many updates as it can", ->
2014-03-11 13:39:40 -04:00
expect(@updates).to.deep.equal [{
meta:
start_ts: @to - 8 * @days - 2 * @minutes
end_ts: @to - 8 * @days
users: [@user]
toV: 4
fromV: 3
}, {
meta:
start_ts: @to - 9 * @days - 2 * @minutes
end_ts: @to - 9 * @days
2014-03-11 13:39:40 -04:00
users: [@user]
toV: 2
fromV: 1
2014-03-11 13:39:40 -04:00
}]