GettingADiffTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { expect } = require('chai')
  14. const { ObjectId } = require('../../../app/js/mongodb')
  15. const Settings = require('@overleaf/settings')
  16. const TrackChangesApp = require('./helpers/TrackChangesApp')
  17. const TrackChangesClient = require('./helpers/TrackChangesClient')
  18. const MockDocUpdaterApi = require('./helpers/MockDocUpdaterApi')
  19. const MockWebApi = require('./helpers/MockWebApi')
  20. describe('Getting a diff', function () {
  21. beforeEach(function (done) {
  22. sinon.spy(MockDocUpdaterApi, 'getDoc')
  23. this.now = Date.now()
  24. this.from = this.now - 100000000
  25. this.to = this.now
  26. this.user_id = ObjectId().toString()
  27. this.doc_id = ObjectId().toString()
  28. this.project_id = ObjectId().toString()
  29. MockWebApi.projects[this.project_id] = { features: { versioning: true } }
  30. MockWebApi.users[this.user_id] = this.user = {
  31. email: 'user@sharelatex.com',
  32. first_name: 'Leo',
  33. last_name: 'Lion',
  34. id: this.user_id
  35. }
  36. sinon.spy(MockWebApi, 'getUserInfo')
  37. const twoMinutes = 2 * 60 * 1000
  38. this.updates = [
  39. {
  40. op: [{ i: 'one ', p: 0 }],
  41. meta: { ts: this.from - twoMinutes, user_id: this.user_id },
  42. v: 3
  43. },
  44. {
  45. op: [{ i: 'two ', p: 4 }],
  46. meta: { ts: this.from + twoMinutes, user_id: this.user_id },
  47. v: (this.fromVersion = 4)
  48. },
  49. {
  50. op: [{ i: 'three ', p: 8 }],
  51. meta: { ts: this.to - twoMinutes, user_id: this.user_id },
  52. v: (this.toVersion = 5)
  53. },
  54. {
  55. op: [{ i: 'four', p: 14 }],
  56. meta: { ts: this.to + twoMinutes, user_id: this.user_id },
  57. v: 6
  58. }
  59. ]
  60. this.lines = ['one two three four']
  61. this.expected_diff = [
  62. { u: 'one ' },
  63. {
  64. i: 'two three ',
  65. meta: {
  66. start_ts: this.from + twoMinutes,
  67. end_ts: this.to - twoMinutes,
  68. user: this.user
  69. }
  70. }
  71. ]
  72. MockDocUpdaterApi.docs[this.doc_id] = {
  73. lines: this.lines,
  74. version: 7
  75. }
  76. TrackChangesApp.ensureRunning(() => {
  77. return TrackChangesClient.pushRawUpdates(
  78. this.project_id,
  79. this.doc_id,
  80. this.updates,
  81. (error) => {
  82. if (error != null) {
  83. throw error
  84. }
  85. return TrackChangesClient.getDiff(
  86. this.project_id,
  87. this.doc_id,
  88. this.fromVersion,
  89. this.toVersion,
  90. (error, diff) => {
  91. if (error != null) {
  92. throw error
  93. }
  94. this.diff = diff.diff
  95. return done()
  96. }
  97. )
  98. }
  99. )
  100. })
  101. return null
  102. })
  103. afterEach(function () {
  104. MockDocUpdaterApi.getDoc.restore()
  105. MockWebApi.getUserInfo.restore()
  106. return null
  107. })
  108. it('should return the diff', function () {
  109. return expect(this.diff).to.deep.equal(this.expected_diff)
  110. })
  111. return it('should get the doc from the doc updater', function () {
  112. MockDocUpdaterApi.getDoc
  113. .calledWith(this.project_id, this.doc_id)
  114. .should.equal(true)
  115. return null
  116. })
  117. })