GettingADiffTests.coffee 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. MockDocUpdaterApi = require "./helpers/MockDocUpdaterApi"
  11. MockWebApi = require "./helpers/MockWebApi"
  12. describe "Getting a diff", ->
  13. before (done) ->
  14. sinon.spy MockDocUpdaterApi, "getDoc"
  15. @now = Date.now()
  16. @from = @now - 100000000
  17. @to = @now
  18. @user_id = ObjectId().toString()
  19. @doc_id = ObjectId().toString()
  20. @project_id = ObjectId().toString()
  21. MockWebApi.projects[@project_id] = features: versioning: true
  22. MockWebApi.users[@user_id] = @user =
  23. email: "user@sharelatex.com"
  24. first_name: "Leo"
  25. last_name: "Lion"
  26. id: @user_id
  27. sinon.spy MockWebApi, "getUser"
  28. twoMinutes = 2 * 60 * 1000
  29. @updates = [{
  30. op: [{ i: "one ", p: 0 }]
  31. meta: { ts: @from - twoMinutes, user_id: @user_id }
  32. v: 3
  33. }, {
  34. op: [{ i: "two ", p: 4 }]
  35. meta: { ts: @from + twoMinutes, user_id: @user_id }
  36. v: @fromVersion = 4
  37. }, {
  38. op: [{ i: "three ", p: 8 }]
  39. meta: { ts: @to - twoMinutes, user_id: @user_id }
  40. v: @toVersion = 5
  41. }, {
  42. op: [{ i: "four", p: 14 }]
  43. meta: { ts: @to + twoMinutes, user_id: @user_id }
  44. v: 6
  45. }]
  46. @lines = ["one two three four"]
  47. @expected_diff = [
  48. { u: "one " }
  49. { i: "two three ", meta: { start_ts: @from + twoMinutes, end_ts: @to - twoMinutes, user: @user } }
  50. ]
  51. MockDocUpdaterApi.docs[@doc_id] =
  52. lines: @lines
  53. version: 7
  54. TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) =>
  55. throw error if error?
  56. TrackChangesClient.getDiff @project_id, @doc_id, @fromVersion, @toVersion, (error, diff) =>
  57. throw error if error?
  58. @diff = diff.diff
  59. done()
  60. after () ->
  61. MockDocUpdaterApi.getDoc.restore()
  62. MockWebApi.getUser.restore()
  63. it "should return the diff", ->
  64. expect(@diff).to.deep.equal @expected_diff
  65. it "should get the doc from the doc updater", ->
  66. MockDocUpdaterApi.getDoc
  67. .calledWith(@project_id, @doc_id)
  68. .should.equal true