GettingADiffTests.coffee 1.8 KB

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