GettingADiffTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon')
  13. const chai = require('chai')
  14. chai.should()
  15. const { expect } = chai
  16. const { ObjectId } = require('../../../app/js/mongodb')
  17. const Settings = require('settings-sharelatex')
  18. const TrackChangesApp = require('./helpers/TrackChangesApp')
  19. const TrackChangesClient = require('./helpers/TrackChangesClient')
  20. const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
  21. const MockWebApi = require('./helpers/MockWebApi')
  22. describe('Getting a diff', function () {
  23. beforeEach(function (done) {
  24. sinon.spy(MockDocUpdaterApi, 'getDoc')
  25. this.now = Date.now()
  26. this.from = this.now - 100000000
  27. this.to = this.now
  28. this.user_id = ObjectId().toString()
  29. this.doc_id = ObjectId().toString()
  30. this.project_id = ObjectId().toString()
  31. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  32. MockWebApi.users[this.user_id] = this.user = {
  33. email: 'user@sharelatex.com',
  34. first_name: 'Leo',
  35. last_name: 'Lion',
  36. id: this.user_id
  37. }
  38. sinon.spy(MockWebApi, 'getUserInfo')
  39. const twoMinutes = 2 * 60 * 1000
  40. this.updates = [
  41. {
  42. op: [{ i: 'one ', p: 0 }],
  43. meta: { ts: this.from - twoMinutes, user_id: this.user_id },
  44. v: 3
  45. },
  46. {
  47. op: [{ i: 'two ', p: 4 }],
  48. meta: { ts: this.from + twoMinutes, user_id: this.user_id },
  49. v: (this.fromVersion = 4)
  50. },
  51. {
  52. op: [{ i: 'three ', p: 8 }],
  53. meta: { ts: this.to - twoMinutes, user_id: this.user_id },
  54. v: (this.toVersion = 5)
  55. },
  56. {
  57. op: [{ i: 'four', p: 14 }],
  58. meta: { ts: this.to + twoMinutes, user_id: this.user_id },
  59. v: 6
  60. }
  61. ]
  62. this.lines = ['one two three four']
  63. this.expected_diff = [
  64. { u: 'one ' },
  65. {
  66. i: 'two three ',
  67. meta: {
  68. start_ts: this.from + twoMinutes,
  69. end_ts: this.to - twoMinutes,
  70. user: this.user
  71. }
  72. }
  73. ]
  74. MockDocUpdaterApi.docs[this.doc_id] = {
  75. lines: this.lines,
  76. version: 7
  77. }
  78. TrackChangesApp.ensureRunning(() => {
  79. return TrackChangesClient.pushRawUpdates(
  80. this.project_id,
  81. this.doc_id,
  82. this.updates,
  83. (error) => {
  84. if (error != null) {
  85. throw error
  86. }
  87. return TrackChangesClient.getDiff(
  88. this.project_id,
  89. this.doc_id,
  90. this.fromVersion,
  91. this.toVersion,
  92. (error, diff) => {
  93. if (error != null) {
  94. throw error
  95. }
  96. this.diff = diff.diff
  97. return done()
  98. }
  99. )
  100. }
  101. )
  102. })
  103. return null
  104. })
  105. afterEach(function () {
  106. MockDocUpdaterApi.getDoc.restore()
  107. MockWebApi.getUserInfo.restore()
  108. return null
  109. })
  110. it('should return the diff', function () {
  111. return expect(this.diff).to.deep.equal(this.expected_diff)
  112. })
  113. return it('should get the doc from the doc updater', function () {
  114. MockDocUpdaterApi.getDoc
  115. .calledWith(this.project_id, this.doc_id)
  116. .should.equal(true)
  117. return null
  118. })
  119. })